Skip to main content
Fede Rico
Associate III
October 25, 2017
Question

Store RTC date into Backup Register

  • October 25, 2017
  • 3 replies
  • 3672 views
Posted on October 25, 2017 at 12:09

Hi there,

I'm using the RTC on my STM32F103 to keep the date and time but when I reset the MCU the date is lost.

I know that this behaviour is due to the RTC implementation ( it uses a counter ).

To prevent the date loss, I think to store the date into one or more backup register.

My date is composed by 3 byte (Year, Month and Date).

Could any one provide me a example about how to solve my problem?

Thanks!

#rtc-time-and-date #backup-registers #rtc-backup

Note: this post was migrated and contained many threaded conversations, some content may be missing.
This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
October 25, 2017
Posted on October 25, 2017 at 14:34

Look at

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

In the SPL we just managed the 32-bit count using epoch calendaring. See source for C library UNIX time management functions.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Fede Rico
Fede RicoAuthor
Associate III
October 25, 2017
Posted on October 25, 2017 at 15:07

Hi Clive,

thanks for the link.

I have one question about the modification into HAL RTC library.

I saw that 'RTC_DateUpdate' is called only when the application try to read the time and the date from the RTC. So, if I never call HAL_RTC_GetDate and HAL_RTC_GetTime the date is never stored into the BKP_DR2. Am I right?

The second option is to store the unix time into the RTC second timer and use the function provided by time.h to convert the unix time to a date/time?

Thanks for the help

Tesla DeLorean
Guru
October 25, 2017
Posted on October 25, 2017 at 15:13

HAL's not my thing... 

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Mark Shoe
Associate III
October 26, 2017
Posted on October 26, 2017 at 13:58

{CODE()}

void save_value(uint32_t save)

{

HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, save);

}

uint32_t get_value(void)

{

return HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0);

}

{CODE}

Mohammad A
Senior
October 26, 2017
Posted on October 26, 2017 at 23:17

Please take a look at this comment. This will help you fix the code:

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

 
Fede Rico
Fede RicoAuthor
Associate III
October 27, 2017
Posted on October 27, 2017 at 08:12

I do the same things but into two custom functions in order to store and load the date from BKP register (DR2 and DR3). Using custom function allow you to store the date in any moment without wait a new day.

Unfortunately this method works only when the power loss is less then a day, otherwise, after two or more days, the date will be wrong.

I think that the best way is to store the unix time into the RTC 32bit counter since the RTC counter has a backup battery.

Vangelis Fortounas
Associate II
October 27, 2017
Posted on October 27, 2017 at 08:35

hello!

Have a look also to

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

regards

vf