cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_RTC_GetTime return 1 instead of 13

RLand.2
Associate II

Hello ST community!

We're using STM32F401RDT6.

We use the RTC to maintain scheduled events that occur in the time we set, but not with the RTC's Alarm mechanism, rather our custom-made mechanism based on the current epoch time which is being updated every ~4 seconds.

Between each epoch sampling, the MCU goes to sleep.

Here is the rtcEpochGet function we have made:

uint32_t rtcEpochGet()
{
	struct tm time_tm;
	time_t t_of_day;
 
	uint32_t tmp;
	RTC_TimeTypeDef MyTimeStruct;
	RTC_DateTypeDef MyDateStruct;
 
	HAL_RTC_GetTime(&hrtc, &MyTimeStruct, RTC_FORMAT_BIN);
	HAL_RTC_GetDate(&hrtc, &MyDateStruct, RTC_FORMAT_BIN);
 
	// Read the last epoch time stored in RTC back-up register
	tmp = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR19);
	// Check if the last epoch match to 12:59:56 PM (or later)
	// If yes - next hour should be 13, not 1 (14 not 2, etc)
//	if (tmp % 86400 >= 46796 && hrtc.Init.HourFormat == RTC_HOURFORMAT_24 && MyTimeStruct.Hours <= 12)
//		MyTimeStruct.Hours += 12;
 
	// Source: https://www.epochconverter.com/programming/c
	time_tm.tm_year	 = MyDateStruct.Year+2000-1900; // Year-1900
	time_tm.tm_mon	 = MyDateStruct.Month-1; // Month, where 0 = jan
	time_tm.tm_mday  = MyDateStruct.Date; // Day of the month
	time_tm.tm_hour  = MyTimeStruct.Hours;
	time_tm.tm_min	 = MyTimeStruct.Minutes;
	time_tm.tm_sec	 = MyTimeStruct.Seconds;
	time_tm.tm_isdst = -1;        // Is DST on? 1 = yes, 0 = no, -1 = unknown
	t_of_day = mktime(&time_tm);
 
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR19, t_of_day);
 
	return t_of_day;
}

In a background-running task we follow the time by using: rtcEpochGet()%(24*3600) which return the current time, in seconds, since midnight.

The issue is that when we are passing 12:59:59 (AM) to 13:00:00 (PM), the HAL function HAL_RTC_GetTime returns 1 instead of 13 to MyTimeStruct.Hours.

As you can see, we are working around this issue by checking if the last epoch time was 12:59:56 or later, and if so, we are adding 12 to the "hours" member. But this is only dealing with the symptom, instead of solving the problem.

RTC driver is set to 24_HOURS_FORMAT, calendar has activated no alarms.

I hope someone here could help us solve this issue.

Thanks,

Raz.

1 ACCEPTED SOLUTION

Accepted Solutions
RLand.2
Associate II

I found the solution for my issue, according to the note in HAL_RTC_TimeGet header comment:

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.

So every place I had TimeGet/DateGet I made sure both functions will be called (time and then date) and it solved the issue - now 12:59:59 + 1 turns to 13:00:00 and to 01:00:00.

View solution in original post

2 REPLIES 2
RLand.2
Associate II

I found the solution for my issue, according to the note in HAL_RTC_TimeGet header comment:

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.

So every place I had TimeGet/DateGet I made sure both functions will be called (time and then date) and it solved the issue - now 12:59:59 + 1 turns to 13:00:00 and to 01:00:00.