Skip to content

Commit 139e56b

Browse files
authored
Merge pull request #8 from manchoz/rtc_support
Add support for PCF8563T RTC
2 parents cb6fc99 + 33b5ba7 commit 139e56b

File tree

17 files changed

+1165
-25
lines changed

17 files changed

+1165
-25
lines changed

examples/Application/IrrigationSimple/Helpers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
/**
44
* Set system and TimeAlarm clock from compile datetime or RTC
55
*/
6-
void setSystemClock(String compileDate, String compileTime)
6+
void setSystemClock(String buildDate, String buildTime)
77
{
88
// Retrieve clock time from compile date...
9-
auto buildTime = compileDateTimeToSystemTime(compileDate, compileTime, true, 2);
9+
auto buildDateTime = buildDateTimeToSystemTime(buildDate, buildTime, true, 2);
1010
// ... ore use the one from integrated RTC.
1111
auto rtcTime = time(NULL);
1212

1313
// Remember to connect at least the CR2032 battery
1414
// to keep the RTC running.
15-
auto actualTime = rtcTime > buildTime ? rtcTime : buildTime;
15+
auto actualTime = rtcTime > buildDateTime ? rtcTime : buildDateTime;
1616

1717
// Set both system time and the alarms one
1818
set_time(actualTime);
1919
setTime(actualTime);
2020

2121
Serial.print("Compile Date and Time: ");
22-
Serial.println(getLocaltime(buildTime));
22+
Serial.println(getLocaltime(buildDateTime));
2323
Serial.print("RTC Date and Time: ");
2424
Serial.println(getLocaltime(rtcTime));
2525
Serial.print("System Clock: ");

examples/Application/IrrigationSimple/TimeHelpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "TimeHelpers.h"
22

33
// Convert compile time to system time
4-
time_t compileDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
4+
time_t buildDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
55
{
66
char s_month[5];
77
int year;

examples/Application/IrrigationSimple/TimeHelpers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <mbed_mktime.h>
55

66
// Convert compile time to system time
7-
time_t compileDateTimeToSystemTime(const String, const String, bool local_time, int tz);
7+
time_t buildDateTimeToSystemTime(const String, const String, bool local_time, int tz);
88
String getLocaltime();
99
String getLocaltime(const time_t &build_time);
1010
String getLocaltime(const char *fmt, bool local_time, int);
11-
String getLocaltime(const time_t build_time, const char *fmt, bool local_time, int tz);
11+
String getLocaltime(const time_t build_time, const char *fmt, bool local_time, int tz);

examples/Application/IrrigationSimpleLCD/Helpers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
/**
44
* Set system and TimeAlarm clock from compile datetime or RTC
55
*/
6-
void setSystemClock(String compileDate, String compileTime)
6+
void setSystemClock(String buildDate, String buildTime)
77
{
88
// Retrieve clock time from compile date...
9-
auto buildTime = compileDateTimeToSystemTime(compileDate, compileTime, true, 2);
9+
auto buildDateTime = buildDateTimeToSystemTime(buildDate, buildTime, true, 2);
1010
// ... ore use the one from integrated RTC.
1111
auto rtcTime = time(NULL);
1212

1313
// Remember to connect at least the CR2032 battery
1414
// to keep the RTC running.
15-
auto actualTime = rtcTime > buildTime ? rtcTime : buildTime;
15+
auto actualTime = rtcTime > buildDateTime ? rtcTime : buildDateTime;
1616

1717
// Set both system time and the alarms one
1818
set_time(actualTime);
1919
setTime(actualTime);
2020

2121
Serial.print("Compile Date and Time: ");
22-
Serial.println(getLocaltime(buildTime));
22+
Serial.println(getLocaltime(buildDateTime));
2323
Serial.print("RTC Date and Time: ");
2424
Serial.println(getLocaltime(rtcTime));
2525
Serial.print("System Clock: ");

examples/Application/IrrigationSimpleLCD/TimeHelpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "TimeHelpers.h"
22

33
// Convert compile time to system time
4-
time_t compileDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
4+
time_t buildDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
55
{
66
char s_month[5];
77
int year;

examples/Application/IrrigationSimpleLCD/TimeHelpers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <mbed_mktime.h>
55

66
// Convert compile time to system time
7-
time_t compileDateTimeToSystemTime(const String, const String, bool local_time, int tz);
7+
time_t buildDateTimeToSystemTime(const String, const String, bool local_time, int tz);
88
String getLocaltime();
99
String getLocaltime(const time_t &build_time);
1010
String getLocaltime(const char *fmt, bool local_time, int);
11-
String getLocaltime(const time_t build_time, const char *fmt, bool local_time, int tz);
11+
String getLocaltime(const time_t build_time, const char *fmt, bool local_time, int tz);

examples/Application/LowPowerDataLogger/Helpers.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int getAverageInputRead(int pin, const size_t loops)
5858
}
5959

6060
// Convert compile time to system time
61-
time_t compileDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
61+
time_t buildDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
6262
{
6363
char s_month[5];
6464
int year;
@@ -108,22 +108,22 @@ String getLocaltime(const time_t& build_time)
108108
/**
109109
* Set system clock from compile datetime or RTC
110110
*/
111-
void setSystemClock(String compileDate, String compileTime)
111+
void setSystemClock(String buildDate, String buildTime)
112112
{
113113
// Retrieve clock time from compile date...
114-
auto buildTime = compileDateTimeToSystemTime(compileDate, compileTime, true, 2);
114+
auto buildDateTime = buildDateTimeToSystemTime(buildDate, buildTime, true, 2);
115115
// ... ore use the one from integrated RTC.
116116
auto rtcTime = time(NULL);
117117

118118
// Remember to connect at least the CR2032 battery
119119
// to keep the RTC running.
120-
auto actualTime = rtcTime > buildTime ? rtcTime : buildTime;
120+
auto actualTime = rtcTime > buildDateTime ? rtcTime : buildDateTime;
121121

122122
// Set both system time
123123
set_time(actualTime);
124124

125125
DebugSerial.print("Compile Date and Time: ");
126-
DebugSerial.println(getLocaltime(buildTime));
126+
DebugSerial.println(getLocaltime(buildDateTime));
127127
DebugSerial.print("RTC Date and Time: ");
128128
DebugSerial.println(getLocaltime(rtcTime));
129129
DebugSerial.print("System Clock: ");

examples/Application/RTCClock/Helpers.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
// Convert compile time to system time
9-
time_t compileDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
9+
time_t buildDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
1010
{
1111
char s_month[5];
1212
int year;
@@ -56,22 +56,22 @@ String getLocaltime(const time_t& build_time)
5656
/**
5757
* Set system clock from compile datetime or RTC
5858
*/
59-
void setSystemClock(String compileDate, String compileTime)
59+
void setSystemClock(String buildDate, String buildTime)
6060
{
6161
// Retrieve clock time from compile date...
62-
auto buildTime = compileDateTimeToSystemTime(compileDate, compileTime, true, 2);
62+
auto buildDateTime = buildDateTimeToSystemTime(buildDate, buildTime, true, 2);
6363
// ... ore use the one from integrated RTC.
6464
auto rtcTime = time(NULL);
6565

6666
// Remember to connect at least the CR2032 battery
6767
// to keep the RTC running.
68-
auto actualTime = rtcTime > buildTime ? rtcTime : buildTime;
68+
auto actualTime = rtcTime > buildDateTime ? rtcTime : buildDateTime;
6969

7070
// Set both system time
7171
set_time(actualTime);
7272

7373
Serial.print("Compile Date and Time: ");
74-
Serial.println(getLocaltime(buildTime));
74+
Serial.println(getLocaltime(buildDateTime));
7575
Serial.print("RTC Date and Time: ");
7676
Serial.println(getLocaltime(rtcTime));
7777
Serial.print("System Clock: ");
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
#include <Arduino_EdgeControl.h>
5+
#include <mbed_mktime.h>
6+
7+
// Convert build time to UNIX time
8+
time_t getBuildDateTime(bool local_time = true, int tz = 0)
9+
{
10+
char s_month[5];
11+
int year;
12+
13+
tm t;
14+
time_t seconds;
15+
16+
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
17+
18+
sscanf(__DATE__, "%s %d %d", s_month, &t.tm_mday, &year);
19+
sscanf(__TIME__, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec);
20+
21+
// Find where is s_month in month_names. Deduce month value.
22+
t.tm_mon = (strstr(month_names, s_month) - month_names) / 3 + 1;
23+
t.tm_year = year - 1900;
24+
_rtc_maketime(&t, &seconds, RTC_FULL_LEAP_YEAR_SUPPORT);
25+
26+
if (!local_time) {
27+
if (tz > 200) {
28+
tz = 0x100 - tz; // Handle negative values
29+
seconds += (3600UL) * tz;
30+
} else {
31+
seconds -= (3600UL) * tz;
32+
}
33+
}
34+
35+
return seconds;
36+
}
37+
38+
String getRTCDate()
39+
{
40+
// APIs to get date fields.
41+
auto years = RealTimeClock.getYears();
42+
auto months = RealTimeClock.getMonths();
43+
auto days = RealTimeClock.getDays();
44+
45+
char buf[12] {};
46+
47+
snprintf(buf, 11, "20%02d-%02d-%02d", years, months, days);
48+
49+
return String(buf);
50+
}
51+
52+
String getRTCTime()
53+
{
54+
// APIs to get time fields.
55+
auto hours = RealTimeClock.getHours();
56+
auto minutes = RealTimeClock.getMinutes();
57+
auto seconds = RealTimeClock.getSeconds();
58+
59+
char buf[11] {};
60+
61+
snprintf(buf, 10, "%02d:%02d:%02d", hours, minutes, seconds);
62+
63+
return String(buf);
64+
}
65+
66+
String getRTCDateTime()
67+
{
68+
auto date = getRTCDate();
69+
auto time = getRTCTime();
70+
71+
auto dateTime = date + ' ' + time;
72+
73+
return dateTime;
74+
}
75+
76+
String getLocaltime()
77+
{
78+
char buffer[32];
79+
tm t;
80+
_rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
81+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
82+
return String(buffer);
83+
}
84+
85+
String getLocaltime(const time_t& build_time)
86+
{
87+
char buffer[32];
88+
tm t;
89+
_rtc_localtime(build_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
90+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
91+
return String(buffer);
92+
}
93+
94+
String getLocaltime(const char* fmt, bool local_time = true, int tz = 0)
95+
{
96+
char buffer[64];
97+
time_t tmp_time = time(NULL);
98+
tm t;
99+
100+
if (!local_time) {
101+
if (tz > 200) {
102+
tz = 0x100 - tz; // Handle negative values
103+
tmp_time -= (3600UL) * tz;
104+
} else {
105+
tmp_time += (3600UL) * tz;
106+
}
107+
}
108+
109+
_rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
110+
strftime(buffer, 64, fmt, &t);
111+
return String(buffer);
112+
}
113+
114+
String getLocaltime(const time_t build_time, const char* fmt, bool local_time = true, int tz = 0)
115+
{
116+
char buffer[64];
117+
time_t tmp_time = build_time;
118+
tm t;
119+
120+
if (!local_time) {
121+
if (tz > 200) {
122+
tz = 0x100 - tz; // Handle negative values
123+
tmp_time -= (3600UL) * tz;
124+
} else {
125+
tmp_time += (3600UL) * tz;
126+
}
127+
}
128+
129+
_rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
130+
strftime(buffer, 64, fmt, &t);
131+
return String(buffer);
132+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Arduino Edge Control - RTC Example
3+
4+
This sketch shows how to use the RTC (PCF8563T) on the Arduino
5+
Edge Control and how to configure the RTC's time registers.
6+
7+
Circuit:
8+
- Arduino Edge Control
9+
- CR2032 Battery
10+
- Optional: Arduino Edge Control LCD + Button brekout
11+
12+
*/
13+
14+
#include "Helpers.h"
15+
#include <Arduino_EdgeControl.h>
16+
17+
void setup()
18+
{
19+
pinMode(POWER_ON, INPUT);
20+
21+
Serial.begin(9600);
22+
23+
for (auto timeout = millis() + 2500l; !Serial && millis() < timeout; delay(250))
24+
;
25+
26+
Serial.println("Hello, Arduino Edge Control!");
27+
28+
EdgeControl.begin();
29+
30+
Power.on(PWR_3V3);
31+
Power.on(PWR_VBAT);
32+
33+
Wire.begin();
34+
delay(500);
35+
36+
Serial.print("Initializating the Real Time Clock...");
37+
while (!RealTimeClock.begin()) {
38+
Serial.println(" failed! Retrying...");
39+
delay(250);
40+
}
41+
Serial.println(" done!");
42+
43+
// APIs to set date's fields: years, months, days, hours, minutes and seconds
44+
// The RTC time can be set as epoch, using one of the following two options:
45+
// - Calendar time: RealTimeClock.setEpoch(years, months, days, hours, minutes, seconds);
46+
// - UTC time: RealTimeClock.setEpoch(date_in_seconds);
47+
48+
// Set the RTC only when LCD's PowerOn button is pressed.
49+
// YMMV.
50+
if (digitalRead(POWER_ON) == LOW) {
51+
Serial.println("Resetting the RTC to Sketch Build Datetime!");
52+
auto buildDateTime = getBuildDateTime();
53+
RealTimeClock.setEpoch(buildDateTime);
54+
Serial.print("Build ");
55+
}
56+
}
57+
58+
void loop()
59+
{
60+
Serial.print("Date: ");
61+
Serial.println(getRTCDateTime());
62+
63+
Serial.print("Unix time: ");
64+
Serial.println(time(NULL));
65+
66+
delay(1000);
67+
}

0 commit comments

Comments
 (0)