Skip to main content
Associate
October 6, 2023
Question

FreeRTOS + SystemClock Behavior

  • October 6, 2023
  • 4 replies
  • 2675 views

Hello,

I am using STM32L4A6VGTxP with CubeIDE and want to create a system with FreeRTOS.
However, I am having the following problem when running the system under FreeRTOS while changing the SystemClock.
Is there a solution?


Configuration
・Custom boards using the STM32L4A6VGTxP microcontroller
・Using STM32CubeIDE v1.9.0
・Firmware Package STM32Cude FW_L4 V1.17.2
・FreeRTOS v10.3.1 with CMSIS v2

Problem Description
FreeRTOS is initialized with the system clock set at HSI 16MHz at microcontroller startup.
If the system clock is subsequently changed, the OS time remains set at 16MHz.
How can I synchronize the OS time when the system clock is changed?

For example
(1) If you change the system clock from HSI 16MHz to HSI 80MHz
-> osDelay(1000), the Delay time will be 250ms
Execute osEventFlagsWait(ef_id, flags, options, 1000), the timeout time will be 250ms
When HAL_Delay(1000) is executed, Delay time becomes 1000ms.

(2) When changing from HSI 16MHz to MSI 4MHz
→When osDelay(1000) is executed, Delay time becomes 4000ms
When osEventFlagsWait(ef_id, flags, options, 1000) is executed, timeout time becomes 4000ms
Execute HAL_Delay(1000), Delay time will be 1000ms

Thank you.

 

    This topic has been closed for replies.

    4 replies

    ma-haraAuthor
    Associate
    October 6, 2023

    There was a mistake in the description of the example.
    The following is the correct execution result.

    For example
    (1) If you change the system clock from HSI 16MHz to HSI 80MHz
    →osDelay(1000), the Delay time will be 200ms
    Execute osEventFlagsWait(ef_id, flags, options, 1000), the timeout time will be 200ms
    When HAL_Delay(1000) is executed, Delay time becomes 1000ms.

    (2) When changing from HSI 16MHz to MSI 4MHz
    →When osDelay(1000) is executed, Delay time becomes 4000ms
    When osEventFlagsWait(ef_id, flags, options, 1000) is executed, timeout time becomes 4000ms
    Execute HAL_Delay(1000), Delay time will be 1000ms

    Thank you.

     

    Piranha
    Principal III
    October 7, 2023

    Isn't it obvious? Reconfigure the timer, on which the OS ticks are based!

    ma-haraAuthor
    Associate
    October 10, 2023

    Thank you for your answer.

    Let me explain in more detail.

    When SystemClock is changed from 16MHz to 80MHz,
    If we use Systick for Timebase Source,
    Delay time for HAL_Delay(1000) and osDelay(1000) is the same at 1000ms.

    However, when TIM6 is used for Timebase Source,
    Delay time for HAL_Delay(1000) is 1000ms, but Delay time for osDelay(1000) is 200ms.

    When using RTOS with STM32CubeIDE, it is strongly recommended to use a HAL timebase source other than Systick.

    I do not understand why there is a difference in delay time between HAL_Delay and osDelay when Timebase is set to TIM6.
    I would also like to know what kind of reconfiguration would maintain 1Tick=1ms.
    Thank you in advance for your help.

    Thanks.

    Piranha
    Principal III
    October 12, 2023

    When using RTOS with STM32CubeIDE, it is strongly recommended to use a HAL timebase source other than Systick.


    Because the RTOS uses SysTick for it's ticks and sets it to the lowest interrupt priority, but the HAL design requires the HAL timebase ticks to be incremented in the highest priority interrupt. So in the end, because of ST's code design, you have two tick timers. If you change the system clock, you have to readjust both of them.

    Edited by moderators to adhere to community guidelines.