From 81c5d5dbcc59ee49575dc6c749869a5307b283fb Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Wed, 6 Oct 2021 18:18:32 +0200 Subject: [PATCH 1/4] sys/event/timeout: split xtimer, ztimer backends, don't force usec This PR makes `event_timeout` and `event_timeout_ztimer` two distinct pseudomodules, where the only api difference is in the init function. If only `event_timeout_ztimer` is selected then no default ZTIMER backend is selected and the old init function is not implemented. If only `event_timeout` is selected then `xtimer` is used unless `ztimer_usec` is included. In which case the `xtimer` wrapper on top of `ztimer` is used and `xtimer` is not directly selected. This allows for the legacy api to be supported with `ztimer_usec` as a drop-in replacement. If `event_timeout` and `event_timeut_ztimer` are selected then `event_timeout` SRC file is excluded from compilation. --- makefiles/pseudomodules.inc.mk | 1 + sys/Makefile.dep | 16 ++++++----- sys/event/Kconfig | 26 ++++++++++++++++-- sys/event/Makefile | 6 +++++ sys/event/timeout.c | 37 ++++--------------------- sys/event/timeout_ztimer.c | 49 ++++++++++++++++++++++++++++++++++ sys/include/event/timeout.h | 8 ++++-- 7 files changed, 100 insertions(+), 43 deletions(-) create mode 100644 sys/event/timeout_ztimer.c diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index e3607b0c24..8ac0fba286 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -25,6 +25,7 @@ PSEUDOMODULES += dhcpv6_relay PSEUDOMODULES += dns_msg PSEUDOMODULES += ecc_% PSEUDOMODULES += event_% +PSEUDOMODULES += event_timeout PSEUDOMODULES += event_timeout_ztimer PSEUDOMODULES += evtimer_mbox PSEUDOMODULES += evtimer_on_ztimer diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 70077f33f5..397beef26f 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -512,22 +512,24 @@ ifneq (,$(filter event_thread_%,$(USEMODULE))) endif ifneq (,$(filter event_timeout_ztimer,$(USEMODULE))) - USEMODULE += event_timeout -endif - -ifneq (,$(filter event_periodic_timeout,$(USEMODULE))) - USEMODULE += event_timeout - USEMODULE += ztimer_periodic + USEMODULE += ztimer endif ifneq (,$(filter event_timeout,$(USEMODULE))) ifneq (,$(filter event_timeout_ztimer,$(USEMODULE))) USEMODULE += ztimer_usec else - USEMODULE += xtimer + ifeq (,$(filter ztimer_usec,$(USEMODULE))) + USEMODULE += xtimer + endif endif endif +ifneq (,$(filter event_periodic_timeout,$(USEMODULE))) + USEMODULE += event_timeout + USEMODULE += ztimer_periodic +endif + ifneq (,$(filter event,$(USEMODULE))) USEMODULE += core_thread_flags endif diff --git a/sys/event/Kconfig b/sys/event/Kconfig index 68b51dd446..8a66481042 100644 --- a/sys/event/Kconfig +++ b/sys/event/Kconfig @@ -39,8 +39,30 @@ config MODULE_EVENT_THREAD_HIGHEST endif # MODULE_EVENT_THREAD +config MODULE_EVENT_TIMEOUT_ZTIMER + bool "Support for triggering events after timeout, ztimer backend" + select MODULE_ZTIMER + config MODULE_EVENT_TIMEOUT - bool "Support for triggering events after timeout" - select MODULE_XTIMER + bool "Legacy API, support for triggering events after timeout" + +if MODULE_EVENT_TIMEOUT + +choice EVENT_TIMEOUT_IMPLEMENTATION + bool "Event Timeout Implementation" + default EVENT_TIMEOUT_ON_XTIMER + +config EVENT_TIMEOUT_ON_ZTIMER + bool "Use ztimer as backend" + select MODULE_EVENT_TIMEOUT_ZTIMER + select MODULE_ZTIMER_USEC + +config EVENT_TIMEOUT_ON_XTIMER + bool "Use xtimer as backend" + select MODULE_XTIMER if !ZTIMER_USEC + +endchoice # EVENT_TIMEOUT_IMPLEMENTATION + +endif # MODULE_EVENT_TIMEOUT endif # MODULE_EVENT diff --git a/sys/event/Makefile b/sys/event/Makefile index 5343c5af5d..0f3bbf926a 100644 --- a/sys/event/Makefile +++ b/sys/event/Makefile @@ -2,4 +2,10 @@ SRC := event.c SUBMODULES := 1 +ifneq (,$(filter event_timeout_ztimer,$(USEMODULE))) + ifneq (,$(filter ztimer_usec,$(USEMODULE))) + SUBMODULES_NO_SRC += timeout.c + endif +endif + include $(RIOTBASE)/Makefile.base diff --git a/sys/event/timeout.c b/sys/event/timeout.c index b4b8ec271e..e30157201c 100644 --- a/sys/event/timeout.c +++ b/sys/event/timeout.c @@ -9,20 +9,21 @@ */ #include "kernel_defines.h" -#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) -#include "ztimer.h" +#include "event/timeout.h" +#if IS_USED(MODULE_ZTIMER_USEC) +#include "ztimer/xtimer_compat.h" #else #include "xtimer.h" #endif -#include "event/timeout.h" static void _event_timeout_callback(void *arg) { event_timeout_t *event_timeout = (event_timeout_t *)arg; + event_post(event_timeout->queue, event_timeout->event); } -static void _event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, event_t *event) +void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, event_t *event) { event_timeout->timer.callback = _event_timeout_callback; event_timeout->timer.arg = event_timeout; @@ -30,40 +31,12 @@ static void _event_timeout_init(event_timeout_t *event_timeout, event_queue_t *q event_timeout->event = event; } -void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, event_t *event) -{ -#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) - event_timeout_ztimer_init(event_timeout, ZTIMER_USEC, queue, event); -#else - _event_timeout_init(event_timeout, queue, event); -#endif -} - -#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) -void event_timeout_ztimer_init(event_timeout_t *event_timeout, ztimer_clock_t* clock, - event_queue_t *queue, event_t *event) -{ - event_timeout->clock = clock; - _event_timeout_init(event_timeout, queue, event); -} -#endif - void event_timeout_set(event_timeout_t *event_timeout, uint32_t timeout) { -#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) - ztimer_set(event_timeout->clock, &event_timeout->timer, timeout); -#else xtimer_set(&event_timeout->timer, timeout); -#endif } void event_timeout_clear(event_timeout_t *event_timeout) { -#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) - if (event_timeout->clock) { - ztimer_remove(event_timeout->clock, &event_timeout->timer); - } -#else xtimer_remove(&event_timeout->timer); -#endif } diff --git a/sys/event/timeout_ztimer.c b/sys/event/timeout_ztimer.c new file mode 100644 index 0000000000..84fd34d9b3 --- /dev/null +++ b/sys/event/timeout_ztimer.c @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2017 Inria + * 2017 Freie Universität Berlin + * 2017 Kaspar Schleiser + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +#include "kernel_defines.h" +#include "event/timeout.h" +#include "ztimer.h" + +static void _event_timeout_callback(void *arg) +{ + event_timeout_t *event_timeout = (event_timeout_t *)arg; + + event_post(event_timeout->queue, event_timeout->event); +} + +#if IS_USED(MODULE_EVENT_TIMEOUT) +void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, event_t *event) +{ + event_timeout_ztimer_init(event_timeout, ZTIMER_USEC, queue, event); +} +#endif + +void event_timeout_ztimer_init(event_timeout_t *event_timeout, ztimer_clock_t *clock, + event_queue_t *queue, event_t *event) +{ + event_timeout->clock = clock; + event_timeout->timer.callback = _event_timeout_callback; + event_timeout->timer.arg = event_timeout; + event_timeout->queue = queue; + event_timeout->event = event; +} + +void event_timeout_set(event_timeout_t *event_timeout, uint32_t timeout) +{ + ztimer_set(event_timeout->clock, &event_timeout->timer, timeout); +} + +void event_timeout_clear(event_timeout_t *event_timeout) +{ + if (event_timeout->clock) { + ztimer_remove(event_timeout->clock, &event_timeout->timer); + } +} diff --git a/sys/include/event/timeout.h b/sys/include/event/timeout.h index 5280cb268c..af5a0362dc 100644 --- a/sys/include/event/timeout.h +++ b/sys/include/event/timeout.h @@ -38,7 +38,7 @@ #define EVENT_TIMEOUT_H #include "event.h" -#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) +#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) || IS_USED(MODULE_ZTIMER_USEC) #include "ztimer.h" #else #include "xtimer.h" @@ -53,8 +53,10 @@ extern "C" { */ typedef struct { #if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) - ztimer_t timer; /**< ztimer object used for timeout */ ztimer_clock_t *clock; /**< ztimer clock to use */ +#endif +#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER) || IS_USED(MODULE_ZTIMER_USEC) + ztimer_t timer; /**< ztimer object used for timeout */ #else xtimer_t timer; /**< ztimer object used for timeout */ #endif @@ -75,6 +77,7 @@ void event_timeout_ztimer_init(event_timeout_t *event_timeout, ztimer_clock_t *c event_queue_t *queue, event_t *event); #endif +#if IS_USED(MODULE_EVENT_TIMEOUT) || DOXYGEN /** * @brief Initialize timeout event object * @@ -86,6 +89,7 @@ void event_timeout_ztimer_init(event_timeout_t *event_timeout, ztimer_clock_t *c */ void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, event_t *event); +#endif /** * @brief Set a timeout From 39b896e9330d4d85a4cfe3a32e60e1c74be003a9 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Wed, 6 Oct 2021 18:23:09 +0200 Subject: [PATCH 2/4] examples/nimble_heart_rate_sensor: use event_timeout_ztimer --- examples/nimble_heart_rate_sensor/Makefile | 2 +- examples/nimble_heart_rate_sensor/main.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/nimble_heart_rate_sensor/Makefile b/examples/nimble_heart_rate_sensor/Makefile index 70f65c6e5d..a5b1472fe6 100644 --- a/examples/nimble_heart_rate_sensor/Makefile +++ b/examples/nimble_heart_rate_sensor/Makefile @@ -8,7 +8,7 @@ BOARD ?= nrf52dk RIOTBASE ?= $(CURDIR)/../.. # Some RIOT modules needed for this example -USEMODULE += event_timeout +USEMODULE += event_timeout_ztimer # Include NimBLE USEPKG += nimble diff --git a/examples/nimble_heart_rate_sensor/main.c b/examples/nimble_heart_rate_sensor/main.c index a4def95b9d..96eee26700 100644 --- a/examples/nimble_heart_rate_sensor/main.c +++ b/examples/nimble_heart_rate_sensor/main.c @@ -36,7 +36,7 @@ #define HRS_FLAGS_DEFAULT (0x01) /* 16-bit BPM value */ #define SENSOR_LOCATION (0x02) /* wrist sensor */ -#define UPDATE_INTERVAL (250U * US_PER_MS) +#define UPDATE_INTERVAL (250U) #define BPM_MIN (80U) #define BPM_MAX (210U) #define BPM_STEP (2) @@ -287,7 +287,7 @@ int main(void) /* setup local event queue (for handling heart rate updates) */ event_queue_init(&_eq); _update_evt.handler = _hr_update; - event_timeout_init(&_update_timeout_evt, &_eq, &_update_evt); + event_timeout_ztimer_init(&_update_timeout_evt, ZTIMER_MSEC, &_eq, &_update_evt); /* verify and add our custom services */ res = ble_gatts_count_cfg(gatt_svr_svcs); From 73babe546c14e61aa37b5f395b8b4ba5e641463b Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Thu, 4 Nov 2021 09:27:43 +0100 Subject: [PATCH 3/4] tests/events: add Kconfig --- tests/events/app.config.test | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/events/app.config.test diff --git a/tests/events/app.config.test b/tests/events/app.config.test new file mode 100644 index 0000000000..e6ce8f1f76 --- /dev/null +++ b/tests/events/app.config.test @@ -0,0 +1,6 @@ +# this file enables modules defined in Kconfig. Do not use this file for +# application configuration. This is only needed during migration. +# enable event thread in lowest priority +CONFIG_MODULE_EVENT=y +CONFIG_MODULE_EVENT_CALLBACK=y +CONFIG_MODULE_EVENT_TIMEOUT=y From c74e35c48352ceb9a76cee2f8cd71c20baea052d Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Thu, 4 Nov 2021 12:21:15 +0100 Subject: [PATCH 4/4] tests/event_ztimer: add missing ztimer_usec dependency --- tests/event_ztimer/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/event_ztimer/Makefile b/tests/event_ztimer/Makefile index bce9ef00d5..23445037cf 100644 --- a/tests/event_ztimer/Makefile +++ b/tests/event_ztimer/Makefile @@ -5,5 +5,6 @@ USEMODULE += event_thread USEMODULE += event_callback USEMODULE += event_timeout_ztimer USEMODULE += event_periodic +USEMODULE += ztimer_usec include $(RIOTBASE)/Makefile.include