AceTimeClock  1.3.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 // Use the STM32RTC library if compiling under Stm32duino.
9 #if ! defined(EPOXY_DUINO)
10  #include <STM32RTC.h>
11 #endif
12 #include "HardwareDateTime.h"
13 #include "StmRtc.h"
14 
15 namespace ace_time {
16 namespace hw {
17 
18 void StmRtc::readDateTime(HardwareDateTime* dateTime) const {
19 #if defined(EPOXY_DUINO)
20  // Hardcode the date to 2000-01-01T00:00:00 under EpoxyDuino.
21  dateTime->second = 0;
22  dateTime->minute = 0;
23  dateTime->hour = 0;
24  dateTime->dayOfWeek = 6;
25  dateTime->day = 1;
26  dateTime->month = 1;
27  dateTime->year = 0; // 2 digit year, so 0 means year 2000
28 #else
29  STM32RTC& rtc = STM32RTC::getInstance();
30  if (rtc.isTimeSet()) {
31  dateTime->second = rtc.getSeconds();
32  dateTime->minute = rtc.getMinutes();
33  dateTime->hour = rtc.getHours();
34  dateTime->dayOfWeek = rtc.getWeekDay();
35  dateTime->day = rtc.getDay();
36  dateTime->month = rtc.getMonth();
37  dateTime->year = rtc.getYear();
38  } else {
39  dateTime->second = 0;
40  dateTime->minute = 0;
41  dateTime->hour = 0;
42 
43  // The STM32RTC weekday parameter ranges from (1-7).
44  // AceTime uses ISO weekday numbers, where Monday=1.
45  // The date 2000-01-01 was a Saturday=6, so this should be set to 6.
46  //
47  // AceTime does not actually use this field, because it derives the weekday
48  // from the (year, month, day) tuple. But it seems prudent to set this
49  // corectly in case something else in the system uses STM32RTC directly.
50  dateTime->dayOfWeek = 6;
51 
52  dateTime->day = 1;
53  dateTime->month = 1;
54  dateTime->year = 0; // 2 digit year, so 0 means year 2000
55  }
56 #endif
57 }
58 
59 void StmRtc::setDateTime(const HardwareDateTime& dateTime) const {
60 #if defined(EPOXY_DUINO)
61  (void) dateTime;
62 #else
63  STM32RTC& rtc = STM32RTC::getInstance();
64  rtc.setTime(dateTime.hour, dateTime.minute, dateTime.second);
65  rtc.setDate(dateTime.dayOfWeek, dateTime.day, dateTime.month, dateTime.year);
66 #endif
67 }
68 
69 bool StmRtc::isTimeSet() const {
70 #if defined(EPOXY_DUINO)
71  return false;
72 #else
73  STM32RTC& rtc = STM32RTC::getInstance();
74  return rtc.isTimeSet();
75 #endif
76 }
77 
78 } // hw
79 } // ace_time
80 
81 #endif // #if defined(ARDUINO_ARCH_STM32) || defined(EPOXY_DUINO)
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
void setDateTime(const HardwareDateTime &dateTime) const
Set the STM with the HardwareDateTime values.
Definition: StmRtc.cpp:59
bool isTimeSet() const
Return true if the RTC is available and the time is set.
Definition: StmRtc.cpp:69
void readDateTime(HardwareDateTime *dateTime) const
Read the time into the HardwareDateTime object.
Definition: StmRtc.cpp:18