Skip to content

Commit

Permalink
sys/ztimer: add xtimer_on_ztimer
Browse files Browse the repository at this point in the history
This commit adds logic to make xtimer use ztimer_usec as backend
(instead of periph_timer). This allows ztimer_usec and xtimer to
coexist. It also allows xtimer to profit from eventually implemented
power mode blocking in ztimer's periph_timer backend.
kaspar030 committed Mar 4, 2020
1 parent 21613b0 commit dd21833
Showing 5 changed files with 73 additions and 13 deletions.
26 changes: 18 additions & 8 deletions Makefile.dep
Original file line number Diff line number Diff line change
@@ -675,14 +675,6 @@ ifneq (,$(filter arduino_pwm,$(FEATURES_USED)))
FEATURES_REQUIRED += periph_pwm
endif

ifneq (,$(filter xtimer,$(USEMODULE)))
ifeq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
DEFAULT_MODULE += auto_init_xtimer
FEATURES_REQUIRED += periph_timer
USEMODULE += div
endif
endif

ifneq (,$(filter saul,$(USEMODULE)))
USEMODULE += phydat
endif
@@ -1015,10 +1007,28 @@ ifneq (,$(filter periph_uart_nonblocking,$(USEMODULE)))
FEATURES_REQUIRED += periph_uart
endif

# include ztimer dependencies
ifneq (,$(filter ztimer%,$(USEMODULE)))
include $(RIOTBASE)/sys/ztimer/Makefile.dep
endif

# handle xtimer's deps. Needs to be done *after* ztimer
ifneq (,$(filter xtimer,$(USEMODULE)))
ifeq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
# xtimer is used, ztimer xtimer wrapper is not
DEFAULT_MODULE += auto_init_xtimer
USEMODULE += div
ifeq (,$(filter xtimer_on_ztimer,$(USEMODULE)))
# ztimer is not used, so use *periph_timer as low-level timer*.
FEATURES_REQUIRED += periph_timer
else
# will use *ztimer_usec as low-level timer*
endif
else
# ztimer_xtimer_compat is used, all of *xtimer's API will be mapped on ztimer.*
endif
endif

# Enable periph_gpio when periph_gpio_irq is enabled
ifneq (,$(filter periph_gpio_irq,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio
2 changes: 2 additions & 0 deletions sys/include/xtimer.h
Original file line number Diff line number Diff line change
@@ -42,8 +42,10 @@
#include "ztimer/xtimer_compat.h"
#else

#ifndef MODULE_XTIMER_ON_ZTIMER
#include "board.h"
#include "periph_conf.h"
#endif

#ifdef __cplusplus
extern "C" {
11 changes: 11 additions & 0 deletions sys/include/xtimer/implementation.h
Original file line number Diff line number Diff line change
@@ -27,9 +27,15 @@
#error "Do not include this file directly! Use xtimer.h instead"
#endif

#ifdef MODULE_XTIMER_ON_ZTIMER
#include "ztimer.h"
#else
#include "periph/timer.h"
#endif

#include "irq.h"


#ifdef __cplusplus
extern "C" {
#endif
@@ -46,7 +52,12 @@ extern volatile uint64_t _xtimer_current_time;
*/
static inline uint32_t _xtimer_lltimer_now(void)
{
#ifndef MODULE_XTIMER_ON_ZTIMER
return timer_read(XTIMER_DEV);
#else
return ztimer_now(ZTIMER_USEC);
#endif

}

/**
23 changes: 23 additions & 0 deletions sys/xtimer/xtimer_core.c
Original file line number Diff line number Diff line change
@@ -24,9 +24,12 @@

#include <stdint.h>
#include <string.h>

#ifndef MODULE_XTIMER_ON_ZTIMER
#include "board.h"
#include "periph/timer.h"
#include "periph_conf.h"
#endif

#include "xtimer.h"
#include "irq.h"
@@ -50,12 +53,20 @@ static inline void _update_long_timers(uint64_t *now);
static inline void _schedule_earliest_lltimer(uint32_t now);

static void _timer_callback(void);

#ifndef MODULE_XTIMER_ON_ZTIMER
static void _periph_timer_callback(void *arg, int chan);
#else
static void _ztimer_callback(void *arg);
static ztimer_t _ztimer = { .callback=_ztimer_callback };
#endif

void xtimer_init(void)
{
#ifndef MODULE_XTIMER_ON_ZTIMER
/* initialize low-level timer */
timer_init(XTIMER_DEV, XTIMER_HZ, _periph_timer_callback, NULL);
#endif

/* register initial overflow tick */
_schedule_earliest_lltimer(_xtimer_now());
@@ -107,12 +118,20 @@ void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_offset)
irq_restore(state);
}

#ifndef MODULE_XTIMER_ON_ZTIMER
static void _periph_timer_callback(void *arg, int chan)
{
(void)arg;
(void)chan;
_timer_callback();
}
#else
static void _ztimer_callback(void *arg)
{
(void)arg;
_timer_callback();
}
#endif

static void _shoot(xtimer_t *timer)
{
@@ -141,7 +160,11 @@ static inline void _schedule_earliest_lltimer(uint32_t now)
}

DEBUG("_schedule_earliest_lltimer(): setting %" PRIu32 "\n", _xtimer_lltimer_mask(target));
#ifndef MODULE_XTIMER_ON_ZTIMER
timer_set_absolute(XTIMER_DEV, XTIMER_CHAN, _xtimer_lltimer_mask(target));
#else
ztimer_set(ZTIMER_USEC, &_ztimer, target - ztimer_now(ZTIMER_USEC));
#endif
_lltimer_ongoing = true;
}

24 changes: 19 additions & 5 deletions sys/ztimer/Makefile.dep
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@
# ztimer dependencies
#


# "ztimer" is the default meta-module of ztimer
ifneq (,$(filter ztimer,$(USEMODULE)))
USEMODULE += ztimer_core
@@ -24,8 +23,23 @@ ifneq (,$(filter ztimer,$(USEMODULE)))
endif
endif

# unless ztimer_xtimer_compat is used, make xtimer use ztimer as backend.
ifneq (,$(filter ztimer,$(USEMODULE)))
ifneq (,$(filter xtimer,$(USEMODULE)))
ifeq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
USEMODULE += xtimer_on_ztimer
endif
endif
endif

# make xtimer use ztimer_usec as low level timer
ifneq (,$(filter xtimer_on_ztimer,$(USEMODULE)))
USEMODULE += ztimer_usec
PSEUDOMODULES += xtimer_on_ztimer
endif

# "ztimer_xtimer_compat" is a wrapper of the xtimer API on ztimer_used
# (it is currently incomplete)
# (it is currently incomplete). Unless doing testing, use "xtimer_on_ztimer".
ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
USEMODULE += div
USEMODULE += ztimer_usec
@@ -41,11 +55,11 @@ ifneq (,$(filter ztimer_convert_%,$(USEMODULE)))
USEMODULE += ztimer_convert
endif

ifneq (,$(filter ztimer_periph,$(USEMODULE)))
ifneq (,$(filter ztimer_periph_timer,$(USEMODULE)))
FEATURES_REQUIRED += periph_timer
endif

ifneq (,$(filter ztimer_rtt,$(USEMODULE)))
ifneq (,$(filter ztimer_periph_rtt,$(USEMODULE)))
FEATURES_REQUIRED += periph_rtt
endif

@@ -55,7 +69,7 @@ endif

ifneq (,$(filter ztimer_usec,$(USEMODULE)))
USEMODULE += ztimer
USEMODULE += ztimer_periph
USEMODULE += ztimer_periph_timer
endif

ifneq (,$(filter ztimer_msec,$(USEMODULE)))

0 comments on commit dd21833

Please sign in to comment.