Skip to content

Commit

Permalink
sys/ztimer/xtimer_compat: provide more fallback options
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Nov 15, 2024
1 parent 77c4a24 commit a5c385a
Showing 1 changed file with 147 additions and 24 deletions.
171 changes: 147 additions & 24 deletions sys/include/ztimer/xtimer_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
/* make sure to overwrite potentially conflicting XTIMER_WIDTH definition from
* board.h by eagerly including it */
#include "board.h"
#include "busy_wait.h"
#include "timex.h"
#ifdef MODULE_CORE_MSG
#include "msg.h"
#endif /* MODULE_CORE_MSG */
#include "macros/math.h"
#include "mutex.h"
#include "sched.h"

Expand Down Expand Up @@ -88,6 +90,11 @@ static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t 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)
{
return ticks;
Expand All @@ -105,12 +112,28 @@ static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)

static inline xtimer_ticks32_t xtimer_now(void)
{
return ztimer_now(ZTIMER_USEC);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
return ztimer_now(ZTIMER_USEC);
}
else if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
return ztimer_now(ZTIMER_MSEC) * US_PER_MS;
}
else {
return 0;
}
}

static inline uint32_t xtimer_now_usec(void)
{
return ztimer_now(ZTIMER_USEC);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
return ztimer_now(ZTIMER_USEC);
}
else if (IS_ACTIVE(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,
Expand Down Expand Up @@ -142,19 +165,30 @@ static inline void xtimer_msleep(uint32_t milliseconds)
if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
ztimer_sleep(ZTIMER_MSEC, milliseconds);
}
else {
else if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
_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)
{
ztimer_sleep(ZTIMER_USEC, microseconds);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_sleep(ZTIMER_USEC, microseconds);
}
else if (IS_ACTIVE(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)
{
ztimer_sleep(ZTIMER_USEC, nanoseconds / NS_PER_US);
xtimer_usleep(nanoseconds / NS_PER_US);
}

static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
Expand All @@ -176,60 +210,141 @@ static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)

static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
{
ztimer_set(ZTIMER_USEC, timer, offset);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_set(ZTIMER_USEC, timer, offset);
}
else if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
ztimer_set(ZTIMER_MSEC, timer, DIV_ROUND_UP(offset, US_PER_MS));
}
else {
assert(0);
}
}

static inline void xtimer_remove(xtimer_t *timer)
{
ztimer_remove(ZTIMER_USEC, timer);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_remove(ZTIMER_USEC, timer);
}
else if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
ztimer_remove(ZTIMER_MSEC, timer);
}
else {
assert(0);
}
}

static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg,
kernel_pid_t target_pid)
{
ztimer_set_msg(ZTIMER_USEC, timer, offset, msg, target_pid);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_set_msg(ZTIMER_USEC, timer, offset, msg, target_pid);
}
else if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
ztimer_set_msg(ZTIMER_MSEC, timer, DIV_ROUND_UP(offset, US_PER_MS),
msg, target_pid);
}
else {
assert(0);
}
}

static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup,
uint32_t period)
{
ztimer_periodic_wakeup(ZTIMER_USEC, last_wakeup, period);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_periodic_wakeup(ZTIMER_USEC, last_wakeup, period);
}
else if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
ztimer_periodic_wakeup(ZTIMER_MSEC, last_wakeup, DIV_ROUND_UP(period, US_PER_MS));
}
else {
assert(0);
}
}

static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
{
return ztimer_msg_receive_timeout(ZTIMER_USEC, msg, timeout);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
return ztimer_msg_receive_timeout(ZTIMER_USEC, msg, timeout);
}
else if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
return ztimer_msg_receive_timeout(ZTIMER_MSEC, msg, DIV_ROUND_UP(timeout, US_PER_MS));
}
else {
assert(0);
}
}

static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset,
kernel_pid_t pid)
{
ztimer_set_wakeup(ZTIMER_USEC, timer, offset, pid);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_set_wakeup(ZTIMER_USEC, timer, offset, pid);
}
else if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
ztimer_set_wakeup(ZTIMER_MSEC, timer, DIV_ROUND_UP(offset, US_PER_MS), pid);
}
else {
assert(0);
}
}

static inline int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us)
{
assert(us <= UINT32_MAX);
if (ztimer_mutex_lock_timeout(ZTIMER_USEC, mutex, (uint32_t)us)) {
/* Impedance matching required: Convert -ECANCELED error code to -1: */
return -1;
int res;

if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
assert(us <= UINT32_MAX);
res = ztimer_mutex_lock_timeout(ZTIMER_USEC, mutex, (uint32_t)us);
}
else if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
us = DIV_ROUND_UP(us, US_PER_MS);
assert(us <= UINT32_MAX);
res = ztimer_mutex_lock_timeout(ZTIMER_SEC, mutex, (uint32_t)us);
}
else {
assert(0);
res = -1;
}
return 0;

/* Impedance matching required: Convert -ECANCELED error code to -1: */
return res ? -1 : 0;
}

static inline int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t us)
static inline int xtimer_rmutex_lock_timeout(rmutex_t *mutex, 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: */
return -1;
int res;

if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
assert(us <= UINT32_MAX);
res = ztimer_rmutex_lock_timeout(ZTIMER_USEC, mutex, (uint32_t)us);
}
else if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
us = DIV_ROUND_UP(us, US_PER_MS);
assert(us <= UINT32_MAX);
res = ztimer_rmutex_lock_timeout(ZTIMER_SEC, mutex, (uint32_t)us);
}
return 0;
else {
assert(0);
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)
{
ztimer_set_timeout_flag(ZTIMER_USEC, t, timeout);
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_set_timeout_flag(ZTIMER_USEC, t, timeout);
}
else if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_set_timeout_flag(ZTIMER_USEC, t, DIV_ROUND_UP(timeout, US_PER_MS));
}
else {
assert(0);
}
}

static inline void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
Expand All @@ -240,7 +355,15 @@ static inline void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)

static inline void xtimer_spin(xtimer_ticks32_t ticks)
{
ztimer_spin(ZTIMER_USEC, xtimer_usec_from_ticks(ticks));
if (IS_ACTIVE(MODULE_ZTIMER_USEC)) {
ztimer_spin(ZTIMER_USEC, xtimer_usec_from_ticks(ticks));
}
else if (IS_ACTIVE(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,
Expand Down

0 comments on commit a5c385a

Please sign in to comment.