Skip to content

Commit 9fb4735

Browse files
authored
Merge pull request stm32duino#202 from fpistm/micros
Improves micros() accuracy using SysTick COUNTFLAG
2 parents 3530d05 + 65de89f commit 9fb4735

File tree

1 file changed

+8
-75
lines changed

1 file changed

+8
-75
lines changed

cores/arduino/stm32/clock.c

+8-75
Original file line numberDiff line numberDiff line change
@@ -35,84 +35,28 @@
3535
*
3636
******************************************************************************
3737
*/
38-
/** @addtogroup CMSIS
39-
* @{
40-
*/
41-
42-
/** @addtogroup stm32f4xx_system
43-
* @{
44-
*/
45-
46-
/** @addtogroup STM32F4xx_System_Private_Includes
47-
* @{
48-
*/
4938
#include "stm32_def.h"
5039

5140
#ifdef __cplusplus
5241
extern "C" {
5342
#endif
5443

55-
/**
56-
* @}
57-
*/
58-
59-
/** @addtogroup STM32F4xx_System_Private_TypesDefinitions
60-
* @{
61-
*/
62-
63-
/**
64-
* @}
65-
*/
66-
67-
/** @addtogroup STM32F4xx_System_Private_Defines
68-
* @{
69-
*/
70-
71-
/**
72-
* @}
73-
*/
74-
75-
/** @addtogroup STM32F4xx_System_Private_Macros
76-
* @{
77-
*/
78-
79-
/**
80-
* @}
81-
*/
82-
83-
/** @addtogroup STM32F4xx_System_Private_Variables
84-
* @{
85-
*/
86-
87-
/**
88-
* @}
89-
*/
90-
91-
/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
92-
* @{
93-
*/
94-
95-
/**
96-
* @}
97-
*/
98-
9944
/**
10045
* @brief Function called to read the current micro second
10146
* @param None
10247
* @retval None
10348
*/
10449
uint32_t GetCurrentMicro(void)
10550
{
106-
uint32_t m0 = HAL_GetTick();
107-
uint32_t u0 = SysTick->LOAD - SysTick->VAL;
108-
uint32_t m1 = HAL_GetTick();
109-
uint32_t u1 = SysTick->LOAD - SysTick->VAL;
110-
111-
if (m1 > m0) {
112-
return ( m1 * 1000 + (u1 * 1000) / SysTick->LOAD);
113-
} else {
114-
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;
11558
}
59+
return ( m * 1000 + (u * 1000) / SysTick->LOAD);
11660
}
11761

11862
/**
@@ -176,17 +120,6 @@ void delayInsideIT(uint32_t delay_us)
176120
#endif
177121
}
178122

179-
/**
180-
* @}
181-
*/
182-
183-
/**
184-
* @}
185-
*/
186-
187-
/**
188-
* @}
189-
*/
190123
#ifdef __cplusplus
191124
}
192125
#endif

0 commit comments

Comments
 (0)