Skip to content

Commit 2cc5ab4

Browse files
author
Owen
authoredJul 16, 2020
Merge pull request #234 from sparkfun/release-candidate
2 parents 1287ee0 + 8dbc5d1 commit 2cc5ab4

File tree

11 files changed

+587
-134
lines changed

11 files changed

+587
-134
lines changed
 

‎libraries/RTC/examples/Example1_getTime/Example1_getTime.ino renamed to ‎libraries/RTC/examples/Example1_Get_Time/Example1_Get_Time.ino

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
/* Author: Nathan Seidle
1+
/*
2+
Author: Nathan Seidle
23
Created: Septempter 27th, 2019
34
License: MIT. See SparkFun Arduino Apollo3 Project for more information
45
5-
This example demonstrates how to initialize and read from the on board RTC.
6+
This example demonstrates how to initialize and read from the on-board RTC.
7+
68
Most SparkFun Artemis boards have the necessary external 32kHz crystal to
79
enable the RTC. If you are using the Artemis module bare you will either
810
need an external 32kHz xtal or use the internal LFRC. Read the datasheet
@@ -11,16 +13,19 @@
1113
This example is based on the Ambiq SDK EVB2 RTC example.
1214
*/
1315

14-
#include "RTC.h" //Include RTC library included with the Aruino_Apollo3 core
15-
APM3_RTC myRTC; //Create instance of RTC class
16+
#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core
17+
APM3_RTC myRTC; // Create instance of RTC class
1618

1719
void setup()
1820
{
1921
Serial.begin(115200);
2022
Serial.println("SparkFun RTC Example");
2123

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+
// Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
25+
myRTC.setToCompilerTime();
26+
27+
// Manually set RTC date and time
28+
//myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy)
2429
}
2530

2631
void loop()

‎libraries/RTC/examples/Example2_RTCwithSleep/Example2_RTCwithSleep.ino

-81
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Author: Adam Garbo and Nathan Seidle
3+
Created: June 3rd, 2020
4+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
5+
6+
This example demonstrates how to read and set the RTC alarms.
7+
8+
It is necessary to first set the RTC alarm date and time and then specify
9+
the alarm mode, which determines which date and time values will be used
10+
for comparison when generating an alarm interrupt.
11+
12+
The code is configured so that the RTC alarm will trigger every minute.
13+
The RTC interrupt service routine will set an alarm flag each time the
14+
alarm triggers and the RTC date and time will printed to the Serial Monitor.
15+
*/
16+
17+
#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core
18+
APM3_RTC myRTC; // Create instance of RTC class
19+
20+
volatile bool alarmFlag = false;
21+
22+
void setup()
23+
{
24+
Serial.begin(115200);
25+
Serial.println("SparkFun RTC Set Alarm Example");
26+
27+
// Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
28+
//myRTC.setToCompilerTime();
29+
30+
// Manually set RTC date and time
31+
myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy)
32+
33+
// Set the RTC's alarm
34+
myRTC.setAlarm(13, 0, 0, 0, 3, 6); // 13:00:00.000, June 3rd (hund, ss, mm, hh, dd, mm). Note: No year alarm register
35+
// Set the RTC alarm mode
36+
/*
37+
0: Alarm interrupt disabled
38+
1: Alarm match every year (hundredths, seconds, minutes, hour, day, month)
39+
2: Alarm match every month (hundredths, seconds, minutes, hours, day)
40+
3: Alarm match every week (hundredths, seconds, minutes, hours, weekday)
41+
4: Alarm match every day (hundredths, seconds, minute, hours)
42+
5: Alarm match every hour (hundredths, seconds, minutes)
43+
6: Alarm match every minute (hundredths, seconds)
44+
7: Alarm match every second (hundredths)
45+
*/
46+
myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover
47+
myRTC.attachInterrupt(); // Attach RTC alarm interrupt
48+
49+
// Print the RTC's alarm date and time
50+
Serial.print("Next alarm: "); printAlarm();
51+
}
52+
53+
void loop()
54+
{
55+
// Check if alarm flag was set
56+
if (alarmFlag == true)
57+
{
58+
// Print date and time of RTC alarm trigger
59+
Serial.print("Alarm triggered: "); printDateTime();
60+
61+
// Clear alarm flag
62+
alarmFlag = false;
63+
}
64+
65+
// Print RTC's date and time while waiting for alarm
66+
static uint32_t nextPrint = 0;
67+
if(millis() > nextPrint){
68+
printDateTime();
69+
nextPrint = millis() + 1000;
70+
}
71+
}
72+
73+
// Print the RTC's current date and time
74+
void printDateTime()
75+
{
76+
myRTC.getTime();
77+
char dateTimeBuffer[25];
78+
sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d",
79+
myRTC.year, myRTC.month, myRTC.dayOfMonth,
80+
myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths);
81+
Serial.println(dateTimeBuffer);
82+
}
83+
84+
// Print the RTC's alarm
85+
void printAlarm()
86+
{
87+
myRTC.getAlarm();
88+
char alarmBuffer[25];
89+
sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d",
90+
myRTC.alarmMonth, myRTC.alarmDayOfMonth,
91+
myRTC.alarmHour, myRTC.alarmMinute,
92+
myRTC.alarmSeconds, myRTC.alarmHundredths);
93+
Serial.println(alarmBuffer);
94+
}
95+
96+
// Interrupt handler for the RTC
97+
extern "C" void am_rtc_isr(void)
98+
{
99+
// Clear the RTC alarm interrupt.
100+
am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM);
101+
102+
// Set alarm flag
103+
alarmFlag = true;
104+
}

‎libraries/RTC/examples/Example3_TestRTC/Example3_TestRTC.ino renamed to ‎libraries/RTC/examples/Example3_Test_RTC/Example3_Test_RTC.ino

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
/* Author: Nathan Seidle and stephenf7072
1+
/*
2+
Author: Nathan Seidle and stephenf7072
23
Created: January 28th, 2020
34
License: MIT. See SparkFun Arduino Apollo3 Project for more information
45
56
This example test the internal HAL to make sure the days advance correctly.
67
*/
78

89
#include "RTC.h"
9-
APM3_RTC myRTC; //Create instance of RTC class
10+
APM3_RTC myRTC; // Create instance of RTC class
1011

11-
int previousDay = 1;
12+
int previousDay;
1213

1314
void setup()
1415
{
1516
Serial.begin(115200);
1617
delay(10);
17-
Serial.println("Artemis RTC testing");
18+
Serial.println("Artemis RTC Testing");
1819

19-
//myRTC.setTime(hh, mm, ss, hund, dd, mm, yy);
20-
myRTC.setTime(23, 59, 59, 99, 1, 1, 19); //Manually set RTC to 1s before midnight
20+
// Manually set RTC date and time to the start of 2020 so that myRTC contains valid times
21+
myRTC.setTime(23, 59, 59, 0, 1, 1, 20); // Set to 1 second before midnight Jan 1
2122
}
2223

2324
void loop()
2425
{
25-
printArtemisTime();
26+
myRTC.setTime(23, 59, 59, 99, myRTC.dayOfMonth, myRTC.month, myRTC.year); // Manually set RTC 1/100th of a second from the next day
27+
previousDay = myRTC.weekday;
28+
delay(11); //Allow us to roll from midnight the night before to the new day
2629

27-
myRTC.getTime();
28-
myRTC.setTime(23, 59, 59, 99, myRTC.dayOfMonth, myRTC.month, myRTC.year); //Manually set RTC
29-
delay(11); //Allow us to roll from midnight the night before to the new day
30+
printArtemisTime();
3031
}
3132

3233
void printArtemisTime()
@@ -76,7 +77,5 @@ void printArtemisTime()
7677
;
7778
}
7879

79-
previousDay = myRTC.weekday;
80-
8180
Serial.println();
8281
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Author: Adam Garbo and Nathan Seidle
3+
Created: June 3rd, 2020
4+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
5+
6+
This example demonstrates how to set the RTC using UNIX Epoch time.
7+
*/
8+
9+
#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core
10+
APM3_RTC myRTC; // Create instance of RTC class
11+
12+
void setup()
13+
{
14+
Serial.begin(115200);
15+
Serial.println("SparkFun RTC Set UNIX Epoch Time Example");
16+
17+
// Set the RTC time using UNIX Epoch time
18+
myRTC.setEpoch(1591185600); // E.g. 12:00:00, June 3rd, 2020
19+
}
20+
21+
void loop()
22+
{
23+
// Print UNIX Epoch timestamp
24+
Serial.print("Epoch time: "); Serial.println(myRTC.getEpoch());
25+
26+
// Print RTC's date and time
27+
Serial.print("Timestamp: "); printDateTime();
28+
29+
delay(1000);
30+
}
31+
32+
// Print the RTC's current date and time
33+
void printDateTime()
34+
{
35+
myRTC.getTime();
36+
char dateTime[20];
37+
sprintf(dateTime, "20%02d-%02d-%02d %02d:%02d:%02d",
38+
myRTC.year, myRTC.month, myRTC.dayOfMonth,
39+
myRTC.hour, myRTC.minute, myRTC.seconds);
40+
Serial.println(dateTime);
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Author: Adam Garbo and Nathan Seidle
3+
Created: June 3rdrd, 2020
4+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
5+
6+
This example demonstrates how to read and set rolling RTC alarms. Each time
7+
the alarm triggers, a user-specified additional amount of time (seconds,
8+
minutes or hours) can be added to create a rolling RTC alarm. Second,
9+
minute and hour rollovers are handled using modulo calculations.
10+
11+
The current code is configured as a 5-second rolling RTC alarm.
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+
volatile bool alarmFlag = false;
18+
int alarmSeconds = 5;
19+
int alarmMinutes = 0;
20+
int alarmHours = 0;
21+
22+
void setup()
23+
{
24+
Serial.begin(115200);
25+
Serial.println("SparkFun RTC Set Rolling Alarms Example");
26+
27+
// Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
28+
//myRTC.setToCompilerTime();
29+
30+
// Manually set RTC date and time
31+
myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy)
32+
33+
// Set the RTC's alarm
34+
myRTC.setAlarm(13, 0, 0, 0, 3, 6); // 13:00:00.000, June 3rd (hund, ss, mm, hh, dd, mm). Note: No year alarm register
35+
36+
// Set the RTC alarm mode
37+
/*
38+
0: Alarm interrupt disabled
39+
1: Alarm match every year (hundredths, seconds, minutes, hour, day, month)
40+
2: Alarm match every month (hundredths, seconds, minutes, hours, day)
41+
3: Alarm match every week (hundredths, seconds, minutes, hours, weekday)
42+
4: Alarm match every day (hundredths, seconds, minute, hours)
43+
5: Alarm match every hour (hundredths, seconds, minutes)
44+
6: Alarm match every minute (hundredths, seconds)
45+
7: Alarm match every second (hundredths)
46+
*/
47+
myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover
48+
myRTC.attachInterrupt(); // Attach RTC alarm interrupt
49+
50+
// Print the RTC's alarm date and time
51+
Serial.print("Next alarm: "); printAlarm();
52+
}
53+
54+
void loop()
55+
{
56+
// Check if alarm flag was set
57+
if (alarmFlag == true)
58+
{
59+
// Print date and time of RTC alarm trigger
60+
Serial.print("Alarm triggered: "); printDateTime();
61+
62+
// Clear alarm flag
63+
alarmFlag = false;
64+
65+
// Set the RTC's rolling alarm
66+
myRTC.setAlarm((myRTC.hour + alarmHours) % 24,
67+
(myRTC.minute + alarmMinutes) % 60,
68+
(myRTC.seconds + alarmSeconds) % 60,
69+
0,
70+
myRTC.dayOfMonth,
71+
myRTC.month);
72+
myRTC.setAlarmMode(6);
73+
74+
// Print next RTC alarm date and time
75+
Serial.print("Next rolling alarm: "); printAlarm();
76+
}
77+
}
78+
79+
// Print the RTC's current date and time
80+
void printDateTime()
81+
{
82+
myRTC.getTime();
83+
char dateTimeBuffer[25];
84+
sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d",
85+
myRTC.year, myRTC.month, myRTC.dayOfMonth,
86+
myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths);
87+
Serial.println(dateTimeBuffer);
88+
}
89+
90+
// Print the RTC's alarm
91+
void printAlarm()
92+
{
93+
myRTC.getAlarm();
94+
char alarmBuffer[25];
95+
sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d",
96+
myRTC.alarmMonth, myRTC.alarmDayOfMonth,
97+
myRTC.alarmHour, myRTC.alarmMinute,
98+
myRTC.alarmSeconds, myRTC.alarmHundredths);
99+
Serial.println(alarmBuffer);
100+
}
101+
102+
// Interrupt handler for the RTC
103+
extern "C" void am_rtc_isr(void)
104+
{
105+
// Clear the RTC alarm interrupt.
106+
am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM);
107+
108+
// Set alarm flag
109+
alarmFlag = true;
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
Author: Adam Garbo and Nathan Seidle
3+
Created: June 3rd, 2020
4+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
5+
6+
This example demonstrates how to set an RTC alarm and enter deep sleep.
7+
8+
The code is configured to set an RTC alarm every minute and enter
9+
deep sleep between interrupts. The RTC interrupt service routine will
10+
wake the board and print the date and time upon each alarm interrupt.
11+
12+
Tested with a SparkFun Edge 2. Confirmed sleep current of 2.5 uA.
13+
*/
14+
15+
#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core
16+
APM3_RTC myRTC; // Create instance of RTC class
17+
18+
void setup()
19+
{
20+
Serial.begin(115200);
21+
Serial.println("SparkFun RTC Low-power Alarm Example");
22+
23+
// Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
24+
//myRTC.setToCompilerTime();
25+
26+
// Manually set RTC date and time
27+
myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy)
28+
29+
// Set the RTC's alarm
30+
myRTC.setAlarm(13, 0, 0, 0, 3, 6); // 13:00:00.000, June 3rd (hund, ss, mm, hh, dd, mm). Note: No year alarm register
31+
32+
// Set the RTC alarm mode
33+
/*
34+
0: Alarm interrupt disabled
35+
1: Alarm match every year (hundredths, seconds, minutes, hour, day, month)
36+
2: Alarm match every month (hundredths, seconds, minutes, hours, day)
37+
3: Alarm match every week (hundredths, seconds, minutes, hours, weekday)
38+
4: Alarm match every day (hundredths, seconds, minute, hours)
39+
5: Alarm match every hour (hundredths, seconds, minutes)
40+
6: Alarm match every minute (hundredths, seconds)
41+
7: Alarm match every second (hundredths)
42+
*/
43+
myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover
44+
myRTC.attachInterrupt(); // Attach RTC alarm interrupt
45+
}
46+
47+
void loop()
48+
{
49+
// Print date and time of RTC alarm trigger
50+
Serial.print("Alarm interrupt: "); printDateTime();
51+
52+
// Enter deep sleep and await RTC alarm interrupt
53+
goToSleep();
54+
}
55+
56+
// Print the RTC's current date and time
57+
void printDateTime()
58+
{
59+
myRTC.getTime();
60+
char dateTimeBuffer[25];
61+
sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d",
62+
myRTC.year, myRTC.month, myRTC.dayOfMonth,
63+
myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths);
64+
Serial.println(dateTimeBuffer);
65+
}
66+
67+
// Power down gracefully
68+
void goToSleep()
69+
{
70+
// Disable UART
71+
Serial.end();
72+
73+
// Disable ADC
74+
power_adc_disable();
75+
76+
// Force the peripherals off
77+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM0);
78+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM1);
79+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM2);
80+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM3);
81+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM4);
82+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM5);
83+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_ADC);
84+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_UART0);
85+
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_UART1);
86+
87+
// Disable all pads
88+
for (int x = 0 ; x < 50 ; x++)
89+
am_hal_gpio_pinconfig(x, g_AM_HAL_GPIO_DISABLE);
90+
91+
//Power down Flash, SRAM, cache
92+
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_CACHE); // Turn off CACHE
93+
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_FLASH_512K); // Turn off everything but lower 512k
94+
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_SRAM_64K_DTCM); // Turn off everything but lower 64k
95+
//am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_ALL); //Turn off all memory (doesn't recover)
96+
97+
// Keep the 32kHz clock running for RTC
98+
am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
99+
am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ);
100+
101+
am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP); // Sleep forever
102+
103+
// And we're back!
104+
wakeUp();
105+
}
106+
107+
// Power up gracefully
108+
void wakeUp()
109+
{
110+
// Power up SRAM, turn on entire Flash
111+
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_MAX);
112+
113+
// Go back to using the main clock
114+
am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
115+
am_hal_stimer_config(AM_HAL_STIMER_HFRC_3MHZ);
116+
117+
// Enable ADC
118+
ap3_adc_setup();
119+
120+
// Enable Serial
121+
Serial.begin(115200);
122+
}
123+
124+
// Interrupt handler for the RTC
125+
extern "C" void am_rtc_isr(void)
126+
{
127+
// Clear the RTC alarm interrupt
128+
am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM);
129+
}

‎libraries/RTC/keywords.txt

+22-5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,36 @@ RTC KEYWORD1
1313
#######################################
1414

1515
getTime KEYWORD2
16+
getEpoch KEYWORD2
17+
1618
setTime KEYWORD2
1719
setTimeToCompiler KEYWORD2
20+
setEpoch KEYWORD2
21+
22+
getAlarm KEYWORD2
23+
setAlarm KEYWORD2
24+
setAlarmMode KEYWORD2
25+
attachInterrupt KEYWORD2
26+
detachInterrupt KEYWORD2
1827

28+
weekday KEYWORD2
29+
textWeekday KEYWORD2
30+
year KEYWORD2
31+
month KEYWORD2
32+
dayOfMonth KEYWORD2
1933
hour KEYWORD2
2034
minute KEYWORD2
2135
seconds KEYWORD2
2236
hundredths KEYWORD2
2337

24-
month KEYWORD2
25-
dayOfMonth KEYWORD2
26-
year KEYWORD2
27-
weekday KEYWORD2
28-
textWeekday KEYWORD2
38+
alarmWeekday KEYWORD2
39+
alarmMonth KEYWORD2
40+
alarmDayOfMonth KEYWORD2
41+
alarmHour KEYWORD2
42+
alarmMinute KEYWORD2
43+
alarmSeconds KEYWORD2
44+
alarmHundredths KEYWORD2
45+
alarmTextWeekday KEYWORD2
2946

3047
#######################################
3148
# Constants (LITERAL1)

‎libraries/RTC/library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=RTC
2-
version=1.0
2+
version=1.2
33
author=SparkFun Electronics
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Real Time Clock (RTC)) library for the SparkFun Artemis

‎libraries/RTC/src/RTC.cpp

+133-20
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
*/
44

55
#include "RTC.h"
6+
#include <time.h>
67

78
am_hal_rtc_time_t hal_time;
9+
am_hal_rtc_time_t alm_time;
810

9-
// String arrays to index Days and Months with the values returned by the RTC.
11+
#define EPOCH_TIME 946684800 // UNIX Epoch time = 2000-01-01 00:00:00
12+
13+
//String arrays to index Days and Months with the values returned by the RTC.
1014
char const *pcWeekday[] =
1115
{
1216
"Sunday",
@@ -37,31 +41,32 @@ char const *pcMonth[] =
3741
//Constructor
3842
APM3_RTC::APM3_RTC()
3943
{
40-
// Enable the XT for the RTC.
44+
//Enable the XT for the RTC.
4145
am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_XTAL_START, 0);
4246

43-
// Select XT for RTC clock source
47+
//Select XT for RTC clock source
4448
am_hal_rtc_osc_select(AM_HAL_RTC_OSC_XT);
4549

46-
// Enable the RTC.
50+
//Enable the RTC.
4751
am_hal_rtc_osc_enable();
4852
}
4953

54+
55+
// Note: Order of parameters to change in v2.0.0.
5056
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)
5157
{
58+
hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + year, month, dayOfMonth); //computeDayofWeek is expecting 1 to 12 months
59+
hal_time.ui32Century = 0;
60+
hal_time.ui32Year = year;
61+
hal_time.ui32Month = month; //HAL is expecting 1 to 12 months
62+
hal_time.ui32DayOfMonth = dayOfMonth;
5263
hal_time.ui32Hour = hour;
5364
hal_time.ui32Minute = min;
5465
hal_time.ui32Second = sec;
5566
hal_time.ui32Hundredths = hund;
5667

57-
hal_time.ui32DayOfMonth = dayOfMonth;
58-
hal_time.ui32Month = month; //HAL is expecting 1 to 12 months
59-
hal_time.ui32Year = year;
60-
hal_time.ui32Century = 0;
61-
62-
hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + year, month, dayOfMonth); //computeDayofWeek is expecting 1 to 12 months
63-
6468
am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time
69+
getTime();
6570
}
6671

6772
//Takes the time from the last build and uses it as the current time
@@ -70,15 +75,39 @@ void APM3_RTC::setToCompilerTime()
7075
{
7176
//Get the current date/time from the compiler
7277
//Alternatively, you can set these values manually
78+
hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + toVal(&__DATE__[9]), mthToIndex(&__DATE__[0]) + 1, toVal(&__DATE__[4]));
79+
hal_time.ui32Century = 0;
80+
hal_time.ui32Year = toVal(&__DATE__[9]);
81+
hal_time.ui32Month = mthToIndex(&__DATE__[0]) + 1; //Compiler ouputs months in 0-11.
82+
hal_time.ui32DayOfMonth = toVal(&__DATE__[4]);
7383
hal_time.ui32Hour = toVal(&__TIME__[0]);
7484
hal_time.ui32Minute = toVal(&__TIME__[3]);
7585
hal_time.ui32Second = toVal(&__TIME__[6]);
7686
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]) + 1; //Compiler ouputs months in 0-11.
80-
hal_time.ui32Year = toVal(&__DATE__[9]);
87+
88+
am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time
89+
getTime();
90+
}
91+
92+
void APM3_RTC::setEpoch(uint32_t ts)
93+
{
94+
if (ts < EPOCH_TIME) {
95+
ts = EPOCH_TIME;
96+
}
97+
98+
struct tm tm;
99+
100+
time_t t = ts;
101+
struct tm* tmp = gmtime(&t);
102+
hal_time.ui32Weekday = 0;
81103
hal_time.ui32Century = 0;
104+
hal_time.ui32Year = tmp->tm_year - 100;
105+
hal_time.ui32Month = tmp->tm_mon + 1;
106+
hal_time.ui32DayOfMonth = tmp->tm_mday;
107+
hal_time.ui32Hour = tmp->tm_hour;
108+
hal_time.ui32Minute = tmp->tm_min;
109+
hal_time.ui32Second = tmp->tm_sec;
110+
hal_time.ui32Hundredths = 0;
82111

83112
am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time
84113
}
@@ -87,17 +116,101 @@ void APM3_RTC::getTime()
87116
{
88117
am_hal_rtc_time_get(&hal_time);
89118

119+
weekday = hal_time.ui32Weekday;
120+
textWeekday = pcWeekday[hal_time.ui32Weekday]; //Given a number (day of week) return the string that represents the name
121+
year = hal_time.ui32Year;
122+
month = hal_time.ui32Month; //HAL outputs months in 1 to 12 form
123+
dayOfMonth = hal_time.ui32DayOfMonth;
90124
hour = hal_time.ui32Hour;
91125
minute = hal_time.ui32Minute;
92126
seconds = hal_time.ui32Second;
93127
hundredths = hal_time.ui32Hundredths;
128+
}
94129

95-
month = hal_time.ui32Month; //HAL outputs months in 1 to 12 form
96-
dayOfMonth = hal_time.ui32DayOfMonth;
97-
year = hal_time.ui32Year;
130+
uint32_t APM3_RTC::getEpoch()
131+
{
132+
am_hal_rtc_time_get(&hal_time);
98133

99-
weekday = hal_time.ui32Weekday;
100-
textWeekday = pcWeekday[hal_time.ui32Weekday]; //Given a number (day of week) return the string that represents the name
134+
struct tm tm;
135+
136+
tm.tm_isdst = -1;
137+
tm.tm_yday = 0;
138+
tm.tm_wday = 0;
139+
tm.tm_year = hal_time.ui32Year + 100; //Number of years since 1900.
140+
tm.tm_mon = hal_time.ui32Month - 1; //mktime is expecting 0 to 11 months
141+
tm.tm_mday = hal_time.ui32DayOfMonth;
142+
tm.tm_hour = hal_time.ui32Hour;
143+
tm.tm_min = hal_time.ui32Minute;
144+
tm.tm_sec = hal_time.ui32Second;
145+
146+
return mktime(&tm);
147+
}
148+
149+
void APM3_RTC::getAlarm()
150+
{
151+
am_hal_rtc_alarm_get(&alm_time); //Get the RTC's alarm time
152+
153+
alarmWeekday = alm_time.ui32Weekday;
154+
alarmMonth = alm_time.ui32Month; //HAL outputs months in 1 to 12 form
155+
alarmDayOfMonth = alm_time.ui32DayOfMonth;
156+
alarmHour = alm_time.ui32Hour;
157+
alarmMinute = alm_time.ui32Minute;
158+
alarmSeconds = alm_time.ui32Second;
159+
alarmHundredths = alm_time.ui32Hundredths;
160+
}
161+
162+
// Note: Order of parameters to change in v2.0.0.
163+
void APM3_RTC::setAlarm(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month)
164+
{
165+
alm_time.ui32Weekday = 0; // WIP
166+
alm_time.ui32Month = month;
167+
alm_time.ui32DayOfMonth = dayOfMonth;
168+
alm_time.ui32Hour = hour;
169+
alm_time.ui32Minute = min;
170+
alm_time.ui32Second = sec;
171+
alm_time.ui32Hundredths = hund;
172+
173+
am_hal_rtc_alarm_set(&alm_time, AM_HAL_RTC_ALM_RPT_DIS); //Initialize the RTC alarm with this date/time
174+
}
175+
176+
// Sets the RTC alarm repeat interval.
177+
/*
178+
RTC alarm repeat intervals
179+
0: AM_HAL_RTC_ALM_RPT_DIS Alarm interrupt disabled
180+
1: AM_HAL_RTC_ALM_RPT_YR Interrupt every year
181+
2: AM_HAL_RTC_ALM_RPT_MTH Interrupt every month
182+
3: AM_HAL_RTC_ALM_RPT_WK Interrupt every week
183+
4: AM_HAL_RTC_ALM_RPT_DAY Interrupt every day
184+
5: AM_HAL_RTC_ALM_RPT_HR Interrupt every hour
185+
6: AM_HAL_RTC_ALM_RPT_MIN Interrupt every minute
186+
7: AM_HAL_RTC_ALM_RPT_SEC Interrupt every second
187+
8: AM_HAL_RTC_ALM_RPT_10TH Interrupt every 10th second (unused)
188+
9: AM_HAL_RTC_ALM_RPT_100TH Interrupt every 100th second (unused)
189+
*/
190+
void APM3_RTC::setAlarmMode(uint8_t mode)
191+
{
192+
am_hal_rtc_alarm_interval_set(mode);
193+
}
194+
195+
void APM3_RTC::attachInterrupt()
196+
{
197+
// Enable the RTC interrupt.
198+
am_hal_rtc_int_enable(AM_HAL_RTC_INT_ALM);
199+
200+
// Clear the RTC interrupt.
201+
am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM);
202+
203+
// Enable RTC interrupts to the NVIC.
204+
NVIC_EnableIRQ(RTC_IRQn);
205+
}
206+
207+
void APM3_RTC::detachInterrupt()
208+
{
209+
// Disable RTC interrupts to the NVIC.
210+
NVIC_DisableIRQ(RTC_IRQn);
211+
212+
// Disable the RTC interrupt.
213+
am_hal_rtc_int_disable(AM_HAL_RTC_INT_ALM);
101214
}
102215

103216
// mthToIndex() converts a string indicating a month to an index value.

‎libraries/RTC/src/RTC.h

+25-9
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,40 @@ class APM3_RTC
88
public:
99
APM3_RTC();
1010

11-
void getTime(); //Query the RTC for the current time/date. Loads .seconds, .minute, etc.
11+
void getTime(); //Query the RTC for the current time/date
12+
uint32_t getEpoch(); //Return the current RTC time/date as UNIX Epoch time
13+
1214
void setTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund,
1315
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
16+
void setToCompilerTime(); //Set to time when sketch was compiled
17+
void setEpoch(uint32_t ts); //Set current time to provided UNIX Epoch time
1518

19+
void getAlarm(); //Query the RTC for the current alarm time/date
20+
void setAlarm(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month); //Set alarm time to provided hundredths/seconds/etc
21+
void setAlarmMode(uint8_t mode); //Set the RTC alarm repeat interval
22+
void attachInterrupt(); //Attach the RTC alarm interrupt
23+
void detachInterrupt(); //Detach the RTC alarm interrupt
24+
25+
uint32_t weekday; //0 to 6 representing the day of the week
26+
uint32_t century;
27+
uint32_t year;
28+
uint32_t month;
29+
uint32_t dayOfMonth;
1630
uint32_t hour;
1731
uint32_t minute;
1832
uint32_t seconds;
1933
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
2734
const char *textWeekday;
2835

36+
uint32_t alarmWeekday; //0 to 6 representing the day of the week
37+
uint32_t alarmMonth;
38+
uint32_t alarmDayOfMonth;
39+
uint32_t alarmHour;
40+
uint32_t alarmMinute;
41+
uint32_t alarmSeconds;
42+
uint32_t alarmHundredths;
43+
const char *alarmTextWeekday;
44+
2945
private:
3046
//Helper functions to convert compiler date/time to ints
3147
int toVal(char const *pcAsciiStr);

0 commit comments

Comments
 (0)
Please sign in to comment.