cancel
Showing results for 
Search instead for 
Did you mean: 

Does RTC backup reset when power is turned off?

ckim.2390
Associate II

RTC was set in Cube MX. (0y/0M/0d 0h:0m:0s)

A backup register is used to store the date and time.

For example, if you press the reset button at 0/0/0 0:1:1, RTC is activated from 1m 1s.

But when I turn off the power and turn on the power again, it goes back to 0/0/0 0:0:0.

And the backup register value is cleared.

I checked that HAL_RTCEx_BKUPWrite() is executed again.

I think it should work from the time the power is turned off.

Does RTC backup return to Cube MX settings when power is turned off?

Is the backup register value also deleted?

static void MX_RTC_Init(void)

{

  /* USER CODE BEGIN RTC_Init 0 */

  __HAL_RCC_RTC_ENABLE();

  __HAL_RCC_RTCAPB_CLK_ENABLE();

  /* USER CODE END RTC_Init 0 */

  RTC_TimeTypeDef sTime = {0};

  RTC_DateTypeDef sDate = {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;

  hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;

  if (HAL_RTC_Init(&hrtc) != HAL_OK)

  {

    Error_Handler();

  }

  /* USER CODE BEGIN Check_RTC_BKUP */

  if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) == 0x32F2)

    return;

  else

    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);

  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date

  */

  sTime.Hours = 0;

  sTime.Minutes = 0;

  sTime.Seconds = 0;

  sTime.SubSeconds = 0;

  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

  sTime.StoreOperation = RTC_STOREOPERATION_RESET;

  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)

  {

    Error_Handler();

  }

  sDate.WeekDay = RTC_WEEKDAY_MONDAY;

  sDate.Month = RTC_MONTH_JANUARY;

  sDate.Date = 0;

  sDate.Year = 0;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)

  {

    Error_Handler();

  }

  /** Enable the WakeUp

  */

  if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 60, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) != HAL_OK)

  {

    Error_Handler();

  }

  /* USER CODE BEGIN RTC_Init 2 */

  /* USER CODE END RTC_Init 2 */

}

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Backup registers are in SRAM and are erased when power is lost. VBAT supplies power to them, so the typical way to retain them is to attach a battery to VBAT to keep it powered when your main source of power is gone.

From the RM:

0693W00000DlTnsQAF.png

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

1 REPLY 1
TDK
Guru

Backup registers are in SRAM and are erased when power is lost. VBAT supplies power to them, so the typical way to retain them is to attach a battery to VBAT to keep it powered when your main source of power is gone.

From the RM:

0693W00000DlTnsQAF.png

If you feel a post has answered your question, please click "Accept as Solution".