Skip to content

Commit b43a9cc

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

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
@@ -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

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)