diff --git a/libraries/RTC/examples/Example1_getTime/Example1_getTime.ino b/libraries/RTC/examples/Example1_Get_Time/Example1_Get_Time.ino similarity index 71% rename from libraries/RTC/examples/Example1_getTime/Example1_getTime.ino rename to libraries/RTC/examples/Example1_Get_Time/Example1_Get_Time.ino index 766daaac..f0aa8607 100644 --- a/libraries/RTC/examples/Example1_getTime/Example1_getTime.ino +++ b/libraries/RTC/examples/Example1_Get_Time/Example1_Get_Time.ino @@ -1,8 +1,10 @@ -/* Author: Nathan Seidle +/* + Author: Nathan Seidle Created: Septempter 27th, 2019 License: MIT. See SparkFun Arduino Apollo3 Project for more information - This example demonstrates how to initialize and read from the on board RTC. + This example demonstrates how to initialize and read from the on-board RTC. + Most SparkFun Artemis boards have the necessary external 32kHz crystal to enable the RTC. If you are using the Artemis module bare you will either need an external 32kHz xtal or use the internal LFRC. Read the datasheet @@ -11,16 +13,19 @@ This example is based on the Ambiq SDK EVB2 RTC example. */ -#include "RTC.h" //Include RTC library included with the Aruino_Apollo3 core -APM3_RTC myRTC; //Create instance of RTC class +#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core +APM3_RTC myRTC; // Create instance of RTC class void setup() { Serial.begin(115200); Serial.println("SparkFun RTC Example"); - myRTC.setToCompilerTime(); //Easily set RTC using the system __DATE__ and __TIME__ macros from compiler - //myRTC.setTime(7, 28, 51, 0, 21, 10, 15); //Manually set RTC back to the future: Oct 21st, 2015 at 7:28.51 AM + // Easily set RTC using the system __DATE__ and __TIME__ macros from compiler + myRTC.setToCompilerTime(); + + // Manually set RTC date and time + //myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy) } void loop() diff --git a/libraries/RTC/examples/Example2_RTCwithSleep/Example2_RTCwithSleep.ino b/libraries/RTC/examples/Example2_RTCwithSleep/Example2_RTCwithSleep.ino deleted file mode 100644 index 0103a64c..00000000 --- a/libraries/RTC/examples/Example2_RTCwithSleep/Example2_RTCwithSleep.ino +++ /dev/null @@ -1,81 +0,0 @@ -/* Author: Nathan Seidle - Created: Septempter 27th, 2019 - License: MIT. See SparkFun Arduino Apollo3 Project for more information - - This example demonstrates how to put the core to sleep for a number of - milliseconds before waking and printing the current time/date. This - is helpful for checking power consumption of the core while RTC+CT6 are running. -*/ - -#include "RTC.h" //Include RTC library included with the Aruino_Apollo3 core -APM3_RTC myRTC; //Create instance of RTC class - -uint32_t msToSleep = 2000; //This is the user editable number of ms to sleep between RTC checks -#define TIMER_FREQ 3000000L //Counter/Timer 6 will use the HFRC oscillator of 3MHz -uint32_t sysTicksToSleep = msToSleep * (TIMER_FREQ / 1000); - -void setup() -{ - Serial.begin(115200); - Serial.println("SparkFun RTC Example"); - - myRTC.setToCompilerTime(); //Easily set RTC using the system __DATE__ and __TIME__ macros from compiler - //myRTC.setTime(7, 28, 51, 0, 21, 10, 15); //Manually set RTC back to the future: Oct 21st, 2015 at 7:28.51 AM - - setupWakeTimer(); -} - -void loop() -{ - myRTC.getTime(); - - Serial.printf("It is now "); - Serial.printf("%d:", myRTC.hour); - Serial.printf("%02d:", myRTC.minute); - Serial.printf("%02d.", myRTC.seconds); - Serial.printf("%02d", myRTC.hundredths); - - Serial.printf(" %02d/", myRTC.month); - Serial.printf("%02d/", myRTC.dayOfMonth); - Serial.printf("%02d", myRTC.year); - - Serial.printf(" Day of week: %d =", myRTC.weekday); - Serial.printf(" %s", myRTC.textWeekday); - - Serial.println(); - - am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP); //Sleepy time -} - -//We use counter/timer 6 for this example but 0 to 7 are available -//CT 7 is used for Software Serial. All CTs are used for Servo. -void setupWakeTimer() -{ - //Clear compare interrupt - am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREG); //Use CT6 - - am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREG); // Enable C/T G=6 - - //Don't change from 3MHz system timer, but enable G timer - am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); - am_hal_stimer_config(AM_HAL_STIMER_HFRC_3MHZ | AM_HAL_STIMER_CFG_COMPARE_G_ENABLE); - - //Setup ISR to trigger when the number of ms have elapsed - am_hal_stimer_compare_delta_set(6, sysTicksToSleep); - - //Enable the timer interrupt in the NVIC. - NVIC_EnableIRQ(STIMER_CMPR6_IRQn); -} - -//Called once number of milliseconds has passed -extern "C" void am_stimer_cmpr6_isr(void) -{ - uint32_t ui32Status = am_hal_stimer_int_status_get(false); - if (ui32Status & AM_HAL_STIMER_INT_COMPAREG) - { - am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREG); - - //Reset compare value. ISR will trigger when the number of ms have elapsed - am_hal_stimer_compare_delta_set(6, sysTicksToSleep); - } -} diff --git a/libraries/RTC/examples/Example2_Set_Alarms/Example2_Set_Alarms.ino b/libraries/RTC/examples/Example2_Set_Alarms/Example2_Set_Alarms.ino new file mode 100644 index 00000000..e25ef0ca --- /dev/null +++ b/libraries/RTC/examples/Example2_Set_Alarms/Example2_Set_Alarms.ino @@ -0,0 +1,104 @@ +/* + Author: Adam Garbo and Nathan Seidle + Created: June 3rd, 2020 + License: MIT. See SparkFun Arduino Apollo3 Project for more information + + This example demonstrates how to read and set the RTC alarms. + + It is necessary to first set the RTC alarm date and time and then specify + the alarm mode, which determines which date and time values will be used + for comparison when generating an alarm interrupt. + + The code is configured so that the RTC alarm will trigger every minute. + The RTC interrupt service routine will set an alarm flag each time the + alarm triggers and the RTC date and time will printed to the Serial Monitor. +*/ + +#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core +APM3_RTC myRTC; // Create instance of RTC class + +volatile bool alarmFlag = false; + +void setup() +{ + Serial.begin(115200); + Serial.println("SparkFun RTC Set Alarm Example"); + + // Easily set RTC using the system __DATE__ and __TIME__ macros from compiler + //myRTC.setToCompilerTime(); + + // Manually set RTC date and time + myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy) + + // Set the RTC's alarm + 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 + // Set the RTC alarm mode + /* + 0: Alarm interrupt disabled + 1: Alarm match every year (hundredths, seconds, minutes, hour, day, month) + 2: Alarm match every month (hundredths, seconds, minutes, hours, day) + 3: Alarm match every week (hundredths, seconds, minutes, hours, weekday) + 4: Alarm match every day (hundredths, seconds, minute, hours) + 5: Alarm match every hour (hundredths, seconds, minutes) + 6: Alarm match every minute (hundredths, seconds) + 7: Alarm match every second (hundredths) + */ + myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover + myRTC.attachInterrupt(); // Attach RTC alarm interrupt + + // Print the RTC's alarm date and time + Serial.print("Next alarm: "); printAlarm(); +} + +void loop() +{ + // Check if alarm flag was set + if (alarmFlag == true) + { + // Print date and time of RTC alarm trigger + Serial.print("Alarm triggered: "); printDateTime(); + + // Clear alarm flag + alarmFlag = false; + } + + // Print RTC's date and time while waiting for alarm + static uint32_t nextPrint = 0; + if(millis() > nextPrint){ + printDateTime(); + nextPrint = millis() + 1000; + } +} + +// Print the RTC's current date and time +void printDateTime() +{ + myRTC.getTime(); + char dateTimeBuffer[25]; + sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d", + myRTC.year, myRTC.month, myRTC.dayOfMonth, + myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths); + Serial.println(dateTimeBuffer); +} + +// Print the RTC's alarm +void printAlarm() +{ + myRTC.getAlarm(); + char alarmBuffer[25]; + sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d", + myRTC.alarmMonth, myRTC.alarmDayOfMonth, + myRTC.alarmHour, myRTC.alarmMinute, + myRTC.alarmSeconds, myRTC.alarmHundredths); + Serial.println(alarmBuffer); +} + +// Interrupt handler for the RTC +extern "C" void am_rtc_isr(void) +{ + // Clear the RTC alarm interrupt. + am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM); + + // Set alarm flag + alarmFlag = true; +} diff --git a/libraries/RTC/examples/Example3_TestRTC/Example3_TestRTC.ino b/libraries/RTC/examples/Example3_Test_RTC/Example3_Test_RTC.ino similarity index 74% rename from libraries/RTC/examples/Example3_TestRTC/Example3_TestRTC.ino rename to libraries/RTC/examples/Example3_Test_RTC/Example3_Test_RTC.ino index ba5fdcca..e47f7cae 100644 --- a/libraries/RTC/examples/Example3_TestRTC/Example3_TestRTC.ino +++ b/libraries/RTC/examples/Example3_Test_RTC/Example3_Test_RTC.ino @@ -1,4 +1,5 @@ -/* Author: Nathan Seidle and stephenf7072 +/* + Author: Nathan Seidle and stephenf7072 Created: January 28th, 2020 License: MIT. See SparkFun Arduino Apollo3 Project for more information @@ -6,27 +7,27 @@ */ #include "RTC.h" -APM3_RTC myRTC; //Create instance of RTC class +APM3_RTC myRTC; // Create instance of RTC class -int previousDay = 1; +int previousDay; void setup() { Serial.begin(115200); delay(10); - Serial.println("Artemis RTC testing"); + Serial.println("Artemis RTC Testing"); - //myRTC.setTime(hh, mm, ss, hund, dd, mm, yy); - myRTC.setTime(23, 59, 59, 99, 1, 1, 19); //Manually set RTC to 1s before midnight + // Manually set RTC date and time to the start of 2020 so that myRTC contains valid times + myRTC.setTime(23, 59, 59, 0, 1, 1, 20); // Set to 1 second before midnight Jan 1 } void loop() { - printArtemisTime(); + myRTC.setTime(23, 59, 59, 99, myRTC.dayOfMonth, myRTC.month, myRTC.year); // Manually set RTC 1/100th of a second from the next day + previousDay = myRTC.weekday; + delay(11); //Allow us to roll from midnight the night before to the new day - myRTC.getTime(); - myRTC.setTime(23, 59, 59, 99, myRTC.dayOfMonth, myRTC.month, myRTC.year); //Manually set RTC - delay(11); //Allow us to roll from midnight the night before to the new day + printArtemisTime(); } void printArtemisTime() @@ -76,7 +77,5 @@ void printArtemisTime() ; } - previousDay = myRTC.weekday; - Serial.println(); } \ No newline at end of file diff --git a/libraries/RTC/examples/Example4_Set_Epoch/Example4_Set_Epoch.ino b/libraries/RTC/examples/Example4_Set_Epoch/Example4_Set_Epoch.ino new file mode 100644 index 00000000..04dd33d5 --- /dev/null +++ b/libraries/RTC/examples/Example4_Set_Epoch/Example4_Set_Epoch.ino @@ -0,0 +1,41 @@ +/* + Author: Adam Garbo and Nathan Seidle + Created: June 3rd, 2020 + License: MIT. See SparkFun Arduino Apollo3 Project for more information + + This example demonstrates how to set the RTC using UNIX Epoch time. +*/ + +#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core +APM3_RTC myRTC; // Create instance of RTC class + +void setup() +{ + Serial.begin(115200); + Serial.println("SparkFun RTC Set UNIX Epoch Time Example"); + + // Set the RTC time using UNIX Epoch time + myRTC.setEpoch(1591185600); // E.g. 12:00:00, June 3rd, 2020 +} + +void loop() +{ + // Print UNIX Epoch timestamp + Serial.print("Epoch time: "); Serial.println(myRTC.getEpoch()); + + // Print RTC's date and time + Serial.print("Timestamp: "); printDateTime(); + + delay(1000); +} + +// Print the RTC's current date and time +void printDateTime() +{ + myRTC.getTime(); + char dateTime[20]; + sprintf(dateTime, "20%02d-%02d-%02d %02d:%02d:%02d", + myRTC.year, myRTC.month, myRTC.dayOfMonth, + myRTC.hour, myRTC.minute, myRTC.seconds); + Serial.println(dateTime); +} diff --git a/libraries/RTC/examples/Example5_Rolling_Alarms/Example5_Rolling_Alarms.ino b/libraries/RTC/examples/Example5_Rolling_Alarms/Example5_Rolling_Alarms.ino new file mode 100644 index 00000000..41bdbd29 --- /dev/null +++ b/libraries/RTC/examples/Example5_Rolling_Alarms/Example5_Rolling_Alarms.ino @@ -0,0 +1,110 @@ +/* + Author: Adam Garbo and Nathan Seidle + Created: June 3rdrd, 2020 + License: MIT. See SparkFun Arduino Apollo3 Project for more information + + This example demonstrates how to read and set rolling RTC alarms. Each time + the alarm triggers, a user-specified additional amount of time (seconds, + minutes or hours) can be added to create a rolling RTC alarm. Second, + minute and hour rollovers are handled using modulo calculations. + + The current code is configured as a 5-second rolling RTC alarm. +*/ + +#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core +APM3_RTC myRTC; // Create instance of RTC class + +volatile bool alarmFlag = false; +int alarmSeconds = 5; +int alarmMinutes = 0; +int alarmHours = 0; + +void setup() +{ + Serial.begin(115200); + Serial.println("SparkFun RTC Set Rolling Alarms Example"); + + // Easily set RTC using the system __DATE__ and __TIME__ macros from compiler + //myRTC.setToCompilerTime(); + + // Manually set RTC date and time + myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy) + + // Set the RTC's alarm + 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 + + // Set the RTC alarm mode + /* + 0: Alarm interrupt disabled + 1: Alarm match every year (hundredths, seconds, minutes, hour, day, month) + 2: Alarm match every month (hundredths, seconds, minutes, hours, day) + 3: Alarm match every week (hundredths, seconds, minutes, hours, weekday) + 4: Alarm match every day (hundredths, seconds, minute, hours) + 5: Alarm match every hour (hundredths, seconds, minutes) + 6: Alarm match every minute (hundredths, seconds) + 7: Alarm match every second (hundredths) + */ + myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover + myRTC.attachInterrupt(); // Attach RTC alarm interrupt + + // Print the RTC's alarm date and time + Serial.print("Next alarm: "); printAlarm(); +} + +void loop() +{ + // Check if alarm flag was set + if (alarmFlag == true) + { + // Print date and time of RTC alarm trigger + Serial.print("Alarm triggered: "); printDateTime(); + + // Clear alarm flag + alarmFlag = false; + + // Set the RTC's rolling alarm + myRTC.setAlarm((myRTC.hour + alarmHours) % 24, + (myRTC.minute + alarmMinutes) % 60, + (myRTC.seconds + alarmSeconds) % 60, + 0, + myRTC.dayOfMonth, + myRTC.month); + myRTC.setAlarmMode(6); + + // Print next RTC alarm date and time + Serial.print("Next rolling alarm: "); printAlarm(); + } +} + +// Print the RTC's current date and time +void printDateTime() +{ + myRTC.getTime(); + char dateTimeBuffer[25]; + sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d", + myRTC.year, myRTC.month, myRTC.dayOfMonth, + myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths); + Serial.println(dateTimeBuffer); +} + +// Print the RTC's alarm +void printAlarm() +{ + myRTC.getAlarm(); + char alarmBuffer[25]; + sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d", + myRTC.alarmMonth, myRTC.alarmDayOfMonth, + myRTC.alarmHour, myRTC.alarmMinute, + myRTC.alarmSeconds, myRTC.alarmHundredths); + Serial.println(alarmBuffer); +} + +// Interrupt handler for the RTC +extern "C" void am_rtc_isr(void) +{ + // Clear the RTC alarm interrupt. + am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM); + + // Set alarm flag + alarmFlag = true; +} diff --git a/libraries/RTC/examples/Example6_LowPower_Alarm/Example6_LowPower_Alarm.ino b/libraries/RTC/examples/Example6_LowPower_Alarm/Example6_LowPower_Alarm.ino new file mode 100644 index 00000000..fd9efcd3 --- /dev/null +++ b/libraries/RTC/examples/Example6_LowPower_Alarm/Example6_LowPower_Alarm.ino @@ -0,0 +1,129 @@ +/* + Author: Adam Garbo and Nathan Seidle + Created: June 3rd, 2020 + License: MIT. See SparkFun Arduino Apollo3 Project for more information + + This example demonstrates how to set an RTC alarm and enter deep sleep. + + The code is configured to set an RTC alarm every minute and enter + deep sleep between interrupts. The RTC interrupt service routine will + wake the board and print the date and time upon each alarm interrupt. + + Tested with a SparkFun Edge 2. Confirmed sleep current of 2.5 uA. +*/ + +#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core +APM3_RTC myRTC; // Create instance of RTC class + +void setup() +{ + Serial.begin(115200); + Serial.println("SparkFun RTC Low-power Alarm Example"); + + // Easily set RTC using the system __DATE__ and __TIME__ macros from compiler + //myRTC.setToCompilerTime(); + + // Manually set RTC date and time + myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy) + + // Set the RTC's alarm + 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 + + // Set the RTC alarm mode + /* + 0: Alarm interrupt disabled + 1: Alarm match every year (hundredths, seconds, minutes, hour, day, month) + 2: Alarm match every month (hundredths, seconds, minutes, hours, day) + 3: Alarm match every week (hundredths, seconds, minutes, hours, weekday) + 4: Alarm match every day (hundredths, seconds, minute, hours) + 5: Alarm match every hour (hundredths, seconds, minutes) + 6: Alarm match every minute (hundredths, seconds) + 7: Alarm match every second (hundredths) + */ + myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover + myRTC.attachInterrupt(); // Attach RTC alarm interrupt +} + +void loop() +{ + // Print date and time of RTC alarm trigger + Serial.print("Alarm interrupt: "); printDateTime(); + + // Enter deep sleep and await RTC alarm interrupt + goToSleep(); +} + +// Print the RTC's current date and time +void printDateTime() +{ + myRTC.getTime(); + char dateTimeBuffer[25]; + sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d", + myRTC.year, myRTC.month, myRTC.dayOfMonth, + myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths); + Serial.println(dateTimeBuffer); +} + +// Power down gracefully +void goToSleep() +{ + // Disable UART + Serial.end(); + + // Disable ADC + power_adc_disable(); + + // Force the peripherals off + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM0); + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM1); + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM2); + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM3); + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM4); + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM5); + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_ADC); + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_UART0); + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_UART1); + + // Disable all pads + for (int x = 0 ; x < 50 ; x++) + am_hal_gpio_pinconfig(x, g_AM_HAL_GPIO_DISABLE); + + //Power down Flash, SRAM, cache + am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_CACHE); // Turn off CACHE + am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_FLASH_512K); // Turn off everything but lower 512k + am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_SRAM_64K_DTCM); // Turn off everything but lower 64k + //am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_ALL); //Turn off all memory (doesn't recover) + + // Keep the 32kHz clock running for RTC + am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); + am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ); + + am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP); // Sleep forever + + // And we're back! + wakeUp(); +} + +// Power up gracefully +void wakeUp() +{ + // Power up SRAM, turn on entire Flash + am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_MAX); + + // Go back to using the main clock + am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); + am_hal_stimer_config(AM_HAL_STIMER_HFRC_3MHZ); + + // Enable ADC + ap3_adc_setup(); + + // Enable Serial + Serial.begin(115200); +} + +// Interrupt handler for the RTC +extern "C" void am_rtc_isr(void) +{ + // Clear the RTC alarm interrupt + am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM); +} diff --git a/libraries/RTC/keywords.txt b/libraries/RTC/keywords.txt index d6069c6e..6b719deb 100644 --- a/libraries/RTC/keywords.txt +++ b/libraries/RTC/keywords.txt @@ -13,19 +13,36 @@ RTC KEYWORD1 ####################################### getTime KEYWORD2 +getEpoch KEYWORD2 + setTime KEYWORD2 setTimeToCompiler KEYWORD2 +setEpoch KEYWORD2 + +getAlarm KEYWORD2 +setAlarm KEYWORD2 +setAlarmMode KEYWORD2 +attachInterrupt KEYWORD2 +detachInterrupt KEYWORD2 +weekday KEYWORD2 +textWeekday KEYWORD2 +year KEYWORD2 +month KEYWORD2 +dayOfMonth KEYWORD2 hour KEYWORD2 minute KEYWORD2 seconds KEYWORD2 hundredths KEYWORD2 -month KEYWORD2 -dayOfMonth KEYWORD2 -year KEYWORD2 -weekday KEYWORD2 -textWeekday KEYWORD2 +alarmWeekday KEYWORD2 +alarmMonth KEYWORD2 +alarmDayOfMonth KEYWORD2 +alarmHour KEYWORD2 +alarmMinute KEYWORD2 +alarmSeconds KEYWORD2 +alarmHundredths KEYWORD2 +alarmTextWeekday KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/libraries/RTC/library.properties b/libraries/RTC/library.properties index 0d9e581f..659e1890 100644 --- a/libraries/RTC/library.properties +++ b/libraries/RTC/library.properties @@ -1,5 +1,5 @@ name=RTC -version=1.0 +version=1.2 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Real Time Clock (RTC)) library for the SparkFun Artemis diff --git a/libraries/RTC/src/RTC.cpp b/libraries/RTC/src/RTC.cpp index f061809c..c15eec3c 100644 --- a/libraries/RTC/src/RTC.cpp +++ b/libraries/RTC/src/RTC.cpp @@ -3,10 +3,14 @@ */ #include "RTC.h" +#include am_hal_rtc_time_t hal_time; +am_hal_rtc_time_t alm_time; -// String arrays to index Days and Months with the values returned by the RTC. +#define EPOCH_TIME 946684800 // UNIX Epoch time = 2000-01-01 00:00:00 + +//String arrays to index Days and Months with the values returned by the RTC. char const *pcWeekday[] = { "Sunday", @@ -37,31 +41,32 @@ char const *pcMonth[] = //Constructor APM3_RTC::APM3_RTC() { - // Enable the XT for the RTC. + //Enable the XT for the RTC. am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_XTAL_START, 0); - // Select XT for RTC clock source + //Select XT for RTC clock source am_hal_rtc_osc_select(AM_HAL_RTC_OSC_XT); - // Enable the RTC. + //Enable the RTC. am_hal_rtc_osc_enable(); } + +// Note: Order of parameters to change in v2.0.0. 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) { + hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + year, month, dayOfMonth); //computeDayofWeek is expecting 1 to 12 months + hal_time.ui32Century = 0; + hal_time.ui32Year = year; + hal_time.ui32Month = month; //HAL is expecting 1 to 12 months + hal_time.ui32DayOfMonth = dayOfMonth; hal_time.ui32Hour = hour; hal_time.ui32Minute = min; hal_time.ui32Second = sec; hal_time.ui32Hundredths = hund; - hal_time.ui32DayOfMonth = dayOfMonth; - hal_time.ui32Month = month; //HAL is expecting 1 to 12 months - hal_time.ui32Year = year; - hal_time.ui32Century = 0; - - hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + year, month, dayOfMonth); //computeDayofWeek is expecting 1 to 12 months - am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time + getTime(); } //Takes the time from the last build and uses it as the current time @@ -70,15 +75,39 @@ void APM3_RTC::setToCompilerTime() { //Get the current date/time from the compiler //Alternatively, you can set these values manually + hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + toVal(&__DATE__[9]), mthToIndex(&__DATE__[0]) + 1, toVal(&__DATE__[4])); + hal_time.ui32Century = 0; + hal_time.ui32Year = toVal(&__DATE__[9]); + hal_time.ui32Month = mthToIndex(&__DATE__[0]) + 1; //Compiler ouputs months in 0-11. + hal_time.ui32DayOfMonth = toVal(&__DATE__[4]); hal_time.ui32Hour = toVal(&__TIME__[0]); hal_time.ui32Minute = toVal(&__TIME__[3]); hal_time.ui32Second = toVal(&__TIME__[6]); hal_time.ui32Hundredths = 00; - hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + toVal(&__DATE__[9]), mthToIndex(&__DATE__[0]) + 1, toVal(&__DATE__[4])); - hal_time.ui32DayOfMonth = toVal(&__DATE__[4]); - hal_time.ui32Month = mthToIndex(&__DATE__[0]) + 1; //Compiler ouputs months in 0-11. - hal_time.ui32Year = toVal(&__DATE__[9]); + + am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time + getTime(); +} + +void APM3_RTC::setEpoch(uint32_t ts) +{ + if (ts < EPOCH_TIME) { + ts = EPOCH_TIME; + } + + struct tm tm; + + time_t t = ts; + struct tm* tmp = gmtime(&t); + hal_time.ui32Weekday = 0; hal_time.ui32Century = 0; + hal_time.ui32Year = tmp->tm_year - 100; + hal_time.ui32Month = tmp->tm_mon + 1; + hal_time.ui32DayOfMonth = tmp->tm_mday; + hal_time.ui32Hour = tmp->tm_hour; + hal_time.ui32Minute = tmp->tm_min; + hal_time.ui32Second = tmp->tm_sec; + hal_time.ui32Hundredths = 0; am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time } @@ -87,17 +116,101 @@ void APM3_RTC::getTime() { am_hal_rtc_time_get(&hal_time); + weekday = hal_time.ui32Weekday; + textWeekday = pcWeekday[hal_time.ui32Weekday]; //Given a number (day of week) return the string that represents the name + year = hal_time.ui32Year; + month = hal_time.ui32Month; //HAL outputs months in 1 to 12 form + dayOfMonth = hal_time.ui32DayOfMonth; hour = hal_time.ui32Hour; minute = hal_time.ui32Minute; seconds = hal_time.ui32Second; hundredths = hal_time.ui32Hundredths; +} - month = hal_time.ui32Month; //HAL outputs months in 1 to 12 form - dayOfMonth = hal_time.ui32DayOfMonth; - year = hal_time.ui32Year; +uint32_t APM3_RTC::getEpoch() +{ + am_hal_rtc_time_get(&hal_time); - weekday = hal_time.ui32Weekday; - textWeekday = pcWeekday[hal_time.ui32Weekday]; //Given a number (day of week) return the string that represents the name + struct tm tm; + + tm.tm_isdst = -1; + tm.tm_yday = 0; + tm.tm_wday = 0; + tm.tm_year = hal_time.ui32Year + 100; //Number of years since 1900. + tm.tm_mon = hal_time.ui32Month - 1; //mktime is expecting 0 to 11 months + tm.tm_mday = hal_time.ui32DayOfMonth; + tm.tm_hour = hal_time.ui32Hour; + tm.tm_min = hal_time.ui32Minute; + tm.tm_sec = hal_time.ui32Second; + + return mktime(&tm); +} + +void APM3_RTC::getAlarm() +{ + am_hal_rtc_alarm_get(&alm_time); //Get the RTC's alarm time + + alarmWeekday = alm_time.ui32Weekday; + alarmMonth = alm_time.ui32Month; //HAL outputs months in 1 to 12 form + alarmDayOfMonth = alm_time.ui32DayOfMonth; + alarmHour = alm_time.ui32Hour; + alarmMinute = alm_time.ui32Minute; + alarmSeconds = alm_time.ui32Second; + alarmHundredths = alm_time.ui32Hundredths; +} + +// Note: Order of parameters to change in v2.0.0. +void APM3_RTC::setAlarm(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month) +{ + alm_time.ui32Weekday = 0; // WIP + alm_time.ui32Month = month; + alm_time.ui32DayOfMonth = dayOfMonth; + alm_time.ui32Hour = hour; + alm_time.ui32Minute = min; + alm_time.ui32Second = sec; + alm_time.ui32Hundredths = hund; + + am_hal_rtc_alarm_set(&alm_time, AM_HAL_RTC_ALM_RPT_DIS); //Initialize the RTC alarm with this date/time +} + +// Sets the RTC alarm repeat interval. +/* + RTC alarm repeat intervals + 0: AM_HAL_RTC_ALM_RPT_DIS Alarm interrupt disabled + 1: AM_HAL_RTC_ALM_RPT_YR Interrupt every year + 2: AM_HAL_RTC_ALM_RPT_MTH Interrupt every month + 3: AM_HAL_RTC_ALM_RPT_WK Interrupt every week + 4: AM_HAL_RTC_ALM_RPT_DAY Interrupt every day + 5: AM_HAL_RTC_ALM_RPT_HR Interrupt every hour + 6: AM_HAL_RTC_ALM_RPT_MIN Interrupt every minute + 7: AM_HAL_RTC_ALM_RPT_SEC Interrupt every second + 8: AM_HAL_RTC_ALM_RPT_10TH Interrupt every 10th second (unused) + 9: AM_HAL_RTC_ALM_RPT_100TH Interrupt every 100th second (unused) +*/ +void APM3_RTC::setAlarmMode(uint8_t mode) +{ + am_hal_rtc_alarm_interval_set(mode); +} + +void APM3_RTC::attachInterrupt() +{ + // Enable the RTC interrupt. + am_hal_rtc_int_enable(AM_HAL_RTC_INT_ALM); + + // Clear the RTC interrupt. + am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM); + + // Enable RTC interrupts to the NVIC. + NVIC_EnableIRQ(RTC_IRQn); +} + +void APM3_RTC::detachInterrupt() +{ + // Disable RTC interrupts to the NVIC. + NVIC_DisableIRQ(RTC_IRQn); + + // Disable the RTC interrupt. + am_hal_rtc_int_disable(AM_HAL_RTC_INT_ALM); } // mthToIndex() converts a string indicating a month to an index value. diff --git a/libraries/RTC/src/RTC.h b/libraries/RTC/src/RTC.h index a4262bce..93cede37 100644 --- a/libraries/RTC/src/RTC.h +++ b/libraries/RTC/src/RTC.h @@ -8,24 +8,40 @@ class APM3_RTC public: APM3_RTC(); - void getTime(); //Query the RTC for the current time/date. Loads .seconds, .minute, etc. + void getTime(); //Query the RTC for the current time/date + uint32_t getEpoch(); //Return the current RTC time/date as UNIX Epoch time + void setTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month, uint16_t year); //Set current time to provided hundredths/seconds/etc - void setToCompilerTime(); //Set to time when sketch was compiled + void setToCompilerTime(); //Set to time when sketch was compiled + void setEpoch(uint32_t ts); //Set current time to provided UNIX Epoch time + void getAlarm(); //Query the RTC for the current alarm time/date + 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 + void setAlarmMode(uint8_t mode); //Set the RTC alarm repeat interval + void attachInterrupt(); //Attach the RTC alarm interrupt + void detachInterrupt(); //Detach the RTC alarm interrupt + + uint32_t weekday; //0 to 6 representing the day of the week + uint32_t century; + uint32_t year; + uint32_t month; + uint32_t dayOfMonth; uint32_t hour; uint32_t minute; uint32_t seconds; uint32_t hundredths; - - uint32_t dayOfMonth; - uint32_t month; - uint32_t year; - uint32_t century; - - uint32_t weekday; //0 to 6 representing the day of the week const char *textWeekday; + uint32_t alarmWeekday; //0 to 6 representing the day of the week + uint32_t alarmMonth; + uint32_t alarmDayOfMonth; + uint32_t alarmHour; + uint32_t alarmMinute; + uint32_t alarmSeconds; + uint32_t alarmHundredths; + const char *alarmTextWeekday; + private: //Helper functions to convert compiler date/time to ints int toVal(char const *pcAsciiStr);