cancel
Showing results for 
Search instead for 
Did you mean: 

STM32cubeMX generating HSE divider in wrong location

NickAssink
Associate II

When changing the HSE clock divider to 2 in STM32cubeMX the code:

/** Enable the HSE Prescaler
  */
  __HAL_RCC_HSE_DIV2_ENABLE();

Is generated after the HAL_RCC_ClockConfig is called. causing the clock divider on the HSE not to be taken into account when calculation the peripheral clock dividers/periods.

3 REPLIES 3
Ghofrane GSOURI
ST Employee

Hello @NickAssink​ 

First let me thank you for posting.

After double check the issue mentioned above , I would like to share with you my results please have a look.

After generating the code (without changing the location of the generated call of function " __HAL_RCC_HSE_DIV2_ENABLE();" ) and debug it


_legacyfs_online_stmicro_images_0693W00000bkM6zQAE.png 

and based on the RM0473 (8.4.1 RCC clock control register (RCC_CR )


_legacyfs_online_stmicro_images_0693W00000bkM7OQAU.png 

The bit HSEPRE=1 as shown in the screenshot that means that the clock divider on the HSE is taken into account when calculation the peripheral clock dividers/periods.

I will be waiting for your feedback .

Thx

Ghofrane

NickAssink
Associate II

Hello Ghofrane,

Thanks for the fast response. There was a national holiday in the Netherlands so I could not respond immediately. but I should have given a more in depth explanation.

The HAL_RCC_ClockConfig uses the HSE clock divider to calculate the configuration of the SysTick timer. But as shown in the image below the HAL_RCC_ClockConfig function is called before the clock divider is set.


_legacyfs_online_stmicro_images_0693W00000bkXifQAE.pngthis will cause the SysTick timer to be set without the correct clock config.

another way to see this is:

  • __HAL_RCC_HSE_DIV2_ENABLE() is a define to LL_RCC_HSE_EnableDiv2.
  • To read the Div2 LL_RCC_HSE_IsEnabledDiv2 is used.
  • LL_RCC_HSE_IsEnabledDiv2 is used in HAL_RCC_GetSysClockFreq(void)
  • HAL_RCC_GetSysClockFreq is used in HAL_RCC_GetHCLKFreq
  • HAL_RCC_GetHCLKFreq is used in HAL_RCC_ClockConfig

So if __HAL_RCC_HSE_DIV2_ENABLE is called after HAL_RCC_ClockConfig the div 2 config will not be used in HAL_RCC_ClockConfig. The HAL_RCC_ClockConfig uses HAL_RCC_GetHCLKFreq to set the SystemCoreClock so if the clock divider is changed after the fact, the SystemCoreClock is not set correctly. and that is the same for the SysTick timer set in HAL_RCC_ClockConfig.

With kind regards,

Nick Assink

Ghofrane GSOURI
ST Employee

Hello @NickAssink​ 

Thank you for your contributions and for your detailed explanations,

I confirm the issue on my side, it has been reported to STM32CubeMX development team .

Internal ticket number:   152728(This is an internal tracking number and is not accessible or usable by customers).

I will keep you posted with updates.

Ghofrane