cancel
Showing results for 
Search instead for 
Did you mean: 

[solved]STM32G RTC sets to some value afterr reset

lauris
Associate II

Hello,

I have an application which I would like to build on to of cubeMX generated HAL driver, but I came across strange issue that I am unable to keep RTC value on reset. Drivers part is as generated with cube using freeRTOS. system reset is triggered with `NVIC_SystemReset()`, MX_RTC_Init is as follows:

/* RTC init function */
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;
  hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */
  if (__HAL_RTC_IS_CALENDAR_INITIALIZED(&hrtc) == 0U)
  {
	  TRice("[RTC] - init\n");
  /* 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_SET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_WEDNESDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 1;
  sDate.Year = 25;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enable the Alarm A
  */
  sAlarm.AlarmTime.Hours = 0;
  sAlarm.AlarmTime.Minutes = 0;
  sAlarm.AlarmTime.Seconds = 0;
  sAlarm.AlarmTime.SubSeconds = 0;
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_SET;
  sAlarm.AlarmMask = RTC_ALARMMASK_SECONDS;
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  sAlarm.AlarmDateWeekDay = 1;
  sAlarm.Alarm = RTC_ALARM_A;
  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enable the Alarm B
  */
  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
  sAlarm.Alarm = RTC_ALARM_B;
  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */
  } else {
		RTC_TimeTypeDef time = { 0 };
		RTC_DateTypeDef date = { 0 };
		HAL_RTC_WaitForSynchro(&hrtc);
		HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
		HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
	  TRice("[RTC] - no init: %d-%02d-%02d %02d:%02d:%02d\n", (2000 + date.Year), date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);
  }
  /* USER CODE END RTC_Init 2 */

}

 After MCU is being loaded with built FW, on tracer I see :

[RTC] - no init: 2025-01-01 00:00:02

If system time is set to some date/time and MCU is being reset with debugger, I get proper date/time with `no init` line printed. But if I reset system from app using NVIC_SystemReset() - I get some magic time '2025-03-16 23:59:44'. Which I thin was used some time ago during development for initial time, but now not present in code.

So far I'm still working on getting trace info of these instances.

 

Anyone having any suggestions on what and why can be happening?

 

 

Sorry, it seems like false alarms And can't find how to delete thread. Application was switching to other similar app and behaving without new code changes. In other word - boot-loader issues.

0 REPLIES 0