Cannot set time with HAL_RTC_SetTime on L412KB (Nucleo32) after init.
I seem to be unable to set the time more than once after reset. Here is a way to reproduce my problem:
- Create a new CubeMX project with the RTC enabled
- Edit the MSP init function the following way:
- ...After the time and date are set by MSP init
- ...change the hour in the sTime structure, e.g. sTime.Hours = 0x09;
- ...and then issue HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);
- ...and then issue HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BCD);
- run the code in the debugger and break in the new readTime call from step 6
- note that the value of hour has not changed
Or insert this code in your main() function:
/* USER CODE BEGIN 2 */
if (1) {
RTC_TimeTypeDef tm;
HAL_RTC_GetTime(&hrtc, &tm, RTC_FORMAT_BCD);
tm.Hours = 0x06; /* assuming you initialized to something other than 6 */
HAL_RTC_SetTime(&hrtc, &tm, RTC_FORMAT_BCD);
HAL_RTC_GetTime(&hrtc, &tm, RTC_FORMAT_BCD); /* tm.Hours does not change to 6 */
}
/* USER CODE END 2 */Reading the reference manual, the HAL already issues the CR unlock bytes. I've read some posts about writing the RTC_BKP_DR1 with 0x32f2, but that also does not help (and is not mentioned in the reference manual).
Seems like I'm missing something obvious?
Thanks.
