cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F405 RTC counting more than 24h!

Bruno Loureiro
Associate II
Posted on April 27, 2018 at 15:47

The RTC continues to count after 23:59:59, just changes the day at 39:59:59, what's the problem?

I use HAL Library.

RTC Output:

2018-04-06 weak5, 23:59:57

2018-04-06 weak5, 23:59:58

2018-04-06 weak5, 23:59:59

2018-04-06 weak5, 24:00:00

2018-04-06 weak5, 24:00:01

2018-04-06 weak5, 24:00:02

2018-04-06 weak5, 24:00:03

2018-04-06 weak5, 24:00:04
13 REPLIES 13
Bruno Loureiro
Associate II
Posted on April 28, 2018 at 14:27

Thanks to everyone, the solution is to change the location of this line in RTC Init

if (HAL_RTCEx_BKUPRead (& hrtc, RTC_BKP_DR0)! = 0x32F2) {

maybe some bug um STMCubeMX32

see function 

/* RTC init function */

static 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 = 0x0;

sTime.Minutes = 0x0;

sTime.Seconds = 0x0;

sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

sTime.StoreOperation = RTC_STOREOPERATION_RESET;

if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

sDate.WeekDay = RTC_WEEKDAY_MONDAY;

sDate.Month = RTC_MONTH_JANUARY;

sDate.Date = 0x1;

sDate.Year = 0x0;

if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);

}

}

Posted on April 28, 2018 at 18:54

'

maybe some bug um STMCubeMX32'

understatement of the century,

that's the irony of a buggy library: it hurts the most people it is designed to help. If you were to simply write out your own code, you would have figured this out in a couple hours, not days.

Posted on April 29, 2018 at 10:39

the solution is to change the location of this line in RTC Init

if (HAL_RTCEx_BKUPRead (& hrtc, RTC_BKP_DR0)! = 0x32F2) {

Certainly not.

As long as the clock is running, i.e. battery is connected to VBAT, the RTC retains its settings and there's no need to reinitialize it upon reset. Contrary, by reinitialization you may disturb the clock (see numerous

https://community.st.com/0D50X00009XkgBWSAZ

in this very forum on subseconds being reset, thus losing up to 1 second upon each reset).

I am not going to comment on the quality of examples nor Cube. Read the RTC chapter in RM and write your own code.

JW

Bruno Loureiro
Associate II
Posted on April 29, 2018 at 10:57

Hi, Thank you for the advice.