Skip to content

Commit f28864d

Browse files
committed
Handle reset
Require STM32 core version higher than 1.5.0. Fix #11 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 11ba332 commit f28864d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ _Time and date configuration (added for convenience)_
5858
* **`void getTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, AM_PM *period = NULL)`**
5959
* **`void getDate(uint8_t *weekDay, uint8_t *day, uint8_t *month, uint8_t *year)`**
6060

61+
### Since STM32 Core version > 1.5.0
62+
_Reset time management_
63+
By default, if a time is set it will not be reset afer a reboot.
64+
65+
Using `begin(true)` or `begin(true, HOUR_24)` will reset the RTC registers.
66+
67+
To know if a time has already been set use:
68+
* **`bool isTimeSet(void)`**
69+
```
70+
if (!rtc.isTimeSet()) {
71+
// Set the time
72+
rtc.setHours(hours);
73+
rtc.setMinutes(minutes);
74+
rtc.setSeconds(seconds);
75+
}
76+
```
77+
6178
Refer to the Arduino RTC documentation for the other functions
6279
http://arduino.cc/en/Reference/RTC
6380

src/STM32RTC.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
// Initialize static variable
4747
bool STM32RTC::_configured = false;
48+
bool STM32RTC::_reset = false;
4849

4950
/**
5051
* @brief initializes the RTC
@@ -54,6 +55,7 @@ bool STM32RTC::_configured = false;
5455
*/
5556
void STM32RTC::begin(bool resetTime, Hour_Format format)
5657
{
58+
_reset = resetTime;
5759
if(resetTime == true) {
5860
_configured = false;
5961
_alarmEnabled = false;
@@ -71,7 +73,11 @@ void STM32RTC::begin(Hour_Format format)
7173
if(_configured == false) {
7274
RTC_init((format == HOUR_12)? HOUR_FORMAT_12: HOUR_FORMAT_24,
7375
(_clockSource == LSE_CLOCK)? ::LSE_CLOCK:
74-
(_clockSource == HSE_CLOCK)? ::HSE_CLOCK : ::LSI_CLOCK);
76+
(_clockSource == HSE_CLOCK)? ::HSE_CLOCK : ::LSI_CLOCK
77+
#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01050000)
78+
, _reset
79+
#endif
80+
);
7581
// Must be set before call of sync methods
7682
_configured = true;
7783
syncTime();

src/STM32RTC.h

+9
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,21 @@ class STM32RTC {
185185
bool isAlarmEnabled(void) {
186186
return _alarmEnabled;
187187
}
188+
bool isTimeSet(void) {
189+
#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01050000)
190+
return RTC_IsTimeSet();
191+
#else
192+
return false;
193+
#endif
194+
}
195+
188196
friend class STM32LowPower;
189197

190198
private:
191199
STM32RTC(void): _clockSource(LSI_CLOCK) {}
192200

193201
static bool _configured;
202+
static bool _reset;
194203

195204
AM_PM _hoursPeriod;
196205
uint8_t _hours;

0 commit comments

Comments
 (0)