Skip to content

Commit bf1d3db

Browse files
ananglcarlescufi
authored andcommitted
drivers: nrf_rtc_timer: Simplify sys_clock_set_timeout calculations
Remove unnecessary decreasing of the number of ticks by 1 (it was then increased by 1 when it was converted to the number of cycles) and add a comment that clarifies the way that ticks < 1 are handled. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent 426c9cb commit bf1d3db

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

drivers/timer/nrf_rtc_timer.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <zephyr/drivers/clock_control/nrf_clock_control.h>
1212
#include <zephyr/drivers/timer/system_timer.h>
1313
#include <zephyr/drivers/timer/nrf_rtc_timer.h>
14+
#include <zephyr/sys/util.h>
1415
#include <zephyr/sys_clock.h>
1516
#include <hal/nrf_rtc.h>
1617
#include <zephyr/irq.h>
@@ -592,12 +593,17 @@ void sys_clock_set_timeout(int32_t ticks, bool idle)
592593
return;
593594
}
594595

595-
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks;
596-
ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
597-
/* If timeout is set to max we assume that system is idle and timeout
598-
* is set to forever.
599-
*/
600-
sys_busy = (ticks < (MAX_TICKS - 1));
596+
if (ticks == K_TICKS_FOREVER) {
597+
cyc = MAX_TICKS * CYC_PER_TICK;
598+
sys_busy = false;
599+
} else {
600+
/* Value of ticks can be zero or negative, what means "announce
601+
* the next tick" (the same as ticks equal to 1).
602+
*/
603+
cyc = CLAMP(ticks, 1, (int32_t)MAX_TICKS);
604+
cyc *= CYC_PER_TICK;
605+
sys_busy = true;
606+
}
601607

602608
uint32_t unannounced = z_nrf_rtc_timer_read() - last_count;
603609

@@ -607,15 +613,14 @@ void sys_clock_set_timeout(int32_t ticks, bool idle)
607613
* before the existing one triggers the interrupt.
608614
*/
609615
if (unannounced >= COUNTER_HALF_SPAN) {
610-
ticks = 0;
616+
cyc = 0;
611617
}
612618

613619
/* Get the cycles from last_count to the tick boundary after
614620
* the requested ticks have passed starting now.
615621
*/
616-
cyc = ticks * CYC_PER_TICK + 1 + unannounced;
617-
cyc += (CYC_PER_TICK - 1);
618-
cyc = (cyc / CYC_PER_TICK) * CYC_PER_TICK;
622+
cyc += unannounced;
623+
cyc = ceiling_fraction(cyc, CYC_PER_TICK) * CYC_PER_TICK;
619624

620625
/* Due to elapsed time the calculation above might produce a
621626
* duration that laps the counter. Don't let it.

0 commit comments

Comments
 (0)