Skip to content

Commit dccad04

Browse files
author
Josh
committed
Add date retention
Add date retention functionality for STM32F1xx based boards.
1 parent df63712 commit dccad04

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/rtc.c

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
*
3434
******************************************************************************
3535
*/
36-
3736
#include "rtc.h"
3837

3938
#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01090000) &&\
@@ -346,17 +345,38 @@ void RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
346345
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
347346
#endif /* STM32F1xx */
348347

348+
/* Ensure backup domain is enabled before we init the RTC so we can use the backup registers for date retention on stm32f1xx baords */
349+
enableBackupDomain();
350+
349351
HAL_RTC_Init(&RtcHandle);
350352

353+
#if defined(STM32F1xx)
354+
// Copy date data back out of the BackUp registers
355+
RTC_DateTypeDef BackupDate;
356+
RTC_TimeTypeDef DummyTime;
357+
uint32_t dateMem;
358+
dateMem = HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR3);
359+
dateMem |= HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR2) << 16;
360+
memcpy(&BackupDate, &dateMem, sizeof(uint32_t));
361+
if(IS_RTC_YEAR(BackupDate.Year) && IS_RTC_MONTH(BackupDate.Month) && IS_RTC_DATE(BackupDate.Date)) {
362+
/* Change the date retrieved from the backup registers */
363+
RtcHandle.DateToUpdate.Year = BackupDate.Year;
364+
RtcHandle.DateToUpdate.Month = BackupDate.Month;
365+
RtcHandle.DateToUpdate.Date = BackupDate.Date;
366+
367+
/* Read the time so that the date is rolled over if required */
368+
HAL_RTC_GetTime(&RtcHandle, &DummyTime, RTC_FORMAT_BIN);
369+
}
370+
#endif
371+
351372
#if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32L1xx) || defined(STM32L1_ULPH)
352373
/* Enable Direct Read of the calendar registers (not through Shadow) */
353374
HAL_RTCEx_EnableBypassShadow(&RtcHandle);
354375
#endif /* !STM32F1xx && !STM32F2xx */
355376

356377
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
357378
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
358-
/* Ensure backup domain is enabled */
359-
enableBackupDomain();
379+
360380
}
361381

362382
/**
@@ -438,6 +458,12 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
438458
RTC_TimeTypeDef RTC_TimeStruct;
439459

440460
if ((hours != NULL) && (minutes != NULL) && (seconds != NULL)) {
461+
#if defined(STM32F1xx)
462+
/* Store the date prior to checking the time, this may roll over to the next day as part of the time check,
463+
we need to the new date details in the backuip registers if it changes */
464+
uint8_t current_date = RtcHandle.DateToUpdate.Date;
465+
#endif
466+
441467
HAL_RTC_GetTime(&RtcHandle, &RTC_TimeStruct, RTC_FORMAT_BIN);
442468
*hours = RTC_TimeStruct.Hours;
443469
*minutes = RTC_TimeStruct.Minutes;
@@ -461,6 +487,13 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
461487
UNUSED(period);
462488
UNUSED(subSeconds);
463489
#endif /* !STM32F1xx */
490+
491+
#if defined(STM32F1xx)
492+
if (current_date != RtcHandle.DateToUpdate.Date) {
493+
RTC_StoreDate();
494+
}
495+
#endif
496+
464497
}
465498
}
466499

@@ -483,6 +516,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday)
483516
RTC_DateStruct.WeekDay = wday;
484517
HAL_RTC_SetDate(&RtcHandle, &RTC_DateStruct, RTC_FORMAT_BIN);
485518
setBackupRegister(RTC_BKP_INDEX, RTC_BKP_VALUE);
519+
RTC_StoreDate();
486520
}
487521
}
488522

@@ -711,6 +745,17 @@ void RTC_Alarm_IRQHandler(void)
711745
HAL_RTC_AlarmIRQHandler(&RtcHandle);
712746
}
713747

748+
#if defined(STM32F1xx)
749+
void RTC_StoreDate(void)
750+
{
751+
/* Store the date in the backup registers */
752+
uint32_t dateToStore;
753+
memcpy(&dateToStore, &RtcHandle.DateToUpdate, 4);
754+
HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR2, dateToStore >> 16);
755+
HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR3, dateToStore & 0xffff);
756+
}
757+
#endif
758+
714759
#ifdef __cplusplus
715760
}
716761
#endif

src/rtc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ void RTC_GetAlarm(uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *secon
165165
void attachAlarmCallback(voidCallbackPtr func, void *data);
166166
void detachAlarmCallback(void);
167167

168+
#if defined(STM32F1xx)
169+
void RTC_StoreDate(void);
170+
#endif
171+
168172
#ifdef __cplusplus
169173
}
170174
#endif

0 commit comments

Comments
 (0)