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
Mark Shoe
Senior
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
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

 
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.

Posted on October 27, 2017 at 08:35

hello!

Have a look also to

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

regards

vf

Posted on October 27, 2017 at 08:54

No, it does work even after power loss for a day or more. Take a look at HAL_RTC_GetTime, it

explicitly calculates days elapsed since power loss and then passes that to  RTC_DateUpdate(hrtc, days_elapsed); which will update the date based on days elapsed.

Seems they have implemented this functionality, but they have forgotten to implement date save/restore.

Posted on October 27, 2017 at 09:50

Thanks for the explanation. I don't look inside RTC_DateUpdate.

I solved in this way:

  1. One function to store the date into the BKP registers:

    static void RTC_StoreDateIntoBkpReg( RTC_DateTypeDef* dateRtc )

    {

    uint32_t dateToStore;

    memcpy( &dateToStore, dateRtc, sizeof(dateToStore) );

    HAL_RTCEx_BKUPWrite( &hrtc, RTC_BKP_DR2, (dateToStore >> 16) );

    HAL_RTCEx_BKUPWrite( &hrtc, RTC_BKP_DR3, (dateToStore & 0xFFFF) );

    }
  2. One function to read the date form BKP registers

    static void RTC_LoadDateFromBkpReg( RTC_DateTypeDef* dateRtc )

    {

    uint32_t dateToStore;

    dateToStore = HAL_RTCEx_BKUPRead( &hrtc, RTC_BKP_DR3 );

    dateToStore |= ( HAL_RTCEx_BKUPRead( &hrtc, RTC_BKP_DR2 ) << 16 );

    memcpy( dateRtc, &dateToStore, sizeof(dateToStore) );

    }
  3. My initialisation task load the the at the startup. I don't touch the code generated automatically by Cube.

    void RTC_Initialize( void )

    {

    RTC_DateTypeDef dateRtc;

    /* Restore the date form backup registers */

    RTC_LoadDateFromBkpReg( &dateRtc );

    RTC_SetDate( &dateRtc, RTC_FORMAT_BCD );

    /* Get the date in order to allow HAL to check if a day is elapsed

    and update the date if it is necessaty */

    RTC_GetDate( &dateRtc, RTC_FORMAT_BCD );

    }

I tested now this implementation setting the time ad 23:59:00 and when power up the board the date in correct 2017/10/28.

Thanks to everybody for the help!