2024-07-15 02:42 AM
When changing the timebase source in CubeIDE from Systick to any peripheral timer (TIM6 in my test case), the timer is set up in HAL_InitTick() called from the HAL_Init() function, which is the first function that is called in main().
HAL_InitTick() uses several calls to get the clock configuration from RCC in order to set up the timer.
However, the clocktree is configured only AFTER the timer has been initialized, since SystemClock_Config() is the second function called in main().
This should mean, that the peripheral timer that provides the system clock is based on uninitialized clock settings.
The simple solution is to swap SystemClock_Config() and HAL_Init() in main(), so clocks are configured first and the timer afterwards.
Am i missing anything, or is this a bug?
Setup:
STM32F427VGTx
CubeIDE 1.16.0
STM32Cube FW_F4 V1.28.0
Selected Timer for System clock source: TIM6
My .ioc file is attached.
Solved! Go to Solution.
2024-07-15 03:44 AM
The time base is updated after changing the system clock config.
If you look at HAL_RCC_ClockConfig() implementation in the HAL, you can see the call of HAL_InitTick() at the end:
/* Update the SystemCoreClock global variable */
SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_Pos];
/* Configure the source of time base considering new system clocks settings */
HAL_InitTick (uwTickPrio);
return HAL_OK;
Knowing that HAL_InitTick() is using the updated SystemCoreClock value:
__weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
/* Configure the SysTick to have interrupt in 1ms time basis*/
if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
{
return HAL_ERROR;
}
.
.
Hope it's clear for you.
2024-07-15 02:47 AM - edited 2024-07-15 02:48 AM
Hello,
Did you test a simple program with a HAL time base based on TIM6 and found an issue with the timings?
or this was just an assumption based on your observation of the code implementation?
2024-07-15 03:24 AM
I thought it was linked to a timing issue I encountered, but it seems it had another cause.
Further tests have shown that it actually works both ways, so the order does not seem to matter.
However it is still weird that the timer works correctly when configured based on uninitialized values.
2024-07-15 03:44 AM
The time base is updated after changing the system clock config.
If you look at HAL_RCC_ClockConfig() implementation in the HAL, you can see the call of HAL_InitTick() at the end:
/* Update the SystemCoreClock global variable */
SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_Pos];
/* Configure the source of time base considering new system clocks settings */
HAL_InitTick (uwTickPrio);
return HAL_OK;
Knowing that HAL_InitTick() is using the updated SystemCoreClock value:
__weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
/* Configure the SysTick to have interrupt in 1ms time basis*/
if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
{
return HAL_ERROR;
}
.
.
Hope it's clear for you.
2024-07-15 03:54 AM
Thanks, i overlooked the call to HAL_InitTick() at the end of the clock config. Everyting makes sense now, thank you ;)