Skip to main content
Visitor II
June 26, 2024
Question

STANDBY and SLEEP Modes using RTC

  • June 26, 2024
  • 1 reply
  • 2594 views

Hello,
I am trying to use the SLEEP and STANDBY modes with the RTC. I want the device to go to sleep/standby mode for 10 seconds and wake up after the 10 seconds are done. I have tried it with an interrupt using UART and Button and both the modes are working fine.But with RTC I am facing an issue.
Can anyone pls help with this query of mine?

Thanking you.

    This topic has been closed for replies.

    1 reply

    Visitor II
    June 27, 2024

    Its working for STANDBY Mode.But for SLEEP Mode I am unable to get the core to wake up pls.

    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    	 HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
    	 sprintf(date, "Date: %02d.%02d.%02d\t", sDate.Date, sDate.Month, sDate.Year);
    	 sprintf(time, "Time: %02d:%02d:%02d\r\n", sTime.Hours, sTime.Minutes, sTime.Seconds);
    	 HAL_UART_Transmit(&huart2, (uint8_t *)date, strlen(date), HAL_MAX_DELAY);
    	 HAL_UART_Transmit(&huart2, (uint8_t *)time, strlen(time), HAL_MAX_DELAY);
    
    	 // Configure RTC alarm to trigger after 10 seconds
    	 RTC_AlarmTypeDef sAlarm;
    	 sAlarm.AlarmTime.Hours = 0;
    	 sAlarm.AlarmTime.Minutes = 0;
    	 sAlarm.AlarmTime.Seconds = 10;
    	 sAlarm.Alarm = RTC_ALARM_A;
    	 if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
    	 {
    	 Error_Handler();
    	 }
    
    	 // Enter Sleep mode
    	 HAL_UART_Transmit(&huart2, (uint8_t *)"MCU is going to sleep...\r\n", strlen("MCU is going to sleep...\r\n"), HAL_MAX_DELAY);
    	 HAL_SuspendTick(); // Suspend HAL tick
    	 HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFE);
    
    	 // MCU wakes up here
    
    	 // Resume operations after wake-up
    	 //SystemClock_Config(); // Restore system clock settings
    	 HAL_ResumeTick(); // Resume HAL tick
    
    	 HAL_UART_Transmit(&huart2, (uint8_t *)"MCU woke up from sleep mode\r\n", strlen("MCU woke up from sleep mode\r\n"), HAL_MAX_DELAY);
    
    	 // Delay to avoid immediate sleep cycle
    	 HAL_Delay(1000);
    
    ...
    
    void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
    
    	 // Print current time and date after wake-up
    	 HAL_RTC_GetTime(hrtc, &sTime, RTC_FORMAT_BIN);
    	 HAL_RTC_GetDate(hrtc, &sDate, RTC_FORMAT_BIN);
    	 sprintf(date, "Date: %02d.%02d.%02d\t", sDate.Date, sDate.Month, sDate.Year);
    	 sprintf(time, "Time: %02d:%02d:%02d\r\n", sTime.Hours, sTime.Minutes, sTime.Seconds);
    	 HAL_UART_Transmit(&huart2, (uint8_t *)date, strlen(date), HAL_MAX_DELAY);
    	 HAL_UART_Transmit(&huart2, (uint8_t *)time, strlen(time), HAL_MAX_DELAY);
    }

    This is the code I wrote.Can you pls tell where I went wrong?
     

    ST Employee
    June 27, 2024

    Hello @AnanyaRavi29 

    In Sleep mode, only certain interrupts can wake up the microcontroller, make sure that the RTC wakeup interrupt is enabled