cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G0 race condition power-saving shutdown mode

Kmax18
Senior II

I am looking for a software solution for this race condition:
* The power button is connected to both the On/Off pin PD3 and the wakeup pin PB5. See the partial schematic.
* The issue is that the STM32G0 enters shutdown mode, but immediately wakes up. Please see my function below.
What is missing? Thank you!

Power_OnOff_Wakeup_race_condition.jpg

 

 

void Enter_Shutdown_Mode(void)
{
    sprintf(msgString, "Enter_Shutdown_Mode...\r\n");
    Debug_Print( msgString );
    HAL_Delay(50);

    /* Enable the Power Controller (PWR) APB clock */
    __HAL_RCC_PWR_CLK_ENABLE();

    /* Wait for the pin to be HIGH after the button release.
       If the power button is currently pressed (LOW), entering shutdown
       with "LOW" polarity will cause an instant restart. */
    while (HAL_GPIO_ReadPin(WAKEUP_GPIO_Port, WAKEUP_Pin) == GPIO_PIN_RESET)
    {
        // Wait for user to release the button
        HAL_Delay(10);
    }

    // Debounce the button press
    HAL_Delay(50);

    /* Clear the Wakeup Flag.
       If a flag is set from a previous wakeup, the MCU will fail to enter
       Shutdown and stay in Run mode. */
    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF6);

    /* Disable and re-enable the wakeup pin to ensure a clean start. */
    HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN6);
    HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN6_LOW);

    /* Enter Shutdown Mode:
       Set the LPMS bits in PWR_CR1 to '011' (Shutdown)
       Set the SLEEPDEEP bit in the Cortex-M0+ System Control Register
       Execute the WFI (Wait For Interrupt) instruction */
    HAL_PWREx_EnterSHUTDOWNMode();

    /* --- THE MCU IS NOW IN SHUTDOWN ---
       NOTE: After a wakeup from Shutdown, the MCU resets. The code below
       this line will NEVER execute. */
}

 

2 REPLIES 2
TDK
Super User

What is missing is disabling systick. Could be other things.

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

Hello @Kmax18 

In addition to @TDK recommandation, you should:

  1. Clear all relevant WUF flags (not just WUF6) before enabling the wakeup line.
  2. Also clear the Standby/Shutdown flag if it’s set.
/* Enable PWR clock */
__HAL_RCC_PWR_CLK_ENABLE();

/* Clear all wakeup flags and shutdown/standby flags */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF1 | PWR_FLAG_WUF2 | PWR_FLAG_WUF3 |
                     PWR_FLAG_WUF4 | PWR_FLAG_WUF5 | PWR_FLAG_WUF6 |
                     PWR_FLAG_SB);

In addition to 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om