1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/event/timeout: add option to use ztimer as backend

This commit is contained in:
Francisco Molina 2021-05-05 10:12:14 +02:00
parent 77864c660d
commit 4f34e50130
No known key found for this signature in database
GPG Key ID: 3E94EAC3DBDEEDA8
5 changed files with 85 additions and 5 deletions

View File

@ -25,6 +25,7 @@ PSEUDOMODULES += devfs_%
PSEUDOMODULES += dhcpv6_%
PSEUDOMODULES += ecc_%
PSEUDOMODULES += event_%
PSEUDOMODULES += event_timeout_ztimer
PSEUDOMODULES += evtimer_mbox
PSEUDOMODULES += evtimer_on_ztimer
PSEUDOMODULES += fmt_%

View File

@ -474,8 +474,16 @@ ifneq (,$(filter event_thread_%,$(USEMODULE)))
USEMODULE += event_thread
endif
ifneq (,$(filter event_timeout_ztimer,$(USEMODULE)))
USEMODULE += event_timeout
endif
ifneq (,$(filter event_timeout,$(USEMODULE)))
USEMODULE += xtimer
ifneq (,$(filter event_timeout_ztimer,$(USEMODULE)))
USEMODULE += ztimer_usec
else
USEMODULE += xtimer
endif
endif
ifneq (,$(filter event,$(USEMODULE)))

View File

@ -8,6 +8,12 @@
* directory for more details.
*/
#include "kernel_defines.h"
#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER)
#include "ztimer.h"
#else
#include "xtimer.h"
#endif
#include "event/timeout.h"
static void _event_timeout_callback(void *arg)
@ -16,7 +22,7 @@ static void _event_timeout_callback(void *arg)
event_post(event_timeout->queue, event_timeout->event);
}
void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, event_t *event)
static 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;
@ -24,12 +30,38 @@ void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, ev
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)
ztimer_remove(event_timeout->clock, &event_timeout->timer);
#else
xtimer_remove(&event_timeout->timer);
#endif
}

View File

@ -38,7 +38,11 @@
#define EVENT_TIMEOUT_H
#include "event.h"
#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER)
#include "ztimer.h"
#else
#include "xtimer.h"
#endif
#ifdef __cplusplus
extern "C" {
@ -48,15 +52,35 @@ extern "C" {
* @brief Timeout Event structure
*/
typedef struct {
xtimer_t timer; /**< xtimer object used for timeout */
#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER)
ztimer_t timer; /**< ztimer object used for timeout */
ztimer_clock_t *clock; /**< ztimer clock to use */
#else
xtimer_t timer; /**< ztimer object used for timeout */
#endif
event_queue_t *queue; /**< event queue to post event to */
event_t *event; /**< event to post after timeout */
} event_timeout_t;
#if IS_USED(MODULE_EVENT_TIMEOUT_ZTIMER)
/**
* @brief Initialize timeout event object
*
* @param[in] event_timeout event_timeout object to initialize
* @param[in] clock the clock backend, eg: ZTIMER_USEC, ZTIMER_MSEC
* @param[in] queue queue that the timed-out event will be added to
* @param[in] event event to add to queue after timeout
*/
void event_timeout_ztimer_init(event_timeout_t *event_timeout, ztimer_clock_t* clock,
event_queue_t *queue, event_t *event);
#endif
/**
* @brief Initialize timeout event object
*
* @note If ztimer is used the default time clock backend is ZTIMER_USEC
*
* @param[in] event_timeout event_timeout object to initialize
* @param[in] queue queue that the timed-out event will be added to
* @param[in] event event to add to queue after timeout
*/
@ -67,13 +91,15 @@ void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue,
* @brief Set a timeout
*
* This will make the event as configured in @p event_timeout be triggered
* after @p timeout microseconds.
* after @p timeout microseconds (if using @ref xtimer) or the the @ref
* ztimer_clock_t ticks.
*
* @note: the used event_timeout struct must stay valid until after the timeout
* event has been processed!
*
* @param[in] event_timeout event_timout context object to use
* @param[in] timeout timeout in microseconds
* @param[in] timeout timeout in microseconds ot the ztimer_clock_t
* ticks units
*/
void event_timeout_set(event_timeout_t *event_timeout, uint32_t timeout);

View File

@ -23,11 +23,16 @@
#include <stdio.h>
#include "test_utils/expect.h"
#include "timex.h"
#include "thread.h"
#include "event.h"
#include "event/timeout.h"
#include "event/callback.h"
#if IS_USED(MODULE_ZTIMER)
#include "ztimer.h"
#else
#include "xtimer.h"
#endif
#define STACKSIZE THREAD_STACKSIZE_DEFAULT
#define PRIO (THREAD_PRIORITY_MAIN - 1)
@ -83,7 +88,11 @@ static void timed_callback(void *arg)
order++;
expect(order == 6);
expect(arg == event_callback.arg);
#if IS_USED(MODULE_ZTIMER)
uint32_t now = ztimer_now(ZTIMER_USEC);
#else
uint32_t now = xtimer_now_usec();
#endif
expect((now - before >= 100000LU));
printf("triggered timed callback with arg 0x%08x after %" PRIu32 "us\n", (unsigned)arg, now - before);
puts("[SUCCESS]");
@ -177,7 +186,11 @@ int main(void)
puts("posting timed callback with timeout 1sec");
event_timeout_init(&event_timeout, &queue, (event_t *)&event_callback);
#if IS_USED(MODULE_ZTIMER)
before = ztimer_now(ZTIMER_USEC);
#else
before = xtimer_now_usec();
#endif
event_timeout_set(&event_timeout, (1 * US_PER_SEC));
event_timeout_t event_timeout_canceled;