Skip to main content
Lyu.1
Senior
July 10, 2025
Solved

‘HAL_RTC_GetTime’ was not updating the time.

  • July 10, 2025
  • 2 replies
  • 537 views

Hi Master:

Platform: STM32H743
HAL library firmware: STM32Cube FW_H7 V1.12.1
Question:

As shown in the following code, it takes a long time to update 'sTime', perhaps a few minutes. 

 

RTC_TimeTypeDef sTime = { 0 };
RTC_DateTypeDef sDate = { 0 };

ASSERT(HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN) == HAL_OK);
ASSERT(HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK);

 

But when I switch the order of execution, it is updated every second

RTC_TimeTypeDef sTime = { 0 };
RTC_DateTypeDef sDate = { 0 };

ASSERT2(HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK);
ASSERT2(HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN) == HAL_OK);

What is the reason for this? Thank you so much

Best answer by Saket_Om

Hello @Lyu.1 

To ensure consistent and correct behavior, always follow the recommended order of reading the RTC registers:

  1. Read the time first using HAL_RTC_GetTime().
  2. Then read the date using HAL_RTC_GetDate()

Higher order means less frequently updated. Lowest order is the SSR, highest order is DR. The read must be done from lower order to higher order to have consistent result, because low order register read freezes higher order registers in the shadow zone. That's why your code produces the weird inconsistent updates.

Please refer to the example below: 

STM32CubeH7/Projects/NUCLEO-H743ZI/Examples/RTC/RTC_TimeStamp at master · STMicroelectronics/STM32CubeH7 · GitHub

2 replies

Saket_OmBest answer
Technical Moderator
July 10, 2025

Hello @Lyu.1 

To ensure consistent and correct behavior, always follow the recommended order of reading the RTC registers:

  1. Read the time first using HAL_RTC_GetTime().
  2. Then read the date using HAL_RTC_GetDate()

Higher order means less frequently updated. Lowest order is the SSR, highest order is DR. The read must be done from lower order to higher order to have consistent result, because low order register read freezes higher order registers in the shadow zone. That's why your code produces the weird inconsistent updates.

Please refer to the example below: 

STM32CubeH7/Projects/NUCLEO-H743ZI/Examples/RTC/RTC_TimeStamp at master · STMicroelectronics/STM32CubeH7 · GitHub

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question.Saket_Om"
Lyu.1
Lyu.1Author
Senior
July 11, 2025

Ok, thank you again

Associate
July 10, 2025

Hi,

The RTC time and date registers are locked together.

Always call HAL_RTC_GetTime() before HAL_RTC_GetDate() to get up-to-date values.

HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); // Get time first
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); // Then get date

Lyu.1
Lyu.1Author
Senior
July 11, 2025

OK, thank you again