Skip to content

Commit 3a2718b

Browse files
nvlsianpuadbridge
authored andcommitted
If rtc overflow occurr while setting of timestam then the ccompara-event ocurre (erroneusly) in 512s. - move ovf handler at the begining of rtc handler for mitigate the case (mitigate issue for exexution from rtc handler) - add repeating of operation of set a timestamp in cas that rtc overflow occured during the operation.
1 parent 1cbcf7d commit 3a2718b

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

targets/TARGET_NORDIC/TARGET_NRF5/us_ticker.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ void common_rtc_irq_handler(void)
5858
void COMMON_RTC_IRQ_HANDLER(void)
5959
#endif
6060
{
61+
62+
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) {
63+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
64+
// Don't disable this event. It shall occur periodically.
65+
66+
++m_common_rtc_overflows;
67+
}
68+
6169
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, US_TICKER_EVENT)) {
6270
us_ticker_irq_handler();
6371
}
@@ -69,12 +77,6 @@ void COMMON_RTC_IRQ_HANDLER(void)
6977
}
7078
#endif
7179

72-
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) {
73-
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
74-
// Don't disable this event. It shall occur periodically.
75-
76-
++m_common_rtc_overflows;
77-
}
7880
}
7981

8082
#if (defined (__ICCARM__)) && defined(TARGET_MCU_NRF51822)//IAR
@@ -159,7 +161,7 @@ uint64_t common_rtc_64bit_us_get(void)
159161
return ROUNDED_DIV(((uint64_t)ticks) * 1000000, RTC_INPUT_FREQ);
160162
}
161163

162-
void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
164+
__STATIC_INLINE void internal_common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
163165
uint32_t int_mask)
164166
{
165167
// The internal counter is clocked with a frequency that cannot be easily
@@ -198,6 +200,35 @@ void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
198200
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(compare_value));
199201
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, int_mask);
200202
}
203+
204+
void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
205+
uint32_t int_mask)
206+
{
207+
uint32_t prev_overflows;
208+
209+
while (1)
210+
{
211+
prev_overflows = m_common_rtc_overflows;
212+
213+
internal_common_rtc_set_interrupt(us_timestamp, cc_channel, int_mask);
214+
215+
// check in case of preemption by RTC OVF event (apply if call was from a low priority level)
216+
if (prev_overflows != m_common_rtc_overflows)
217+
{
218+
continue;
219+
} // check in case that OVF occurred during execution of a RTC handler (apply if call was from RTC handler)
220+
else if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW))
221+
{
222+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
223+
// Don't disable this event. It shall occur periodically.
224+
225+
++m_common_rtc_overflows;
226+
continue;
227+
}
228+
229+
break;
230+
}
231+
}
201232
//------------------------------------------------------------------------------
202233

203234

0 commit comments

Comments
 (0)