33
33
*
34
34
******************************************************************************
35
35
*/
36
-
37
36
#include "rtc.h"
38
37
39
38
#if defined(STM32_CORE_VERSION ) && (STM32_CORE_VERSION > 0x01090000 ) && \
@@ -346,17 +345,38 @@ void RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
346
345
RtcHandle .Init .OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN ;
347
346
#endif /* STM32F1xx */
348
347
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
+
349
351
HAL_RTC_Init (& RtcHandle );
350
352
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
+
351
372
#if !defined(STM32F1xx ) && !defined(STM32F2xx ) && !defined(STM32L1xx ) || defined(STM32L1_ULPH )
352
373
/* Enable Direct Read of the calendar registers (not through Shadow) */
353
374
HAL_RTCEx_EnableBypassShadow (& RtcHandle );
354
375
#endif /* !STM32F1xx && !STM32F2xx */
355
376
356
377
HAL_NVIC_SetPriority (RTC_Alarm_IRQn , RTC_IRQ_PRIO , RTC_IRQ_SUBPRIO );
357
378
HAL_NVIC_EnableIRQ (RTC_Alarm_IRQn );
358
- /* Ensure backup domain is enabled */
359
- enableBackupDomain ();
379
+
360
380
}
361
381
362
382
/**
@@ -438,6 +458,12 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
438
458
RTC_TimeTypeDef RTC_TimeStruct ;
439
459
440
460
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
+
441
467
HAL_RTC_GetTime (& RtcHandle , & RTC_TimeStruct , RTC_FORMAT_BIN );
442
468
* hours = RTC_TimeStruct .Hours ;
443
469
* minutes = RTC_TimeStruct .Minutes ;
@@ -461,6 +487,13 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
461
487
UNUSED (period );
462
488
UNUSED (subSeconds );
463
489
#endif /* !STM32F1xx */
490
+
491
+ #if defined(STM32F1xx )
492
+ if (current_date != RtcHandle .DateToUpdate .Date ) {
493
+ RTC_StoreDate ();
494
+ }
495
+ #endif
496
+
464
497
}
465
498
}
466
499
@@ -483,6 +516,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday)
483
516
RTC_DateStruct .WeekDay = wday ;
484
517
HAL_RTC_SetDate (& RtcHandle , & RTC_DateStruct , RTC_FORMAT_BIN );
485
518
setBackupRegister (RTC_BKP_INDEX , RTC_BKP_VALUE );
519
+ RTC_StoreDate ();
486
520
}
487
521
}
488
522
@@ -711,6 +745,17 @@ void RTC_Alarm_IRQHandler(void)
711
745
HAL_RTC_AlarmIRQHandler (& RtcHandle );
712
746
}
713
747
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
+
714
759
#ifdef __cplusplus
715
760
}
716
761
#endif
0 commit comments