cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX does not preserve conditional RTC initialization (it reinitialize when backup register is set)

gianmarco.cerutti
Associate II

Hi ST Support Team,

I am using MXCube for STM32U5A5VJ. I have enabled the RTC, and cube generates the following Init Function. In this way, everytime I restart the controller, the RTC date is gonna be overwritten. In practice, I would like to have the bracket at line 9 down in line 21. Thank you for your support.

Best Regards, 

Gianmarco Cerutti

 

void MX_RTC_Init(void)
{
...
  /** Initialize RTC and set the Time and Date
  */
  if(LL_RTC_BKP_GetRegister(RTC,LL_RTC_BKP_DR0) != 0x32F2){

    LL_RTC_BKP_SetRegister(RTC,LL_RTC_BKP_DR0,0x32F2);
  }
  RTC_TimeStruct.Hours = 0x0;
  RTC_TimeStruct.Minutes = 0x0;
  RTC_TimeStruct.Seconds = 0x0;

  LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_TimeStruct);
  RTC_DateStruct.WeekDay = LL_RTC_WEEKDAY_MONDAY;
  RTC_DateStruct.Month = LL_RTC_MONTH_FEBRUARY;
  RTC_DateStruct.Day = 0x11;
  RTC_DateStruct.Year = 0x25;

  LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_DateStruct);
...
}

 

5 REPLIES 5
Ghofrane GSOURI
ST Employee

Hello @gianmarco.cerutti 

CubeMX will overwrite any changes you make outside the /* USER CODE BEGIN ... */ and /* USER CODE END ... */ blocks.

Try to put your code in the user section.

GhofraneGSOURI_0-1759930783703.png

THX

Ghofrane

 

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.

Thank you for your answer. The problem is that the cube-generated code is resetting date by reboot. I am using the LL library.

Hello @gianmarco.cerutti 

As a workaround, copy and paste the MX_RTC_Init function into the user code section and make the required modifications.

void MX_RTC_Init(void)
{
...
  /** Initialize RTC and set the Time and Date
  */
  if(LL_RTC_BKP_GetRegister(RTC,LL_RTC_BKP_DR0) != 0x32F2){

  LL_RTC_BKP_SetRegister(RTC,LL_RTC_BKP_DR0,0x32F2);
  RTC_TimeStruct.Hours = 0x0;
  RTC_TimeStruct.Minutes = 0x0;
  RTC_TimeStruct.Seconds = 0x0;

  LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_TimeStruct);
  RTC_DateStruct.WeekDay = LL_RTC_WEEKDAY_MONDAY;
  RTC_DateStruct.Month = LL_RTC_MONTH_FEBRUARY;
  RTC_DateStruct.Day = 0x11;
  RTC_DateStruct.Year = 0x25;

  LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_DateStruct);
...
}
}

Then, go back to CubeMX and untick the "Generate code" option for MX_RTC_Init.

GhofraneGSOURI_0-1759937625592.png

 

This way, CubeMX will not generate the MX_RTC_Init function, and you will already have the customized function with your modifications.

Remember to call your modified MX_RTC_Init function in the main function to ensure the RTC is properly initialized.

THX

Ghofrane 

 

 

 

 

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.

Thank you for the solution. This is actually what I have been doing. However, I would like CubeMX to do the job for me. Is there any way I can change the code generation in CubeMX? It looks like a bug — is it going to be fixed in a new release?

Hello @gianmarco.cerutti 

I appreciate you bringing this issue to our attention.

A ticket has been escalated to our development team to address and correct the current behavior.

Internal ticket number is 219211 

I will keep you posted with updates.

THX

Ghofrane 

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.