2025-02-08 8:44 AM - edited 2025-02-08 8:57 AM
Hi, I want to go to sleep/stop mode with STM32 and wake on interrupt. After last interrupt it should wait for 10sec and go back to sleep. But I see an edge case where interrupt could occur just before going to sleep/stop mode.
How do I handle interrupt just before going to sleep/stop mode (cancel sleep/stop mode)?
Basically force WAKE from sleep/stop mode even if STM32 is not yet in sleep/stop mode?
#define STAY_WAKE_FOR_TICK 10000
static volatile uint32_t GoToSleepAfterTick;
void main(void)
{
//...
while (1)
{
HAL_SuspendTick();
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
HAL_ResumeTick();
while (HAL_GetTick() < GoToSleepAfterTick)
{
//...
}
// interupt could occure here???
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == CALL_Pin)
{
GoToSleepAfterTick = HAL_GetTick() + STAY_WAKE_FOR_TICK;
}
}
2026-01-08 7:48 PM
I agree with @gbm here, this is a non-issue.
If the interrupt happens before entering sleep, it will be handled before entering sleep in the interrupt routine. If it happens after, it'll wake up from sleep and be handled then.
In either case, it will be handled. It doesn't matter if it happens after suspend tick and before WFI, or after WFI, or anywhere. It will never be ignored. Total non-issue.
I don't see the point of checking for pending interrupts. If they are pending, they will happen regardless.