cancel
Showing results for 
Search instead for 
Did you mean: 

HAL RTC SetDate Bug or some another way to do??

Joshin Joy
Associate
Posted on February 23, 2017 at 10:49

I am using STM32F103 in my new application. I have used STM32Cube to generate my code including Internal RTC. My

problem is that the time is ok saved when reseted but date is always reset. when i inspected the code i found that the HAL_RTC_SetDate() function is not setting the RTC_CNTH and RTC_CNTL registers appropriately. Is it a bug or is there any other way of doing this. when we add day shouldn't  the RTC_CNTH and RTC_CNTL added with (24hrs x 60 Mins x 60 Secs) this is not happening.

#cube-mx #hal-library
5 REPLIES 5
lhofstaetter9
Associate II
Posted on June 02, 2017 at 09:30

For me its the same. I use RTC_SetDate and RTC_SetTime to set the date and the time. After that I use RTC_GetDate and RTC_GetTime to read the date and the time. The time works, but the date is still the same. If I do then a system reset by pressing the reset button, the date can be read properly. So writing date and time seems to work. But reading does not work.

Posted on June 02, 2017 at 13:33

I don't know if this is related or not:

I had an issue a few years ago with the STM32F407 (using SPL). After changing the date, RTC_GetDate was not returning the correct date for sometimes upwards of 100 attempts (my test was to read and display the time and date every second). I never resolved the cause, but I came up with a rework; if I first called RTC_GetTime BEFORE calling RTC_GetDate, all was well. RTC_WaitForSynchro did not correct the problem.

Posted on June 06, 2017 at 14:48

I had the same problem (on STM32L152)... and I found this subject last week...

I tried to invert the call to have 

RTC_GetDate() AFTER 

RTC_GetTime()... and I got good results. As a feeling, I guess it solved the problem......... h

oping it is not a coincidence!

Posted on June 06, 2017 at 15:12

This is a feature. Read the fine manual, RTC chapter, Reading the calendar subchapter:

 To ensure consistency between the 3 values, reading either RTC_SSR or

RTC_TR locks the values in the higher-order calendar shadow registers until RTC_DR is

read.

I believe this is mentioned even in the Cube documentation (I am not going to provide pointers to that).

JW

OleksiiUA
Associate

I myself spent the whole day to figure it out. Implemented the clock using third-party functions. And only in the evening while analyzing code HAL_RTC_GetTime() did it come to me. The function counts only one day days_elapsed in a circle and counts days. ->> 

/* Update date */

RTC_DateUpdate(hrtc, days_elapsed);

They need to be stored in another memory, for example, in backup registers.