Skip to content

Commit

Permalink
arch/arm64/src/common/arm64_arch_timer.c: Remove clock drift from tic…
Browse files Browse the repository at this point in the history
…k timer

This fixes two issues with the tick timer
1) Each tick was longer than the requested period. This is because setting
   the compare register was done by first reading the current time, and only
   after that setting the compare register. In addition, when handling the
   timer interrupts in arch_alarm.c / oneshot_callback, the current_tick is
   first read, all the tick handling is done and only after that the next tick
   is started. The whole tick processing time was added to the total tick time.

2) When the compare time is not aligned with tick period, and is drifting,
   eventually any call to ONESHOT_TICK_CURRENT would either return the current
   tick, or the next one, depending on the rounding of division by the
   cycle_per_tick. This again leads to oneshot_callback randomly handling
   two ticks at a time, which breaks all wdog based timers, causing them to
   randomly timeout too early.

The issues are fixed as follows:

Align the compare time register to be evenly divisible by cycle_per_tick.
This will lead arm64_tick_current always to return the currently ongoing tick,
fixing 2). Also calculating the next tick's start from the aligned current
count will fix 1), as there is no time drift in the start cycle.

Signed-off-by: Jukka Laitinen <[email protected]>
  • Loading branch information
jlaitine committed Sep 30, 2024
1 parent ffbbade commit ffb1437
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions arch/arm64/src/common/arm64_arch_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ static int arm64_tick_start(struct oneshot_lowerhalf_s *lower,
{
struct arm64_oneshot_lowerhalf_s *priv =
(struct arm64_oneshot_lowerhalf_s *)lower;
uint64_t next_cycle;

DEBUGASSERT(priv != NULL && callback != NULL);

Expand All @@ -252,10 +253,11 @@ static int arm64_tick_start(struct oneshot_lowerhalf_s *lower,
priv->callback = callback;
priv->arg = arg;

/* Set the timeout */
next_cycle =
arm64_arch_timer_count() / priv->cycle_per_tick * priv->cycle_per_tick +
ticks * priv->cycle_per_tick;

arm64_arch_timer_set_compare(arm64_arch_timer_count() +
priv->cycle_per_tick * ticks);
arm64_arch_timer_set_compare(next_cycle);
arm64_arch_timer_set_irq_mask(false);

return OK;
Expand Down Expand Up @@ -418,4 +420,4 @@ void arm64_arch_timer_secondary_init()
arm64_arch_timer_enable(true);
#endif
}
#endif
#endif

0 comments on commit ffb1437

Please sign in to comment.