Skip to content

Commit dc4fdf2

Browse files
authored
Merge pull request #68 from sparkfun/addRTC
Add RTC library and examples
2 parents 60dccb3 + 5b2d9f7 commit dc4fdf2

File tree

6 files changed

+315
-0
lines changed

6 files changed

+315
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Author: Nathan Seidle
2+
Created: Septempter 27th, 2019
3+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
4+
5+
This example demonstrates how to initialize and read from the on board RTC.
6+
Most SparkFun Artemis boards have the necessary external 32kHz crystal to
7+
enable the RTC. If you are using the Artemis module bare you will either
8+
need an external 32kHz xtal or use the internal LFRC. Read the datasheet
9+
section 12.1 for more information.
10+
11+
This example is based on the Ambiq SDK EVB2 RTC example.
12+
*/
13+
14+
#include "RTC.h" //Include RTC library included with the Aruino_Apollo3 core
15+
APM3_RTC myRTC; //Create instance of RTC class
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println("SparkFun RTC Example");
21+
22+
myRTC.setToCompilerTime(); //Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
23+
//myRTC.setTime(7, 28, 51, 0, 21, 10, 15); //Manually set RTC back to the future: Oct 21st, 2015 at 7:28.51 AM
24+
}
25+
26+
void loop()
27+
{
28+
myRTC.getTime();
29+
30+
Serial.printf("It is now ");
31+
Serial.printf("%d:", myRTC.hour);
32+
Serial.printf("%02d:", myRTC.minute);
33+
Serial.printf("%02d.", myRTC.seconds);
34+
Serial.printf("%02d", myRTC.hundredths);
35+
36+
Serial.printf(" %02d/", myRTC.month);
37+
Serial.printf("%02d/", myRTC.dayOfMonth);
38+
Serial.printf("%02d", myRTC.year);
39+
40+
Serial.printf(" Day of week: %d =", myRTC.weekday);
41+
Serial.printf(" %s", myRTC.textWeekday);
42+
43+
Serial.println();
44+
45+
delay(1000);
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* Author: Nathan Seidle
2+
Created: Septempter 27th, 2019
3+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
4+
5+
This example demonstrates how to put the core to sleep for a number of
6+
milliseconds before waking and printing the current time/date. This
7+
is helpful for checking power consumption of the core while RTC+CT6 are running.
8+
*/
9+
10+
#include "RTC.h" //Include RTC library included with the Aruino_Apollo3 core
11+
APM3_RTC myRTC; //Create instance of RTC class
12+
13+
uint32_t msToSleep = 2000; //This is the user editable number of ms to sleep between RTC checks
14+
#define TIMER_FREQ 3000000L //Counter/Timer 6 will use the HFRC oscillator of 3MHz
15+
uint32_t sysTicksToSleep = msToSleep * (TIMER_FREQ / 1000);
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println("SparkFun RTC Example");
21+
22+
myRTC.setToCompilerTime(); //Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
23+
//myRTC.setTime(7, 28, 51, 0, 21, 10, 15); //Manually set RTC back to the future: Oct 21st, 2015 at 7:28.51 AM
24+
25+
setupWakeTimer();
26+
}
27+
28+
void loop()
29+
{
30+
myRTC.getTime();
31+
32+
Serial.printf("It is now ");
33+
Serial.printf("%d:", myRTC.hour);
34+
Serial.printf("%02d:", myRTC.minute);
35+
Serial.printf("%02d.", myRTC.seconds);
36+
Serial.printf("%02d", myRTC.hundredths);
37+
38+
Serial.printf(" %02d/", myRTC.month);
39+
Serial.printf("%02d/", myRTC.dayOfMonth);
40+
Serial.printf("%02d", myRTC.year);
41+
42+
Serial.printf(" Day of week: %d =", myRTC.weekday);
43+
Serial.printf(" %s", myRTC.textWeekday);
44+
45+
Serial.println();
46+
47+
am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP); //Sleepy time
48+
}
49+
50+
//We use counter/timer 6 for this example but 0 to 7 are available
51+
//CT 7 is used for Software Serial. All CTs are used for Servo.
52+
void setupWakeTimer()
53+
{
54+
//Clear compare interrupt
55+
am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREG); //Use CT6
56+
57+
am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREG); // Enable C/T G=6
58+
59+
//Don't change from 3MHz system timer, but enable G timer
60+
am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
61+
am_hal_stimer_config(AM_HAL_STIMER_HFRC_3MHZ | AM_HAL_STIMER_CFG_COMPARE_G_ENABLE);
62+
63+
//Setup ISR to trigger when the number of ms have elapsed
64+
am_hal_stimer_compare_delta_set(6, sysTicksToSleep);
65+
66+
//Enable the timer interrupt in the NVIC.
67+
NVIC_EnableIRQ(STIMER_CMPR6_IRQn);
68+
}
69+
70+
//Called once number of milliseconds has passed
71+
extern "C" void am_stimer_cmpr6_isr(void)
72+
{
73+
uint32_t ui32Status = am_hal_stimer_int_status_get(false);
74+
if (ui32Status & AM_HAL_STIMER_INT_COMPAREG)
75+
{
76+
am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREG);
77+
78+
//Reset compare value. ISR will trigger when the number of ms have elapsed
79+
am_hal_stimer_compare_delta_set(6, sysTicksToSleep);
80+
}
81+
}

libraries/RTC/keywords.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#######################################
2+
# Syntax Coloring Map
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
RTC KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
getTime KEYWORD2
16+
setTime KEYWORD2
17+
setTimeToCompiler KEYWORD2
18+
19+
#######################################
20+
# Constants (LITERAL1)
21+
#######################################

libraries/RTC/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=RTC
2+
version=1.0
3+
author=SparkFun Electronics
4+
maintainer=SparkFun Electronics <sparkfun.com>
5+
sentence=Real Time Clock (RTC)) library for the SparkFun Artemis
6+
paragraph=Enables the setting and reading of the RTC hardware built into Apollo based modules like the Artemis. Many SparkFun Artemis carrier boards have a built in 32kHz crystals. This library enables the reading of the current date and time.
7+
category=Timing
8+
url=
9+
architectures=apollo3

libraries/RTC/src/RTC.cpp

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
This example is based on the Ambiq SDK EVB2 RTC example.
3+
*/
4+
5+
#include "RTC.h"
6+
7+
am_hal_rtc_time_t hal_time;
8+
9+
// String arrays to index Days and Months with the values returned by the RTC.
10+
char const *pcWeekday[] =
11+
{
12+
"Sunday",
13+
"Monday",
14+
"Tuesday",
15+
"Wednesday",
16+
"Thursday",
17+
"Friday",
18+
"Saturday",
19+
"Invalid day"};
20+
21+
char const *pcMonth[] =
22+
{
23+
"January",
24+
"February",
25+
"March",
26+
"April",
27+
"May",
28+
"June",
29+
"July",
30+
"August",
31+
"September",
32+
"October",
33+
"November",
34+
"December",
35+
"Invalid month"};
36+
37+
//Constructor
38+
APM3_RTC::APM3_RTC()
39+
{
40+
// Enable the XT for the RTC.
41+
am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_XTAL_START, 0);
42+
43+
// Select XT for RTC clock source
44+
am_hal_rtc_osc_select(AM_HAL_RTC_OSC_XT);
45+
46+
// Enable the RTC.
47+
am_hal_rtc_osc_enable();
48+
}
49+
50+
void APM3_RTC::setTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month, uint16_t year)
51+
{
52+
hal_time.ui32Hour = hour;
53+
hal_time.ui32Minute = min;
54+
hal_time.ui32Second = sec;
55+
hal_time.ui32Hundredths = hund;
56+
57+
hal_time.ui32DayOfMonth = dayOfMonth;
58+
hal_time.ui32Month = month - 1; //HAL is expecting 0 to 11 months
59+
hal_time.ui32Year = year;
60+
hal_time.ui32Century = 0;
61+
62+
hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + year, month + 1, dayOfMonth);
63+
64+
am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time
65+
}
66+
67+
//Takes the time from the last build and uses it as the current time
68+
//Works well as an arduino sketch
69+
void APM3_RTC::setToCompilerTime()
70+
{
71+
//Get the current date/time from the compiler
72+
//Alternatively, you can set these values manually
73+
hal_time.ui32Hour = toVal(&__TIME__[0]);
74+
hal_time.ui32Minute = toVal(&__TIME__[3]);
75+
hal_time.ui32Second = toVal(&__TIME__[6]);
76+
hal_time.ui32Hundredths = 00;
77+
hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + toVal(&__DATE__[9]), mthToIndex(&__DATE__[0]) + 1, toVal(&__DATE__[4]));
78+
hal_time.ui32DayOfMonth = toVal(&__DATE__[4]);
79+
hal_time.ui32Month = mthToIndex(&__DATE__[0]);
80+
hal_time.ui32Year = toVal(&__DATE__[9]);
81+
hal_time.ui32Century = 0;
82+
83+
am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time
84+
}
85+
86+
void APM3_RTC::getTime()
87+
{
88+
am_hal_rtc_time_get(&hal_time);
89+
90+
hour = hal_time.ui32Hour;
91+
minute = hal_time.ui32Minute;
92+
seconds = hal_time.ui32Second;
93+
hundredths = hal_time.ui32Hundredths;
94+
95+
month = hal_time.ui32Month + 1; //Convert from 0-11 to 1-12
96+
dayOfMonth = hal_time.ui32DayOfMonth;
97+
year = hal_time.ui32Year;
98+
99+
weekday = hal_time.ui32Weekday;
100+
textWeekday = pcWeekday[hal_time.ui32Weekday]; //Given a number (day of week) return the string that represents the name
101+
}
102+
103+
// mthToIndex() converts a string indicating a month to an index value.
104+
// The return value is a value 0-12, with 0-11 indicating the month given
105+
// by the string, and 12 indicating that the string is not a month.
106+
int APM3_RTC::mthToIndex(char const *pcMon)
107+
{
108+
int idx;
109+
for (idx = 0; idx < 12; idx++)
110+
{
111+
if (am_util_string_strnicmp(pcMonth[idx], pcMon, 3) == 0)
112+
return idx;
113+
}
114+
return 12; //Error
115+
}
116+
117+
// toVal() converts a string to an ASCII value.
118+
int APM3_RTC::toVal(char const *pcAsciiStr)
119+
{
120+
int iRetVal = 0;
121+
iRetVal += pcAsciiStr[1] - '0';
122+
iRetVal += pcAsciiStr[0] == ' ' ? 0 : (pcAsciiStr[0] - '0') * 10;
123+
return iRetVal;
124+
}

libraries/RTC/src/RTC.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef RTC_H
2+
#define RTC_H
3+
4+
#include <Arduino.h>
5+
6+
class APM3_RTC
7+
{
8+
public:
9+
APM3_RTC();
10+
11+
void getTime(); //Query the RTC for the current time/date. Loads .seconds, .minute, etc.
12+
void setTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund,
13+
uint8_t dayOfMonth, uint8_t month, uint16_t year); //Set current time to provided hundredths/seconds/etc
14+
void setToCompilerTime(); //Set to time when sketch was compiled
15+
16+
uint32_t hour;
17+
uint32_t minute;
18+
uint32_t seconds;
19+
uint32_t hundredths;
20+
21+
uint32_t dayOfMonth;
22+
uint32_t month;
23+
uint32_t year;
24+
uint32_t century;
25+
26+
uint32_t weekday; //0 to 6 representing the day of the week
27+
const char *textWeekday;
28+
29+
private:
30+
//Helper functions to convert compiler date/time to ints
31+
int toVal(char const *pcAsciiStr);
32+
int mthToIndex(char const *pcMon);
33+
};
34+
#endif //RTC_H

0 commit comments

Comments
 (0)