|
| 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(0, 50, 59, 12, 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(0, 0, 0, 13, 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 |
| 36 | + 2: Alarm match every month |
| 37 | + 3: Alarm match every week |
| 38 | + 4: Alarm match every day |
| 39 | + 5: Alarm match every hour |
| 40 | + 6: Alarm match every minute |
| 41 | + 7: Alarm match every second |
| 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 | +} |
0 commit comments