Skip to content

Commit a4d824c

Browse files
committed
Merge branch 'master' of github.com:arduino-libraries/RTCZero
2 parents 38449cd + 54c281d commit a4d824c

File tree

3 files changed

+66
-28
lines changed

3 files changed

+66
-28
lines changed

examples/SimpleRTC/SimpleRTC.ino

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/*
22
Simple RTC for Arduino Zero and MKR1000
33
4-
Demonstrates the use of the RTC library for the Arduino Zero and MKR1000
4+
Demonstrates the use of the RTC library for the Arduino Zero and MKR1000
55
6-
This example code is in the public domain
6+
This example code is in the public domain
77
8-
http://arduino.cc/en/Tutorial/SimpleRTC
8+
http://arduino.cc/en/Tutorial/SimpleRTC
99
1010
created by Arturo Guadalupi <[email protected]>
1111
15 Jun 2015
12-
13-
modified
12+
modified
1413
18 Feb 2016
14+
modified by Andrea Richetta <[email protected]>
15+
24 Aug 2016
1516
*/
1617

1718
#include <RTCZero.h>
@@ -32,19 +33,19 @@ const byte year = 15;
3233
void setup()
3334
{
3435
Serial.begin(9600);
35-
36+
3637
rtc.begin(); // initialize RTC
37-
38+
3839
// Set the time
3940
rtc.setHours(hours);
4041
rtc.setMinutes(minutes);
4142
rtc.setSeconds(seconds);
42-
43+
4344
// Set the date
4445
rtc.setDay(day);
4546
rtc.setMonth(month);
4647
rtc.setYear(year);
47-
48+
4849
// you can use also
4950
//rtc.setTime(hours, minutes, seconds);
5051
//rtc.setDate(day, month, year);
@@ -53,21 +54,30 @@ void setup()
5354
void loop()
5455
{
5556
// Print date...
56-
Serial.print(rtc.getDay());
57+
print2digits(rtc.getDay());
5758
Serial.print("/");
58-
Serial.print(rtc.getMonth());
59+
print2digits(rtc.getMonth());
5960
Serial.print("/");
60-
Serial.print(rtc.getYear());
61-
Serial.print("\t");
62-
61+
print2digits(rtc.getYear());
62+
Serial.print(" ");
63+
6364
// ...and time
64-
Serial.print(rtc.getHours());
65+
print2digits(rtc.getHours());
6566
Serial.print(":");
66-
Serial.print(rtc.getMinutes());
67+
print2digits(rtc.getMinutes());
6768
Serial.print(":");
68-
Serial.print(rtc.getSeconds());
69-
69+
print2digits(rtc.getSeconds());
70+
7071
Serial.println();
71-
72+
7273
delay(1000);
7374
}
75+
76+
77+
78+
void print2digits(int number) {
79+
if (number < 10) {
80+
Serial.print("0"); // print a 0 before if the number is < than 10
81+
}
82+
Serial.print(number);
83+
}

src/RTCZero.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ void RTCZero::begin(bool resetTime)
5252
}
5353

5454
// Setup clock GCLK2 with OSC32K divided by 32
55-
GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4);
56-
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
57-
;
58-
GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL );
59-
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
60-
;
61-
GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos)));
62-
while (GCLK->STATUS.bit.SYNCBUSY)
63-
;
55+
configureClock();
6456

6557
RTCdisable();
6658

@@ -390,6 +382,21 @@ uint32_t RTCZero::getY2kEpoch()
390382
return (getEpoch() - EPOCH_TIME_OFF);
391383
}
392384

385+
void RTCZero::setAlarmEpoch(uint32_t ts)
386+
{
387+
if (_configured) {
388+
if (ts < EPOCH_TIME_OFF) {
389+
ts = EPOCH_TIME_OFF;
390+
}
391+
392+
time_t t = ts;
393+
struct tm* tmp = gmtime(&t);
394+
395+
setAlarmDate(tmp->tm_year - EPOCH_TIME_YEAR_OFF, tmp->tm_mon + 1, tmp->tm_mday);
396+
setAlarmTime(tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
397+
}
398+
}
399+
393400
void RTCZero::setEpoch(uint32_t ts)
394401
{
395402
if (_configured) {
@@ -419,6 +426,19 @@ void RTCZero::setY2kEpoch(uint32_t ts)
419426
}
420427
}
421428

429+
/* Attach peripheral clock to 32k oscillator */
430+
void RTCZero::configureClock() {
431+
GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4);
432+
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
433+
;
434+
GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL );
435+
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
436+
;
437+
GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos)));
438+
while (GCLK->STATUS.bit.SYNCBUSY)
439+
;
440+
}
441+
422442
/*
423443
* Private Utility Functions
424444
*/

src/RTCZero.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,19 @@ class RTCZero {
9595
uint32_t getY2kEpoch();
9696
void setEpoch(uint32_t ts);
9797
void setY2kEpoch(uint32_t ts);
98+
void setAlarmEpoch(uint32_t ts);
99+
100+
bool isConfigured() {
101+
_configured = RTC->MODE2.CTRL.reg & RTC_MODE2_CTRL_ENABLE;
102+
configureClock();
103+
return _configured;
104+
}
98105

99106
private:
100107
bool _configured;
101108

102109
void config32kOSC(void);
110+
void configureClock(void);
103111
void RTCreadRequest();
104112
bool RTCisSyncing(void);
105113
void RTCdisable();

0 commit comments

Comments
 (0)