Skip to content

Commit

Permalink
ztimer/periodic: reinit stop and handle aquire
Browse files Browse the repository at this point in the history
  • Loading branch information
kfessel committed Jul 14, 2023
1 parent 7dd7d1e commit abecdd0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions sys/ztimer/periodic.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,22 @@ void ztimer_periodic_init(ztimer_clock_t *clock, ztimer_periodic_t *timer,
bool (*callback)(
void *), void *arg, uint32_t interval)
{
ztimer_remove(clock, &timer->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
.callback = _ztimer_periodic_callback
} };
}

void ztimer_periodic_start(ztimer_periodic_t *timer)
{
ztimer_periodic_stop(timer);
timer->timer.arg = timer;
ztimer_acquire(timer->clock);

uint32_t now = ztimer_now(timer->clock);
Expand All @@ -79,7 +83,9 @@ void ztimer_periodic_start(ztimer_periodic_t *timer)

void ztimer_periodic_stop(ztimer_periodic_t *timer)
{
ztimer_remove(timer->clock, &timer->timer);

ztimer_release(timer->clock);
if (timer->timer.arg == timer) {
ztimer_remove(timer->clock, &timer->timer);
ztimer_release(timer->clock);
timer->timer.arg = NULL;
}
}

0 comments on commit abecdd0

Please sign in to comment.