2017-04-06 08:09:03 +00:00
|
|
|
/* mbed Microcontroller Library
|
2017-12-20 09:35:31 +00:00
|
|
|
* Copyright (c) 2017-2018 ARM Limited
|
2017-04-06 08:09:03 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
/**
|
2018-09-05 13:16:43 +00:00
|
|
|
* Supports the High-resolution Ticker for mbed by implementing
|
|
|
|
* \ref us_ticker_api.h, using a CMSDK Timer \ref timer_cmsdk_dev_t.
|
2017-12-20 09:35:31 +00:00
|
|
|
*/
|
2017-04-06 08:09:03 +00:00
|
|
|
|
|
|
|
#include "us_ticker_api.h"
|
2017-12-20 09:35:31 +00:00
|
|
|
#include "platform_devices.h"
|
2018-09-05 13:16:43 +00:00
|
|
|
#include "timer_cmsdk_drv.h"
|
2017-04-06 08:09:03 +00:00
|
|
|
|
2018-09-05 13:16:43 +00:00
|
|
|
static uint64_t total_ticks = 0;
|
|
|
|
/* Stores the last reload value, or the last tick value read when a read API
|
|
|
|
* call occurs from the upper layer, needed to keep total_ticks
|
|
|
|
* accumulated properly.
|
2018-04-19 13:46:45 +00:00
|
|
|
*/
|
2018-09-05 13:16:43 +00:00
|
|
|
static uint32_t previous_ticks = 0;
|
|
|
|
static uint32_t last_read = 0;
|
2018-04-19 13:46:45 +00:00
|
|
|
|
2018-09-05 13:16:43 +00:00
|
|
|
static void restart_timer(uint32_t new_reload)
|
2017-12-20 09:35:31 +00:00
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
timer_cmsdk_disable(&USEC_TIMER_DEV);
|
|
|
|
timer_cmsdk_set_reload_value(&USEC_TIMER_DEV,
|
|
|
|
new_reload);
|
|
|
|
timer_cmsdk_reset(&USEC_TIMER_DEV);
|
|
|
|
timer_cmsdk_clear_interrupt(&USEC_TIMER_DEV);
|
|
|
|
timer_cmsdk_enable_interrupt(&USEC_TIMER_DEV);
|
|
|
|
timer_cmsdk_enable(&USEC_TIMER_DEV);
|
2017-12-20 09:35:31 +00:00
|
|
|
}
|
2017-04-06 08:09:03 +00:00
|
|
|
|
2018-09-05 13:16:43 +00:00
|
|
|
void us_ticker_init(void)
|
2017-12-20 09:35:31 +00:00
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
timer_cmsdk_init(&USEC_TIMER_DEV);
|
|
|
|
previous_ticks = TIMER_CMSDK_MAX_RELOAD;
|
|
|
|
NVIC_EnableIRQ(USEC_INTERVAL_IRQ);
|
|
|
|
restart_timer(previous_ticks);
|
2017-12-20 09:35:31 +00:00
|
|
|
}
|
2017-04-06 08:09:03 +00:00
|
|
|
|
2018-09-05 13:16:43 +00:00
|
|
|
void us_ticker_free(void)
|
2017-04-06 08:09:03 +00:00
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
timer_cmsdk_disable(&USEC_TIMER_DEV);
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 13:16:43 +00:00
|
|
|
uint32_t us_ticker_read(void)
|
2017-04-06 08:09:03 +00:00
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
if (timer_cmsdk_is_interrupt_active(&USEC_TIMER_DEV)) {
|
|
|
|
total_ticks += previous_ticks;
|
|
|
|
previous_ticks = TIMER_CMSDK_MAX_RELOAD;
|
|
|
|
restart_timer(previous_ticks);
|
|
|
|
} else {
|
|
|
|
uint32_t tick = timer_cmsdk_get_current_value(&USEC_TIMER_DEV);
|
|
|
|
|
|
|
|
if (tick < previous_ticks) {
|
|
|
|
uint32_t delta = previous_ticks - tick;
|
|
|
|
total_ticks += delta;
|
|
|
|
previous_ticks = tick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_read = (uint32_t)(total_ticks >> USEC_REPORTED_SHIFT);
|
|
|
|
return last_read;
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 13:16:43 +00:00
|
|
|
/* To ensure that timestamp is always bigger than the current time, it should
|
|
|
|
* be calculated by using the us_ticker_read() method.
|
|
|
|
*/
|
2017-04-06 08:09:03 +00:00
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
uint32_t reload = (timestamp - last_read) << USEC_REPORTED_SHIFT;
|
|
|
|
previous_ticks = reload;
|
|
|
|
restart_timer(previous_ticks);
|
2017-12-20 09:35:31 +00:00
|
|
|
}
|
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
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
void us_ticker_disable_interrupt(void)
|
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
timer_cmsdk_disable_interrupt(&USEC_TIMER_DEV);
|
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
|
|
|
}
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
void us_ticker_clear_interrupt(void)
|
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
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
timer_cmsdk_clear_interrupt(&USEC_TIMER_DEV);
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
void us_ticker_fire_interrupt(void)
|
2017-04-06 08:09:03 +00:00
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
NVIC_SetPendingIRQ(USEC_INTERVAL_IRQ);
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 13:16:43 +00:00
|
|
|
const ticker_info_t* us_ticker_get_info()
|
2018-07-25 06:40:30 +00:00
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
static const ticker_info_t info = {
|
|
|
|
USEC_REPORTED_FREQ_HZ,
|
|
|
|
USEC_REPORTED_BITS
|
|
|
|
};
|
|
|
|
return &info;
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
2018-04-19 13:46:45 +00:00
|
|
|
|
2018-09-05 13:16:43 +00:00
|
|
|
#ifndef usec_interval_irq_handler
|
|
|
|
#error "usec_interval_irq_handler should be defined, check device_cfg.h!"vector!
|
|
|
|
#endif
|
|
|
|
void usec_interval_irq_handler(void)
|
2018-04-19 13:46:45 +00:00
|
|
|
{
|
2018-09-05 13:16:43 +00:00
|
|
|
us_ticker_read();
|
|
|
|
us_ticker_irq_handler();
|
2018-04-19 13:46:45 +00:00
|
|
|
}
|