@@ -292,10 +292,14 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch)
292292 * @param format: enable the RTC in 12 or 24 hours mode
293293 * @retval None
294294 */
295- void RTC_init (hourFormat_t format , sourceClock_t source )
295+ void RTC_init (hourFormat_t format , sourceClock_t source , bool reset )
296296{
297297 initFormat = format ;
298298
299+ if (reset ) {
300+ resetBackupRegister ();
301+ }
302+
299303 /* Init RTC clock */
300304 RTC_initClock (source );
301305
@@ -323,19 +327,15 @@ void RTC_init(hourFormat_t format, sourceClock_t source)
323327
324328 HAL_RTC_Init (& RtcHandle );
325329
326- /*Sunday 1st January 2017*/
327- RTC_SetDate (17 , 1 , 1 , 7 );
328-
329- /*at 0:0:0*/
330- RTC_SetTime (0 , 0 , 0 , 0 , HOUR_AM );
331-
332330#if !defined(STM32F1xx ) && !defined(STM32F2xx ) && !defined(STM32L1xx ) || defined(STM32L1_ULPH )
333331 /* Enable Direct Read of the calendar registers (not through Shadow) */
334332 HAL_RTCEx_EnableBypassShadow (& RtcHandle );
335333#endif /* !STM32F1xx && !STM32F2xx */
336334
337335 HAL_NVIC_SetPriority (RTC_Alarm_IRQn , 2 , 0 );
338336 HAL_NVIC_EnableIRQ (RTC_Alarm_IRQn );
337+ /* Ensure backup domain is enabled */
338+ enableBackupRegister ();
339339}
340340
341341/**
@@ -349,6 +349,15 @@ void RTC_DeInit(void)
349349 callbackUserData = NULL ;
350350}
351351
352+ /**
353+ * @brief Check wether time is already set
354+ * @retval True if set else false
355+ */
356+ bool RTC_IsTimeSet (void )
357+ {
358+ return (getBackupRegister (RTC_BKP_INDEX ) == RTC_BKP_VALUE ) ? true : false;
359+ }
360+
352361/**
353362 * @brief Set RTC time
354363 * @param hours: 0-12 or 0-23. Depends on the format used.
@@ -392,6 +401,7 @@ void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSe
392401#endif /* !STM32F1xx */
393402
394403 HAL_RTC_SetTime (& RtcHandle , & RTC_TimeStruct , RTC_FORMAT_BIN );
404+ setBackupRegister (RTC_BKP_INDEX , RTC_BKP_VALUE );
395405 }
396406}
397407
@@ -453,6 +463,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday)
453463 RTC_DateStruct .Date = day ;
454464 RTC_DateStruct .WeekDay = wday ;
455465 HAL_RTC_SetDate (& RtcHandle , & RTC_DateStruct , RTC_FORMAT_BIN );
466+ setBackupRegister (RTC_BKP_INDEX , RTC_BKP_VALUE );
456467 }
457468}
458469
0 commit comments