Skip to content

Commit

Permalink
sys/evtimer: introduce ZTIMER_MSEC as timer backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jue89 committed Mar 19, 2020
1 parent 028ac3f commit b8d8159
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
44 changes: 37 additions & 7 deletions sys/evtimer/evtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include "div.h"
#include "irq.h"
#include "xtimer.h"

#include "evtimer.h"

Expand Down Expand Up @@ -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();
Expand All @@ -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)
{
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions sys/include/evtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@

#include <stdint.h>

#ifdef MODULE_EVTIMER_ON_ZTIMER
#include "ztimer.h"
#else
#include "xtimer.h"
#endif

#ifdef __cplusplus
extern "C" {
Expand All @@ -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 */
Expand Down
8 changes: 7 additions & 1 deletion sys/ztimer/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))
Expand Down

0 comments on commit b8d8159

Please sign in to comment.