2018-05-11 02:16 AM
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
2018-05-11 03:01 AM
Seem to remember others complaining of HR > 24 in recent weeks, might want to scan the threads for related issues.
2018-05-11 05:46 AM
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.
2018-05-11 06:20 AM
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
2018-05-11 06:28 AM
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.
2018-05-11 06:44 AM
It's OK. Thank you both.