Skip to content

Commit fcacdee

Browse files
committed
[RTC] Handle reset time
Fix #266 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 0e9e1e0 commit fcacdee

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

cores/arduino/stm32/rtc.c

+18-7
Original file line numberDiff line numberDiff line change
@@ -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

cores/arduino/stm32/rtc.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#define __RTC_H
4141

4242
/* Includes ------------------------------------------------------------------*/
43+
#include <stdbool.h>
44+
#include "backup.h"
4345
#include "clock.h"
4446

4547
#ifdef HAL_RTC_MODULE_ENABLED
@@ -135,8 +137,9 @@ void RTC_SetClockSource(sourceClock_t source);
135137
void RTC_getPrediv(int8_t *asynch, int16_t *synch);
136138
void RTC_setPrediv(int8_t asynch, int16_t synch);
137139

138-
void RTC_init(hourFormat_t format, sourceClock_t source);
140+
void RTC_init(hourFormat_t format, sourceClock_t source, bool reset);
139141
void RTC_DeInit(void);
142+
bool RTC_IsTimeSet(void);
140143

141144
void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, hourAM_PM_t period);
142145
void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, hourAM_PM_t *period);

0 commit comments

Comments
 (0)