From 0694e47f9789b6cb388ad3337e441f4b4b928822 Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Thu, 10 Feb 2022 14:22:51 -0600 Subject: [PATCH] drivers: mcux_gpt_timer: Fix rounding error on tick boundary GPT timer driver was announcing progress to the kernel too soon when an announcement was requested via sys_clock_set_timeout() on a tick boundary. Fix rounding to add a tick worth of cycles. Fixes #42665 Signed-off-by: Daniel DeGrasse --- drivers/timer/mcux_gpt_timer.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/timer/mcux_gpt_timer.c b/drivers/timer/mcux_gpt_timer.c index 67d5654c323..e913788dbf1 100644 --- a/drivers/timer/mcux_gpt_timer.c +++ b/drivers/timer/mcux_gpt_timer.c @@ -192,7 +192,7 @@ void sys_clock_set_timeout(int32_t ticks, bool idle) } ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks; /* Clamp ticks */ - ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS); + ticks = CLAMP((ticks - 1), 0, (int32_t)MAX_TICKS); key = k_spin_lock(&lock); @@ -220,6 +220,13 @@ void sys_clock_set_timeout(int32_t ticks, bool idle) reload_value = ((reload_value + CYC_PER_TICK - 1) / CYC_PER_TICK) * CYC_PER_TICK; reload_value -= unannounced_cycles; + if (reload_value == ticks * CYC_PER_TICK) { + /* We are on a tick boundary. Since we subtracted from + * 'ticks' earlier, we need to add one tick worth of + * cycles to announce to the kernel at the right time. + */ + reload_value += CYC_PER_TICK; + } /* Clamp reload value */ reload_value = CLAMP(reload_value, MIN_DELAY, MAX_CYCLES); }