2015-09-01 08:06 AM
MX_RTC_Init still overwrites RTC date and time on startup.
Cube 4.1 / F4 1.8.0 #no-cheers2015-09-01 08:51 AM
Before you blame, make sure you did it OK!
RTC should be initialized ONLY if it does not running and times is not set. It both is OK, RTC should not be reinitialized.All examples also works like that. Don't think CubeMX will do everything for you.When you init RTC, save some status to RTC backup register. Before you init RTC again, check if value on RTC backup is the same as you expect. If it is not, init rtc, set clock and set variable on backup register.2015-09-01 11:10 PM
thats exactly how we solve it. We have main.c created in a different folder by cube and do a beyond compare each time we create it through cube. However I understand that the cube was intended to create a main.c where the user can add his code which is then regarded in future creations which works better and better with each version. So I don't expect Cube to do all the work, but somehow come up with a conditional hook where the user can insert his own RTC init in this example.
2015-09-02 07:44 AM
In my opinion this is a short sighted implementation by ST. They overlooked the standard case where the RTC is on battery backup. They put RTC time and date setting outside of user generated code, so every time the processor is reset, so is the RTC, which defeats the purpose of battery backup.
Hal2016-12-09 07:08 AM
Hi,
I face the same issue.
I would like to keep the cube generated code free of modifications from my side but the MX_RTC_Init function which is always called in the main (by code generated as well) always set the date and time in regards to the configuration done in cube.
And since no //USER CODE BEGIN - END blocks are available, there is no way to disable or modify the set date and time in a way that will not be erased by a new code generation from stm cube.
Did someone find a way to handle that properly?
Thanks and regards,
Bertrand
2016-12-09 07:27 AM
Hi
,I submit your request internally to our CubeMX team
-Walid F-
2017-04-07 05:52 AM
Hi,
Given that several people are having this issue and that the bug is still not fixed, here is a workaround that keep your custom code on the next generation:
in main.c
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/*override MX_RTC_Init with a custom implementation
that check if the rtc was already initialized*/
#define MX_RTC_Init MY_MX_RTC_Init
/* USER CODE END PFP */�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
in rtc.h or in a code section above MX_RTC_INIT prototype if you don't have the generated code splitted
/* USER CODE BEGIN Private defines */
void MY_MX_RTC_Init(void);
/* USER CODE END Private defines */�?�?�?
and in the corresponding .c file
/* USER CODE BEGIN 0 */
void MY_MX_RTC_Init(void)
{
hrtc.Instance = RTC;
if((hrtc.Instance->ISR & RTC_ISR_INITS) != RTC_ISR_INITS)
{
//first RTC init
MX_RTC_Init();
}
}
/* USER CODE END 0 */�?�?�?�?�?�?�?�?�?�?�?
Hope it helps.
Bertrand.
2017-05-09 03:40 PM
I used Zoyhar's code strategy and found that it did not work out-of-the-box when using the MX_RTC_init() function generated by CubeMX. The INITS flag in ISR always read 0 when re-starting the code even though the RTC was already previously initialized and happily updating the time-of-day.
The CubeMX
MX_RTC_init()
code initializes the calendar as follows:sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY; sDate.Date = 0x1; sDate.Year = 0x0;According to the Reference Manual RM0038, section 2.3.5 'RTC initialization and configuration', the setting of the INITS flag is determined by the year fields being non-zero.
After a system reset, the application can read the INITS flag in the RTC_ISR register to
check if the calendar has been initialized or not. If this flag equals 0, the calendar has notbeen initialized since the year field is set at its power-on reset default value (0x00).In summary, the generated CubeMX generated software will continue to re-initialize the RTC, and the INITS flag will not be set until the year is changed from 0x00 (e.g. year 2000 or 2100).
2017-05-11 01:12 AM
Hi Peter,
You could just check the calendar box in RTC's pinout and give a date in the configuration. That way, you will end up with non-default generated code.