2025-11-13 12:11 PM
Hi everyone,
I'm new to STM32 solutions. My goal is to use the RTC Wake-Up Timer (WUT) to wake the MCU from Standby mode. I'm starting with a simple test:
The Problem: The MCU enters Standby, but it never wakes up.
I am calculating the WUT for ck_spre (256 Hz) as follows: wakeup_counter = 5 (seconds) * 256 (Hz) = 1280.
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_RTC_Init();
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
{
// LSE FAILED!
while (1)
{
//(error)
HAL_GPIO_WritePin (LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
HAL_Delay (100);
HAL_GPIO_WritePin (LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
HAL_Delay (100);
}
}
uint32_t time_wkp_seg = 5;
uint32_t wakeup_counter = time_wkp_seg * 256;
if (__HAL_PWR_GET_FLAG(PWR_FLAG_SBF) != RESET)
{
// 1. Woke up from Standby
// 2. Get the state (e.g., a counter)
uint32_t wake_count = HAL_RTCEx_BKUPRead (&hrtc, RTC_BKP_DR0);
wake_count++;
// 3. Execute the wake-up task
HAL_GPIO_WritePin (LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET); // Turn on LED
HAL_Delay (50); // Keep it on for 50ms (to be visible)
HAL_GPIO_WritePin (LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); // Turn off LED
// 4. Save the new state
HAL_RTCEx_BKUPWrite (&hrtc, RTC_BKP_DR0, wake_count);
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
HAL_RTCEx_SetWakeUpTimer_IT (&hrtc, wakeup_counter, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, 0);
}
else
{
// 5. It's a "cold" boot
// 6. Save the initial state
HAL_RTCEx_BKUPWrite (&hrtc, RTC_BKP_DR0, 0); // Start counter
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
HAL_RTCEx_SetWakeUpTimer_IT (&hrtc, wakeup_counter, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, 0);
}
// --- Safety Clear BEFORE sleeping ---
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);// Clear the RTC flag
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SBF); // Clear PWR flags
HAL_PWR_EnterSTANDBYMode ();