From 7e73ab708050ee889e8d1b28ac3af16762b514df Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Fri, 29 Apr 2022 17:34:33 +0200 Subject: [PATCH] fix: make micros compatible with ISR It is kind of revert commit 15dc04b0a0ceaa612e80f7c26d78c4fb68aa8b56. Note: `delayMicroseconds()` does not rely anymore on `micros()` so accuracy is less important. Fixes #1680 Signed-off-by: Frederic Pillon --- libraries/SrcWrapper/src/stm32/clock.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/libraries/SrcWrapper/src/stm32/clock.c b/libraries/SrcWrapper/src/stm32/clock.c index d1e4bae99b..8e3029f761 100644 --- a/libraries/SrcWrapper/src/stm32/clock.c +++ b/libraries/SrcWrapper/src/stm32/clock.c @@ -28,16 +28,17 @@ extern "C" { */ uint32_t getCurrentMicros(void) { - /* Ensure COUNTFLAG is reset by reading SysTick control and status register */ - LL_SYSTICK_IsActiveCounterFlag(); - uint32_t m = HAL_GetTick(); + uint32_t m0 = HAL_GetTick(); + __IO uint32_t u0 = SysTick->VAL; + uint32_t m1 = HAL_GetTick(); + __IO uint32_t u1 = SysTick->VAL; const uint32_t tms = SysTick->LOAD + 1; - __IO uint32_t u = tms - SysTick->VAL; - if (LL_SYSTICK_IsActiveCounterFlag()) { - m = HAL_GetTick(); - u = tms - SysTick->VAL; + + if (m1 != m0) { + return (m1 * 1000 + ((tms - u1) * 1000) / tms); + } else { + return (m0 * 1000 + ((tms - u0) * 1000) / tms); } - return (m * 1000 + (u * 1000) / tms); } /**