Skip to content

Commit

Permalink
ztimer/periodic: stop timer before reinit, remove aquire/release
Browse files Browse the repository at this point in the history
    - remove ztimer_now calls -> remove aquire
  • Loading branch information
kfessel committed Jul 14, 2023
1 parent 7dd7d1e commit a2054e8
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions sys/ztimer/periodic.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ static void _ztimer_periodic_reset(ztimer_periodic_t *timer, ztimer_now_t now)
ztimer_set(timer->clock, &timer->timer, offset);
}

/* this function returns the current list offset (now at the time of interrupt)
it only valid to call this while handling a ztimer callback of this clock */
static ztimer_now_t ztimer_clock_list_now(ztimer_clock_t *clock){
return clock->list.offset;
}

static void _ztimer_periodic_callback(void *arg)
{
ztimer_periodic_t *timer = arg;

if (timer->callback(timer->arg) == ZTIMER_PERIODIC_KEEP_GOING) {
ztimer_now_t now = ztimer_now(timer->clock);
if (timer->callback(timer->arg) == true) {
ztimer_now_t now = ztimer_clock_list_now(timer->clock);
_ztimer_periodic_reset(timer, now);
}
else {
Expand All @@ -57,29 +63,25 @@ 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,
.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);
}

0 comments on commit a2054e8

Please sign in to comment.