cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 RTC + VBAT date resets after power off

rokko
Associate II
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
13 REPLIES 13
Imen.D
ST Employee
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

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
NamSoo Kim
Associate
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

..

.

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

William Trevine
Associate
Posted on June 06, 2017 at 21:44

Any solution?

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

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.

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
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

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