2025-07-25 6:28 AM - last edited on 2025-07-25 7:36 AM by Andrew Neil
I am trying to get the RTC alarm working whilst also using a periodic wake, I can get each working independently but not both together.
If I DON'T shutdown for RTC wakeup, the RTC alarm DOES work.
If I DO shutdown for RTC wakeup, the RTC alarm DOES NOT work.
To summarize:
Below is the function I call to shutdown the mcu for 5 seconds, before waking up again...
void shutdownRun (void){
/* The Following Wakeup sequence is highly recommended prior to each Standby mode entry
mainly when using more than one wakeup source this is to not miss any wakeup event.
- Disable all used wakeup sources,
- Clear all related wakeup flags,
- Re-enable all used wakeup sources,
- Enter the shutdown mode.
*/
// Save pressure before shutdwon
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, *(uint32_t*)&pressure_bak);
/* Disable all used wakeup sources*/
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 5, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
/* Clear all related wakeup flags */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
ampPowerDown();
/* Enter the shutdown mode */
HAL_PWREx_EnterSHUTDOWNMode();
}
Below is the RTC initialization (note I have commented where hours/minutes/seconds is set to zero, otherwise it would be reset each time the RTC wakes the mcu and would never trigger the alarm)...
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
RTC_AlarmTypeDef sAlarm = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
// sTime.Hours = 0x0;
// sTime.Minutes = 0x0;
// sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 0x1;
sDate.Year = 0x0;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/** Enable the Alarm A
*/
sAlarm.AlarmTime.Hours = 0x0;
sAlarm.AlarmTime.Minutes = 0x1;
sAlarm.AlarmTime.Seconds = 0x0;
sAlarm.AlarmTime.SubSeconds = 0x0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 0x1;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
Below is my alarm interrupt, which is only triggered if I never call the 'shutdownRun' function ...
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
shutdownFull();
}
2025-07-25 6:56 AM
Don't shut down within the RTC interrupt. It will not pre-empt itself.
Instead, set a flag within the interrupt and shut down within the main thread itself.
2025-07-25 7:35 AM
I changed the interrupt and main as you suggested but its still not working.
Interrupt is now....
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
flgReqShudownFull = 1;
}
And main is now...
while (1) {
if (flgReqShudownFull){
shutdownFull();
}
}
Going into shutdown and using the RTC to wake after 5 seconds prevents the alarm interrupt from working, but I cant find the issue
If I dont go into shutdown and wake every 5 seconds, the alarm interrupt works.
2025-07-25 7:39 AM
Can you show the full code? Doesn't seem to be any issues with the code you've presented.
2025-07-25 7:54 AM