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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

This behavior is intentional, as there are possibilities for misconfiguration of the RTC.

You are right, that init code will be recreated if you regenerate the code using CubeMX, but there is a simple trick to avoid a manual removal of that code afterwards - a pragma.

Just use a conditional pragma, e.g. #ifdef RTC_SET_VALUES to exclude two blocks from being compiled when RTC_SET_VALUES is not defined. The pragmas are not touched by CubeMX when inserted between USER CODE BEGIN ... USER CODE END:

  • block 1:
    • place the #ifdef within USER CODE ... RTC_Init 0
    • place the #endif within USER CODE ... RTC_Init 1
  • block 2:
    • place the #ifdef within USER CODE ... Check_RTC_BKUP
    • place the #endif within USER CODE ... RTC_Init 2

If you for whatever reason want to include these blocks again which sets the RTC to the predefined values at every reset just define RTC_SET_VALUES, e.g. in the block USER CODE ... PD with #define RTC_SET_VALUES.

Good luck!

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

6 REPLIES 6
Peter BENSCH
ST Employee

This behavior is intentional, as there are possibilities for misconfiguration of the RTC.

You are right, that init code will be recreated if you regenerate the code using CubeMX, but there is a simple trick to avoid a manual removal of that code afterwards - a pragma.

Just use a conditional pragma, e.g. #ifdef RTC_SET_VALUES to exclude two blocks from being compiled when RTC_SET_VALUES is not defined. The pragmas are not touched by CubeMX when inserted between USER CODE BEGIN ... USER CODE END:

  • block 1:
    • place the #ifdef within USER CODE ... RTC_Init 0
    • place the #endif within USER CODE ... RTC_Init 1
  • block 2:
    • place the #ifdef within USER CODE ... Check_RTC_BKUP
    • place the #endif within USER CODE ... RTC_Init 2

If you for whatever reason want to include these blocks again which sets the RTC to the predefined values at every reset just define RTC_SET_VALUES, e.g. in the block USER CODE ... PD with #define RTC_SET_VALUES.

Good luck!

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

> This behavior is intentional,

You're surely kidding. Doesn't this squarely defeat the very purpose of RTC?

JW

Peter BENSCH
ST Employee

> You're surely kidding.

Well, no.

But the reasons would go beyond the scope of the thread.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
gjanet
Associate II

Thanks for the help !

pragmas were my next stop but I wanted to make sure I hadn't missed something.

I have to admit that I was surprised to see that keeping date and time values between soft resets was not the default behaviour and am now quite curious as to why it is not.

Thank you for the help again.

Guillaume

Please excuse me for posting to an eight month old thread. I've had the same problems, and solved them by commenting out code even more aggressively.

But the actual problem is that you do want to initialize the RTC to zero if the battery is not present, or if this is your first boot with the battery. How can I detect these cases and act accordingly? Could you augment CubeMX in this way?

(Thanks, Jan, for linking to this post.)

I arrived at the same solution myself having been tripped up with this issue on multiple occasions because it is unexpected behavior.  Having users go digging through the library code and search the forum to figure this out should be ample evidence that resetting the RTC each time is the incorrect behavior.  And I'm not sure what the justification for resetting it can be, but nearly every person using the RTC will surely #ifdef it out.  I would propose that Cube makes this an option in the RTC params and defaults it to NOT being reset upon initialization.  I think that would make 99% of the users happy.