I am using an STM32L0 series MCU. I configured my MCU to wake up from Stop Mode using an EXTI line (EXTI0 at PORTA0) during falling edge. At the beginning, it wasn't going to the Stop Mode after I called _WFE(). After tried several things I called _WFE() twice, one after another and that did the trick. Although this solved my problem I was wondering if anyone can shed some light. Maybe I am missing something.
Here is my code:
void HW_WakeupPinInit(void)
{
GPIO_InitTypeDef gpioInitStruct = {0};
gpioInitStruct.Mode = GPIO_MODE_INPUT;
gpioInitStruct.Pull = GPIO_PULLDOWN;
gpioInitStruct.Speed = GPIO_SPEED_HIGH;
HW_GPIO_Init( WAKEUP_PORT, WAKEUP_PIN, &gpioInitStruct );
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTA, LL_SYSCFG_EXTI_LINE0);
LL_EXTI_InitTypeDef EXTIinitStruct = {0};
EXTIinitStruct.Line_0_31 = LL_EXTI_LINE_0;
EXTIinitStruct.LineCommand = ENABLE;
EXTIinitStruct.Mode = LL_EXTI_MODE_EVENT;
EXTIinitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
LL_EXTI_Init(&EXTIinitStruct);
}
void HW_EnterStopMode(void)
{
uint32_t tmpreg = 0U;
BACKUP_PRIMASK();
DISABLE_IRQ();
HW_IoDeInit();
/*clear wake up flag*/
LL_PWR_ClearFlag_WU();
RESTORE_PRIMASK();
tmpreg = PWR->CR;
/* Clear PDDS and LPSDSR bits */
CLEAR_BIT(tmpreg, (PWR_CR_PDDS | PWR_CR_LPSDSR));
Set LPSDSR bit according to PWR_Regulator value */
SET_BIT(tmpreg, LL_PWR_REGU_LPMODES_LOW_POWER);
/* Store the new value */
PWR->CR = tmpreg;
LL_LPM_EnableDeepSleep();
__WFE();
__WFE(); // <------- Calling __WFE() second time works.
}
Thanks in advance.