cancel
Showing results for 
Search instead for 
Did you mean: 

Store RTC date into Backup Register

Fede Rico
Associate III
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.
15 REPLIES 15
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 Venmo
Up vote any posts that you find helpful, it shows what's working..
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

Posted on October 25, 2017 at 15:13

HAL's not my thing... 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 25, 2017 at 15:16

And about the second option?

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?

Posted on October 25, 2017 at 16:52

Hello!

CNTH ,CNTL battery backuped real time clock registers can keep time and date for 136 years until wrapped.

Second option actually is not just an option if you want to have a battery backuped RTC with Calendar. It is imperative to use the

32b counter to keep the callendar and time.

Regards

vf

Posted on October 25, 2017 at 17:01

Hi Vangelis,

On my stm32f103, after a reset, only the time is resumed. And this is a know behaviour. 

I also added a backup battery and when I re-plug the Vdd the MCU resume only the time but the date i always lost.

Every time I reboot the board I lose the Date!

The option B in to use the second RTC counter and keep track of unixtime?

Thanks!

Posted on October 25, 2017 at 17:17

The hardware only implements a second counter (can arguably tick faster/slower), it has no concept of date.

As stated, I've used SPL to implement an epoch based solution, I use one I wrote for WinCE, which is very similar to UNIX, but a different start date. Other times I use GPS day zero, depends on implementation simplicity and reduction of conversion back-and-forth between formats.

If the library you use is defective your option is to code something better.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 25, 2017 at 17:18

>>

And about the second option?

I think I already stated that was the method I used/preferred.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 25, 2017 at 17:22

I'm sorry but what is an SPL?

By the way, Is the second counter inside the RTC? I look into the datasheet but I don't find nothing.

A third option is to store the date into the internal flash memory.

Thanks