cancel
Showing results for 
Search instead for 
Did you mean: 

Is safe to deinit RTC and reinit RTC for the RTC alarm wakeup?

jiangpen
Associate II

I am trying to use RTC to wake up my stop mode every 30 second.

I have two questions:

so every time when RTC wakes up, I use below code to

HAL_RTC_DeInit(&hrtc);:

MX_RTC_Init( true );

to reinit RTC, and I don't use the interrupt handler.

It appears working fine, but I am not sure any risk of this.

My second question is I am a little confused about the RTC configuration.

I want a 15 s interval alarm for stop mode and a 24 hour alarm for standby mode.

So my code like below, but I know it is wrong, but not sure what went wrong.

I use LSI 40K RTC clock.

void MX_RTC_Init(bool stopMode)
{
  RTC_TimeTypeDef sTime;
  RTC_DateTypeDef sDate;
  RTC_AlarmTypeDef sAlarm;
 
    /**Initialize RTC Only 
    */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 319;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
    /**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(__FILE__, __LINE__);
  }
 
  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(__FILE__, __LINE__);
  }
 
  if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
  }
    /**Enable the Alarm A 
    */
 
  sAlarm.AlarmTime.Minutes = 0x0;
  if(stopMode)
  {
      sAlarm.AlarmTime.Hours = 0x0;
      sAlarm.AlarmTime.Seconds = 15;
      sAlarm.AlarmMask =RTC_ALARMMASK_SECONDS;
  }
  else
  {
      sAlarm.AlarmTime.Hours = 23;
      sAlarm.AlarmTime.Seconds = 0;
      sAlarm.AlarmMask = RTC_ALARMMASK_HOURS;
  }
 
 
  sAlarm.AlarmTime.SubSeconds = 0x0;
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
 
  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(__FILE__, __LINE__);
  }
 
}

0 REPLIES 0