1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 19:12:45 +01:00

sys/ztimer/xtimer_compat: provide more fallback options

This commit is contained in:
Benjamin Valentin 2024-02-01 11:58:36 +01:00
parent 77c4a24bcf
commit be2c455c22

View File

@ -27,6 +27,7 @@
/* make sure to overwrite potentially conflicting XTIMER_WIDTH definition from /* make sure to overwrite potentially conflicting XTIMER_WIDTH definition from
* board.h by eagerly including it */ * board.h by eagerly including it */
#include "board.h" #include "board.h"
#include "busy_wait.h"
#include "timex.h" #include "timex.h"
#ifdef MODULE_CORE_MSG #ifdef MODULE_CORE_MSG
#include "msg.h" #include "msg.h"
@ -64,10 +65,27 @@ extern "C" {
#define XTIMER_BACKOFF 1 #define XTIMER_BACKOFF 1
#endif #endif
/**
* @brief Generate a link-time failure if a function is used that can not be
* implemented with the selected backends.
*/
#define _XTIMER_BACKEND_NOT_IMPLEMENTED extern void xtimer_function_called_but_no_backend_available(void); \
xtimer_function_called_but_no_backend_available()
typedef ztimer_t xtimer_t; typedef ztimer_t xtimer_t;
typedef uint32_t xtimer_ticks32_t; typedef uint32_t xtimer_ticks32_t;
typedef uint64_t xtimer_ticks64_t; typedef uint64_t xtimer_ticks64_t;
static inline uint32_t _div_round_up(uint32_t a, uint32_t b)
{
return (a + b - 1) / b;
}
static inline uint32_t _div_round_up64(uint64_t a, uint32_t b)
{
return (a + b - 1) / b;
}
static inline void xtimer_init(void) static inline void xtimer_init(void)
{ {
ztimer_init(); ztimer_init();
@ -88,6 +106,11 @@ static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
return ticks; return ticks;
} }
static inline uint32_t xtimer_msec_from_ticks(xtimer_ticks32_t ticks)
{
return ticks * US_PER_MS;
}
static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks) static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
{ {
return ticks; return ticks;
@ -105,13 +128,29 @@ static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
static inline xtimer_ticks32_t xtimer_now(void) static inline xtimer_ticks32_t xtimer_now(void)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
return ztimer_now(ZTIMER_USEC); return ztimer_now(ZTIMER_USEC);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
return ztimer_now(ZTIMER_MSEC) * US_PER_MS;
}
else {
return 0;
}
}
static inline uint32_t xtimer_now_usec(void) static inline uint32_t xtimer_now_usec(void)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
return ztimer_now(ZTIMER_USEC); return ztimer_now(ZTIMER_USEC);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
return ztimer_now(ZTIMER_MSEC) * US_PER_MS;
}
else {
return 0;
}
}
static inline void _ztimer_sleep_scale(ztimer_clock_t *clock, uint32_t time, static inline void _ztimer_sleep_scale(ztimer_clock_t *clock, uint32_t time,
uint32_t scale) uint32_t scale)
@ -129,7 +168,7 @@ static inline void _ztimer_sleep_scale(ztimer_clock_t *clock, uint32_t time,
static inline void xtimer_sleep(uint32_t seconds) static inline void xtimer_sleep(uint32_t seconds)
{ {
/* TODO: use ZTIMER_SEC */ /* TODO: use ZTIMER_SEC */
if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) { if (IS_USED(MODULE_ZTIMER_MSEC)) {
_ztimer_sleep_scale(ZTIMER_MSEC, seconds, MS_PER_SEC); _ztimer_sleep_scale(ZTIMER_MSEC, seconds, MS_PER_SEC);
} }
else { else {
@ -139,22 +178,33 @@ static inline void xtimer_sleep(uint32_t seconds)
static inline void xtimer_msleep(uint32_t milliseconds) static inline void xtimer_msleep(uint32_t milliseconds)
{ {
if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) { if (IS_USED(MODULE_ZTIMER_MSEC)) {
ztimer_sleep(ZTIMER_MSEC, milliseconds); ztimer_sleep(ZTIMER_MSEC, milliseconds);
} }
else { else if (IS_USED(MODULE_ZTIMER_USEC)) {
_ztimer_sleep_scale(ZTIMER_USEC, milliseconds, US_PER_MS); _ztimer_sleep_scale(ZTIMER_USEC, milliseconds, US_PER_MS);
} }
else {
busy_wait_us(US_PER_MS * milliseconds);
}
} }
static inline void xtimer_usleep(uint32_t microseconds) static inline void xtimer_usleep(uint32_t microseconds)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_sleep(ZTIMER_USEC, microseconds); ztimer_sleep(ZTIMER_USEC, microseconds);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
ztimer_sleep(ZTIMER_MSEC, _div_round_up(microseconds, US_PER_MS));
}
else {
busy_wait_us(microseconds);
}
}
static inline void xtimer_nanosleep(uint32_t nanoseconds) static inline void xtimer_nanosleep(uint32_t nanoseconds)
{ {
ztimer_sleep(ZTIMER_USEC, nanoseconds / NS_PER_US); xtimer_usleep(nanoseconds / NS_PER_US);
} }
static inline void xtimer_tsleep32(xtimer_ticks32_t ticks) static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
@ -176,61 +226,142 @@ static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
static inline void xtimer_set(xtimer_t *timer, uint32_t offset) static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_set(ZTIMER_USEC, timer, offset); ztimer_set(ZTIMER_USEC, timer, offset);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
ztimer_set(ZTIMER_MSEC, timer, _div_round_up(offset, US_PER_MS));
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
}
}
static inline void xtimer_remove(xtimer_t *timer) static inline void xtimer_remove(xtimer_t *timer)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_remove(ZTIMER_USEC, timer); ztimer_remove(ZTIMER_USEC, timer);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
ztimer_remove(ZTIMER_MSEC, timer);
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
}
}
static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg,
kernel_pid_t target_pid) kernel_pid_t target_pid)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_set_msg(ZTIMER_USEC, timer, offset, msg, target_pid); ztimer_set_msg(ZTIMER_USEC, timer, offset, msg, target_pid);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
ztimer_set_msg(ZTIMER_MSEC, timer, _div_round_up(offset, US_PER_MS),
msg, target_pid);
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
}
}
static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup,
uint32_t period) uint32_t period)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_periodic_wakeup(ZTIMER_USEC, last_wakeup, period); ztimer_periodic_wakeup(ZTIMER_USEC, last_wakeup, period);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
ztimer_periodic_wakeup(ZTIMER_MSEC, last_wakeup, _div_round_up(period, US_PER_MS));
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
}
}
static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout) static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
return ztimer_msg_receive_timeout(ZTIMER_USEC, msg, timeout); return ztimer_msg_receive_timeout(ZTIMER_USEC, msg, timeout);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
return ztimer_msg_receive_timeout(ZTIMER_MSEC, msg, _div_round_up(timeout, US_PER_MS));
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
}
}
static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset,
kernel_pid_t pid) kernel_pid_t pid)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_set_wakeup(ZTIMER_USEC, timer, offset, pid); ztimer_set_wakeup(ZTIMER_USEC, timer, offset, pid);
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
ztimer_set_wakeup(ZTIMER_MSEC, timer, _div_round_up(offset, US_PER_MS), pid);
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
}
}
static inline int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us) static inline int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us)
{ {
int res;
if (IS_USED(MODULE_ZTIMER_USEC)) {
assert(us <= UINT32_MAX); assert(us <= UINT32_MAX);
if (ztimer_mutex_lock_timeout(ZTIMER_USEC, mutex, (uint32_t)us)) { res = ztimer_mutex_lock_timeout(ZTIMER_USEC, mutex, (uint32_t)us);
/* Impedance matching required: Convert -ECANCELED error code to -1: */
return -1;
} }
return 0; else if (IS_USED(MODULE_ZTIMER_USEC)) {
us = _div_round_up64(us, US_PER_MS);
assert(us <= UINT32_MAX);
res = ztimer_mutex_lock_timeout(ZTIMER_SEC, mutex, (uint32_t)us);
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
res = -1;
} }
static inline int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t us)
{
assert(us <= UINT32_MAX);
if (ztimer_rmutex_lock_timeout(ZTIMER_USEC, rmutex, us)) {
/* Impedance matching required: Convert -ECANCELED error code to -1: */ /* Impedance matching required: Convert -ECANCELED error code to -1: */
return -1; return res ? -1 : 0;
} }
return 0;
static inline int xtimer_rmutex_lock_timeout(rmutex_t *mutex, uint64_t us)
{
int res;
if (IS_USED(MODULE_ZTIMER_USEC)) {
assert(us <= UINT32_MAX);
res = ztimer_rmutex_lock_timeout(ZTIMER_USEC, mutex, (uint32_t)us);
}
else if (IS_USED(MODULE_ZTIMER_USEC)) {
us = _div_round_up64(us, US_PER_MS);
assert(us <= UINT32_MAX);
res = ztimer_rmutex_lock_timeout(ZTIMER_SEC, mutex, (uint32_t)us);
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
res = -1;
}
/* Impedance matching required: Convert -ECANCELED error code to -1: */
return res ? -1 : 0;
} }
static inline void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout) static inline void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_set_timeout_flag(ZTIMER_USEC, t, timeout); ztimer_set_timeout_flag(ZTIMER_USEC, t, timeout);
} }
else if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_set_timeout_flag(ZTIMER_USEC, t, _div_round_up(timeout, US_PER_MS));
}
else {
_XTIMER_BACKEND_NOT_IMPLEMENTED;
}
}
static inline void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout) static inline void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
{ {
@ -240,8 +371,16 @@ static inline void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
static inline void xtimer_spin(xtimer_ticks32_t ticks) static inline void xtimer_spin(xtimer_ticks32_t ticks)
{ {
if (IS_USED(MODULE_ZTIMER_USEC)) {
ztimer_spin(ZTIMER_USEC, xtimer_usec_from_ticks(ticks)); ztimer_spin(ZTIMER_USEC, xtimer_usec_from_ticks(ticks));
} }
else if (IS_USED(MODULE_ZTIMER_MSEC)) {
ztimer_spin(ZTIMER_MSEC, xtimer_msec_from_ticks(ticks));
}
else {
busy_wait_us(xtimer_usec_from_ticks(ticks));
}
}
static inline xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a, static inline xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a,
xtimer_ticks32_t b) xtimer_ticks32_t b)