diff --git a/sys/include/xtimer.h b/sys/include/xtimer.h index 44b334ddd9..fd732abe05 100644 --- a/sys/include/xtimer.h +++ b/sys/include/xtimer.h @@ -146,6 +146,13 @@ void xtimer_init(void); */ static inline void xtimer_sleep(uint32_t seconds); +/** + * @brief Pause the execution of a thread for some milliseconds + * + * @param[in] milliseconds the amount of milliseconds the thread should sleep + */ +static inline void xtimer_msleep(uint32_t milliseconds); + /** * @brief Pause the execution of a thread for some microseconds * diff --git a/sys/include/xtimer/implementation.h b/sys/include/xtimer/implementation.h index b09a9252fb..56eec32948 100644 --- a/sys/include/xtimer/implementation.h +++ b/sys/include/xtimer/implementation.h @@ -172,6 +172,11 @@ static inline void xtimer_spin(xtimer_ticks32_t ticks) { _xtimer_spin(ticks.ticks32); } +static inline void xtimer_msleep(uint32_t milliseconds) +{ + _xtimer_tsleep64(_xtimer_ticks_from_usec64(milliseconds * US_PER_MS)); +} + static inline void xtimer_usleep(uint32_t microseconds) { _xtimer_tsleep32(_xtimer_ticks_from_usec(microseconds)); diff --git a/sys/include/ztimer/xtimer_compat.h b/sys/include/ztimer/xtimer_compat.h index 8751fac4da..92ff694d57 100644 --- a/sys/include/ztimer/xtimer_compat.h +++ b/sys/include/ztimer/xtimer_compat.h @@ -68,10 +68,35 @@ static inline uint64_t xtimer_now_usec64(void) return ztimer_now(ZTIMER_USEC); } +static inline void _ztimer_sleep_scale(ztimer_clock_t *clock, uint32_t time, uint32_t scale) +{ + const uint32_t max_sleep = UINT32_MAX / scale; + + while (time > max_sleep) { + ztimer_sleep(clock, max_sleep * scale); + time -= max_sleep; + } + + ztimer_sleep(clock, time * scale); +} + static inline void xtimer_sleep(uint32_t seconds) { /* TODO: use ZTIMER_SEC */ - ztimer_sleep(ZTIMER_USEC, seconds * 1000000LU); + if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) { + _ztimer_sleep_scale(ZTIMER_MSEC, seconds, 1000); + } else { + _ztimer_sleep_scale(ZTIMER_USEC, seconds, 1000000); + } +} + +static inline void xtimer_msleep(uint32_t milliseconds) +{ + if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) { + ztimer_sleep(ZTIMER_MSEC, milliseconds); + } else { + _ztimer_sleep_scale(ZTIMER_USEC, seconds, 1000); + } } static inline void xtimer_usleep(uint32_t microseconds)