From b8d8159f9f9ba371b83acddf989c516cc5d7f0de Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Thu, 19 Mar 2020 18:50:43 +0100 Subject: [PATCH] sys/evtimer: introduce ZTIMER_MSEC as timer backend --- Makefile.dep | 4 +++- sys/evtimer/evtimer.c | 44 ++++++++++++++++++++++++++++++++++------- sys/include/evtimer.h | 9 +++++++++ sys/ztimer/Makefile.dep | 8 +++++++- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/Makefile.dep b/Makefile.dep index 867d4615acf76..798dcb05ee102 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -687,7 +687,9 @@ ifneq (,$(filter phydat,$(USEMODULE))) endif ifneq (,$(filter evtimer,$(USEMODULE))) - USEMODULE += xtimer + ifeq (,$(filter evtimer_on_ztimer,$(USEMODULE))) + USEMODULE += xtimer + endif endif ifneq (,$(filter can_linux,$(USEMODULE))) diff --git a/sys/evtimer/evtimer.c b/sys/evtimer/evtimer.c index aabff9fafc63e..8e6743291eeb6 100644 --- a/sys/evtimer/evtimer.c +++ b/sys/evtimer/evtimer.c @@ -24,7 +24,6 @@ #include "div.h" #include "irq.h" -#include "xtimer.h" #include "evtimer.h" @@ -91,27 +90,57 @@ static void _del_event_from_list(evtimer_t *evtimer, evtimer_event_t *event) } } -static void _set_timer(xtimer_t *timer, uint32_t offset_ms) +static void _set_timer(evtimer_t *evtimer) { - uint64_t offset_us = (uint64_t)offset_ms * US_PER_MS; + evtimer_event_t *next_event = evtimer->events; + +#ifdef MODULE_EVTIMER_ON_ZTIMER + evtimer->base = ztimer_now(ZTIMER_MSEC); + + DEBUG("evtimer: now=%" PRIu32 " ms setting ztimer to %" PRIu32 " ms\n", + evtimer->base, next_event->offset); + + ztimer_set(ZTIMER_MSEC, &evtimer->timer, next_event->offset); +#else /* ifdef MODULE_EVTIMER_ON_ZTIMER */ + uint64_t offset_us = (uint64_t)next_event->offset * US_PER_MS; DEBUG("evtimer: now=%" PRIu32 " us setting xtimer to %" PRIu32 ":%" PRIu32 " us\n", xtimer_now_usec(), (uint32_t)(offset_us >> 32), (uint32_t)(offset_us)); - xtimer_set64(timer, offset_us); + xtimer_set64(&evtimer->timer, offset_us); +#endif /* ifndef MODULE_EVTIMER_ON_ZTIMER */ } static void _update_timer(evtimer_t *evtimer) { if (evtimer->events) { - evtimer_event_t *event = evtimer->events; - _set_timer(&evtimer->timer, event->offset); + _set_timer(evtimer); } else { +#ifdef MODULE_EVTIMER_ON_ZTIMER + ztimer_remove(ZTIMER_MSEC, &evtimer->timer); +#else xtimer_remove(&evtimer->timer); +#endif } } +#ifdef MODULE_EVTIMER_ON_ZTIMER +static void _update_head_offset(evtimer_t *evtimer) +{ + if (evtimer->events) { + evtimer_event_t *event = evtimer->events; + uint32_t now = ztimer_now(ZTIMER_MSEC); + uint32_t elapsed = now - evtimer->base; + if (elapsed > event->offset) { + event->offset = 0; + } else { + event->offset -= elapsed; + } + evtimer->base = now; + } +} +#else /* ifdef MODULE_EVTIMER_ON_ZTIMER */ static uint32_t _get_offset(xtimer_t *timer) { uint64_t now_us = xtimer_now_usec64(); @@ -138,6 +167,7 @@ static void _update_head_offset(evtimer_t *evtimer) DEBUG("evtimer: _update_head_offset(): new head offset %" PRIu32 "\n", event->offset); } } +#endif /* ifndef MODULE_EVTIMER_ON_ZTIMER */ void evtimer_add(evtimer_t *evtimer, evtimer_event_t *event) { @@ -148,7 +178,7 @@ void evtimer_add(evtimer_t *evtimer, evtimer_event_t *event) _update_head_offset(evtimer); _add_event_to_list(evtimer, event); if (evtimer->events == event) { - _set_timer(&evtimer->timer, event->offset); + _set_timer(evtimer); } irq_restore(state); if (sched_context_switch_request) { diff --git a/sys/include/evtimer.h b/sys/include/evtimer.h index da8dc9a1efca7..48bb1f2345754 100644 --- a/sys/include/evtimer.h +++ b/sys/include/evtimer.h @@ -45,7 +45,11 @@ #include +#ifdef MODULE_EVTIMER_ON_ZTIMER +#include "ztimer.h" +#else #include "xtimer.h" +#endif #ifdef __cplusplus extern "C" { @@ -68,7 +72,12 @@ typedef void(*evtimer_callback_t)(evtimer_event_t* event); * @brief Event timer */ typedef struct { +#ifdef MODULE_EVTIMER_ON_ZTIMER + ztimer_t timer; /**< Timer */ + uint32_t base; /**< Aboslut time the first event is built on */ +#else xtimer_t timer; /**< Timer */ +#endif evtimer_callback_t callback; /**< Handler function for this evtimer's event type */ evtimer_event_t *events; /**< Event queue */ diff --git a/sys/ztimer/Makefile.dep b/sys/ztimer/Makefile.dep index 1ccc74b29831a..6aff1c84c223c 100644 --- a/sys/ztimer/Makefile.dep +++ b/sys/ztimer/Makefile.dep @@ -27,7 +27,7 @@ endif ifneq (,$(filter ztimer,$(USEMODULE))) ifneq (,$(filter xtimer,$(USEMODULE))) ifeq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) - USEMODULE += xtimer_on_ztimer + USEMODULE += xtimer_on_ztimer endif endif endif @@ -38,6 +38,12 @@ ifneq (,$(filter xtimer_on_ztimer,$(USEMODULE))) PSEUDOMODULES += xtimer_on_ztimer endif +# make evtimer use ztimer_msec as low level timer +ifneq (,$(filter evtimer_on_ztimer,$(USEMODULE))) + USEMODULE += ztimer_msec + PSEUDOMODULES += evtimer_on_ztimer +endif + # "ztimer_xtimer_compat" is a wrapper of the xtimer API on ztimer_used # (it is currently incomplete). Unless doing testing, use "xtimer_on_ztimer". ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))