1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

xtimer: introduce xtimer_msleep()

Add a function that sleeps for ms instead of µs.
This will make the conversion to ztimer easier for users as now there is
a direct mapping to ZTIMER_MSEC.
This commit is contained in:
Benjamin Valentin 2020-09-24 12:07:50 +02:00 committed by Benjamin Valentin
parent 07f0745015
commit ca193cf515
3 changed files with 38 additions and 1 deletions

View File

@ -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
*

View File

@ -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));

View File

@ -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)