2020-11-12 07:19 AM
Hi, I'm trying to make my program enter STOP mode, and wake up periodically on RTC WKUP every second. The RTC part works fine, WKUP is set to 1 Hz, but the CPU won't stay in STOP mode after WFI instruction, unless the BASEPRI is nonzero. I am using FreeRTOS, and I would like to avoid changing BASEPRI on my own.
Here is my code:
__set_BASEPRI(15 << __NVIC_PRIO_BITS);
MODIFY_REG(PWR->CR, (PWR_CR_PDDS | PWR_CR_LPDS), PWR_LOWPOWERREGULATOR_ON);
SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
__WFI();
__NOP(); //workaround for possible WFI problems in debugging
__NOP();
__NOP();
CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
__set_BASEPRI(0);
Most of it is simply HAL_PWR_EnterSTOPMode function with added NOPs for silicone bug workaround as the errata suggest.
If the initial statement __set_BASEPRI... is there, everything works fine, regardless of the value I set the BASEPRI to - i.e. the CPU wakes up after one second from the WFI instruction. But if I set BASEPRI to zero, it wakes up immediately. I have verified the CPU is really going to sleep with the WFI instruction, so there is no problem with an interrupt getting in the way.
The BASEPRI thing suggests an interrupt is a problem, but I don't have any interrupt with priority 15. But anyway I tried to test which interrupt could wake up the CPU - I wrote a function that prints something on a serial console (non-interrupt mode) and put it into every interrupt handler in the stm32f4xx_it.c file, and placed a breakpoint in the function. The result: no interrupt is called, but the CPU still wakes up immediately.
I also searched for other interrupt handlers than those declared in the stm32f4xx_it.c file, but didn't find any.
Do you have any ideas what could possibly wake up the CPU if BASEPRI is zero?