AceTime  1.7.2
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock 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(EPOXY_DUINO)
7 #if defined(ARDUINO_ARCH_STM32)
8 
9 #include <Print.h> // Print
10 #include <AceCommon.h> // bcdToDec(), decToBcd()
11 #include "HardwareDateTime.h"
12 #include "StmRtc.h"
13 
14 using ace_common::bcdToDec;
15 using ace_common::decToBcd;
16 
17 namespace ace_time {
18 namespace hw {
19 
20 StmRtc::StmRtc() {
21 
22  mRtc = &STM32RTC::getInstance();
23  if (mRtc) {
24  mRtc->setClockSource(STM32RTC::LSI_CLOCK);
25  mRtc->begin(HOUR_FORMAT_24);
26  }
27 }
28 
29 
30 void StmRtc::readDateTime(HardwareDateTime* dateTime) const {
31 
32  if (mRtc && mRtc->isTimeSet()) {
33 
34 // Serial.println("StmRtc::readDateTime rtc is set");
35  dateTime->second = mRtc->getSeconds();
36  dateTime->minute = mRtc->getMinutes();
37  dateTime->hour = mRtc->getHours();
38  dateTime->dayOfWeek = mRtc->getWeekDay();
39  dateTime->day = mRtc->getDay();
40  dateTime->month = mRtc->getMonth();
41  dateTime->year = mRtc->getYear();
42  }
43  else {
44 // Serial.println("StmRtc::readDateTime rtc is NOT set");
45  dateTime->second = 0;
46  dateTime->minute = 0;
47  dateTime->hour = 0;
48  dateTime->dayOfWeek = 0;
49  dateTime->day = 1;
50  dateTime->month = 1;
51  dateTime->year = 0;
52  }
53 }
54 
55 
56 // always set in 24h format
57 void StmRtc::setDateTime(const HardwareDateTime& dateTime) const {
58 
59  if (mRtc) {
60 // Serial.println("STMRTC::setDateTime rtc is set");
61  mRtc->setTime(dateTime.hour, dateTime.minute, dateTime.second);
62  mRtc->setDate(
63  dateTime.dayOfWeek, dateTime.day, dateTime.month, dateTime.year
64  );
65  }
66 }
67 
68 bool StmRtc::isTimeSet() const {
69  if (mRtc) {
70  return mRtc->isTimeSet();
71  }
72  return false;
73 }
74 
75 } // hw
76 } // ace_time
77 
78 #endif // #if ! defined(EPOXY_DUINO)
79 #endif // #if defined(ARDUINO_ARCH_STM32)