2020-08-27 01:29 AM
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 ?
Solved! Go to Solution.
2020-08-27 04:45 AM
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:
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
2020-08-27 04:45 AM
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:
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
2020-08-27 06:58 AM
> This behavior is intentional,
You're surely kidding. Doesn't this squarely defeat the very purpose of RTC?
JW
2020-08-27 07:05 AM
> You're surely kidding.
Well, no.
But the reasons would go beyond the scope of the thread.
/Peter
2020-08-31 12:29 AM
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
2021-04-27 07:22 AM
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.)
2023-07-28 09:31 AM
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.
2024-08-09 05:14 AM
in rtc.c
before HAL_RTC_Set....
/* USER CODE BEGIN Check_RTC_BKUP */
#define HAL_RTC_SetTime(a, b, c) (HAL_OK)
#define HAL_RTC_SetDate(a, b, c) (HAL_OK)
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x0;
sTime.Minutes = 0x0;