From fc68d9c3a3973f818ca5df66cf0f61d823c312b6 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Mon, 4 May 2020 15:47:37 +0200 Subject: [PATCH] sys/ztimer: properly initialize intermediate extension callback --- sys/include/ztimer.h | 17 +++++++++++++++++ sys/ztimer/convert_frac.c | 1 + sys/ztimer/convert_muldiv64.c | 1 + sys/ztimer/periph_rtt.c | 1 + sys/ztimer/periph_timer.c | 1 + 5 files changed, 21 insertions(+) diff --git a/sys/include/ztimer.h b/sys/include/ztimer.h index 37dd23a3a6..66dcab6b47 100644 --- a/sys/include/ztimer.h +++ b/sys/include/ztimer.h @@ -485,6 +485,23 @@ void ztimer_update_head_offset(ztimer_clock_t *clock); */ void ztimer_init(void); +/** + * @brief Initialize possible ztimer extension intermediate timer + * + * This will basically just set a timer to (clock->max_value >> 1), *if* + * max_value is not UINT32_MAX. + * + * This is called automatically by all ztimer backends and extension modules. + * + * @internal + */ +static inline void ztimer_init_extend(ztimer_clock_t *clock) +{ + if (clock->max_value < UINT32_MAX) { + clock->ops->set(clock, clock->max_value >> 1); + } +} + /* default ztimer virtual devices */ /** * @brief Default ztimer microsecond clock diff --git a/sys/ztimer/convert_frac.c b/sys/ztimer/convert_frac.c index f468997dd6..71eb74164b 100644 --- a/sys/ztimer/convert_frac.c +++ b/sys/ztimer/convert_frac.c @@ -91,6 +91,7 @@ void ztimer_convert_frac_init(ztimer_convert_frac_t *self, ztimer_clock_t *lower ztimer_convert_frac_compute_scale(self, freq_self, freq_lower); if (freq_self < freq_lower) { self->super.super.max_value = frac_scale(&self->scale_set, UINT32_MAX); + ztimer_init_extend(&self->super.super); } else { DEBUG("ztimer_convert_frac_init: rounding up val:%" PRIu32"\n", diff --git a/sys/ztimer/convert_muldiv64.c b/sys/ztimer/convert_muldiv64.c index 8732448076..d124d581e8 100644 --- a/sys/ztimer/convert_muldiv64.c +++ b/sys/ztimer/convert_muldiv64.c @@ -120,4 +120,5 @@ void ztimer_convert_muldiv64_init( ztimer_convert_muldiv64->super.super.ops = &_ztimer_convert_muldiv64_ops; ztimer_convert_muldiv64->div = div; ztimer_convert_muldiv64->mul = mul; + ztimer_init_extend(&ztimer_convert_muldiv64->super.super); } diff --git a/sys/ztimer/periph_rtt.c b/sys/ztimer/periph_rtt.c index 64b5c63398..42edfebc2c 100644 --- a/sys/ztimer/periph_rtt.c +++ b/sys/ztimer/periph_rtt.c @@ -88,4 +88,5 @@ void ztimer_periph_rtt_init(ztimer_periph_rtt_t *clock) clock->max_value = RTT_MAX_VALUE; rtt_init(); rtt_poweron(); + ztimer_init_extend(clock); } diff --git a/sys/ztimer/periph_timer.c b/sys/ztimer/periph_timer.c index be789a12eb..ce13d06902 100644 --- a/sys/ztimer/periph_timer.c +++ b/sys/ztimer/periph_timer.c @@ -79,4 +79,5 @@ void ztimer_periph_timer_init(ztimer_periph_timer_t *clock, tim_t dev, unsigned clock->super.ops = &_ztimer_periph_timer_ops; clock->super.max_value = max_val; timer_init(dev, freq, _ztimer_periph_timer_callback, clock); + ztimer_init_extend(&clock->super); }