Question
RTC backup date and time
Hi,
I created this function in order to store the date and time. It works !
But the problem is when I load date and time, the time is the last before turning off the system.
void RTC_Init(void)
{
/* Restore the date form backup registers */
RTC_LoadDateTimeFromBkpReg();
HAL_RTC_SetDate(&hrtc, ¤tDate, RTC_FORMAT_BIN);
HAL_RTC_SetTime(&hrtc, ¤tTime, RTC_FORMAT_BIN);
/* Get the date in order to allow HAL to check if a day is elapsed and update the date if it is necessary */
HAL_RTC_GetTime(&hrtc, ¤tTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, ¤tDate, RTC_FORMAT_BIN);
}
void RTC_StoreDateTimeIntoBkpReg(void)
{
uint32_t dateToStore;
uint32_t timeToStore;
memcpy(&dateToStore, ¤tDate, sizeof(dateToStore));
memcpy(&timeToStore, ¤tTime, sizeof(timeToStore));
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, (dateToStore >> 16));
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, (dateToStore & 0xFFFF));
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, (timeToStore >> 16));
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR3, (timeToStore & 0xFFFF));
}
void RTC_LoadDateTimeFromBkpReg(void)
{
uint32_t dateToStore;
uint32_t timeToStore;
dateToStore = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);
dateToStore |= (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) << 16);
timeToStore = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
timeToStore |= (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2) << 16);
memcpy(¤tDate, &dateToStore, sizeof(dateToStore));
memcpy(¤tTime, &timeToStore, sizeof(timeToStore));
}
void Update_DateTime(void)
{
HAL_RTC_GetTime(&hrtc, ¤tTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, ¤tDate, RTC_FORMAT_BIN);
RTC_StoreDateTimeIntoBkpReg();
}How can I configure this to update date and time with VBat ?
Best regards