cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 RTC gets bad format values

ROBERTO ARRIBAS
Associate II
Posted on May 11, 2018 at 11:16

Hi, I'm working with the RTC (LSE source) and I'm getting a bad format value, not a wrong value.

I call HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); and HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); to get the actual date and time, and when I represent it, it shows: 'Thu 10/05/2018 36:08:13.219', when it really has to be 'Fri 11/05/2018 12:08:13.219'. The date and time would be fine if it had subtracted 24 hours in time and the day had been increased.

I've debugged HAL_RTC_GetTime and when it reads the TR register, it gets the following:

/* Get the TR register */

tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);

// tmpreg = 3541011 = 0x360813;

Any ideas of what may be happening?

Thanks in advance

5 REPLIES 5
Posted on May 11, 2018 at 12:01

Seem to remember others complaining of HR > 24 in recent weeks, might want to scan the threads for related issues.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 11, 2018 at 12:46

I think I've found the solution reading other threads

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

First, when setting date and time, both structures must be completely initialized. I've noticed that not setting e.g. Subseconds in time structure, it had a non-zerolarge value. So, to set all parameters is required.

And second, before setting time and date (in that order), HAL_RTC_Init() must be called. STM32CubeMX writes in MX_RTC_Init() the line

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

before executing the HAL_RTC_Init(). That way, if it is not the first boot time, HAL_RTC_Init() is not executed.

Thanks for your support.

Posted on May 11, 2018 at 13:20

First, when setting date and time, both structures must be completely initialized.

This applies to EVERY structure used in all ST 'libraries' (SPL and Cube/HAL and Cube/LL). Often, there are functions which are intended to do this.

JW

Posted on May 11, 2018 at 13:28

Some judicial use of initializers for local/auto variables/structures would save a lot of grief. C pretty much guarantees junk from the stack by default, and ST's use of bit flags and masks allows for side-effects on otherwise unexpected registers.

/* RTC init function */

static void MX_RTC_Init(void)

{

   RTC_TimeTypeDef sTime = {0};

   RTC_DateTypeDef sDate = {0};

..

In this case I'd suspect some miscommunication about values being BIN vs BCD, and the system doubling down on the conversion.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 11, 2018 at 13:44

It's OK. Thank you both.