Skip to main content
Associate II
December 9, 2024
Solved

STM32H562 SysTick 2ms instead of 1ms

I have setted:
SYSCLK: 250 MHz
HCLK: 250 MHz

As a SysTick source I tried: HCLK, HCLK/8, LSI

STM32Cube FW_H5 V1.4.0

I'm getting 2ms instead of 1ms in any configuration. Any idea how to fix it?

This topic has been closed for replies.
PavelSvihalekBest answer
Associate II
December 11, 2024

I try to insert toggle command into interrupt..  1ms was OK.
So problem is at implementation of "HAL_Delay(T)" function - T is (T+1).
When I try HAL_Delay(2), time is 3ms.

ST insert this code into HalDelay function:

 /* Add a freq to guarantee minimum wait */
 if (wait < HAL_MAX_DELAY)
 {
 wait += (uint32_t)(uwTickFreq);
 }

and this causes, the delay time is 1ms longer than I expect.

I understand why they insert this code there, but it's limiting.

 

Thanks for the guidance, I didn't expect this change in the HAL library.

9 replies

Tesla DeLorean
Guru
December 9, 2024

So print out the clocking the part thinks it has.

Unpack the RCC registers

Export the internal clock via PA8 / MCO

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Associate II
December 10, 2024

Here are registers:

PavelSvihalek_0-1733815548284.png

PavelSvihalek_1-1733815597499.png

And PA8:

PavelSvihalek_2-1733815682725.png

 

clocksclocks

And main clock selection:

PavelSvihalek_3-1733815859950.png

 

 

 

Tesla DeLorean
Guru
December 10, 2024

0x7A11 * 8 ~ 249992 seems Ok

Are you TOGGLING a GPIO in Systick_Handler() ?

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
PavelSvihalekAuthorBest answer
Associate II
December 11, 2024

I try to insert toggle command into interrupt..  1ms was OK.
So problem is at implementation of "HAL_Delay(T)" function - T is (T+1).
When I try HAL_Delay(2), time is 3ms.

ST insert this code into HalDelay function:

 /* Add a freq to guarantee minimum wait */
 if (wait < HAL_MAX_DELAY)
 {
 wait += (uint32_t)(uwTickFreq);
 }

and this causes, the delay time is 1ms longer than I expect.

I understand why they insert this code there, but it's limiting.

 

Thanks for the guidance, I didn't expect this change in the HAL library.

waclawek.jan
Super User
December 11, 2024

> but for this testing case it is same

No, it is not.

HAL_Delay(N) provides a *minimum* N-ms delay, and with 1ms granularity of the underlying timer this means, that (with no external intervention like higher-priority interrupt) it provides delay of 1..2ms. And, with the code you wrote, guarantees, that (except for the first period) it will always yield close to 2ms.

Cube/HAL is open source, you can/should look up how things are actually implemented.

JW

Associate II
December 11, 2024

So why isn't the function called HalMinimumDelay()?.. (That's just a rhetorical question)
In older implementations, this function was implemented as I previously expected.

waclawek.jan
Super User
December 11, 2024

Loopdelays which don't provide *minimum* delay (i.e. where there's a chance that the delay they provide is less than what's requested) are fundamentally flawed and should never have existed.

I don't use Cube/HAL and never did.

JW