Skip to content

[NRF5]: fix rtc overflow-while-set-timestamp issue #4016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions targets/TARGET_NORDIC/TARGET_NRF5/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "nrf_drv_common.h"
#include "nrf_drv_config.h"
#include "lp_ticker_api.h"
#include "mbed_critical.h"


//------------------------------------------------------------------------------
Expand All @@ -52,12 +53,25 @@
bool m_common_rtc_enabled = false;
uint32_t volatile m_common_rtc_overflows = 0;

__STATIC_INLINE void rtc_ovf_event_check(void)
{
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd extract the above block to some static function, e.g. check_overflow, since the same code needs to be executed in two places.

}

#if defined(TARGET_MCU_NRF51822)
void common_rtc_irq_handler(void)
#else
void COMMON_RTC_IRQ_HANDLER(void)
#endif
{

rtc_ovf_event_check();

if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, US_TICKER_EVENT)) {
us_ticker_irq_handler();
}
Expand All @@ -69,12 +83,6 @@ void COMMON_RTC_IRQ_HANDLER(void)
}
#endif

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;
}
}

#if (defined (__ICCARM__)) && defined(TARGET_MCU_NRF51822)//IAR
Expand Down Expand Up @@ -142,13 +150,37 @@ void common_rtc_init(void)
m_common_rtc_enabled = true;
}

__STATIC_INLINE void rtc_ovf_event_safe_check(void)
{
core_util_critical_section_enter();

rtc_ovf_event_check();

core_util_critical_section_exit();
}


uint32_t common_rtc_32bit_ticks_get(void)
{
uint32_t 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);
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);

return ticks;
}

Expand Down Expand Up @@ -185,6 +217,8 @@ void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
uint32_t compare_value =
(uint32_t)CEIL_DIV((timestamp64) * RTC_INPUT_FREQ, 1000000);


core_util_critical_section_enter();
// 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
Expand All @@ -197,6 +231,8 @@ void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,

nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(compare_value));
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, int_mask);

core_util_critical_section_exit();
}
//------------------------------------------------------------------------------

Expand Down