Skip to content

Commit ea2324d

Browse files
ioannisgdleach02
authored andcommitted
drivers: timer: SysTick: enforce proper min & max SysTick LOAD values
Similar to what we do in other timer drivers, the maximum ticks supplied in z_clock_set_timeout(..) needs to be MAX_TICKS at maximum, when K_FOREVER is supplied as argument to the function. In addition to that, the value we load onto the SysTick LOAD register shall be truncated to MAX_CYCLES. This is required to prevent loading a trash value to LOAD register, as only the lowest 24 bits may be safely written. Finally, we move the enforcement of the minimum delay to be programmed on LOAD (i.e. MIN_DELAY) at the end step of the calculation of the cycles-to-be-programmed. This prevents from misscalculating the delay, as any required adjustment is applied at the end, after the delay is rounded up to the next tick boundary. Signed-off-by: Ioannis Glaropoulos <[email protected]>
1 parent 3594f13 commit ea2324d

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

drivers/timer/cortex_m_systick.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,8 @@ void z_clock_set_timeout(s32_t ticks, bool idle)
181181
#if defined(CONFIG_TICKLESS_KERNEL)
182182
u32_t delay;
183183

184-
ticks = MIN(MAX_TICKS, MAX(ticks - 1, 0));
185-
186-
/* Desired delay in the future */
187-
delay = (ticks == 0) ? MIN_DELAY : ticks * CYC_PER_TICK;
184+
ticks = (ticks == K_FOREVER) ? MAX_TICKS : ticks;
185+
ticks = MAX(MIN(ticks - 1, (s32_t)MAX_TICKS), 0);
188186

189187
k_spinlock_key_t key = k_spin_lock(&lock);
190188

@@ -204,13 +202,20 @@ void z_clock_set_timeout(s32_t ticks, bool idle)
204202
*/
205203
last_load = MIN_DELAY;
206204
} else {
205+
/* Desired delay in the future */
206+
delay = ticks * CYC_PER_TICK;
207+
207208
/* Round delay up to next tick boundary */
208209
delay += unannounced;
209210
delay =
210211
((delay + CYC_PER_TICK - 1) / CYC_PER_TICK) * CYC_PER_TICK;
211212
delay -= unannounced;
212-
last_load = delay;
213-
213+
delay = MAX(delay, MIN_DELAY);
214+
if (delay > MAX_CYCLES) {
215+
last_load = MAX_CYCLES;
216+
} else {
217+
last_load = delay;
218+
}
214219
}
215220
SysTick->LOAD = last_load - 1;
216221
SysTick->VAL = 0; /* resets timer to last_load */

0 commit comments

Comments
 (0)