Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit d6667c0

Browse files
committed
micros() is going backwards
Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 8825214 commit d6667c0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

cores/arduino/stm32/clock.c

Lines changed: 11 additions & 2 deletions
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) + (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)