Showing results for 
Search instead for 
Did you mean: 

Wrong SysTick prescaler [CUBEMX]

Associate III

In CubeMX, in the clock tree screen, you can select the prescaler that produces "Cortex System Timer (MHz)" (which is the clock feeding SysTick) starting from HCLK.

However, whether you select the /1 prescaler or the /8 prescaler, identical code is generated (see the attachment).

I would expect this setting to be reflected in some initialization code.

The SysTick->CTRL register has a bit (bit #2, defined as SysTick_CTRL_CLKSOURCE_Msk in "core_cm*.h") to select the SysTick clock source.

From the ARM documentation ( this bit selects either a "reference clock" (if 0) or a "processor clock" (if 1).

CubeMX-generated code always sets this bit to 1.

The reference manual (for L073) says, in the RCC chapter:

The RCC feeds the Cortex® System Timer (SysTick) external clock with the AHB clock

(HCLK) divided by 8. The SysTick can work either with this clock or with the Cortex® clock

(HCLK), configurable in the SysTick Control and Status Register.

I've tried changing the CubeMX setting both on an STM32L073 and on an STM32F746; in both cases, the code disregards the prescaler settings.

The issue is that "core_cm*.h" has SysTick_CTRL_CLKSOURCE_Msk hardcoded (in function SysTick_Config()), and this function is probably untouchable, being part of CMSIS.

So, HAL libraries and CubeMX code generation will have to ignore this function, and reimplement it in HAL_SYSTICK_Config() with the proper behavior, by setting SysTick->CTRL to the correct value.

Alternatively, the CubeMX clock tree screen should show a clear warning when selecting "/8", about it not being supported by code generation and about the need to manually tweak SysTick_CTRL_CLKSOURCE.