cancel
Showing results for 
Search instead for 
Did you mean: 

How to ensure interrupt handling before going to sleep/stop mode

JBond.1
Senior

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;
	}
}

 

 

 

10 REPLIES 10
TDK
Super User

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.

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