cancel
Showing results for 
Search instead for 
Did you mean: 

RTC code always setting time/date

RHerm.3
Associate III

Using STM32CubeIDE with a STM32L4R5.

The RTC generated code initializes the RTC then sets the time and date.

Is there a setting that will inhibit setting the RTC time and date during initialization?

3 REPLIES 3
Ogulcan Ariyurek
Associate II

Hi,

I generally overcome such issues by putting prepocessor macros before and after the generated code.

#define ENABLE_CUBEMX_NONSENSES 0
 
#if (ENABLE_CUBEMX_NONSENSES == 1)
  /* USER CODE END RTC_Init 1 */
  /** Initialize RTC and set the Time and Date
  */
  RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
  RTC_InitStruct.AsynchPrescaler = 127;
  RTC_InitStruct.SynchPrescaler = 255;
  LL_RTC_Init(RTC, &RTC_InitStruct);
  LL_RTC_SetAsynchPrescaler(RTC, 127);
  LL_RTC_SetSynchPrescaler(RTC, 255);
  /** Initialize RTC and set the Time and Date
  */
  if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) != 0x32F2){
 
  RTC_TimeStruct.Hours = 0x0;
  RTC_TimeStruct.Minutes = 0x50;
  RTC_TimeStruct.Seconds = 0x0;
  LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_TimeStruct);
  RTC_DateStruct.WeekDay = LL_RTC_WEEKDAY_TUESDAY;
  RTC_DateStruct.Day = 0x20;
  RTC_DateStruct.Month = LL_RTC_MONTH_JULY;
  RTC_DateStruct.Year = 0x21;
  LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_DateStruct);
    LL_RTC_BAK_SetRegister(RTC,LL_RTC_BKP_DR0,0x32F2);
  }
  /* USER CODE BEGIN RTC_Init 2 */
#endif

And you can copy and paste the required lines of codes before or after this, so that whenever you change something in the CubeMX, your code remains safe.

I know this a far from being ideal, but it works. :smiling_face_with_smiling_eyes:

Regards,

Ogulcan

TDK
Guru

Typically this is solved by checking to see if it's already initialized and if so, skipping the initialization code. This can be done in the user code section.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks Ogulcan,

I am already using this method in other places.

The issue this the RTC is that the USER CODE comments cover the actual RTC initialization and TIME/DATE setting; so you lose everything.

My fallback will be to use my own RTC initialization function based on the code that was generated and not use the generated initialization function.

I was hoping for a setting I missed in the GUI.