cancel
Showing results for 
Search instead for 
Did you mean: 

Incorrect RTC operation in STM32L073CZ

AManu.1
Associate II

Good afternoon.

I have developed a device for collecting data from sensers and storing them in external memory.

During testing, I revealed a failure in the operation of the device.

Investigation of the behavior showed that there is an error in the RTC of the microcontroller.

The RTC skips July 31st and goes on August 1st.

July 30 - utc = 1627689599

July 31 - utc = 1627689600 pass

August 1 - utc = 1627776000

If you set the time to 1627689601, then the clock works normally.

How do I fix erroneous RTC behavior?

uint32_t time = 1627689599;
const tm* out = gmtime(&time);
    
    
 LL_RTC_TimeTypeDef RTC_TimeStruct = {0};   
 RTC_TimeStruct.Hours = __LL_RTC_CONVERT_BIN2BCD(out->tm_hour);
 RTC_TimeStruct.Minutes = __LL_RTC_CONVERT_BIN2BCD(out->tm_min);
 RTC_TimeStruct.Seconds = __LL_RTC_CONVERT_BIN2BCD(out->tm_sec);
    
    
 LL_RTC_DateTypeDef RTC_DateStruct = {0};
 RTC_DateStruct.WeekDay = __LL_RTC_CONVERT_BIN2BCD(out->tm_wday);
 RTC_DateStruct.Day = __LL_RTC_CONVERT_BIN2BCD(out->tm_mday);
 RTC_DateStruct.Month = __LL_RTC_CONVERT_BIN2BCD(out->tm_mon);
 RTC_DateStruct.Year = __LL_RTC_CONVERT_BIN2BCD(out->tm_year);
    
  LL_RTC_TIME_Config(RTC, RTC_TimeStruct.TimeFormat, RTC_TimeStruct.Hours,
                             RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);
                                  
                                  
  LL_RTC_DATE_Config(RTC, RTC_DateStruct.WeekDay,
                       RTC_DateStruct.Day, 
                       RTC_DateStruct.Month,
                       RTC_DateStruct.Year);

6 REPLIES 6

Don't enable RTC clock, then read out the RTC registers content after you've executed the above code, and check/post.

JW

Perhaps set the TimeFormat properly, isn't zero binary?

To debug perhaps output data generate by the time library, and what ends up in your structures, and finally in the RTC peripheral.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

The device worked for several months. After reading, they revealed a shift of one day. The shift took place after a month of work. In the description of the problem, I gave how to reproduce it.

I read the documentation but couldn't find the ranges of values in TimeFormat. Standard library for working with time (iar). Possibly the ranges of valid values do not match.

Probably the problem is tm_mon (http://www.cplusplus.com/reference/ctime/tm/)

I don't use Cube/LL. I suspect the problem is in the value which is result of all the conversions, that's why I recommend to find out what exactly gets written into the RTC registers.

JW

There are no problems with the correct writing of the RTC registers.