Skip to content

Commit

Permalink
timer: cortex_m_systick: handle idle timer overflow
Browse files Browse the repository at this point in the history
The idle timer has its max value and can overflow. We measure time passed
since the sys_clock_set_timeout call. Take possibility of the overflow
into account.

Signed-off-by: Dawid Niedzwiecki <[email protected]>
  • Loading branch information
niedzwiecki-dawid authored and carlescufi committed Nov 27, 2023
1 parent 2ae0999 commit 4104f54
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion drivers/timer/cortex_m_systick.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,15 @@ void sys_clock_idle_exit(void)
cycle_post_idle = cycle_count + elapsed();

/* Calculate has much time has pasted since last measurement for both timers */
idle_timer_diff = idle_timer_post - idle_timer_pre_idle;
/* Check IDLE timer overflow */
if (idle_timer_pre_idle > idle_timer_post) {
idle_timer_diff =
(counter_get_top_value(idle_timer) - idle_timer_pre_idle) +
idle_timer_post + 1;

} else {
idle_timer_diff = idle_timer_post - idle_timer_pre_idle;
}
idle_timer_us = counter_ticks_to_us(idle_timer, idle_timer_diff);

#ifndef CONFIG_CORTEX_M_SYSTICK_64BIT_CYCLE_COUNTER
Expand Down

0 comments on commit 4104f54

Please sign in to comment.