Skip to content

Commit 4d56b00

Browse files
committed
common_rtc_set_interrupt: Wrap <ticks_count> before comparisons
RTC counter is 24-bit. Upper layer handles counter size and wraps ticks count when interrupt is to be fired before passing it to common_rtc_set_interrupt(), but for consistency and safety reasons we can wrap it again in the NRF driver.
1 parent ccba52b commit 4d56b00

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/common_rtc.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ void common_rtc_set_interrupt(uint32_t ticks_count, uint32_t cc_channel,
194194

195195
core_util_critical_section_enter();
196196

197+
/* Wrap ticks_count before comparisons. */
198+
ticks_count = RTC_WRAP(ticks_count);
199+
197200
/* COMPARE occurs when a CC register is N and the COUNTER value transitions from N-1 to N.
198201
* If the COUNTER is N, writing N+2 to a CC register is guaranteed to trigger a
199202
* COMPARE event at N+2.
@@ -202,10 +205,10 @@ void common_rtc_set_interrupt(uint32_t ticks_count, uint32_t cc_channel,
202205

203206
if (now == ticks_count ||
204207
RTC_WRAP(now + 1) == ticks_count) {
205-
ticks_count += 2;
208+
ticks_count = RTC_WRAP(ticks_count + 2);
206209
}
207210

208-
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(ticks_count));
211+
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, ticks_count);
209212

210213
if (!nrf_rtc_int_is_enabled(COMMON_RTC_INSTANCE, int_mask)) {
211214
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);

0 commit comments

Comments
 (0)