cancel
Showing results for 
Search instead for 
Did you mean: 

Using timer to wake from sleep

PGood.1
Associate II

Im attempting to put the cpu to sleep between ticks of timer 15 on a stm32L432, The LED flashs without the Sleep code however it never wakes up from the timer interupt

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim) {

HAL_ResumeTick();

if (htim -> Instance == TIM15) {

HAL_GPIO_TogglePin(GPIOB, LD5_Pin);

HAL_SuspendTick();

HAL_PWR_EnableSleepOnExit ();

HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);

}

}

2 REPLIES 2
TDK
Guru

Fairly sure WFI should be called from the main loop and not the interrupt. Enabling sleep on exit is something you only need to do once, before the WFI instruction. It will go back to sleep when it exits the handler.

Not sure what you're trying to do with the ticks by resuming and then immediately suspending them again. I would imagine you'll need to do something else. Or just leave them suspended. Or call HAL_IncTick manually within the timer update.

If you feel a post has answered your question, please click "Accept as Solution".
LamdaElectronics
Associate

I would suggest to trigger start your Sleep Entry in the while(1) loop. When the timer interrupt occurs the uC will wake. On the next while(1) execution the uC will enter sleep mode again. The awakening will be done on the timer interrupt. And so on.