cancel
Showing results for 
Search instead for 
Did you mean: 

RTC

lucas23
Associate II
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);

}

13 REPLIES 13
monta
Associate II
Posted on July 21, 2015 at 21:30

Actually nothing is weird to start with. I use RTC on my project two.

To answer your first question , it's quiet normal for time loss if you disconnect you STM32F from power. The RTC unit need constant power to keep track of time , which is quiet logical.

The Cube MX will generate sample code with default time , and you can change it later. For me my device syncs the date and time later on from my mobile phone via bluetooth.

lucas23
Associate II
Posted on July 21, 2015 at 21:53

I forgot to mention, I am using a battery to maintain the RTC working.

As I said, If those lines are commented, the RTC works just like I want it to.

It would be nice if they insert a option in the Cube allowing choose if you want to set the time to zero when initializing.

monta
Associate II
Posted on July 21, 2015 at 22:00

sTime.RTC_Seconds = 0x20; // sets seconds to 20

sTime.

RTC_Minutes = 0x10; // sets minutes to 10

sTime.RTC_Hours   = 0x16 // sets hour to 16 or 4pm

Add that to your code and you edit the time before flashing the MCU. As far as the power goes on, the RTC will work as long as the STM MCU is powered up. If your system suddenly looses power it will revert to the default time set in the code once powered again.

monta
Associate II
Posted on July 21, 2015 at 22:04

Or you can simply uncomment these 

 // Manually commented

  //sTime.Hours = 0;

  //sTime.Minutes = 0;

  //sTime.Seconds = 0;

to 

  sTime.Hours = 0;

  sTime.Minutes = 0;

  sTime.Seconds = 0;

lucas23
Associate II
Posted on July 21, 2015 at 22:39

I think you are not understanding  my point.

I have a battery to maintain the RTC working when the power goes down. So, I DON'T want the Init function to set a default value each time the processor starts again. I want to maintain the actual value of the RTC.

As I said, the problem is already solved by commenting the initialization

I only created that thread to say that would be nice to have a option in the Cube IDE to choose if you want to set a default value when initializing or not.

monta
Associate II
Posted on July 21, 2015 at 22:44

Aha, I see. my bad. Well I'm glad you're managing to bypass this problem anyway. best of lucks !

lucas23
Associate II
Posted on July 22, 2015 at 13:56

I made a mistake, I copied a older version of my project.

In fact, you need to comment a few more lines in order to maintain the actual RTC values.

/* 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);

}

stm32cube-t
Senior III
Posted on July 24, 2015 at 15:03

Dear user,

Indeed this is a known an issue. It has been escalated to be fixed within our next MX release 4.10.

Best regards

ricardodd
Associate
Posted on December 21, 2015 at 15:21

Hi,

I'm using STM32CubeMX 4.12.0 with a STM32D042C6Tx microcontroller and I'm still facing the same issue.

Have you guys really fixed that on release 4.10?

Regards.