2026-03-30 9:43 AM
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!
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. */
}
2026-03-30 10:14 AM
What is missing is disabling systick. Could be other things.
2026-03-31 2:28 AM
Hello @Kmax18
In addition to @TDK recommandation, you should:
/* 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