From 4f2237dd0bb1d9e124e567fb76fc588aba0ccec8 Mon Sep 17 00:00:00 2001 From: Karl Fessel Date: Thu, 13 Jul 2023 16:04:49 +0200 Subject: [PATCH] ztimer/periodic: stop timer before reinit, remove aquire/release - not ztimer_now calls outsid irq -> remove aquire --- sys/ztimer/periodic.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/sys/ztimer/periodic.c b/sys/ztimer/periodic.c index ddd82b8d77..70cba0ece2 100644 --- a/sys/ztimer/periodic.c +++ b/sys/ztimer/periodic.c @@ -54,32 +54,28 @@ static void _ztimer_periodic_callback(void *arg) } void ztimer_periodic_init(ztimer_clock_t *clock, ztimer_periodic_t *timer, - bool (*callback)( - void *), void *arg, uint32_t interval) + bool (*callback)(void *), void *arg, uint32_t interval) { - ztimer_remove(clock, &timer->timer); - *timer = - (ztimer_periodic_t){ .clock = clock, .interval = interval, - .callback = callback, .arg = arg, - .timer = { - .callback = _ztimer_periodic_callback, - .arg = timer - } }; + /* check if this is a reinit, ensure timer is stopped in case */ + if (timer->timer.callback == _ztimer_periodic_callback) { + ztimer_periodic_stop(timer); + } + *timer = (ztimer_periodic_t){ + .clock = clock, .interval = interval, + .callback = callback, .arg = arg, + .timer = { + .callback = _ztimer_periodic_callback, + .arg = timer + } + }; } void ztimer_periodic_start(ztimer_periodic_t *timer) { - ztimer_acquire(timer->clock); - - uint32_t now = ztimer_now(timer->clock); - - timer->last = now; - _ztimer_periodic_reset(timer, now); + timer->last = ztimer_set(timer->clock, &timer->timer, timer->interval) + timer->interval; } void ztimer_periodic_stop(ztimer_periodic_t *timer) { ztimer_remove(timer->clock, &timer->timer); - - ztimer_release(timer->clock); }