cancel
Showing results for 
Search instead for 
Did you mean: 

FreeRTOS Tickless mode

Martí
Associate II

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 stm32f3xx_it.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.

 

1 REPLY 1
bmckenney
Associate III

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.