Skip to main content
Graduate
September 30, 2025
Solved

FreeRTOS Tickless mode

  • September 30, 2025
  • 1 reply
  • 330 views

Hi all,

I ran into an issue regarding FreeRTOS tickless idle mode. I wanted to share my findings and solution in case others encounter the same problem and also to ask if that's intended structure (so I am missing something) or if its not generated properly.

Background:

  • I was using SysTick as the OS kernel timebase.

  • Tickless idle was enabled (configUSE_TICKLESS_IDLE = 1).

  • I noticed that the portNVIC_SYSTICK_COUNT_FLAG_BIT was cleared unexpectedly during sleep, which caused the tickless idle implementation (vPortSuppressTicksAndSleep) to miscalculate elapsed ticks.

  • Debugging revealed that the auto-generated SysTick_Handler in cmsis_os2.c was clearing the COUNTFLAG, even in tickless mode.

The autogenerated handler looked like this:

 

 
void SysTick_Handler(void) {
 /* Clear overflow flag */
 SysTick->CTRL;

 if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
 xPortSysTickHandler();
 }
}
  • Reading the Cortex-M documentation shows that reading SYST_CSR clears the COUNTFLAG.

  • This means that the autogenerated handler clears COUNTFLAG before the FreeRTOS tickless idle logic can use it, breaking tickless idle functionality.

Solution:

  • The fix is to override the SysTick_Handler :

extern void xPortSysTickHandler(void);

void SysTick_Handler(void) {
#if !defined(configUSE_TICKLESS_IDLE) || (configUSE_TICKLESS_IDLE == 0)
 /* Clear overflow flag only if not using tickless idle */
 SysTick->CTRL;
#endif
 if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
 xPortSysTickHandler();
 }
}
 

With this override, tickless idle works properly, and COUNTFLAG is preserved for vPortSuppressTicksAndSleep.

 

Question: 
Am i supposed to be doing that overwrite? Shouldn't the tickless auto-generated code be "Ready to use"?

Thank you for your time.

 

    This topic has been closed for replies.
    Best answer by bmckenney

    ST is apparently in the process of fixing this:

    https://community.st.com/t5/stm32cubemx-mcus/cubemx-1-19-cmsis-os2-c-systick-handler-breaks-freertos-if/m-p/836854

    Your fix looks more nuanced than that in the other thread.

    1 reply

    bmckenneyAnswer
    Explorer II
    September 30, 2025

    ST is apparently in the process of fixing this:

    https://community.st.com/t5/stm32cubemx-mcus/cubemx-1-19-cmsis-os2-c-systick-handler-breaks-freertos-if/m-p/836854

    Your fix looks more nuanced than that in the other thread.

    MartíAuthor
    Graduate
    October 1, 2025

    Hi bmckenney,

    Thanks for sharing this post! I couldn’t find much information on this topic elsewhere, and this really helped clear things up.
    Much appreciated—cheers!