From 07a1dde68404821c57b6a6f76f7dca9eacbe3b63 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Tue, 29 Sep 2020 11:23:28 +0200 Subject: [PATCH 1/2] ztimer: fix ztimer_spin() time_left == duration case --- sys/include/ztimer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/include/ztimer.h b/sys/include/ztimer.h index 49062d3811..fba948e6e2 100644 --- a/sys/include/ztimer.h +++ b/sys/include/ztimer.h @@ -456,7 +456,7 @@ void ztimer_sleep(ztimer_clock_t *clock, uint32_t duration); */ static inline void ztimer_spin(ztimer_clock_t *clock, uint32_t duration) { uint32_t end = ztimer_now(clock) + duration; - while ((end - ztimer_now(clock)) < duration) {} + while ((end - ztimer_now(clock)) <= duration) {} } /** From 70b160713e954a5ac9f2dd45f13db77fb8a92214 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Tue, 29 Sep 2020 11:53:35 +0200 Subject: [PATCH 2/2] ztimer: add comment explaining ztimer_spin() logic --- sys/include/ztimer.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sys/include/ztimer.h b/sys/include/ztimer.h index fba948e6e2..d52010453f 100644 --- a/sys/include/ztimer.h +++ b/sys/include/ztimer.h @@ -456,6 +456,9 @@ void ztimer_sleep(ztimer_clock_t *clock, uint32_t duration); */ static inline void ztimer_spin(ztimer_clock_t *clock, uint32_t duration) { uint32_t end = ztimer_now(clock) + duration; + + /* Rely on integer overflow. `end - now` will be smaller than `duration`, + * counting down, until it underflows to UINT32_MAX. Loop ends then. */ while ((end - ztimer_now(clock)) <= duration) {} }