Skip to content

Commit 2fa0645

Browse files
committed
Fix #4 micros() is going backwards
Previous fix was not complete as it do not manage millisecond change. Thanks Pito: http://www.stm32duino.com/viewtopic.php?f=51&t=2020&start=10#p27101 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 9a229a1 commit 2fa0645

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,22 @@
9898
*/
9999

100100
/**
101-
* @brief Function called wto read the current micro second
101+
* @brief Function called to read the current micro second
102102
* @param None
103103
* @retval None
104104
*/
105105
uint32_t GetCurrentMicro(void)
106106
{
107-
return (HAL_GetTick()*1000) + ((SystemCoreClock/1000-SysTick->VAL)/(SystemCoreClock/1000000));
107+
uint32_t m0 = HAL_GetTick();
108+
uint32_t u0 = SysTick->LOAD - SysTick->VAL;
109+
uint32_t m1 = HAL_GetTick();
110+
uint32_t u1 = SysTick->LOAD - SysTick->VAL;
111+
112+
if (m1 > m0) {
113+
return ( m1 * 1000 + (u1 * 1000) / SysTick->LOAD);
114+
} else {
115+
return ( m0 * 1000 + (u0 * 1000) / SysTick->LOAD);
116+
}
108117
}
109118

110119
/**

0 commit comments

Comments
 (0)