Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ztimer/periodic: reinit remove from right clock and handle aquired ztimer #19826

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions sys/ztimer/periodic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
kaspar030 marked this conversation as resolved.
Show resolved Hide resolved

ztimer_release(timer->clock);
}