2017-02-28 03:35 AM
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 #vbat2017-02-28 03:55 AM
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
2017-03-01 06:20 AM
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
..
.
2017-05-29 07:23 AM
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
2017-06-06 12:44 PM
Any solution?
2017-07-26 10:10 AM
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
2017-07-26 11:10 AM
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 countingHAL_PWR_EnableBkUpAccess();// enable the clocks__HAL_RCC_RTC_ENABLE();
__HAL_RCC_BKP_CLK_ENABLE();
make a try.
2017-07-26 11:21 AM
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.
2017-07-27 09:16 AM
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
2017-07-27 09:29 AM
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