Skip to content

Commit e36cc61

Browse files
committed
Adding basic RTC functions
1 parent 60dccb3 commit e36cc61

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

libraries/RTC/keywords.txt

Lines changed: 21 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 124 additions & 0 deletions
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

Lines changed: 34 additions & 0 deletions
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)