Skip to content

Commit 2891ef6

Browse files
agdlcmaglie
authored andcommitted
First import
0 parents  commit 2891ef6

File tree

6 files changed

+410
-0
lines changed

6 files changed

+410
-0
lines changed

README.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
= RTC Library for Arduino =
2+
3+
The RTC library enables an Arduino Zero board to take control of the internal RTC.
4+
5+
For more information about this library please visit us at
6+
http://arduino.cc/en/Reference/RTC
7+
8+
== License ==
9+
10+
Copyright (c) Arduino LLC. All right reserved.
11+
12+
This library is free software; you can redistribute it and/or
13+
modify it under the terms of the GNU Lesser General Public
14+
License as published by the Free Software Foundation; either
15+
version 2.1 of the License, or (at your option) any later version.
16+
17+
This library is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20+
Lesser General Public License for more details.
21+
22+
You should have received a copy of the GNU Lesser General Public
23+
License along with this library; if not, write to the Free Software
24+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

examples/SimpleRTC/SimpleRTC.ino

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Simple RTC for Arduino Zero
3+
4+
Demonstrates the use of the RTC library for the Arduino Zero
5+
6+
This example code is in the public domain
7+
8+
http://arduino.cc/en/Tutorial/SimpleRTC
9+
10+
*/
11+
12+
#include <RTCZero.h>
13+
14+
/* Create an rtc object */
15+
RTCZero rtc;
16+
17+
/* Change these values to set the current initial time */
18+
const uint8_t seconds = 0;
19+
const uint8_t minutes = 0;
20+
const uint8_t hours = 16;
21+
22+
/* Change these values to set the current initial date */
23+
const uint8_t day = 15;
24+
const uint8_t month = 6;
25+
const uint8_t year = 15;
26+
27+
void setup()
28+
{
29+
Serial.begin(9600);
30+
31+
rtc.begin(H24); // initialize RTC 24H format. The dual option is H12
32+
33+
// Set the time
34+
rtc.setHours(hours);
35+
rtc.setMinutes(minutes);
36+
rtc.setSeconds(seconds);
37+
38+
// Set the date
39+
rtc.setDay(day);
40+
rtc.setMonth(month);
41+
rtc.setYear(year);
42+
43+
// you can use also
44+
//rtc.setTime(hours, minutes, seconds);
45+
//rtc.setDate(day, month, year);
46+
}
47+
48+
void loop()
49+
{
50+
// Print date...
51+
Serial.print(rtc.getDay());
52+
Serial.print("/");
53+
Serial.print(rtc.getMonth());
54+
Serial.print("/");
55+
Serial.print(rtc.getYear());
56+
Serial.print("\t");
57+
58+
// ...and time
59+
Serial.print(rtc.getHours());
60+
Serial.print(":");
61+
Serial.print(rtc.getMinutes());
62+
Serial.print(":");
63+
Serial.print(rtc.getSeconds());
64+
65+
Serial.println();
66+
67+
delay(1000);
68+
}

keywords.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#######################################
2+
# Syntax Coloring Map For RTC
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
RTCZero KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
getDay KEYWORD2
16+
getMonth KEYWORD2
17+
getYear KEYWORD2
18+
getHours KEYWORD2
19+
getMinutes KEYWORD2
20+
getSeconds KEYWORD2
21+
22+
setDay KEYWORD2
23+
setMonth KEYWORD2
24+
setYear KEYWORD2
25+
setHours KEYWORD2
26+
setMinutes KEYWORD2
27+
setSeconds KEYWORD2
28+
29+
#######################################
30+
# Constants (LITERAL1)
31+
#######################################
32+
H24 LITERAL1
33+
H12 LITERAL1
34+

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=RTCZero
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Allows to use the RTC functionalities. For Arduino Zero only.
6+
paragraph=With this library you can use the RTC peripheral of an Arduino Zero in order to program actions related to date and time.
7+
category=Timing
8+
url=http://arduino.cc/en/Reference/RTCZero
9+
architectures=samd

src/RTCZero.cpp

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/*
2+
RTC library for Arduino Zero.
3+
Copyright (c) 2015 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "RTCZero.h"
21+
22+
static bool __time24 = false;
23+
24+
void RTCZero::begin(bool timeRep)
25+
{
26+
uint16_t tmp_reg = 0;
27+
28+
if (timeRep)
29+
__time24 = true; // 24h representation chosen
30+
31+
PM->APBAMASK.reg |= PM_APBAMASK_RTC; // turn on digital interface clock
32+
config32kOSC();
33+
34+
// Setup clock GCLK2 with OSC32K divided by 32
35+
GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4);
36+
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
37+
;
38+
GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL );
39+
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
40+
;
41+
GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos)));
42+
while (GCLK->STATUS.bit.SYNCBUSY)
43+
;
44+
45+
RTCdisable();
46+
47+
RTCreset();
48+
49+
tmp_reg |= RTC_MODE2_CTRL_MODE_CLOCK; // set clock operating mode
50+
tmp_reg |= RTC_MODE2_CTRL_PRESCALER_DIV1024; // set prescaler to 1024 for MODE2
51+
tmp_reg &= ~RTC_MODE2_CTRL_MATCHCLR; // disable clear on match
52+
53+
if (timeRep)
54+
tmp_reg |= RTC_MODE2_CTRL_CLKREP; // 24h time representation
55+
else
56+
tmp_reg &= ~RTC_MODE2_CTRL_CLKREP; // 12h time representation
57+
58+
RTC->MODE2.READREQ.reg &= ~RTC_READREQ_RCONT; // disable continuously mode
59+
60+
RTC->MODE2.CTRL.reg = tmp_reg;
61+
while (RTCisSyncing())
62+
;
63+
64+
RTCenable();
65+
RTCresetRemove();
66+
}
67+
68+
/*
69+
* Get Functions
70+
*/
71+
72+
uint8_t RTCZero::getSeconds()
73+
{
74+
return RTC->MODE2.CLOCK.bit.SECOND;
75+
}
76+
77+
uint8_t RTCZero::getMinutes()
78+
{
79+
return RTC->MODE2.CLOCK.bit.MINUTE;
80+
}
81+
82+
uint8_t RTCZero::getHours()
83+
{
84+
return RTC->MODE2.CLOCK.bit.HOUR;
85+
}
86+
87+
uint8_t RTCZero::getDay()
88+
{
89+
return RTC->MODE2.CLOCK.bit.DAY;
90+
}
91+
92+
uint8_t RTCZero::getMonth()
93+
{
94+
return RTC->MODE2.CLOCK.bit.MONTH;
95+
}
96+
97+
uint8_t RTCZero::getYear()
98+
{
99+
return RTC->MODE2.CLOCK.bit.YEAR;
100+
}
101+
102+
/*
103+
* Set Functions
104+
*/
105+
106+
void RTCZero::setSeconds(uint8_t seconds)
107+
{
108+
RTC->MODE2.CLOCK.bit.SECOND = seconds;
109+
while (RTCisSyncing())
110+
;
111+
}
112+
113+
void RTCZero::setMinutes(uint8_t minutes)
114+
{
115+
RTC->MODE2.CLOCK.bit.MINUTE = minutes;
116+
while (RTCisSyncing())
117+
;
118+
}
119+
120+
void RTCZero::setHours(uint8_t hours)
121+
{
122+
if (__time24) {
123+
RTC->MODE2.CLOCK.bit.HOUR = hours;
124+
} else {
125+
RTC->MODE2.CLOCK.bit.HOUR = hours - 12;
126+
}
127+
while (RTCisSyncing())
128+
;
129+
}
130+
131+
void RTCZero::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
132+
{
133+
setSeconds(seconds);
134+
setMinutes(minutes);
135+
setHours(hours);
136+
}
137+
138+
void RTCZero::setDay(uint8_t day)
139+
{
140+
RTC->MODE2.CLOCK.bit.DAY = day;
141+
while (RTCisSyncing())
142+
;
143+
}
144+
145+
void RTCZero::setMonth(uint8_t month)
146+
{
147+
RTC->MODE2.CLOCK.bit.MONTH = month;
148+
while (RTCisSyncing())
149+
;
150+
}
151+
152+
void RTCZero::setYear(uint8_t year)
153+
{
154+
RTC->MODE2.CLOCK.bit.YEAR = year;
155+
while (RTCisSyncing())
156+
;
157+
}
158+
159+
void RTCZero::setDate(uint8_t day, uint8_t month, uint8_t year)
160+
{
161+
setDay(day);
162+
setMonth(month);
163+
setYear(year);
164+
}
165+
166+
/*
167+
* Private Utility Functions
168+
*/
169+
170+
/* Configure the 32768Hz Oscillator */
171+
void RTCZero::config32kOSC()
172+
{
173+
SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_ONDEMAND |
174+
SYSCTRL_XOSC32K_EN32K |
175+
SYSCTRL_XOSC32K_XTALEN |
176+
SYSCTRL_XOSC32K_STARTUP(6) |
177+
SYSCTRL_XOSC32K_ENABLE;
178+
}
179+
180+
/* Wait for sync in write operations */
181+
bool RTCZero::RTCisSyncing()
182+
{
183+
return (RTC->MODE2.STATUS.bit.SYNCBUSY);
184+
}
185+
186+
void RTCZero::RTCdisable()
187+
{
188+
RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_ENABLE; // disable RTC
189+
while (RTCisSyncing())
190+
;
191+
}
192+
193+
void RTCZero::RTCenable()
194+
{
195+
RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_ENABLE; // enable RTC
196+
while (RTCisSyncing())
197+
;
198+
}
199+
200+
void RTCZero::RTCreset()
201+
{
202+
RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_SWRST; // software reset
203+
while (RTCisSyncing())
204+
;
205+
}
206+
207+
void RTCZero::RTCresetRemove()
208+
{
209+
RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_SWRST; // software reset remove
210+
while (RTCisSyncing())
211+
;
212+
}
213+

0 commit comments

Comments
 (0)