Skip to content

Commit 78afaae

Browse files
committed
ATLEDGE-229 CurieRTC and Time Library
Initial implementation of the CurieRTC library for the built-in RTC Includes the Time library plus 3 new examples for the Arduino101
1 parent 8fa6bba commit 78afaae

File tree

27 files changed

+2218
-0
lines changed

27 files changed

+2218
-0
lines changed

libraries/CurieRTC/CurieRTC.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* CurieRTC.c - RTC library for Arduino101
3+
4+
Copyright (c) 2015 Intel Corporation. All rights reserved
5+
This library is intended to be uses with Arduino Time.h library functions
6+
7+
The library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+
*/
22+
23+
#include "CurieRTC.h"
24+
25+
CurieRTC::CurieRTC()
26+
{
27+
}
28+
29+
// PUBLIC FUNCTIONS
30+
time_t CurieRTC::get()
31+
{
32+
// Read the value of the RTC_CCVR register
33+
return *(int*)RTC_CCVR;
34+
}
35+
36+
void CurieRTC::set(time_t t)
37+
{
38+
// Write t into the RTC_CLR register
39+
*(int*)RTC_CLR = t;
40+
}
41+
42+
CurieRTC RTC = CurieRTC(); // create an instance for the user

libraries/CurieRTC/CurieRTC.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* CurieRTC.h - RTC library for Arduino101
3+
* This library is intended to be uses with Arduino Time.h library functions
4+
*/
5+
6+
#ifndef CurieRTC_h
7+
#define CurieRTC_h
8+
9+
#define RTC_CCVR 0xb0000400 // Current Counter Value Register
10+
#define RTC_CMR 0xb0000404 // Counter Match Register
11+
#define RTC_CLR 0xb0000408 // Counter Load Register
12+
#define RTC_CCR 0xb000040C // Counter Control Register
13+
#define RTC_STAT 0xb0000410 // Interrupt Status Register
14+
#define RTC_RSTAT 0xb0000414 // Interrupt Raw Status Register
15+
#define RTC_EOI 0xb0000418 // End of Interrupt Register
16+
17+
#include <Time.h>
18+
19+
// library interface description
20+
class CurieRTC
21+
{
22+
// user-accessible "public" interface
23+
public:
24+
CurieRTC();
25+
static time_t get();
26+
static void set(time_t t);
27+
};
28+
29+
#ifdef RTC
30+
#undef RTC // workaround for Arduino Due, which defines "RTC"...
31+
#endif
32+
33+
extern CurieRTC RTC;
34+
35+
#endif
36+
37+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <Time.h>
2+
#include <CurieRTC.h>
3+
4+
void setup() {
5+
Serial.begin(115200);
6+
delay(200);
7+
Serial.println("CurieRTC Read Test");
8+
Serial.println("-------------------");
9+
}
10+
11+
void loop() {
12+
tmElements_t tm;
13+
time_t t = RTC.get();
14+
breakTime(t, tm);
15+
16+
Serial.print("Ok, Time = ");
17+
print2digits(tm.Hour);
18+
Serial.write(':');
19+
print2digits(tm.Minute);
20+
Serial.write(':');
21+
print2digits(tm.Second);
22+
Serial.print(", Date (D/M/Y) = ");
23+
Serial.print(tm.Day);
24+
Serial.write('/');
25+
Serial.print(tm.Month);
26+
Serial.write('/');
27+
Serial.print(tmYearToCalendar(tm.Year));
28+
Serial.println();
29+
delay(1000);
30+
}
31+
32+
void print2digits(int number) {
33+
if (number >= 0 && number < 10) {
34+
Serial.write('0');
35+
}
36+
Serial.print(number);
37+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <Time.h>
2+
#include <CurieRTC.h>
3+
4+
const char *monthName[12] = {
5+
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
6+
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
7+
};
8+
9+
tmElements_t tm;
10+
11+
void setup() {
12+
bool parse=false;
13+
bool config=false;
14+
time_t t;
15+
16+
// get the date and time the compiler was run
17+
if (getDate(__DATE__) && getTime(__TIME__)) {
18+
t = makeTime(tm);
19+
setTime(t);
20+
parse = true;
21+
config = true;
22+
}
23+
24+
Serial.begin(115200);
25+
delay(3000);
26+
27+
if (parse && config) {
28+
Serial.print("Curie configured Time=");
29+
Serial.print(__TIME__);
30+
Serial.print(", Date=");
31+
Serial.println(__DATE__);
32+
} else {
33+
Serial.print("Could not parse info from the compiler, Time=\"");
34+
Serial.print(__TIME__);
35+
Serial.print("\", Date=\"");
36+
Serial.print(__DATE__);
37+
Serial.println("\"");
38+
}
39+
}
40+
41+
void loop() {
42+
}
43+
44+
bool getTime(const char *str)
45+
{
46+
int Hour, Min, Sec;
47+
48+
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
49+
tm.Hour = Hour;
50+
tm.Minute = Min;
51+
tm.Second = Sec;
52+
return true;
53+
}
54+
55+
bool getDate(const char *str)
56+
{
57+
char Month[12];
58+
int Day, Year;
59+
uint8_t monthIndex;
60+
61+
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
62+
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
63+
if (strcmp(Month, monthName[monthIndex]) == 0) break;
64+
}
65+
if (monthIndex >= 12) return false;
66+
tm.Day = Day;
67+
tm.Month = monthIndex + 1;
68+
tm.Year = CalendarYrToTm(Year);
69+
return true;
70+
}
71+

libraries/CurieRTC/readme.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Readme file for CurieRTC Library
2+
3+
The CurieRTC library is provided to demonstrate the Arduino101 Time library.
4+
5+
See the TimeCurieRTC example sketches provided with the Arduino101 Time library download for usage
6+
7+

libraries/Time/DateStrings.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* DateStrings.cpp
2+
* Definitions for date strings for use with the Time library
3+
*
4+
* Updated for Arduino 1.5.7 18 July 2014
5+
*
6+
* No memory is consumed in the sketch if your code does not call any of the string methods
7+
* You can change the text of the strings, make sure the short strings are each exactly 3 characters
8+
* the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in Time.h
9+
*
10+
*/
11+
12+
#if defined(__AVR__)
13+
#include <avr/pgmspace.h>
14+
#else
15+
// for compatiblity with Arduino Due and Teensy 3.0 and maybe others?
16+
#define PROGMEM
17+
#define PGM_P const char *
18+
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
19+
#define pgm_read_word(addr) (*(const unsigned char **)(addr))
20+
#define strcpy_P(dest, src) strcpy((dest), (src))
21+
#endif
22+
#include <string.h> // for strcpy_P or strcpy
23+
#include "Time.h"
24+
25+
// the short strings for each day or month must be exactly dt_SHORT_STR_LEN
26+
#define dt_SHORT_STR_LEN 3 // the length of short strings
27+
28+
static char buffer[dt_MAX_STRING_LEN+1]; // must be big enough for longest string and the terminating null
29+
30+
const char monthStr0[] PROGMEM = "";
31+
const char monthStr1[] PROGMEM = "January";
32+
const char monthStr2[] PROGMEM = "February";
33+
const char monthStr3[] PROGMEM = "March";
34+
const char monthStr4[] PROGMEM = "April";
35+
const char monthStr5[] PROGMEM = "May";
36+
const char monthStr6[] PROGMEM = "June";
37+
const char monthStr7[] PROGMEM = "July";
38+
const char monthStr8[] PROGMEM = "August";
39+
const char monthStr9[] PROGMEM = "September";
40+
const char monthStr10[] PROGMEM = "October";
41+
const char monthStr11[] PROGMEM = "November";
42+
const char monthStr12[] PROGMEM = "December";
43+
44+
const PROGMEM char * const PROGMEM monthNames_P[] =
45+
{
46+
monthStr0,monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,monthStr6,
47+
monthStr7,monthStr8,monthStr9,monthStr10,monthStr11,monthStr12
48+
};
49+
50+
const char monthShortNames_P[] PROGMEM = "ErrJanFebMarAprMayJunJulAugSepOctNovDec";
51+
52+
const char dayStr0[] PROGMEM = "Err";
53+
const char dayStr1[] PROGMEM = "Sunday";
54+
const char dayStr2[] PROGMEM = "Monday";
55+
const char dayStr3[] PROGMEM = "Tuesday";
56+
const char dayStr4[] PROGMEM = "Wednesday";
57+
const char dayStr5[] PROGMEM = "Thursday";
58+
const char dayStr6[] PROGMEM = "Friday";
59+
const char dayStr7[] PROGMEM = "Saturday";
60+
61+
const PROGMEM char * const PROGMEM dayNames_P[] =
62+
{
63+
dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7
64+
};
65+
66+
const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThuFriSat";
67+
68+
/* functions to return date strings */
69+
70+
char* monthStr(uint8_t month)
71+
{
72+
strcpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[month])));
73+
return buffer;
74+
}
75+
76+
char* monthShortStr(uint8_t month)
77+
{
78+
for (int i=0; i < dt_SHORT_STR_LEN; i++)
79+
buffer[i] = pgm_read_byte(&(monthShortNames_P[i+ (month*dt_SHORT_STR_LEN)]));
80+
buffer[dt_SHORT_STR_LEN] = 0;
81+
return buffer;
82+
}
83+
84+
char* dayStr(uint8_t day)
85+
{
86+
strcpy_P(buffer, (PGM_P)pgm_read_word(&(dayNames_P[day])));
87+
return buffer;
88+
}
89+
90+
char* dayShortStr(uint8_t day)
91+
{
92+
uint8_t index = day*dt_SHORT_STR_LEN;
93+
for (int i=0; i < dt_SHORT_STR_LEN; i++)
94+
buffer[i] = pgm_read_byte(&(dayShortNames_P[index + i]));
95+
buffer[dt_SHORT_STR_LEN] = 0;
96+
return buffer;
97+
}

0 commit comments

Comments
 (0)