2016-05-11 15:02:24 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* 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 <stddef.h>
|
|
|
|
#include "cmsis.h"
|
|
|
|
#include "us_ticker_api.h"
|
|
|
|
#include "PeripheralNames.h"
|
|
|
|
/* Private data */
|
|
|
|
/* us_ticker reload value */
|
|
|
|
static uint32_t us_ticker_reload = 0x0; /* Max Value */
|
|
|
|
/* us ticker initialized */
|
|
|
|
static uint32_t us_ticker_inited = 0;
|
|
|
|
/* us ticker overflow */
|
2016-07-20 09:18:00 +00:00
|
|
|
static uint32_t us_ticker_overflow_delta = 0;
|
|
|
|
/* us ticker overflow limit */
|
|
|
|
static uint32_t us_ticker_overflow_limit = 0;
|
2016-05-11 15:02:24 +00:00
|
|
|
|
|
|
|
void __us_ticker_irq_handler(void) {
|
|
|
|
Timer_ClearInterrupt(TIMER1);
|
2016-07-20 09:18:00 +00:00
|
|
|
/*
|
|
|
|
* For each overflow event adds the timer max represented value to
|
|
|
|
* the delta. This allows the us_ticker to keep track of the elapsed
|
|
|
|
* time:
|
|
|
|
* elapsed_time = (num_overflow * overflow_limit) + current_time
|
|
|
|
*/
|
|
|
|
us_ticker_overflow_delta += us_ticker_overflow_limit;
|
2016-05-11 15:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_init(void) {
|
|
|
|
uint32_t us_ticker_irqn0 = 0;
|
|
|
|
uint32_t us_ticker_irqn1 = 0;
|
|
|
|
|
|
|
|
if (us_ticker_inited)
|
|
|
|
return;
|
|
|
|
us_ticker_inited = 1;
|
|
|
|
|
|
|
|
/* Initialize Timer 0 */
|
|
|
|
Timer_Initialize(TIMER0, us_ticker_reload);
|
|
|
|
/* Enable Timer 0 */
|
|
|
|
Timer_Enable(TIMER0);
|
|
|
|
|
|
|
|
/* Initialize Timer 1 */
|
|
|
|
Timer_Initialize(TIMER1, us_ticker_reload);
|
|
|
|
/* Enable Timer 1 */
|
|
|
|
Timer_Enable(TIMER1);
|
|
|
|
|
|
|
|
/* Timer 0 get IRQn */
|
|
|
|
us_ticker_irqn0 = Timer_GetIRQn(TIMER0);
|
|
|
|
NVIC_SetVector((IRQn_Type)us_ticker_irqn0, (uint32_t)us_ticker_irq_handler);
|
|
|
|
NVIC_EnableIRQ((IRQn_Type)us_ticker_irqn0);
|
|
|
|
|
|
|
|
/* Timer 1 get IRQn */
|
|
|
|
us_ticker_irqn1 = Timer_GetIRQn(TIMER1);
|
|
|
|
NVIC_SetVector((IRQn_Type)us_ticker_irqn1, (uint32_t)__us_ticker_irq_handler);
|
|
|
|
NVIC_EnableIRQ((IRQn_Type)us_ticker_irqn1);
|
2016-07-20 09:18:00 +00:00
|
|
|
|
|
|
|
/* Timer set interrupt on TIMER1 */
|
|
|
|
Timer_SetInterrupt(TIMER1, TIMER_DEFAULT_RELOAD);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set us_ticker Overflow limit. The us_ticker overflow limit is required
|
|
|
|
* to calculated the return value of the us_ticker read function in us
|
|
|
|
* on 32bit.
|
|
|
|
* A 32bit us value cannot be represented directly in the Timer Load
|
|
|
|
* register if it is greater than (0xFFFFFFFF ticks)/TIMER_DIVIDER_US.
|
|
|
|
*/
|
|
|
|
us_ticker_overflow_limit = Timer_GetReloadValue(TIMER1);
|
2016-05-11 15:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t us_ticker_read() {
|
|
|
|
uint32_t return_value = 0;
|
|
|
|
|
|
|
|
if (!us_ticker_inited)
|
|
|
|
us_ticker_init();
|
2016-07-20 09:18:00 +00:00
|
|
|
|
|
|
|
return_value = us_ticker_overflow_delta + Timer_Read(TIMER1);
|
|
|
|
|
2016-05-11 15:02:24 +00:00
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp) {
|
|
|
|
if (!us_ticker_inited)
|
|
|
|
us_ticker_init();
|
|
|
|
|
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 - us_ticker_read();
|
2016-05-11 15:02:24 +00:00
|
|
|
Timer_SetInterrupt(TIMER0, 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
|
|
|
void us_ticker_fire_interrupt(void)
|
|
|
|
{
|
2017-10-06 11:50:48 +00:00
|
|
|
uint32_t us_ticker_irqn1 = Timer_GetIRQn(TIMER0);
|
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
|
|
|
NVIC_SetPendingIRQ((IRQn_Type)us_ticker_irqn1);
|
|
|
|
}
|
|
|
|
|
2016-05-11 15:02:24 +00:00
|
|
|
void us_ticker_disable_interrupt(void) {
|
|
|
|
Timer_DisableInterrupt(TIMER0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_clear_interrupt(void) {
|
|
|
|
Timer_ClearInterrupt(TIMER0);
|
|
|
|
}
|
2018-07-25 06:40:30 +00:00
|
|
|
|
|
|
|
void us_ticker_free(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|