Skip to content

Commit 14f3ce2

Browse files
committed
Initial RTC
1 parent c7535cf commit 14f3ce2

File tree

7 files changed

+287
-1
lines changed

7 files changed

+287
-1
lines changed

src/Arduino_PortentaBreakoutCarrier.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3535
#include <Portenta_Ethernet.h>
3636
#include <Ethernet.h>
3737
#include "utility/Analog/Analog.h"
38+
#include "utility/RTC/RTC.h"
3839

3940
#define LAST_ARDUINO_PIN_NUMBER LEDB + 1
4041
typedef enum {
@@ -252,6 +253,7 @@ class BreakoutCarrierClass {
252253
MbedSPI SPI_0;
253254
PDMClass PDM;
254255
arduino::EthernetClass Ethernet;
256+
HWClock RTClock;
255257
BreakoutCarrierClass() : I2C_0(PH_8,PH_7),
256258
I2C_1(PB_7,PB_6),
257259
I2C_2(PH_12,PH_11),
@@ -261,7 +263,8 @@ class BreakoutCarrierClass {
261263
UART3(PJ_8, PJ_9, NC, NC),
262264
SPI_0(PC_2, PC_3, PI_1),
263265
PDM(PB_2, PE_2, NC),
264-
Ethernet()
266+
Ethernet(),
267+
RTClock()
265268
{
266269
}
267270
};

src/utility/RTC/RTC.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
RTC.cpp
3+
Copyright (c) 2021 Arduino LLC. All right reserved.
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
This library is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
Lesser General Public License for more details.
12+
You should have received a copy of the GNU Lesser General Public
13+
License along with this library; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
*/
16+
17+
#include "Arduino.h"
18+
#include "RealTimeClock.h"
19+
#include "RTC.h"
20+
#include "mbed.h"
21+
22+
HWClock::HWClock() : lastTimestamp(0)
23+
{
24+
// Empty
25+
}
26+
27+
28+
// getTime returns the current time as unix-timestamp.
29+
// Return 0 if time has not been set.
30+
Timestamp HWClock :: getTime() {
31+
Timestamp res;
32+
//if (lastTimestamp == 0) {
33+
// return res;
34+
//}
35+
time_t seconds = time(NULL);
36+
res.setFromUnixTimestamp(seconds);
37+
return res;
38+
}
39+
40+
// setTime sets the current time given a unix-timestamp.
41+
bool HWClock :: setTime(Timestamp t) {
42+
lastTimestamp = t.getUnixTimestamp();
43+
::set_time(lastTimestamp);
44+
}
45+
46+
// setTimeWith will sync the current clock with the
47+
// time given by the specified provider.
48+
//void HWClock :: setTimeWith(TimeSyncProvider &ts) {
49+
// setTime(ts.getTime());
50+
//}
51+

src/utility/RTC/RTC.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
RTC.h
3+
Copyright (c) 2021 Arduino LLC. All right reserved.
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
This library is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
Lesser General Public License for more details.
12+
You should have received a copy of the GNU Lesser General Public
13+
License along with this library; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
*/
16+
17+
#ifndef ARDUINO_HW_RTC_H
18+
#define ARDUINO_HW_RTC_H
19+
20+
#include "RealTimeClock.h"
21+
22+
class HWClock : public RealTimeClock {
23+
public:
24+
HWClock();
25+
26+
// Get the current time as unix-timestamp.
27+
// Return 0 if time has not been set.
28+
virtual Timestamp getTime();
29+
30+
// Set the current time as unix-timestamp
31+
virtual bool setTime(Timestamp t);
32+
33+
private:
34+
uint64_t lastTimestamp;
35+
};
36+
37+
#endif

src/utility/RTC/RealTimeClock.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
RealTimeClock.h
3+
4+
Copyright (c) 2021 Arduino LLC. All right reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#pragma once
22+
23+
#include "TimeSyncProvider.h"
24+
25+
// RealTimeClock interface
26+
// RTC should implement this interface.
27+
// A RealTimeClock may be used as a TimeSyncProvider.
28+
class RealTimeClock : public TimeSyncProvider
29+
{
30+
public:
31+
// getTime returns the current time as unix-timestamp.
32+
// Return 0 if time has not been set.
33+
virtual Timestamp getTime() = 0;
34+
35+
// setTime sets the current time given a unix-timestamp.
36+
virtual bool setTime(Timestamp t) = 0;
37+
38+
// setTimeWith will sync the current clock with the
39+
// time given by the specified provider.
40+
//void setTimeWith(TimeSyncProvider &ts) {
41+
// setTime(ts.getTime());
42+
//}
43+
};

src/utility/RTC/TimeSyncProvider.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
TimeSyncProvider.h
3+
4+
Copyright (c) 2021 Arduino LLC. All right reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#pragma once
22+
23+
#include "Timestamp.h"
24+
25+
// TimeSyncProvider interface
26+
// Time providers (like NetworkTimeProtocol, GPS, DCF77, etc) should
27+
// implement this interface.
28+
class TimeSyncProvider
29+
{
30+
public:
31+
// getTime returns the current time as unix-timestamp.
32+
// Return 0 if time has not been set.
33+
virtual Timestamp getTime() = 0;
34+
};

src/utility/RTC/Timestamp.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Timestamp.cpp
3+
Copyright (c) 2021 Arduino LLC. All right reserved.
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
This library is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
Lesser General Public License for more details.
12+
You should have received a copy of the GNU Lesser General Public
13+
License along with this library; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
*/
16+
17+
#include "mbed_mktime.h"
18+
#include "Timestamp.h"
19+
20+
int Timestamp :: minute() {
21+
struct tm timeinfo;
22+
if (_rtc_localtime(Timestamp :: ts32, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
23+
return -1;
24+
}
25+
return timeinfo.tm_min;
26+
}
27+
28+
int Timestamp :: hour() {
29+
struct tm timeinfo;
30+
if (_rtc_localtime(Timestamp :: ts32, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
31+
return -1;
32+
}
33+
return timeinfo.tm_hour;
34+
}
35+
36+
int Timestamp :: day() {
37+
struct tm timeinfo;
38+
if (_rtc_localtime(Timestamp :: ts32, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
39+
return -1;
40+
}
41+
return timeinfo.tm_mday;
42+
}
43+
44+
int Timestamp :: month() {
45+
struct tm timeinfo;
46+
if (_rtc_localtime(Timestamp :: ts32, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
47+
return -1;
48+
}
49+
return timeinfo.tm_mon;
50+
}
51+
52+
int Timestamp :: year() {
53+
struct tm timeinfo;
54+
if (_rtc_localtime(Timestamp :: ts32, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
55+
return -1;
56+
}
57+
return timeinfo.tm_year;
58+
}

src/utility/RTC/Timestamp.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Timestamp.h
3+
4+
Copyright (c) 2021 Arduino LLC. All right reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#pragma once
22+
23+
// Timestamp represents a fixed time
24+
typedef struct {
25+
public:
26+
int second() { return ts32 % 60; }
27+
int minute();
28+
int hour();
29+
30+
int day();
31+
int month();
32+
int year();
33+
34+
// Internal data
35+
private:
36+
// use uint64 to handle the 2038 rollover
37+
uint64_t ts;
38+
39+
// *** to avoid 64bit math on small systems an alternative may be:
40+
41+
public:
42+
void setFromUnixTimestamp(uint64_t t) {
43+
after_2038 = (t >= 0x100000000);
44+
ts32 = t;
45+
}
46+
uint64_t getUnixTimestamp() {
47+
uint64_t t = ts32;
48+
if (after_2038) t += 0x100000000;
49+
return t;
50+
}
51+
52+
// Internal data
53+
private:
54+
uint32_t ts32;
55+
bool after_2038;
56+
57+
// More accessor methods, for example:
58+
//void setTimeVal(struct tm t); // accept posix timeval
59+
//struct tm getTimeVal(); // return posix timeval
60+
} Timestamp;

0 commit comments

Comments
 (0)