cancel
Showing results for 
Search instead for 
Did you mean: 

‘HAL_RTC_GetTime’ was not updating the time.

Lyu.1
Senior

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

1 ACCEPTED SOLUTION

Accepted Solutions
Saket_Om
ST Employee

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

View solution in original post

4 REPLIES 4
Saket_Om
ST Employee

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
Shealtiel
Associate

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

Ok, thank you again

OK, thank you again