Skip to content

Commit 15dc04b

Browse files
committed
Improves micros() accuracy using SysTick COUNTFLAG
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 1e9303c commit 15dc04b

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Diff for: cores/arduino/stm32/clock.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@
4848
*/
4949
uint32_t GetCurrentMicro(void)
5050
{
51-
uint32_t m0 = HAL_GetTick();
52-
uint32_t u0 = SysTick->LOAD - SysTick->VAL;
53-
uint32_t m1 = HAL_GetTick();
54-
uint32_t u1 = SysTick->LOAD - SysTick->VAL;
55-
56-
if (m1 > m0) {
57-
return ( m1 * 1000 + (u1 * 1000) / SysTick->LOAD);
58-
} else {
59-
return ( m0 * 1000 + (u0 * 1000) / SysTick->LOAD);
51+
/* Ensure COUNTFLAG is reset by reading SysTick control and status register */
52+
LL_SYSTICK_IsActiveCounterFlag();
53+
uint32_t m = HAL_GetTick();
54+
uint32_t u = SysTick->LOAD - SysTick->VAL;
55+
if(LL_SYSTICK_IsActiveCounterFlag()) {
56+
m = HAL_GetTick();
57+
u = SysTick->LOAD - SysTick->VAL;
6058
}
59+
return ( m * 1000 + (u * 1000) / SysTick->LOAD);
6160
}
6261

6362
/**

0 commit comments

Comments
 (0)