@@ -285,10 +285,14 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch)
285285 * @param format: enable the RTC in 12 or 24 hours mode
286286 * @retval None
287287 */
288- void RTC_init (hourFormat_t format , sourceClock_t source )
288+ void RTC_init (hourFormat_t format , sourceClock_t source , bool reset )
289289{
290290 initFormat = format ;
291291
292+ if (reset ) {
293+ resetBackupRegister ();
294+ }
295+
292296 /* Init RTC clock */
293297 RTC_initClock (source );
294298
@@ -316,19 +320,15 @@ void RTC_init(hourFormat_t format, sourceClock_t source)
316320
317321 HAL_RTC_Init (& RtcHandle );
318322
319- /*Sunday 1st January 2017*/
320- RTC_SetDate (17 , 1 , 1 , 7 );
321-
322- /*at 0:0:0*/
323- RTC_SetTime (0 , 0 , 0 , 0 , HOUR_AM );
324-
325323#if !defined(STM32F1xx ) && !defined(STM32F2xx ) && !defined(STM32L1xx ) || defined(STM32L1_ULPH )
326324 /* Enable Direct Read of the calendar registers (not through Shadow) */
327325 HAL_RTCEx_EnableBypassShadow (& RtcHandle );
328326#endif /* !STM32F1xx && !STM32F2xx */
329327
330328 HAL_NVIC_SetPriority (RTC_Alarm_IRQn , 2 , 0 );
331329 HAL_NVIC_EnableIRQ (RTC_Alarm_IRQn );
330+ /* Ensure backup domain is enabled */
331+ enableBackupRegister ();
332332}
333333
334334/**
@@ -342,6 +342,15 @@ void RTC_DeInit(void)
342342 callbackUserData = NULL ;
343343}
344344
345+ /**
346+ * @brief Check wether time is already set
347+ * @retval True if set else false
348+ */
349+ bool RTC_IsTimeSet (void )
350+ {
351+ return (getBackupRegister (RTC_BKP_INDEX ) == RTC_BKP_VALUE )? true : false;
352+ }
353+
345354/**
346355 * @brief Set RTC time
347356 * @param hours: 0-12 or 0-23. Depends on the format used.
@@ -385,6 +394,7 @@ void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSe
385394#endif /* !STM32F1xx */
386395
387396 HAL_RTC_SetTime (& RtcHandle , & RTC_TimeStruct , RTC_FORMAT_BIN );
397+ setBackupRegister (RTC_BKP_INDEX , RTC_BKP_VALUE );
388398 }
389399}
390400
@@ -446,6 +456,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday)
446456 RTC_DateStruct .Date = day ;
447457 RTC_DateStruct .WeekDay = wday ;
448458 HAL_RTC_SetDate (& RtcHandle , & RTC_DateStruct , RTC_FORMAT_BIN );
459+ setBackupRegister (RTC_BKP_INDEX , RTC_BKP_VALUE );
449460 }
450461}
451462
0 commit comments