Skip to main content
zhuij.1
Associate II
January 29, 2023
Question

STM32F407 RTC sporadically showing future time

  • January 29, 2023
  • 5 replies
  • 1498 views

Most of the future time occurs around 12:00 in the morning, here are the instances captured on the device:

1. The actual time is: 2023-01-11 11:59:55, the STM32F407 RTC time is: 2023-01-12 12:00:00

2. The actual time is: 2022-12-31 11:59:58, the STM32F407 RTC time is: 2023-01-01 12:00:00

Under normal circumstances, the actual time and the STM32F407 RTC time do not exceed plus or minus 10s

This topic has been closed for replies.

5 replies

waclawek.jan
Super User
January 29, 2023

How do you read it time in your program?

Read it and check/post RTC registers' content.

JW

zhuij.1
zhuij.1Author
Associate II
January 31, 2023

The program will read the RTC value many times a second, or read the RTC value only once in a few seconds, and then upload the RTC time and other information to the server in real time through the network, and the server will regularly compare the local time with the uploaded RTC time. future time

waclawek.jan
Super User
January 31, 2023

Read out and check/post content of RTC registers.

Maybe consequence of using uninitialized structs when setting time/date, e.g. as here.

JW

zhuij.1
zhuij.1Author
Associate II
February 2, 2023

We are using a 24-hour system, and a small number of devices are at 11:59:59 am today, and the next moment becomes 12:00:00 tomorrow, and it will not return to normal until the next time synchronization

set the rtc time as follow:

/* Get the RTC current Time */
			HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
			/* Get the RTC current Date */
			HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
			
			stimestructureget.Hours = tm_p->tm_hour;
			stimestructureget.Minutes = tm_p->tm_min;
			stimestructureget.Seconds = tm_p->tm_sec;
			
			sdatestructureget.Year = tm_p->tm_year - 100;//-1900
			sdatestructureget.Month = tm_p->tm_mon + 1;
			sdatestructureget.Date = tm_p->tm_mday ;
			sdatestructureget.WeekDay = tm_p->tm_wday + 1;
			
			PRINTF("get time:%u-%u-%u %u:%u:%u\n",tm_new.tm_year,tm_new.tm_mon,tm_new.tm_mday,tm_new.tm_hour,tm_new.tm_min,tm_new.tm_sec);
			
			/* Set the RTC current Time */
			HAL_RTC_SetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
			/* Set the RTC current Date */
			HAL_RTC_SetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);

​The RTC initialization configuration is as follows:

void MX_RTC_Init(void)
{
 RTC_TimeTypeDef sTime;
 RTC_DateTypeDef sDate;
 
 /**Initialize RTC Only 
 */
 hrtc.Instance = RTC;
 hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
 hrtc.Init.AsynchPrediv = 127;
 hrtc.Init.SynchPrediv = 255;
 hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
 hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
 hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
 if (HAL_RTC_Init(&hrtc) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 
 /**Initialize RTC and set the Time and Date 
 */
 if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
 sTime.Hours = 0x0;
 sTime.Minutes = 0x0;
 sTime.Seconds = 0x0;
 sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
 sTime.StoreOperation = RTC_STOREOPERATION_RESET;
 if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 
 sDate.WeekDay = RTC_WEEKDAY_MONDAY;
 sDate.Month = RTC_MONTH_JANUARY;
 sDate.Date = 0x1;
 sDate.Year = 0x0;
 
 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 
 HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
 }
 
}

waclawek.jan
Super User
February 2, 2023

Read out and check/post content of RTC registers. Do it. You'll be surprised.

In MX_RTC_Init() you don't set the sTime.TimeFormat before calling HAL_RTC_SetTime(). Set it, or, maybe better, initialize the structs when defining them:

RTC_TimeTypeDef sTime = {0};

RTC_DateTypeDef sDate = {0};

JW