Skip to main content
lucas23
Associate II
July 21, 2015
Question

RTC

  • July 21, 2015
  • 13 replies
  • 3448 views
Posted on July 21, 2015 at 21:18

Hi,

I am using Cube MX to generate the RTC code.

It is working perfectly fine, the time is counted correctly. But every time I turn off the device, the time and date are set back to 0.

I found the problem in the Init function. Every time the RTC is initialized, the time and date are set to zero again.

I solved the problem just by commenting those lines, but I am wondering why cube generates such code! Once that the code is not supposed to be edited, every time I generate a new version I must remember to go there and comment those lines. It is weird. Am I doing something wrong?

/* RTC init function */

void MX_RTC_Init(void)

{

  RTC_TimeTypeDef sTime;

  RTC_DateTypeDef sDate;

  RTC_AlarmTypeDef sAlarm;

    /**Initialize RTC and set the Time and Date 

    */

  hrtc.Instance = RTC;

  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

  hrtc.Init.AsynchPrediv = 127;

  hrtc.Init.SynchPrediv = 255;

  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

  HAL_RTC_Init(&hrtc);

  // Manually commented

  //sTime.Hours = 0;

  //sTime.Minutes = 0;

  //sTime.Seconds = 0;

  //sTime.SubSeconds = 0;

  sTime.TimeFormat = RTC_HOURFORMAT12_AM;

  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

  sTime.StoreOperation = RTC_STOREOPERATION_RESET;

  HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);

  // Manually commented

  //sDate.WeekDay = RTC_WEEKDAY_MONDAY;

  //sDate.Month = RTC_MONTH_JANUARY;

  //sDate.Date = 1;

  //sDate.Year = 0;

  HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);

    /**Enable the Alarm A 

    */

  sAlarm.AlarmTime.Hours = 0;

  sAlarm.AlarmTime.Minutes = 0;

  sAlarm.AlarmTime.Seconds = 0;

  sAlarm.AlarmTime.SubSeconds = 0;

  sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;

  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;

  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;

  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;

  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;

  sAlarm.AlarmDateWeekDay = 1;

  sAlarm.Alarm = RTC_ALARM_A;

  HAL_RTC_SetAlarm(&hrtc, &sAlarm, FORMAT_BCD);

}

    This topic has been closed for replies.

    13 replies

    tekampe
    Associate II
    March 17, 2017
    Posted on March 17, 2017 at 17:36

    Hi ST

    I have same problem.

    When

    you

    fix

    this ?

    Jens

    ST Technical Moderator
    March 19, 2017
    Posted on March 19, 2017 at 12:38

    Hi tekampe.jens,

    Maybe you are using an old CubeMx version. If it is the case, I recommend you to use the last version available V 4.20.

    Then, if the problem is not resolved, it should be better to create a new discussion describing your use case and giving more details about your issue.

    You can also share your .ioc file, this will help user forum and ST team to check your case and answer you.

    Imen

    In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
    bet
    Associate II
    April 7, 2017
    Posted on April 07, 2017 at 14:54

    The same question was posted here:

    https://community.st.com/0D50X00009Xka36SAB

    I provided a workaround if it helps anyone.

    Regards,

    Bertrand.