drivers: timer: smartbond: Fix timer2 timeout set

When function sys_clock_set_timout() is called with small value
(i.e. 1) calculated time to be programmed to TIMER2 reload
register may be such that is expires before code set's it
up. In that case timer interrupt will be scheduled in far
future.

With this change, code checks after it sets reload value if
requested time already passed and if so TIMER2 interrupt
is marked as pending to avoid races.

Signed-off-by: Jerzy Kasenberg <jerzy.kasenberg@codecoup.pl>
This commit is contained in:
Jerzy Kasenberg 2024-05-22 13:31:35 +02:00 committed by Anas Nashif
commit bd6bb64d8a

View file

@ -108,6 +108,18 @@ void sys_clock_set_timeout(int32_t ticks, bool idle)
target_val = ((target_val + CYC_PER_TICK - 1) / CYC_PER_TICK) * CYC_PER_TICK;
set_reload(target_val);
/*
* If time was so small that it already fired or should fire
* just now, mark interrupt as pending to avoid losing timer event.
* Condition is true when target_val (point in time that should be
* used for wakeup) is behind timer value or is equal to it.
* In that case we don't know if reload value was set in time or
* not but time expired anyway so make sure that interrupt is pending.
*/
if ((int32_t)(target_val - timer_val_32_noupdate() - 1) < 0) {
NVIC_SetPendingIRQ(TIMER2_IRQn);
}
}
uint32_t sys_clock_elapsed(void)