2016-07-22 07:41:34 +00:00
|
|
|
/*
|
2016-07-01 15:20:37 +00:00
|
|
|
* Copyright (c) 2013 Nordic Semiconductor ASA
|
|
|
|
* All rights reserved.
|
2016-07-22 07:41:34 +00:00
|
|
|
*
|
2016-07-01 15:20:37 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
2016-07-22 07:41:34 +00:00
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list
|
2016-07-01 15:20:37 +00:00
|
|
|
* of conditions and the following disclaimer.
|
2016-06-15 19:56:03 +00:00
|
|
|
*
|
2016-07-22 07:41:34 +00:00
|
|
|
* 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
|
|
|
|
* integrated circuit in a product or a software update for such product, must reproduce
|
|
|
|
* the above copyright notice, this list of conditions and the following disclaimer in
|
2016-07-01 15:20:37 +00:00
|
|
|
* the documentation and/or other materials provided with the distribution.
|
2016-06-15 19:56:03 +00:00
|
|
|
*
|
2016-07-22 07:41:34 +00:00
|
|
|
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software without specific prior
|
2016-07-01 15:20:37 +00:00
|
|
|
* written permission.
|
2016-06-15 19:56:03 +00:00
|
|
|
*
|
2016-07-22 07:41:34 +00:00
|
|
|
* 4. This software, with or without modification, must only be used with a
|
2016-07-01 15:20:37 +00:00
|
|
|
* Nordic Semiconductor ASA integrated circuit.
|
|
|
|
*
|
2016-07-22 07:41:34 +00:00
|
|
|
* 5. Any software provided in binary or object form under this license must not be reverse
|
|
|
|
* engineered, decompiled, modified and/or disassembled.
|
|
|
|
*
|
2016-07-01 15:20:37 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2016-07-22 07:41:34 +00:00
|
|
|
*
|
2016-06-15 19:56:03 +00:00
|
|
|
*/
|
2016-07-01 15:20:37 +00:00
|
|
|
|
2016-06-15 19:56:03 +00:00
|
|
|
#include "us_ticker_api.h"
|
2016-06-22 12:00:30 +00:00
|
|
|
#include "common_rtc.h"
|
2016-06-15 19:56:03 +00:00
|
|
|
#include "app_util.h"
|
2016-07-22 12:58:17 +00:00
|
|
|
#include "nrf_drv_common.h"
|
2016-07-01 13:03:35 +00:00
|
|
|
#include "lp_ticker_api.h"
|
2017-04-03 12:00:11 +00:00
|
|
|
#include "mbed_critical.h"
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2017-04-26 13:18:49 +00:00
|
|
|
#if defined(NRF52_ERRATA_20)
|
|
|
|
#include "softdevice_handler.h"
|
|
|
|
#endif
|
2016-06-15 19:56:03 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2016-06-22 12:00:30 +00:00
|
|
|
// Common stuff used also by lp_ticker and rtc_api (see "common_rtc.h").
|
2016-06-15 19:56:03 +00:00
|
|
|
//
|
|
|
|
#include "app_util_platform.h"
|
|
|
|
|
2016-06-22 12:00:30 +00:00
|
|
|
bool m_common_rtc_enabled = false;
|
|
|
|
uint32_t volatile m_common_rtc_overflows = 0;
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2017-04-03 12:00:11 +00:00
|
|
|
__STATIC_INLINE void rtc_ovf_event_check(void)
|
2016-06-15 19:56:03 +00:00
|
|
|
{
|
2017-03-24 11:22:28 +00:00
|
|
|
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) {
|
|
|
|
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
|
|
|
|
// Don't disable this event. It shall occur periodically.
|
|
|
|
|
|
|
|
++m_common_rtc_overflows;
|
|
|
|
}
|
2017-04-03 12:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(TARGET_MCU_NRF51822)
|
|
|
|
void common_rtc_irq_handler(void)
|
|
|
|
#else
|
|
|
|
void COMMON_RTC_IRQ_HANDLER(void)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
|
|
|
|
rtc_ovf_event_check();
|
2017-03-24 11:22:28 +00:00
|
|
|
|
2016-07-04 07:21:39 +00:00
|
|
|
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, US_TICKER_EVENT)) {
|
2016-06-15 19:56:03 +00:00
|
|
|
us_ticker_irq_handler();
|
|
|
|
}
|
2016-06-22 12:00:30 +00:00
|
|
|
|
|
|
|
#if DEVICE_LOWPOWERTIMER
|
2016-07-04 07:21:39 +00:00
|
|
|
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, LP_TICKER_EVENT)) {
|
2016-07-01 13:03:35 +00:00
|
|
|
|
|
|
|
lp_ticker_irq_handler();
|
2016-06-22 12:00:30 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-04-26 13:18:49 +00:00
|
|
|
}
|
2016-06-22 12:00:30 +00:00
|
|
|
|
2017-04-26 13:18:49 +00:00
|
|
|
// Function for fix errata 20: RTC Register values are invalid
|
|
|
|
__STATIC_INLINE void errata_20(void)
|
|
|
|
{
|
|
|
|
#if defined(NRF52_ERRATA_20)
|
|
|
|
if (!softdevice_handler_is_enabled())
|
|
|
|
{
|
|
|
|
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
|
|
|
|
NRF_CLOCK->TASKS_LFCLKSTART = 1;
|
|
|
|
|
|
|
|
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NRF_RTC1->TASKS_STOP = 0;
|
|
|
|
#endif
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 09:50:10 +00:00
|
|
|
void RTC1_IRQHandler(void);
|
|
|
|
|
2016-06-22 12:00:30 +00:00
|
|
|
void common_rtc_init(void)
|
2016-06-15 19:56:03 +00:00
|
|
|
{
|
2016-06-22 12:00:30 +00:00
|
|
|
if (m_common_rtc_enabled) {
|
2016-06-15 19:56:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:18:49 +00:00
|
|
|
errata_20();
|
|
|
|
|
2016-10-06 09:50:10 +00:00
|
|
|
NVIC_SetVector(RTC1_IRQn, (uint32_t)RTC1_IRQHandler);
|
|
|
|
|
2016-06-15 19:56:03 +00:00
|
|
|
// RTC is driven by the low frequency (32.768 kHz) clock, a proper request
|
|
|
|
// must be made to have it running.
|
2016-06-22 12:00:30 +00:00
|
|
|
// Currently this clock is started in 'SystemInit' (see "system_nrf51.c"
|
|
|
|
// or "system_nrf52.c", respectively).
|
|
|
|
|
|
|
|
nrf_rtc_prescaler_set(COMMON_RTC_INSTANCE, 0);
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2016-06-23 10:27:18 +00:00
|
|
|
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, US_TICKER_EVENT);
|
|
|
|
#if defined(TARGET_MCU_NRF51822)
|
|
|
|
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, OS_TICK_EVENT);
|
|
|
|
#endif
|
|
|
|
#if DEVICE_LOWPOWERTIMER
|
|
|
|
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);
|
|
|
|
#endif
|
2016-06-22 12:00:30 +00:00
|
|
|
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
|
2016-06-23 10:27:18 +00:00
|
|
|
|
|
|
|
// Interrupts on all related events are enabled permanently. Particular
|
|
|
|
// events will be enabled or disabled as needed (such approach is more
|
|
|
|
// energy efficient).
|
2016-06-22 12:00:30 +00:00
|
|
|
nrf_rtc_int_enable(COMMON_RTC_INSTANCE,
|
2017-04-28 11:13:27 +00:00
|
|
|
#if DEVICE_LOWPOWERTIMER
|
2017-05-09 13:41:03 +00:00
|
|
|
LP_TICKER_INT_MASK |
|
2017-04-28 11:13:27 +00:00
|
|
|
#endif
|
2017-05-09 13:41:03 +00:00
|
|
|
US_TICKER_INT_MASK |
|
|
|
|
NRF_RTC_INT_OVERFLOW_MASK);
|
2016-06-22 12:00:30 +00:00
|
|
|
|
2016-06-23 10:27:18 +00:00
|
|
|
// This event is enabled permanently, since overflow indications are needed
|
|
|
|
// continuously.
|
|
|
|
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, NRF_RTC_INT_OVERFLOW_MASK);
|
|
|
|
// All other relevant events are initially disabled.
|
|
|
|
nrf_rtc_event_disable(COMMON_RTC_INSTANCE,
|
2017-04-28 11:13:27 +00:00
|
|
|
#if defined(TARGET_MCU_NRF51822)
|
2017-05-09 13:41:03 +00:00
|
|
|
OS_TICK_INT_MASK |
|
2017-04-28 11:13:27 +00:00
|
|
|
#endif
|
|
|
|
#if DEVICE_LOWPOWERTIMER
|
2017-05-09 13:41:03 +00:00
|
|
|
LP_TICKER_INT_MASK |
|
2017-04-28 11:13:27 +00:00
|
|
|
#endif
|
2017-05-09 13:41:03 +00:00
|
|
|
US_TICKER_INT_MASK);
|
2016-06-23 10:27:18 +00:00
|
|
|
|
2016-06-22 12:00:30 +00:00
|
|
|
nrf_drv_common_irq_enable(nrf_drv_get_IRQn(COMMON_RTC_INSTANCE),
|
2016-10-10 12:00:29 +00:00
|
|
|
#ifdef NRF51
|
2017-05-09 13:41:03 +00:00
|
|
|
APP_IRQ_PRIORITY_LOW
|
2017-04-26 13:18:49 +00:00
|
|
|
#elif defined(NRF52) || defined(NRF52840_XXAA)
|
2017-05-09 13:41:03 +00:00
|
|
|
APP_IRQ_PRIORITY_LOWEST
|
2016-10-10 12:00:29 +00:00
|
|
|
#endif
|
2017-05-09 13:41:03 +00:00
|
|
|
);
|
2016-06-22 12:00:30 +00:00
|
|
|
|
|
|
|
nrf_rtc_task_trigger(COMMON_RTC_INSTANCE, NRF_RTC_TASK_START);
|
|
|
|
|
|
|
|
m_common_rtc_enabled = true;
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 12:00:11 +00:00
|
|
|
__STATIC_INLINE void rtc_ovf_event_safe_check(void)
|
|
|
|
{
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
|
|
|
|
rtc_ovf_event_check();
|
|
|
|
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-22 12:00:30 +00:00
|
|
|
uint32_t common_rtc_32bit_ticks_get(void)
|
2016-06-15 19:56:03 +00:00
|
|
|
{
|
2017-04-03 12:00:11 +00:00
|
|
|
uint32_t ticks;
|
|
|
|
uint32_t prev_overflows;
|
|
|
|
|
|
|
|
do {
|
|
|
|
prev_overflows = m_common_rtc_overflows;
|
|
|
|
|
|
|
|
ticks = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
|
|
|
|
// The counter used for time measurements is less than 32 bit wide,
|
|
|
|
// so its value is complemented with the number of registered overflows
|
|
|
|
// of the counter.
|
|
|
|
ticks += (m_common_rtc_overflows << RTC_COUNTER_BITS);
|
|
|
|
|
|
|
|
// Check in case that OVF occurred during execution of a RTC handler (apply if call was from RTC handler)
|
|
|
|
// m_common_rtc_overflows might been updated in this call.
|
|
|
|
rtc_ovf_event_safe_check();
|
|
|
|
|
|
|
|
// If call was made from a low priority level m_common_rtc_overflows might have been updated in RTC handler.
|
|
|
|
} while (m_common_rtc_overflows != prev_overflows);
|
|
|
|
|
2016-06-15 19:56:03 +00:00
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2016-07-01 13:03:35 +00:00
|
|
|
uint64_t common_rtc_64bit_us_get(void)
|
2016-06-15 19:56:03 +00:00
|
|
|
{
|
2016-06-22 12:00:30 +00:00
|
|
|
uint32_t ticks = common_rtc_32bit_ticks_get();
|
2016-06-15 19:56:03 +00:00
|
|
|
// [ticks -> microseconds]
|
|
|
|
return ROUNDED_DIV(((uint64_t)ticks) * 1000000, RTC_INPUT_FREQ);
|
|
|
|
}
|
|
|
|
|
2017-04-03 12:00:11 +00:00
|
|
|
void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
|
2016-07-01 13:03:35 +00:00
|
|
|
uint32_t int_mask)
|
2016-06-15 19:56:03 +00:00
|
|
|
{
|
|
|
|
// The internal counter is clocked with a frequency that cannot be easily
|
|
|
|
// multiplied to 1 MHz, therefore besides the translation of values
|
|
|
|
// (microsecond <-> ticks) a special care of overflows handling must be
|
|
|
|
// taken. Here the 32-bit timestamp value is complemented with information
|
|
|
|
// about current the system up time of (ticks + number of overflows of tick
|
|
|
|
// counter on upper bits, converted to microseconds), and such 64-bit value
|
|
|
|
// is then translated to counter ticks. Finally, the lower 24 bits of thus
|
|
|
|
// calculated value is written to the counter compare register to prepare
|
|
|
|
// the interrupt generation.
|
2016-07-01 13:03:35 +00:00
|
|
|
uint64_t current_time64 = common_rtc_64bit_us_get();
|
2016-06-15 19:56:03 +00:00
|
|
|
// [add upper 32 bits from the current time to the timestamp value]
|
2016-07-01 13:03:35 +00:00
|
|
|
uint64_t timestamp64 = us_timestamp +
|
|
|
|
(current_time64 & ~(uint64_t)0xFFFFFFFF);
|
2016-06-15 19:56:03 +00:00
|
|
|
// [if the original timestamp value happens to be after the 32 bit counter
|
|
|
|
// of microsends overflows, correct the upper 32 bits accordingly]
|
2016-07-01 13:03:35 +00:00
|
|
|
if (us_timestamp < (uint32_t)(current_time64 & 0xFFFFFFFF)) {
|
2016-06-15 19:56:03 +00:00
|
|
|
timestamp64 += ((uint64_t)1 << 32);
|
|
|
|
}
|
|
|
|
// [microseconds -> ticks, always round the result up to avoid too early
|
|
|
|
// interrupt generation]
|
|
|
|
uint32_t compare_value =
|
|
|
|
(uint32_t)CEIL_DIV((timestamp64) * RTC_INPUT_FREQ, 1000000);
|
|
|
|
|
2017-04-03 12:00:11 +00:00
|
|
|
|
|
|
|
core_util_critical_section_enter();
|
2016-06-15 19:56:03 +00:00
|
|
|
// The COMPARE event occurs when the value in compare register is N and
|
|
|
|
// the counter value changes from N-1 to N. Therefore, the minimal safe
|
|
|
|
// difference between the compare value to be set and the current counter
|
|
|
|
// value is 2 ticks. This guarantees that the compare trigger is properly
|
|
|
|
// setup before the compare condition occurs.
|
2016-06-22 12:00:30 +00:00
|
|
|
uint32_t closest_safe_compare = common_rtc_32bit_ticks_get() + 2;
|
2016-06-15 19:56:03 +00:00
|
|
|
if ((int)(compare_value - closest_safe_compare) <= 0) {
|
|
|
|
compare_value = closest_safe_compare;
|
|
|
|
}
|
2016-06-22 12:00:30 +00:00
|
|
|
|
2016-07-01 13:03:35 +00:00
|
|
|
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(compare_value));
|
|
|
|
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, int_mask);
|
2017-03-24 11:22:28 +00:00
|
|
|
|
2017-04-03 12:00:11 +00:00
|
|
|
core_util_critical_section_exit();
|
2017-03-24 11:22:28 +00:00
|
|
|
}
|
2016-07-01 13:03:35 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void us_ticker_init(void)
|
|
|
|
{
|
|
|
|
common_rtc_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t us_ticker_read()
|
|
|
|
{
|
2016-07-22 07:41:34 +00:00
|
|
|
us_ticker_init();
|
2016-07-01 13:03:35 +00:00
|
|
|
return (uint32_t)common_rtc_64bit_us_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
|
|
|
common_rtc_set_interrupt(timestamp,
|
|
|
|
US_TICKER_CC_CHANNEL, US_TICKER_INT_MASK);
|
2016-06-15 19:56:03 +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
|
|
|
void us_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
uint32_t closest_safe_compare = common_rtc_32bit_ticks_get() + 2;
|
|
|
|
|
|
|
|
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, US_TICKER_CC_CHANNEL, RTC_WRAP(closest_safe_compare));
|
|
|
|
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, US_TICKER_INT_MASK);
|
|
|
|
}
|
|
|
|
|
2016-06-15 19:56:03 +00:00
|
|
|
void us_ticker_disable_interrupt(void)
|
|
|
|
{
|
2016-06-23 10:27:18 +00:00
|
|
|
nrf_rtc_event_disable(COMMON_RTC_INSTANCE, US_TICKER_INT_MASK);
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_clear_interrupt(void)
|
|
|
|
{
|
2016-07-04 07:21:39 +00:00
|
|
|
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, US_TICKER_EVENT);
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
2016-07-22 10:39:09 +00:00
|
|
|
|
2016-07-22 12:58:17 +00:00
|
|
|
|
|
|
|
// Since there is no SysTick on NRF51, the RTC1 channel 1 is used as an
|
|
|
|
// alternative source of RTOS ticks.
|
|
|
|
#if defined(TARGET_MCU_NRF51822)
|
|
|
|
|
2017-01-27 11:10:28 +00:00
|
|
|
#include "mbed_toolchain.h"
|
2016-07-22 12:58:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define MAX_RTC_COUNTER_VAL ((1uL << RTC_COUNTER_BITS) - 1)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The value previously set in the capture compare register of channel 1
|
|
|
|
*/
|
|
|
|
static uint32_t previous_tick_cc_value = 0;
|
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
/* The Period of RTC oscillator, unit [1/RTC1_CONFIG_FREQUENCY] */
|
|
|
|
static uint32_t os_rtc_period;
|
|
|
|
|
|
|
|
/* Variable for frozen RTC1 counter value. It is used when system timer is disabled. */
|
|
|
|
static uint32_t frozen_sub_tick = 0;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef MBED_CONF_RTOS_PRESENT
|
|
|
|
#include "rtx_os.h" //import osRtxInfo, SysTick_Handler()
|
|
|
|
|
|
|
|
static inline void clear_tick_interrupt();
|
|
|
|
#endif
|
2016-07-22 12:58:17 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
#ifndef RTC1_CONFIG_FREQUENCY
|
|
|
|
#define RTC1_CONFIG_FREQUENCY 32678 // [Hz]
|
|
|
|
#endif
|
2016-07-22 12:58:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
void COMMON_RTC_IRQ_HANDLER(void)
|
2016-07-22 12:58:17 +00:00
|
|
|
{
|
2017-05-15 14:55:45 +00:00
|
|
|
if(nrf_rtc_event_pending(COMMON_RTC_INSTANCE, OS_TICK_EVENT)) {
|
|
|
|
#ifdef MBED_CONF_RTOS_PRESENT
|
|
|
|
clear_tick_interrupt();
|
|
|
|
// Trigger the SysTick_Handler just after exit form RTC Handler.
|
|
|
|
NVIC_SetPendingIRQ(SWI3_IRQn);
|
2016-07-22 12:58:17 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
nrf_gpio_pin_set(11);
|
2016-07-22 12:58:17 +00:00
|
|
|
#endif
|
2017-05-15 14:55:45 +00:00
|
|
|
} else {
|
|
|
|
common_rtc_irq_handler();
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 12:58:17 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
|
|
|
|
#ifdef MBED_CONF_RTOS_PRESENT
|
2016-07-22 12:58:17 +00:00
|
|
|
/**
|
|
|
|
* Return the next number of clock cycle needed for the next tick.
|
2017-05-15 14:55:45 +00:00
|
|
|
* @note This function has been carefully optimized for a systick occurring every 1000us.
|
2016-07-22 12:58:17 +00:00
|
|
|
*/
|
2017-04-28 11:13:27 +00:00
|
|
|
static uint32_t get_next_tick_cc_delta()
|
|
|
|
{
|
2016-07-22 12:58:17 +00:00
|
|
|
uint32_t delta = 0;
|
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
if (osRtxConfig.tick_freq != 1000) {
|
2016-07-22 12:58:17 +00:00
|
|
|
// In RTX, by default SYSTICK is is used.
|
|
|
|
// A tick event is generated every os_trv + 1 clock cycles of the system timer.
|
2017-05-15 14:55:45 +00:00
|
|
|
delta = os_rtc_period;
|
2016-07-22 12:58:17 +00:00
|
|
|
} else {
|
|
|
|
// If the clockrate is set to 1000us then 1000 tick should happen every second.
|
|
|
|
// Unfortunatelly, when clockrate is set to 1000, os_trv is equal to 31.
|
|
|
|
// If (os_trv + 1) is used as the delta value between two ticks, 1000 ticks will be
|
|
|
|
// generated in 32000 clock cycle instead of 32768 clock cycles.
|
|
|
|
// As a result, if a user schedule an OS timer to start in 100s, the timer will start
|
|
|
|
// instead after 97.656s
|
|
|
|
// The code below fix this issue, a clock rate of 1000s will generate 1000 ticks in 32768
|
|
|
|
// clock cycles.
|
|
|
|
// The strategy is simple, for 1000 ticks:
|
|
|
|
// * 768 ticks will occur 33 clock cycles after the previous tick
|
|
|
|
// * 232 ticks will occur 32 clock cycles after the previous tick
|
|
|
|
// By default every delta is equal to 33.
|
|
|
|
// Every five ticks (20%, 200 delta in one second), the delta is equal to 32
|
|
|
|
// The remaining (32) deltas equal to 32 are distributed using primes numbers.
|
|
|
|
static uint32_t counter = 0;
|
|
|
|
if ((counter % 5) == 0 || (counter % 31) == 0 || (counter % 139) == 0 || (counter == 503)) {
|
|
|
|
delta = 32;
|
|
|
|
} else {
|
|
|
|
delta = 33;
|
|
|
|
}
|
|
|
|
++counter;
|
|
|
|
if (counter == 1000) {
|
|
|
|
counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
2017-04-28 11:13:27 +00:00
|
|
|
static inline void clear_tick_interrupt()
|
|
|
|
{
|
2016-07-22 12:58:17 +00:00
|
|
|
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, OS_TICK_EVENT);
|
|
|
|
nrf_rtc_event_disable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicate if a value is included in a range which can be wrapped.
|
|
|
|
* @param begin start of the range
|
|
|
|
* @param end end of the range
|
|
|
|
* @param val value to check
|
|
|
|
* @return true if the value is included in the range and false otherwise.
|
|
|
|
*/
|
2017-04-28 11:13:27 +00:00
|
|
|
static inline bool is_in_wrapped_range(uint32_t begin, uint32_t end, uint32_t val)
|
|
|
|
{
|
2016-07-22 12:58:17 +00:00
|
|
|
// regular case, begin < end
|
|
|
|
// return true if begin <= val < end
|
|
|
|
if (begin < end) {
|
|
|
|
if (begin <= val && val < end) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In this case end < begin because it has wrap around the limits
|
|
|
|
// return false if end < val < begin
|
|
|
|
if (end < val && val < begin) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the next tick.
|
|
|
|
*/
|
2017-04-28 11:13:27 +00:00
|
|
|
static void register_next_tick()
|
|
|
|
{
|
2016-07-22 12:58:17 +00:00
|
|
|
previous_tick_cc_value = nrf_rtc_cc_get(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL);
|
|
|
|
uint32_t delta = get_next_tick_cc_delta();
|
|
|
|
uint32_t new_compare_value = (previous_tick_cc_value + delta) & MAX_RTC_COUNTER_VAL;
|
|
|
|
|
|
|
|
// Disable irq directly for few cycles,
|
|
|
|
// Validation of the new CC value against the COUNTER,
|
|
|
|
// Setting the new CC value and enabling CC IRQ should be an atomic operation
|
|
|
|
// Otherwise, there is a possibility to set an invalid CC value because
|
|
|
|
// the RTC1 keeps running.
|
|
|
|
// This code is very short 20-38 cycles in the worst case, it shouldn't
|
|
|
|
// disturb softdevice.
|
2017-05-09 13:41:03 +00:00
|
|
|
__disable_irq();
|
2016-07-22 12:58:17 +00:00
|
|
|
uint32_t current_counter = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
|
|
|
|
|
|
|
|
// If an overflow occur, set the next tick in COUNTER + delta clock cycles
|
2016-08-10 08:50:04 +00:00
|
|
|
if (is_in_wrapped_range(previous_tick_cc_value, new_compare_value, current_counter + 1) == false) {
|
2016-07-22 12:58:17 +00:00
|
|
|
new_compare_value = current_counter + delta;
|
|
|
|
}
|
|
|
|
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL, new_compare_value);
|
|
|
|
// Enable generation of the compare event for the value set above (this
|
|
|
|
// event will trigger the interrupt).
|
|
|
|
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
|
2017-05-09 13:41:03 +00:00
|
|
|
__enable_irq();
|
2016-07-22 12:58:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
|
2016-07-22 12:58:17 +00:00
|
|
|
/**
|
|
|
|
* Initialize alternative hardware timer as RTX kernel timer
|
|
|
|
* This function is directly called by RTX.
|
|
|
|
* @note this function shouldn't be called directly.
|
|
|
|
* @return IRQ number of the alternative hardware timer
|
|
|
|
*/
|
2017-05-15 14:55:45 +00:00
|
|
|
int32_t osRtxSysTimerSetup(void)
|
2016-07-22 12:58:17 +00:00
|
|
|
{
|
|
|
|
common_rtc_init();
|
2017-05-15 14:55:45 +00:00
|
|
|
|
|
|
|
os_rtc_period = (RTC1_CONFIG_FREQUENCY) / osRtxConfig.tick_freq;
|
|
|
|
|
|
|
|
return nrf_drv_get_IRQn(COMMON_RTC_INSTANCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start SysTickt timer emulation
|
|
|
|
void osRtxSysTimerEnable(void)
|
|
|
|
{
|
2016-11-10 16:47:25 +00:00
|
|
|
nrf_rtc_int_enable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
|
2016-07-22 12:58:17 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
uint32_t current_cnt = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
|
|
|
|
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL, current_cnt);
|
2016-07-22 12:58:17 +00:00
|
|
|
register_next_tick();
|
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
NVIC_SetVector(SWI3_IRQn, (uint32_t)SysTick_Handler);
|
|
|
|
NVIC_SetPriority(SWI3_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Emulated Systick Interrupt */
|
|
|
|
NVIC_EnableIRQ(SWI3_IRQn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop SysTickt timer emulation
|
|
|
|
void osRtxSysTimerDisable(void)
|
|
|
|
{
|
|
|
|
nrf_rtc_int_disable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
|
|
|
|
|
|
|
|
// RTC1 is free runing. osRtxSysTimerGetCount will return proper frozen value
|
|
|
|
// thanks to geting frozen value instead of RTC1 counter value
|
|
|
|
frozen_sub_tick = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
|
2016-07-22 12:58:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
|
|
|
|
|
2016-07-22 12:58:17 +00:00
|
|
|
/**
|
|
|
|
* Acknowledge the tick interrupt.
|
|
|
|
* This function is called by the function OS_Tick_Handler of RTX.
|
|
|
|
* @note this function shouldn't be called directly.
|
|
|
|
*/
|
2017-05-15 14:55:45 +00:00
|
|
|
void osRtxSysTimerAckIRQ(void)
|
2016-07-22 12:58:17 +00:00
|
|
|
{
|
|
|
|
register_next_tick();
|
|
|
|
}
|
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
// provide a free running incremental value over the entire 32-bit range
|
|
|
|
uint32_t osRtxSysTimerGetCount(void)
|
2017-04-28 11:13:27 +00:00
|
|
|
{
|
2017-05-15 14:55:45 +00:00
|
|
|
uint32_t current_cnt;
|
|
|
|
uint32_t sub_tick;
|
|
|
|
|
|
|
|
if (nrf_rtc_int_is_enabled(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK)) {
|
|
|
|
// system timer is enabled
|
|
|
|
current_cnt = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
|
|
|
|
|
|
|
|
if (current_cnt >= previous_tick_cc_value) {
|
|
|
|
//0 prev current MAX
|
|
|
|
//|------|---------|------------|---->
|
|
|
|
sub_tick = current_cnt - previous_tick_cc_value;
|
2016-07-22 12:58:17 +00:00
|
|
|
} else {
|
2017-05-15 14:55:45 +00:00
|
|
|
//0 current prev MAX
|
|
|
|
//|------|---------|------------|---->
|
|
|
|
sub_tick = MAX_RTC_COUNTER_VAL - previous_tick_cc_value + current_cnt;
|
2016-07-22 12:58:17 +00:00
|
|
|
}
|
2017-05-15 14:55:45 +00:00
|
|
|
} else { // system timer is disabled
|
|
|
|
sub_tick = frozen_sub_tick;
|
2016-07-22 12:58:17 +00:00
|
|
|
}
|
2017-05-15 14:55:45 +00:00
|
|
|
|
|
|
|
return (os_rtc_period * osRtxInfo.kernel.tick) + sub_tick;
|
|
|
|
}
|
2016-07-22 12:58:17 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
// Timer Tick frequency
|
|
|
|
uint32_t osRtxSysTimerGetFreq (void) {
|
|
|
|
return RTC1_CONFIG_FREQUENCY;
|
2016-07-22 12:58:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
#endif // #ifdef MBED_CONF_RTOS_PRESENT
|
|
|
|
|
2016-07-22 12:58:17 +00:00
|
|
|
#endif // defined(TARGET_MCU_NRF51822)
|