From 0440d27d768d4650bbb779ed39d19f589cf1effa Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Thu, 19 Mar 2020 18:49:04 +0100 Subject: [PATCH 1/5] tests/evtimer_msg: require xtimer explicitly --- tests/evtimer_msg/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/evtimer_msg/Makefile b/tests/evtimer_msg/Makefile index 1f75bb66a5f4..a980101a4eae 100644 --- a/tests/evtimer_msg/Makefile +++ b/tests/evtimer_msg/Makefile @@ -1,6 +1,7 @@ include ../Makefile.tests_common USEMODULE += evtimer +USEMODULE += xtimer # This test randomly fails on `native` so disable it from CI TEST_ON_CI_BLACKLIST += native From 90e7879ef20340bd6354185e632682ef8e30ccb1 Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Fri, 15 May 2020 20:49:03 +0200 Subject: [PATCH 2/5] tests/evtimer_msg: use evtimer_now_msec() to get current time --- tests/evtimer_msg/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/evtimer_msg/main.c b/tests/evtimer_msg/main.c index 136992709954..1586eb61c2d1 100644 --- a/tests/evtimer_msg/main.c +++ b/tests/evtimer_msg/main.c @@ -54,7 +54,7 @@ void *worker_thread(void *arg) uint32_t now; msg_receive(&m); - now = xtimer_now_usec() / US_PER_MS; + now = evtimer_now_msec(); ctx = m.content.ptr; printf("At %6" PRIu32 " ms received msg %i: \"%s\"\n", now, count++, ctx); } @@ -77,7 +77,7 @@ int main(void) /* Add all the events */ for (unsigned i = 0; i < NEVENTS; i++) { events[i].event.offset = offsets[i]; - now = xtimer_now_usec() / US_PER_MS; + now = evtimer_now_msec(); snprintf(texts[i], sizeof(texts[i]) - 1, "#%u supposed to be %" PRIu32, i, now + events[i].event.offset); events[i].msg.content.ptr = texts[i]; evtimer_add_msg(&evtimer, &events[i], pid); @@ -99,7 +99,7 @@ int main(void) /* Add all the events, again */ for (unsigned i = 0; i < NEVENTS; i++) { events[i].event.offset = offsets[i]; - now = xtimer_now_usec() / US_PER_MS; + now = evtimer_now_msec(); snprintf(texts[i], sizeof(texts[i]) - 1, "#%u supposed to be %" PRIu32, i, now + events[i].event.offset); events[i].msg.content.ptr = texts[i]; evtimer_add_msg(&evtimer, &events[i], pid); From c3a20f8fb82151ffe8f9c0640cbc46501ba1ce9e Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Thu, 19 Mar 2020 18:50:43 +0100 Subject: [PATCH 3/5] sys/evtimer: introduce ZTIMER_MSEC as timer backend --- Makefile.dep | 11 +++++---- makefiles/pseudomodules.inc.mk | 1 + sys/evtimer/evtimer.c | 44 ++++++++++++++++++++++++++++------ sys/include/evtimer.h | 19 +++++++++++++++ sys/ztimer/Makefile.dep | 9 +++++++ 5 files changed, 73 insertions(+), 11 deletions(-) diff --git a/Makefile.dep b/Makefile.dep index 49b10fac63f7..5dece5690e9a 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -737,10 +737,6 @@ ifneq (,$(filter phydat,$(USEMODULE))) USEMODULE += fmt endif -ifneq (,$(filter evtimer,$(USEMODULE))) - USEMODULE += xtimer -endif - ifneq (,$(filter evtimer_mbox,$(USEMODULE))) USEMODULE += evtimer USEMODULE += core_mbox @@ -1067,6 +1063,13 @@ ifneq (,$(filter ztimer% %ztimer,$(USEMODULE))) include $(RIOTBASE)/sys/ztimer/Makefile.dep endif +# handle evtimer's deps. Needs to be done *after* ztimer +ifneq (,$(filter evtimer,$(USEMODULE))) + ifeq (,$(filter evtimer_on_ztimer,$(USEMODULE))) + USEMODULE += xtimer + endif +endif + # handle xtimer's deps. Needs to be done *after* ztimer ifneq (,$(filter xtimer,$(USEMODULE))) ifeq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 4f475711b408..d3e9d847561d 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -21,6 +21,7 @@ PSEUDOMODULES += dhcpv6_% PSEUDOMODULES += ecc_% PSEUDOMODULES += event_% PSEUDOMODULES += evtimer_mbox +PSEUDOMODULES += evtimer_on_ztimer PSEUDOMODULES += fmt_% PSEUDOMODULES += gnrc_dhcpv6_% PSEUDOMODULES += gnrc_ipv6_default diff --git a/sys/evtimer/evtimer.c b/sys/evtimer/evtimer.c index 3c9e13973b2e..7ec9beb8004d 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; + +#if IS_USED(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 + 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 } 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 { +#if IS_USED(MODULE_EVTIMER_ON_ZTIMER) + ztimer_remove(ZTIMER_MSEC, &evtimer->timer); +#else xtimer_remove(&evtimer->timer); +#endif } } +#if IS_USED(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 /* IS_USED(MODULE_EVTIMER_ON_ZTIMER) */ static uint32_t _get_offset(xtimer_t *timer) { uint64_t left = xtimer_left_usec(timer); @@ -127,6 +156,7 @@ static void _update_head_offset(evtimer_t *evtimer) DEBUG("evtimer: _update_head_offset(): new head offset %" PRIu32 "\n", event->offset); } } +#endif /* !IS_USED(MODULE_EVTIMER_ON_ZTIMER) */ void evtimer_add(evtimer_t *evtimer, evtimer_event_t *event) { @@ -137,7 +167,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 f4d7580350dd..b51a03cd9715 100644 --- a/sys/include/evtimer.h +++ b/sys/include/evtimer.h @@ -44,8 +44,14 @@ #define EVTIMER_H #include +#include "kernel_defines.h" +#if IS_USED(MODULE_EVTIMER_ON_ZTIMER) +#include "ztimer.h" +#else #include "xtimer.h" +#endif + #include "timex.h" #ifdef __cplusplus @@ -69,7 +75,12 @@ typedef void(*evtimer_callback_t)(evtimer_event_t* event); * @brief Event timer */ typedef struct { +#if IS_USED(MODULE_EVTIMER_ON_ZTIMER) + ztimer_t timer; /**< Timer */ + uint32_t base; /**< Absolute 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 */ @@ -111,7 +122,11 @@ void evtimer_print(const evtimer_t *evtimer); */ static inline uint32_t evtimer_now_msec(void) { +#if IS_USED(MODULE_EVTIMER_ON_ZTIMER) + return ztimer_now(ZTIMER_MSEC); +#else return xtimer_now_usec64() / US_PER_MS; +#endif } /** @@ -119,7 +134,11 @@ static inline uint32_t evtimer_now_msec(void) */ static inline uint32_t evtimer_now_min(void) { +#if IS_USED(MODULE_EVTIMER_ON_ZTIMER) + return ztimer_now(ZTIMER_MSEC) / (MS_PER_SEC * SEC_PER_MIN); +#else return xtimer_now_usec64() / (US_PER_SEC * SEC_PER_MIN); +#endif } #ifdef __cplusplus diff --git a/sys/ztimer/Makefile.dep b/sys/ztimer/Makefile.dep index d616f8284931..c72b3c3828e3 100644 --- a/sys/ztimer/Makefile.dep +++ b/sys/ztimer/Makefile.dep @@ -30,6 +30,9 @@ ifneq (,$(filter ztimer_periph_timer,$(USEMODULE))) USEMODULE += xtimer_on_ztimer endif endif + ifneq (,$(filter evtimer,$(USEMODULE))) + USEMODULE += evtimer_on_ztimer + endif endif # make xtimer use ztimer_usec as low level timer @@ -37,6 +40,12 @@ ifneq (,$(filter xtimer_on_ztimer,$(USEMODULE))) USEMODULE += ztimer_usec endif +# make evtimer use ztimer_msec as low level timer +ifneq (,$(filter evtimer_on_ztimer,$(USEMODULE))) + USEMODULE += ztimer_msec + USEMODULE += ztimer_now64 +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))) From 838d1007b3fb451dab84303465322ca349d0d7e6 Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Thu, 24 Sep 2020 13:06:47 +0200 Subject: [PATCH 4/5] gnrc/mac/timeout: add missing include --- sys/include/net/gnrc/mac/timeout.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sys/include/net/gnrc/mac/timeout.h b/sys/include/net/gnrc/mac/timeout.h index 9a84940bfb2f..9fbbe34f5280 100644 --- a/sys/include/net/gnrc/mac/timeout.h +++ b/sys/include/net/gnrc/mac/timeout.h @@ -21,6 +21,7 @@ #ifndef NET_GNRC_MAC_TIMEOUT_H #define NET_GNRC_MAC_TIMEOUT_H +#include #include #include From dfec601dea5610b5cd3312c960db83969a079a3a Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Fri, 25 Sep 2020 18:55:01 +0200 Subject: [PATCH 5/5] sys/evtimer: adjusted documentation for evtimer_on_ztimer --- sys/include/evtimer.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sys/include/evtimer.h b/sys/include/evtimer.h index b51a03cd9715..575815081c58 100644 --- a/sys/include/evtimer.h +++ b/sys/include/evtimer.h @@ -29,7 +29,9 @@ * the necessary fields, which can be extended as needed, and handlers define * actions taken on timer triggers. Check out @ref evtimer_msg_event_t as * example. - * - uses @ref sys_xtimer "xtimer" as backend + * - uses @ref sys_xtimer "xtimer" as backend by default. Alternatively, with + * the pseudomodule "evtimer_on_ztimer" compiled in, evtimer is backend by + * @ref sys_ztimer "ZTIMER_MSEC". * * @{ *