From 4f7c5fa64769f836b1cb3d357be4864807e10694 Mon Sep 17 00:00:00 2001 From: Krzysztof Blazewicz Date: Thu, 8 Sep 2016 18:13:22 +0200 Subject: [PATCH] stmhal/hal: reapply HAL commit 9db719b for f4 --- stmhal/hal/f4/src/stm32f4xx_hal.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/stmhal/hal/f4/src/stm32f4xx_hal.c b/stmhal/hal/f4/src/stm32f4xx_hal.c index d388dbf1c2..c0965368ed 100644 --- a/stmhal/hal/f4/src/stm32f4xx_hal.c +++ b/stmhal/hal/f4/src/stm32f4xx_hal.c @@ -337,11 +337,14 @@ __weak uint32_t HAL_GetTick(void) */ __weak void HAL_Delay(__IO uint32_t Delay) { - uint32_t tickstart = 0U; - tickstart = HAL_GetTick(); - while((HAL_GetTick() - tickstart) < Delay) - { - } + uint32_t start = HAL_GetTick(); + + // Note that the following works (due to the magic of 2's complement numbers) + // even when Delay causes wraparound. + + while (HAL_GetTick() - start <= Delay) { + __WFI(); // enter sleep mode, waiting for interrupt + } } /**