cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 won't wake-up from standby mode using RTC alarm.

Costas Papachristou
Associate II

MCU wakes-up from standby mode by external signal on the wake-up pin (PA0). Also, the RTC alarm does produce an interrupt when NOT in standby mode (callback function does send some characters to UART). BUT, if the MCU is in standby it cannot wake-up by the RTC alarm. Below are all relevant routines. Initialization code by CubeMX version 4.27.0. If anyone can see any problem with the code please let me know! THANKS!!!

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
 
  if(hrtc->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspInit 0 */
 
  /* USER CODE END RTC_MspInit 0 */
    HAL_PWR_EnableBkUpAccess();
    /* Enable BKP CLK enable for backup registers */
    __HAL_RCC_BKP_CLK_ENABLE();
    /* Peripheral clock enable */
    __HAL_RCC_RTC_ENABLE();
    /* RTC interrupt Init */
    HAL_NVIC_SetPriority(RTC_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(RTC_IRQn);
   		
  /* USER CODE BEGIN RTC_MspInit 1 */
 
  /* USER CODE END RTC_MspInit 1 */
  }
 
}
void RTC_IRQHandler(void)
{
  /* USER CODE BEGIN RTC_IRQn 0 */
 
  /* USER CODE END RTC_IRQn 0 */
  HAL_RTC_AlarmIRQHandler(&hrtc);
  /* USER CODE BEGIN RTC_IRQn 1 */
 
  /* USER CODE END RTC_IRQn 1 */
/* RTC init function */
static void MX_RTC_Init(void)
{
    /**Initialize RTC Only 
    */
  hrtc.Instance = RTC;
  hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
		 
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
		{
			_Error_Handler(__FILE__, __LINE__);
		}
 
    /**Initialize RTC and set the Time and Date 
    */
  if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x32F2)
		{
		 sTime.Hours = 0x00;
		 sTime.Minutes = 0x00;
		 sTime.Seconds = 0x00;
 
  	 if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
			 {
				_Error_Handler(__FILE__, __LINE__);
			 }
 
		 DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
		 DateToUpdate.Month = RTC_MONTH_NOVEMBER;
		 DateToUpdate.Date = 0x01;
		 DateToUpdate.Year = 0x12;
 
		 if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
				{
					_Error_Handler(__FILE__, __LINE__);
				}
 
     HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR1,0x32F2);
		}
 
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
    while (HAL_UART_Transmit(&huart1, (uint8_t*) "Hello!!\r\n", 9, UART_Timeout)==HAL_BUSY) {HAL_Delay(1);}	 	 // transmit data
		
}
void RTC_Alarm(void)  // Alarm setup routine
{
	 /*#################  Configure the RTC Alarm peripheral #################*/
  sAlarm.Alarm = RTC_ALARM_A;
  			
  sAlarm.AlarmTime.Hours   = 0x0;
  sAlarm.AlarmTime.Minutes = 0x00;
  sAlarm.AlarmTime.Seconds = 0x10;
	
	HAL_RTC_SetAlarm_IT(&hrtc,&sAlarm,RTC_FORMAT_BCD);
}	 	
void Enter_StandbyMode(void)
{
	
  /* Enable Power Clock*/
  __HAL_RCC_PWR_CLK_ENABLE();
  
  /* Allow access to Backup */
  HAL_PWR_EnableBkUpAccess();
 
  /* Reset RTC Domain */
  __HAL_RCC_BACKUPRESET_FORCE();
  __HAL_RCC_BACKUPRESET_RELEASE();
  
  /* Disable all used wake-up sources: Pin1(PA.0) */
  HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
			  
  /* Clear all related wake-up flags */
  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
  
  /* Re-enable all used wake-up sources: Pin1(PA.0) */
  HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
			
  /* Request to enter STANDBY mode  */
  HAL_PWR_EnterSTANDBYMode();
}

4 REPLIES 4
Costas Papachristou
Associate II

Could it be a faulty/clone/bad MCU? I bought it from ebay, it's one of these development boards sold for around $2. I have also noticed that the power flag doesn't always work when waking-up from standby.

if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
{
 // Handle wake-up from standby
}

Costas Papachristou
Associate II

Regarding the flag, it turns out that when the MCU wakes-up from standby, it raises the WU flag and not the SB flag. So, this issue has been solved. Any thoughts about the RTC wake-up issue?

Thanks

amit_kotal
Associate II

Greetings,

I too was facing the same issue and by going through the reference manual, it seems that this series of microcontrollers do not support RTC Wake from Standby/Sleep.

Thanks

DLech.2
Associate

Hi guys,

it seems I'm currently facing a similar problem. My STM32F103CB perfectly executes RTC alarm interrupts during normal operation but does not wake from STOP mode.

<quote> I too was facing the same issue and by going through the reference manual, it seems that this series of microcontrollers do not support RTC Wake from Standby/Sleep. </quote>

@amit_kotal​ I know it's been a while, but could you please give me a hint were in the manual you found that this does not work on that MCU series?

All I was able to find says that this should definitely work.

Thanks