cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Calendar resets in CubeMX generated Init function

gjanet
Associate II

Hi everyone !

first time poster, hope I'm doing it right.

I'm working with an STM32F107VCT and using CubeMX to generate code.

I have an issue with the RTC calendar, it goes back to 0 every reset which, if I'm not mistaken, it should not on a soft reset.

I've found that in the MX_RTC_Init function, the date and time are set to the default values (see the CubeMX generated code below).

/** Initialize RTC and set the Time and Date
   */
  sTime.Hours = 0x0;
  sTime.Minutes = 0x0;
  sTime.Seconds = 0x0;
 
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) {
    Error_Handler();
  }
  DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
  DateToUpdate.Month = RTC_MONTH_JANUARY;
  DateToUpdate.Date = 0x1;
  DateToUpdate.Year = 0x0;
 
  if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK) {
    Error_Handler();
  }

Removing this piece of code solves the issue but since it is not user code, it is back every time I need to re-generated code.

Am I missing something or is this a CubeMX Issue ?

10 REPLIES 10

Hi, 

 

Solution with adding code in USER CODE sections works, but not in all. I have been added 

" if (READ_BIT(hrtc.Instance->ISR, RTC_ISR_INITS) == 0U) { " in Check_RTC_BKUP user code

and

" } " in RTC_Init 2 user code.

This solution works fine. Problem is with Alarm initiating section - it is also between Check_RTC_BKUP  and  RTC_Init 2 so it is also skipped. In my opinion at least new USER CODE section between time/date setting section and alarms initialization section should be added. Better solution is to add of some kind of checks if RTC is working in generated code, maybe configurable in RTC configuration.

R.