Skip to content

Improves micros() accuracy using SysTick COUNTFLAG #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 8 additions & 75 deletions cores/arduino/stm32/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,84 +35,28 @@
*
******************************************************************************
*/
/** @addtogroup CMSIS
* @{
*/

/** @addtogroup stm32f4xx_system
* @{
*/

/** @addtogroup STM32F4xx_System_Private_Includes
* @{
*/
#include "stm32_def.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @}
*/

/** @addtogroup STM32F4xx_System_Private_TypesDefinitions
* @{
*/

/**
* @}
*/

/** @addtogroup STM32F4xx_System_Private_Defines
* @{
*/

/**
* @}
*/

/** @addtogroup STM32F4xx_System_Private_Macros
* @{
*/

/**
* @}
*/

/** @addtogroup STM32F4xx_System_Private_Variables
* @{
*/

/**
* @}
*/

/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
* @{
*/

/**
* @}
*/

/**
* @brief Function called to read the current micro second
* @param None
* @retval None
*/
uint32_t GetCurrentMicro(void)
{
uint32_t m0 = HAL_GetTick();
uint32_t u0 = SysTick->LOAD - SysTick->VAL;
uint32_t m1 = HAL_GetTick();
uint32_t u1 = SysTick->LOAD - SysTick->VAL;

if (m1 > m0) {
return ( m1 * 1000 + (u1 * 1000) / SysTick->LOAD);
} else {
return ( m0 * 1000 + (u0 * 1000) / SysTick->LOAD);
/* Ensure COUNTFLAG is reset by reading SysTick control and status register */
LL_SYSTICK_IsActiveCounterFlag();
uint32_t m = HAL_GetTick();
uint32_t u = SysTick->LOAD - SysTick->VAL;
if(LL_SYSTICK_IsActiveCounterFlag()) {
m = HAL_GetTick();
u = SysTick->LOAD - SysTick->VAL;
}
return ( m * 1000 + (u * 1000) / SysTick->LOAD);
}

/**
Expand Down Expand Up @@ -176,17 +120,6 @@ void delayInsideIT(uint32_t delay_us)
#endif
}

/**
* @}
*/

/**
* @}
*/

/**
* @}
*/
#ifdef __cplusplus
}
#endif
Expand Down