STM32L4R5 Sleep Mode problem with FreeRTOS

Hi everybody. I’m working currently in STM32L4R5 with CubeIDE to implement Low-Power mode with FreeRTOS. I try to enter in Sleep Mode but after the WFI instruction the CPU isn’t going to sleep and the following instruction (LED on) is directly executed. I think an interrupt occurs and causes the wakeup. However I have suspended the Systick Timer. So I have no idea.

Thank you for your help.

Software : STM32CubeIDE 1.3.0
Target : STM32L4R5ZI on Nucleo board

Code :

void StartDefaultTask ( void const * argument)
{

for ( ; ; ) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET ); // User Led on

osDelay(5000);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET ); // User Led off

osDelay(1000);

HAL_SuspendTick();

HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI);

}

}

I want to add that the Stop Mode 0, 1 and 2 works, but the sleep mode doesn’t.

Are the HAL tick and the FreeRTOS Tick using the same timer?

I set the TIM7 as timebase source with CubeMX so I think HAL tick and FreeRTOS tick have both TIM7 source. Is it correct ?

I think I was wrong. HAL timebase source is Timer 7 for sure. I think FreeRTOS use SysTick timer. It’s not the same timer in fact.

I think you’re right that the SysTick is waking you up. It is relentlessly interrupting the system every 1ms (typical). You should consider using tickless idle. The idle task takes care of entry and exit into sleep mode.

However the default implementation of tickless idle that comes with FreeRTOS doesn’t support the Stop modes so please reply back if you really need the Stop modes.

By default, On a Cortex M part FreeRTOS will use the Systick in the Core for its timer. This is configurable, but unless you change it, that is what will use. I have no idea if the Cube makes those changes, as I have found the cube not that reliable for actual code.

@richard-damon @jefftenney Thanks for your explanations.
I have understood that the function HAL_SuspendTick() I used in my code only suspends the TIM7 set as timebase but not the Systick Timer of FreeRTOS. That’s why the SysTick occurs and wake up the system. To solve it I need to use tickless mode as @jefftenney said.

However I would like to combine sleep mode and stop modes depending on the MCU environment and situation. Is it possible with the FreeRTOS tickless mode ?

Yes, you can combine them, but not with the default tickless implementation. I wrote a custom implementation that uses LPTIM1 for the FreeRTOS tick instead of SysTick. LPTIM1 keeps running in all the stop modes. You can use the code here.

There are also some examples in the FreeRTOS distribution but I’m not sure how applicable they are to the 'L4R5. They may also increase “clock drift” which may or may not be a problem for your application.

Thank you !
I’m going to read your code and try it.

I have also used the LPTIM1 for the systick, your FreeRTOS tick isn’t nice round number of milliseconds, but that is ok for what I need, and you can just always keep the timer running and have tickles idle just raise the threshold point for the interrupt, and never get any slippage.

1 Like