2016-05-11 15:02:24 +00:00
|
|
|
/*
|
|
|
|
* PackageLicenseDeclared: Apache-2.0
|
|
|
|
* Copyright (c) 2015 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cmsis.h"
|
|
|
|
#include "device.h"
|
|
|
|
#include "objects.h"
|
|
|
|
#include "lp_ticker_api.h"
|
|
|
|
|
|
|
|
/* Private lp_ticker data */
|
|
|
|
/* lp_ticker initialize */
|
|
|
|
static uint32_t lp_ticker_initialized = 0;
|
|
|
|
/* lp_ticker reload value */
|
|
|
|
static uint32_t lp_ticker_reload = 0x0; /* Max Value */
|
2016-07-20 09:13:15 +00:00
|
|
|
/* Store Overflow Delta */
|
|
|
|
static uint32_t lp_ticker_overflows_delta = 0;
|
|
|
|
/* lp_ticker Overflow limit */
|
|
|
|
static uint32_t lp_ticker_overflow_limit = 0;
|
2016-05-11 15:02:24 +00:00
|
|
|
|
2018-03-13 17:11:18 +00:00
|
|
|
#if DEVICE_LPTICKER
|
2016-05-11 15:02:24 +00:00
|
|
|
/**
|
|
|
|
* Interrupt Handler
|
|
|
|
*/
|
|
|
|
void __lp_ticker_irq_handler(void)
|
|
|
|
{
|
|
|
|
if (DualTimer_GetIRQInfo(DUALTIMER0) == SINGLETIMER2) {
|
|
|
|
DualTimer_ClearInterrupt(DUALTIMER0);
|
2016-07-20 09:13:15 +00:00
|
|
|
/*
|
|
|
|
* For each overflow event adds the timer max represented value to
|
|
|
|
* the delta. This allows the lp_ticker to keep track of the elapsed
|
|
|
|
* time:
|
|
|
|
* elapsed_time = (num_overflow * overflow_limit) + current_time
|
|
|
|
*/
|
|
|
|
lp_ticker_overflows_delta += lp_ticker_overflow_limit;
|
2016-05-11 15:02:24 +00:00
|
|
|
} else {
|
|
|
|
lp_ticker_irq_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the low power ticker
|
|
|
|
*/
|
|
|
|
void lp_ticker_init(void)
|
|
|
|
{
|
|
|
|
uint32_t lp_ticker_irqn = 0;
|
|
|
|
/* Verify if lp_ticker has been already Initialized */
|
|
|
|
if (lp_ticker_initialized == 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lp_ticker_initialized = 1;
|
|
|
|
|
|
|
|
/* Dualtimer Initialize */
|
|
|
|
DualTimer_Initialize(DUALTIMER0, lp_ticker_reload);
|
|
|
|
/* Dualtimer Enable */
|
|
|
|
DualTimer_Enable(DUALTIMER0, DUALTIMER_COUNT_32
|
|
|
|
//| DUALTIMER_PERIODIC
|
|
|
|
);
|
|
|
|
/* DualTimer get IRQn */
|
|
|
|
lp_ticker_irqn = DualTimer_GetIRQn(DUALTIMER0);
|
|
|
|
/* Enable lp_ticker IRQ */
|
|
|
|
NVIC_SetVector((IRQn_Type)lp_ticker_irqn,
|
|
|
|
(uint32_t)__lp_ticker_irq_handler);
|
|
|
|
NVIC_EnableIRQ((IRQn_Type)lp_ticker_irqn);
|
2016-07-20 09:13:15 +00:00
|
|
|
|
|
|
|
/* DualTimer set interrupt on SINGLETIMER2 */
|
|
|
|
DualTimer_SetInterrupt_2(DUALTIMER0, DUALTIMER_DEFAULT_RELOAD,
|
|
|
|
DUALTIMER_COUNT_32 | DUALTIMER_PERIODIC);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set lp_ticker Overflow limit. The lp_ticker overflow limit is required
|
|
|
|
* to calculated the return value of the lp_ticker read function in us
|
|
|
|
* on 32bit.
|
|
|
|
* A 32bit us value cannot be represented directly in the Dual Timer Load
|
|
|
|
* register if it is greater than (0xFFFFFFFF ticks)/DUALTIMER_DIVIDER_US.
|
|
|
|
*/
|
|
|
|
lp_ticker_overflow_limit = DualTimer_GetReloadValue(DUALTIMER0,
|
|
|
|
SINGLETIMER2);
|
2016-05-11 15:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the current counter
|
|
|
|
* @return: The current timer's counter value in microseconds
|
|
|
|
*/
|
|
|
|
uint32_t lp_ticker_read(void)
|
|
|
|
{
|
|
|
|
uint32_t microseconds = 0;
|
|
|
|
|
|
|
|
/* Verify if lp_ticker has not been Initialized */
|
|
|
|
if (lp_ticker_initialized == 0)
|
|
|
|
lp_ticker_init();
|
|
|
|
|
|
|
|
/* Read Timer Value */
|
2016-07-20 09:13:15 +00:00
|
|
|
microseconds = lp_ticker_overflows_delta + DualTimer_Read_2(DUALTIMER0);
|
2016-05-11 15:02:24 +00:00
|
|
|
|
|
|
|
return microseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set interrupt for specified timestamp
|
|
|
|
* timestamp: The time in microseconds to be set
|
|
|
|
*/
|
|
|
|
void lp_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
|
|
|
/* Verify if lp_ticker has been not Initialized */
|
|
|
|
if (lp_ticker_initialized == 0)
|
|
|
|
lp_ticker_init();
|
|
|
|
|
|
|
|
/* Calculate the delta */
|
Ticker: add fire interrupt now function
fire_interrupt function should be used for events in the past. As we have now
64bit timestamp, we can figure out what is in the past, and ask a target to invoke
an interrupt immediately. The previous attemps in the target HAL tickers were not ideal, as it can wrap around easily (16 or 32 bit counters). This new
functionality should solve this problem.
set_interrupt for tickers in HAL code should not handle anything but the next match interrupt. If it was in the past is handled by the upper layer.
It is possible that we are setting next event to the close future, so once it is set it is already in the past. Therefore we add a check after set interrupt to verify it is in future.
If it is not, we fire interrupt immediately. This results in
two events - first one immediate, correct one. The second one might be scheduled in far future (almost entire ticker range),
that should be discarded.
The specification for the fire_interrupts are:
- should set pending bit for the ticker interrupt (as soon as possible),
the event we are scheduling is already in the past, and we do not want to skip
any events
- no arguments are provided, neither return value, not needed
- ticker should be initialized prior calling this function (no need to check if it is already initialized)
All our targets provide this new functionality, removing old misleading if (timestamp is in the past) checks.
2017-06-27 11:18:59 +00:00
|
|
|
uint32_t delta = timestamp - lp_ticker_read();
|
2016-05-11 15:02:24 +00:00
|
|
|
|
|
|
|
/* Enable interrupt on SingleTimer1 */
|
|
|
|
DualTimer_SetInterrupt_1(DUALTIMER0, delta,
|
|
|
|
DUALTIMER_COUNT_32 | DUALTIMER_ONESHOT);
|
|
|
|
}
|
|
|
|
|
Ticker: add fire interrupt now function
fire_interrupt function should be used for events in the past. As we have now
64bit timestamp, we can figure out what is in the past, and ask a target to invoke
an interrupt immediately. The previous attemps in the target HAL tickers were not ideal, as it can wrap around easily (16 or 32 bit counters). This new
functionality should solve this problem.
set_interrupt for tickers in HAL code should not handle anything but the next match interrupt. If it was in the past is handled by the upper layer.
It is possible that we are setting next event to the close future, so once it is set it is already in the past. Therefore we add a check after set interrupt to verify it is in future.
If it is not, we fire interrupt immediately. This results in
two events - first one immediate, correct one. The second one might be scheduled in far future (almost entire ticker range),
that should be discarded.
The specification for the fire_interrupts are:
- should set pending bit for the ticker interrupt (as soon as possible),
the event we are scheduling is already in the past, and we do not want to skip
any events
- no arguments are provided, neither return value, not needed
- ticker should be initialized prior calling this function (no need to check if it is already initialized)
All our targets provide this new functionality, removing old misleading if (timestamp is in the past) checks.
2017-06-27 11:18:59 +00:00
|
|
|
void lp_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
uint32_t lp_ticker_irqn = DualTimer_GetIRQn(DUALTIMER0);
|
|
|
|
NVIC_SetPendingIRQ((IRQn_Type)lp_ticker_irqn);
|
|
|
|
}
|
|
|
|
|
2016-05-11 15:02:24 +00:00
|
|
|
/**
|
|
|
|
* Disable low power ticker interrupt
|
|
|
|
*/
|
|
|
|
void lp_ticker_disable_interrupt(void)
|
|
|
|
{
|
|
|
|
/* Disable Interrupt */
|
2016-10-17 13:19:21 +00:00
|
|
|
DualTimer_DisableInterrupt(DUALTIMER0,
|
|
|
|
SINGLETIMER1);
|
2016-05-11 15:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the low power ticker interrupt
|
|
|
|
*/
|
|
|
|
void lp_ticker_clear_interrupt(void)
|
|
|
|
{
|
|
|
|
/* Clear Interrupt */
|
|
|
|
DualTimer_ClearInterrupt(DUALTIMER0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|