Skip to main content
Visitor II
February 28, 2017
Question

STM32F103 RTC + VBAT date resets after power off

  • February 28, 2017
  • 6 replies
  • 5953 views
Posted on February 28, 2017 at 12:35

Im using STM32F103C8T6 and have issues with RTC and VBAT. Project generated in CubeMX 4.19.0 for Keil, I commented date and time setup in MX_RTC_Init() and time restores fine after reset/power off, but date becomes 1-1-2000, how can I restore date?

#date #rtc #vbat
    This topic has been closed for replies.

    6 replies

    Technical Moderator
    February 28, 2017
    Posted on February 28, 2017 at 12:55

    Hello,

    Date is saved in SRAM. Then, when MCU is in STOP or STANDBY mode, date will be lost. 

    So, you should save date before entering in low power mode.

    You can refer to the

     example provided with STM32CubeF1 firmware package based on backup registers.

    Hope this helps you.

    Imen

    Visitor II
    May 29, 2017
    Posted on May 29, 2017 at 14:23

    Hello,

    You helped me with your tip. I made it work.

    But when I turn on the device the other day, the date will be wrong. That's right? Does this device have no date tracking?

    Thank you

    Visitor II
    March 1, 2017
    Posted on March 01, 2017 at 15:20

    Hello, Imen .

    I also have the same problem.

    What is the matter?

    I tested the following environment.

    Board : STM3210E_EVAL

    Test source : STM32Cube_FW_F1_V1.4.0\Projects\STM3210E_EVAL\Examples\RTC\RTC_Calendar

    * terminal log

    Power on reset occured

    External   reset occured

    23:59:30 2- 28-2017

    23:59:31 2- 28-2017

    23:59:32 2- 28-2017

    External   reset occured

    23:59:33 1-   1-2000

    23:59:34 1-   1-2000

    23:59:35 1-   1-2000

    ..

    .

    Explorer
    March 10, 2025

    any solution  plz tell me  same problem   02-01-2000
    00:00:00
    02-01-2000
    00:00:01
    02-01-2000
    00:00:02
    02-01-2000
    00:00:03
    02-01-2000
    00:00:04
    02-01-2000
    00:00:05
    System Initialized
    01-01-2000
    00:00:06
    System Initialized
    01-01-2000
    00:00:06
    System Initialized
    01-01-2000
    00:00:06
    01-01-2000
    00:00:06
    01-01-2000
    00:00:07
    Serial port COM9 closed

     

    Visitor II
    June 6, 2017
    Posted on June 06, 2017 at 21:44

    Any solution?

    Visitor II
    July 26, 2017
    Posted on July 26, 2017 at 17:10

    Hello,

    unfortunantetly there is no solution for the lost of date because it is a bug in the STM32F1 series.

    There is little comment in one RTC examples of the cube-library:

    @note On STM32F1 families, as there are restrictions on the RTC version V1, date

          will be lost in all the cases.

    Best regards

    Andreas

    Graduate II
    July 26, 2017
    Posted on July 26, 2017 at 18:21

    I don't recall the F1 having such a bug, it had a 32-bit RTC that could represent whatever timeline epoch you wanted (UNIX, WinCE), if it counts seconds, it can retain time + date.

    Visitor II
    July 26, 2017
    Posted on July 26, 2017 at 20:10

    Hello !!

    I use this family of MCUs some years..

    I never faced any kind of problem with rtc

    It keeps the time and calendal like the other 'modern and complexer' RTCs

    The behaviour you described is from software provided to 'support' the rtc

    Here are some fundamedal functions to work with . in C

    keep in mind to full fill the structures with valid values before write the clock

    write all theese code to a module xxx.c

    #include 'stm32f1xx_hal.h'

    #include <time.h>

    //##############################################################################

    extern void SystemClock_Config(void);

    extern RTC_HandleTypeDef hrtc;

    extern HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter);

    //##############################################################################

    void RTCSetAlarm(struct tm* cl)

    {    

        uint32_t TimeCounter= mktime(cl);    

        //TimeCounter +=100;

        while((hrtc.Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET);

        /* Disable the write protection for RTC registers */

        SET_BIT(hrtc.Instance->CRL, RTC_CRL_CNF);

      /* Set RTC COUNTER MSB word */

        WRITE_REG(hrtc.Instance->ALRH, (TimeCounter >> 16));

        /* Set RTC COUNTER LSB word */

        WRITE_REG(hrtc.Instance->ALRL, (TimeCounter & RTC_ALRL_RTC_ALR));  

      /* Enable the write protection for RTC registers */

        CLEAR_BIT(hrtc.Instance->CRL, RTC_CRL_CNF);

      /* Wait till RTC is in INIT state */

      while((hrtc.Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET);

        

        /* Clear flag alarm A */

        hrtc.Instance->CRL &= ~(RTC_FLAG_ALRAF);

        /* Configure the Alarm interrupt */

        SET_BIT(hrtc.Instance->CRH, (RTC_IT_ALRA));

        /* RTC Alarm Interrupt Configuration: EXTI configuration */

        __HAL_RTC_ALARM_EXTI_ENABLE_IT();

        __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();

        

    }

    //##############################################################################

    void RTCGetClock(struct tm** cl)

    {

        uint16_t high1 = 0, high2 = 0, low = 0;

      uint32_t timecounter = 0;   

      high1 = READ_REG(hrtc.Instance->CNTH & RTC_CNTH_RTC_CNT);

        again2:

      low   = READ_REG(hrtc.Instance->CNTL & RTC_CNTL_RTC_CNT);

      high2 = READ_REG(hrtc.Instance->CNTH & RTC_CNTH_RTC_CNT);

      if (high1 != high2)

      {

            high1=high2;//read again CNTL register then return the counter value

            goto again2;

      }

        timecounter = (((uint32_t) high1 << 16 ) | low);    

        *cl= localtime(&timecounter);

    }

    //##############################################################################

    void RTCSetClock(struct tm* cl)

    {    

        uint32_t TimeCounter= mktime(cl);    

        while((hrtc.Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET);    

        hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;

      hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;

      HAL_RTC_Init(&hrtc);    

        __HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);    

        /* Set RTC COUNTER MSB word */

        WRITE_REG(hrtc.Instance->CNTH, (TimeCounter >> 16));

        /* Set RTC COUNTER LSB word */

        WRITE_REG(hrtc.Instance->CNTL, (TimeCounter & RTC_CNTL_RTC_CNT));  

      /* Disable the write protection for RTC registers */

      __HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);

      /* Wait till RTC is in INIT state and if Time out is reached exit */

      while((hrtc.Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET);

    }

    //##############################################################################

    At HAL initialiation before use the above  module, must do at least this:(at main function)

    RTC_HandleTypeDef hrtc;//in global scope

    The LSE must be running allready and the rtc must have as source LSE(vbat is connected)

    hrtc.Instance = RTC;// it exists no need to initialise again

    hrtc.State = HAL_RTC_STATE_READY;// and is counting

    HAL_PWR_EnableBkUpAccess();// enable the clocks

    __HAL_RCC_RTC_ENABLE();

    __HAL_RCC_BKP_CLK_ENABLE();

    make a try.

    Visitor II
    July 27, 2017
    Posted on July 27, 2017 at 16:16

    Hello,

    I've tested it today with your code and it is the same problem as before.

    Only the time is correct. All variables of the date after reboot are zero.

    Tested with STM32F103 (100pins).

    I've tested my code with a STM32F3 (64pins) it works correct.

    So I will switch to the STMF303 for my project.

    Best regards

    Visitor II
    July 27, 2017
    Posted on July 27, 2017 at 16:29

    which variables?

    there are no variables saved. . Only the RTC counters HI and LO .

    Theese counters are in Vbackup domain and they never stop .(keep always the clock)

    My previous post suggest to use other ways to use the full functionality of RTC not the official way.

    this means that you must  not use any function relative to RTC from HAL .

    Just Enable LSE and RTC only once

    Visitor II
    July 27, 2017
    Posted on July 27, 2017 at 18:44

    Hello,

    sorry this was a missunderstanding.

    I don't want to built my own calender, I must use the HAL functions.

    With the STM32F303 it works fine with HAL.

    In my case this type is cheaper than the STM32F103, so the decision was easy.

    But whats about this comment in the examples at the cube library?

    @note On STM32F1 families, as there are restrictions on the RTC version V1, date

          will be lost in all the cases.

    Best regards

    Andreas

    Visitor II
    July 27, 2017
    Posted on July 27, 2017 at 19:08

     ,

     ,

    Hi!

    this comment is true.

    Inside the stm32f1xx_hal_rtc.c Line 55 is writen ' ♯ ♯ ♯ ♯ ♯ WARNING: Drivers Restrictions , ♯ ♯ ♯ ♯ ♯ '

    It concerns the 'software aproach' operation of rtc, not the hardware.

    There is no any hardware issue in errata sheet about this

    Best Regards!

    vf

    Visitor II
    October 26, 2017
    Posted on October 26, 2017 at 23:13

    Maybe too late for you, but may be useful for googlers

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

    rokkoAuthor
    Visitor II
    November 24, 2017
    Posted on November 24, 2017 at 22:18

    The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6tD&d=%2Fa%2F0X0000000bxP%2FPGCh_8QlpMkCk5KMRTnQhfp_b9pgbFuLWBSGOfzBltE&asPdf=false