AceTimeClock  1.2.0
Clock classes for Arduino that can synchronize from an NTP server or an RTC chip
StmRtc.cpp
1 /*
2  * MIT License
3  * Copyright (c) 2020 Brian T. Park, Anatoli Arkhipenko
4  */
5 
6 #if defined(ARDUINO_ARCH_STM32) || defined(EPOXY_DUINO)
7 
8 #if defined(EPOXY_DUINO)
9  #include <EpoxyMockSTM32RTC.h>
10 #else
11  #include <STM32RTC.h>
12 #endif
13 #include "HardwareDateTime.h"
14 #include "StmRtc.h"
15 
16 namespace ace_time {
17 namespace hw {
18 
19 void StmRtc::readDateTime(HardwareDateTime* dateTime) const {
20  STM32RTC& rtc = STM32RTC::getInstance();
21  if (rtc.isTimeSet()) {
22  dateTime->second = rtc.getSeconds();
23  dateTime->minute = rtc.getMinutes();
24  dateTime->hour = rtc.getHours();
25  dateTime->dayOfWeek = rtc.getWeekDay();
26  dateTime->day = rtc.getDay();
27  dateTime->month = rtc.getMonth();
28  dateTime->year = rtc.getYear();
29  } else {
30  dateTime->second = 0;
31  dateTime->minute = 0;
32  dateTime->hour = 0;
33 
34  // The STM32RTC weekday parameter ranges from (1-7).
35  // AceTime uses ISO weekday numbers, where Monday=1.
36  // The date 2000-01-01 was a Saturday=6, so this should be set to 6.
37  //
38  // AceTime does not actually use this field, because it derives the weekday
39  // from the (year, month, day) tuple. But it seems prudent to set this
40  // corectly in case something else in the system uses STM32RTC directly.
41  dateTime->dayOfWeek = 6;
42 
43  dateTime->day = 1;
44  dateTime->month = 1;
45  dateTime->year = 0; // 2 digit year, so 0 means year 2000
46  }
47 }
48 
49 void StmRtc::setDateTime(const HardwareDateTime& dateTime) const {
50  STM32RTC& rtc = STM32RTC::getInstance();
51  rtc.setTime(dateTime.hour, dateTime.minute, dateTime.second);
52  rtc.setDate(dateTime.dayOfWeek, dateTime.day, dateTime.month, dateTime.year);
53 }
54 
55 bool StmRtc::isTimeSet() const {
56  STM32RTC& rtc = STM32RTC::getInstance();
57  return rtc.isTimeSet();
58 }
59 
60 } // hw
61 } // ace_time
62 
63 #endif // #if defined(ARDUINO_ARCH_STM32) || defined(EPOXY_DUINO)
void setDateTime(const HardwareDateTime &dateTime) const
Set the STM with the HardwareDateTime values.
Definition: StmRtc.cpp:49
bool isTimeSet() const
Return true if the RTC is available and the time is set.
Definition: StmRtc.cpp:55
void readDateTime(HardwareDateTime *dateTime) const
Read the time into the HardwareDateTime object.
Definition: StmRtc.cpp:19
The date (year, month, day) and time (hour, minute, second) fields supported by the DS3231 RTC chip.
uint8_t year
[00, 99], year - 2000
uint8_t dayOfWeek
[1, 7], interpretation undefined, increments every day