cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX RTC, STM32F407, weird values by HAL_RTC_GetTime

George Eliozov
Associate II
Posted on July 18, 2017 at 16:34

Hello,

I'm new to CubeMX and HAL drivers. Recently I build a test project with RTC module, but now I'm experiencing quite weird behavior of the HAL_RTC_GetTime/HAL_RTC_GetDate.

In my case HAL_RTC_GetTime function can return Hours values which is greater then 24 ....  and Date is also not incrementing ...

I have standard initialization code from STM32CubeMX (updated today, no change).

Thanks

#rtc-hal #stm32f407
8 REPLIES 8
Posted on July 18, 2017 at 17:12

You see 24 hours after having rolled over from 23 hours?

There is no magic in the RTC, if you set it to garbage e.g. 24 hours it will run up to 32 and then rolls over to 0.

JW

Posted on July 18, 2017 at 18:31

No I did set it to 23:59:00, and after a minute the time showed me 24:00 ..........

Posted on July 18, 2017 at 18:33

You say 32 haha , I leave it running and now it showing 2017-07-17 35:02:30 ... what am I doing wrong ?!

Posted on July 18, 2017 at 21:58

Ah, it's BCD, so up to 39 hours.

what am I doing wrong?

My guess is that you have RTC_CR.FMT set to AM/PM.

JW

Posted on July 19, 2017 at 07:46

weird, here is the code samples from the initialization and the way how I'm getting date and time:

/* RTC init function */
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 = 0;
 sTime.Minutes = 0;
 sTime.Seconds = 0;
 sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
 sTime.StoreOperation = RTC_STOREOPERATION_SET;
 if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 sDate.WeekDay = RTC_WEEKDAY_TUESDAY;
 sDate.Month = RTC_MONTH_JULY;
 sDate.Date = 9;
 sDate.Year = 17;
 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

And here is how i getting date and time:

RTC_TimeTypeDef time;
RTC_DateTypeDef date;
if(HAL_RTC_GetTime( &hrtc, &time, RTC_FORMAT_BIN ) != HAL_OK){
dev_sprintf(io->stream,'
Error getting time
');
return 0;
}
if(HAL_RTC_GetDate( &hrtc, &date, RTC_FORMAT_BIN ) !=HAL_OK){
dev_sprintf(io->stream,'
Error getting time
');
return 0;
}
dev_sprintf(io->stream,'Current date: %04d-%02d-%02d %02d:%02d:%02d
',
date.Year+CFG_DATE_MILLENNIUM,
date.Month +1, // 0..11
date.Date +1, // 0..30
time.Hours,
time.Minutes,
time.Seconds);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

If this is a BCD format, then tell me, how can I get a Binary date from HAL driver ?

Thanks

Posted on July 19, 2017 at 10:36

hello!

I faced recently this kind of problem and i solved it. (it was because time structure was not corectly initialised)

First of all check (in case you have ) the voltage of VBAT.

Make a backup domain reset.

Set the Clock and Date (it is important to set them both)

dont forget to correctly and fully initialize the time and date structures.

an example is :

RTC_DateTypeDef dt;

RTC_TimeTypeDef tt;

    

 tt.Hours=xxx;

 tt.Minutes=xxx;

 tt.Seconds=xxx;

 tt.SecondFraction=0;

 tt.SubSeconds=0;

 tt.DayLightSaving=xxx;

 tt.TimeFormat=RTC_HOURFORMAT12_AM;//

 tt.StoreOperation=RTC_STOREOPERATION_RESET;

 dt.Date=xxx;

 dt.Month=xxx;

 dt.Year=xxx;

 dt.WeekDay=xxx;   

hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

hrtc.Init.AsynchPrediv = 127;// I use 32768 crystal

hrtc.Init.SynchPrediv = 255;

hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

HAL_RTC_Init(&hrtc);

    

HAL_RTC_SetTime(&hrtc, &tt, RTC_FORMAT_BIN);// or bcd ofcourse

HAL_RTC_SetDate(&hrtc,&dt, RTC_FORMAT_BIN);// or bcd ofcourse

Check the input values xxx  to be inside the limits.

finaly  when you decide to read clock  read both time and date.

Posted on July 19, 2017 at 09:58

Put a new limit for hours

Current date: 2017-07-17 39:18:25

Posted on July 19, 2017 at 19:24

Thank you so much, that does solved not all but part of the issues.

Now time structure looks like working OK, but date doing same weird increments. But now I know where to dig in ...

Thanks !