cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Not Running

VijayRakeshM
Associate III

Hi, I had configured the clock to LSI RC 37KHz using cubemx with AsynchPrediv is 124 and synchPredivdivider is 295 after the RTC initialisation i'm trying to read the time using HAL_RTC_GetTime() but i'm getting the values what i initialised but doesn't increment. can anyone suggest how to solve above issue.

Thanks & Regards,

Vijay Rakesh

15 REPLIES 15

Read the RTC chapter in RM.

You can also set the BYPSHAD bit.

JW

eBirdman
Senior

None of the above answers solved the same problem for me. Celilertop pointed correctly to the nature of the issue - the necessity to have the date reading when only time reading is needed. But his provided sequence must be reversed. Time reading must be prior to the date reading, not after, then the time reading returns correct updated values. This has a reason: using HAL apis read the "shadow" registers which are synchronized with "true" counting calendar registers using a flag RSF bit in RTC_ISR register.

Details of this are in Ref. Manual Ch.46.3.9 :

The RSF bit is set in RTC_ISR register each time the calendar registers are copied into the
RTC_SSR, RTC_TR and RTC_DR shadow registers. The copy is performed every
RTCCLK cycles. 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.

So the working calls to get time only (and both time and date) are:

   HAL_RTC_GetTime(&hrtc, &now_t, RTC_FORMAT_BIN);

   HAL_RTC_GetDate(&hrtc, &date_t, RTC_FORMAT_BIN);   // required for Time register synchronization with calendar registers

BTW:  Setting the BYPSHAD control bit to 1 is not recommended because you'll likely end up with out-of-sync values from the direct subsequent readings of the calendar registers (RTC counter keeps counting). Reading from the shadow registers (when BYPSHAD = 0) using HAL apis ensures the proper sync.

Note, that in older STM32 families the shadow registers "locking" mechanism is flawed, see errata.

JW

eBirdman
Senior

Indeed this bug is still there... I found the same exact description in the current errata for my MCU STM32H743 errata doc (see RTC errata section 2.11.1) : ES0392STM32H742xIG and STM32H743xIG device limitationsV8.0

I'm still using shadow registers with BYPSHAD = 0 without double checking of SSR because my timestamp usage is not critical - just to provide a timestamp to the logfile entry. So far I never saw erroneous timestamp over thousands of lines of log entries... probably this situation is rare... or may be fixed?

Not only hitting the bug may be rare (depending on how exactly and how often do you read the timestamp), but also, would you notice it, by "humanly" reading the logfile ?

JW

MarkRE
Associate

I appreciate that this is somewhat of a late reply but it might be worthwhile noting the reason that the RTC appears not to increment after reading the time and there is a simple reason for this.  The solution is hidden in the HAL RTC documentation within the source code. 

To put it simply "You must call HAL_RTC_GetDate() after HAL_RTC_GetTime()" because GetTime essentially locks the RTC registers in order to maintain consistency between the date and time values.

Here are the first few lines of the definition of HAL_RTC_GetTime.  I have marked the relevant note in bold.

I hope this is useful.

Kind regards

Mark

 

/**

* @brief Gets RTC current time.

* @PAram hrtc pointer to a RTC_HandleTypeDef structure that contains

* the configuration information for RTC.

* @PAram sTime Pointer to Time structure

* @PAram Format Specifies the format of the entered parameters.

* This parameter can be one of the following values:

* @arg RTC_FORMAT_BIN: Binary data format

* @arg RTC_FORMAT_BCD: BCD data format

* @note You can use SubSeconds and SecondFraction (sTime structure fields

* returned) to convert SubSeconds value in second fraction ratio with

* time unit following generic formula:

* Second fraction ratio * time_unit =

* [(SecondFraction - SubSeconds) / (SecondFraction + 1)] * time_unit

* This conversion can be performed only if no shift operation is pending

* (ie. SHFP=0) when PREDIV_S >= SS

* @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the

* values in the higher-order calendar shadow registers to ensure

* consistency between the time and date values.

* Reading RTC current time locks the values in calendar shadow registers

* until current date is read to ensure consistency between the time and

* date values.

* @retval HAL status

*/

HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)