Skip to content

Commit 31b2540

Browse files
nordic-krchcarlescufi
authored andcommitted
drivers: timer: nrf_rtc_timer: Prevent prolonged timeout setting
CC setting algorithm is handling a case when CC is too soon (next tick from now). It was setting CC to one tick further in the future if that was detected. Step was repeated if counter incremented during setting CC and CC was behind the counter because of risk of setting CC too late. In certain scenarios we might spend a lot of time in that loop, especially if optimization is turned off. Test shown that loop was executed dozens of time (up. to 700us). To prevent prolonged execution whenever CC setting fails we set CC to one more tick further in future. Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
1 parent 79ca8f7 commit 31b2540

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

drivers/timer/nrf_rtc_timer.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ static uint32_t set_absolute_alarm(int32_t chan, uint32_t abs_val)
233233
uint32_t now2;
234234
uint32_t cc_val = abs_val & COUNTER_MAX;
235235
uint32_t prev_cc = get_comparator(chan);
236+
uint32_t tick_inc = 2;
236237

237238
do {
238239
now = counter();
@@ -251,12 +252,16 @@ static uint32_t set_absolute_alarm(int32_t chan, uint32_t abs_val)
251252
k_busy_wait(19);
252253
}
253254

254-
/* If requested cc_val is in the past or next tick, set to 2
255-
* ticks from now. RTC may not generate event if CC is set for
256-
* 1 tick from now.
255+
/* RTC may not generate event if CC is set for 1 tick from now.
256+
* Because of that if requested cc_val is in the past or next tick,
257+
* set CC to further in future. Start with 2 ticks from now but
258+
* if that fails go even futher. It may fail if operation got
259+
* interrupted and RTC counter progressed or if optimization is
260+
* turned off.
257261
*/
258262
if (counter_sub(cc_val, now + 2) > COUNTER_HALF_SPAN) {
259-
cc_val = now + 2;
263+
cc_val = now + tick_inc;
264+
tick_inc++;
260265
}
261266

262267
event_clear(chan);

0 commit comments

Comments
 (0)