Skip to content

Commit adf8418

Browse files
committed
[Time] Fix ticks per ms value used to compute micros
The timer interrupt or COUNTFLAG bit (in the SysTick Control and Status register) is activated on the transition from 1 to 0, therefore it activates every n+1 clock ticks. So, ticks per ms is SysTick->LOAD +1. Signed-off-by: Frederic Pillon <[email protected]>
1 parent 21d3d5c commit adf8418

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

cores/arduino/stm32/clock.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ uint32_t getCurrentMicros(void)
5353
/* Ensure COUNTFLAG is reset by reading SysTick control and status register */
5454
LL_SYSTICK_IsActiveCounterFlag();
5555
uint32_t m = HAL_GetTick();
56-
uint32_t u = SysTick->LOAD - SysTick->VAL;
56+
uint32_t tms = SysTick->LOAD + 1;
57+
uint32_t u = tms - SysTick->VAL;
5758
if (LL_SYSTICK_IsActiveCounterFlag()) {
5859
m = HAL_GetTick();
59-
u = SysTick->LOAD - SysTick->VAL;
60+
u = tms - SysTick->VAL;
6061
}
61-
return (m * 1000 + (u * 1000) / SysTick->LOAD);
62+
return (m * 1000 + (u * 1000) / tms);
6263
}
6364

6465
/**

0 commit comments

Comments
 (0)