|
| 1 | +/* |
| 2 | + Author: Adam Garbo |
| 3 | + Created: August 1st, 2020 |
| 4 | + License: MIT. See SparkFun Arduino Apollo3 Project for more information |
| 5 | +
|
| 6 | + This example demonstrates the combined use of the Artemis Watchdog Timer (WDT) |
| 7 | + and real-time clock (RTC). |
| 8 | +
|
| 9 | + Both RTC and WDT interrupts will wake the system, print the date and time, |
| 10 | + and then re-enter deep sleep. |
| 11 | +
|
| 12 | + The WDT is configured to trigger every 10 seconds. If the WDT is not "pet" |
| 13 | + after 100 seconds, a system reset will be triggered. |
| 14 | + |
| 15 | + The RTC alarm is configured to trigger every minute and enter deep sleep |
| 16 | + between interrupts. Alarm interuptswill also restart the WDT and reset |
| 17 | + the watchdog interrput counter. |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "RTC.h" |
| 21 | +#include "WDT.h" |
| 22 | + |
| 23 | +APM3_RTC rtc; |
| 24 | +APM3_WDT wdt; |
| 25 | + |
| 26 | +volatile bool alarmFlag = false; // RTC ISR flag |
| 27 | +volatile bool watchdogFlag = false; // Watchdog Timer ISR flag |
| 28 | +volatile int watchdogInterrupt = 0; // Watchdog interrupt counter |
| 29 | + |
| 30 | +void setup() |
| 31 | +{ |
| 32 | + Serial.begin(115200); |
| 33 | + |
| 34 | + Serial.println("Artemis Watchdog Low Power Example"); |
| 35 | + |
| 36 | + // Set the RTC time using UNIX Epoch time |
| 37 | + rtc.setEpoch(1596240000); // Saturday, August 1, 2020 00:00:00 |
| 38 | + |
| 39 | + // Set the RTC's alarm |
| 40 | + rtc.setAlarm(0, 0, 0, 0, 0, 0); // Set alarm (hund, ss, mm, hh, dd, mm) |
| 41 | + rtc.setAlarmMode(6); // Set the RTC alarm to trigger every minute |
| 42 | + rtc.attachInterrupt(); // Attach RTC alarm interrupt |
| 43 | + |
| 44 | + // Configure the watchdog timer |
| 45 | + // See Example2_WDT_Config for more information on how to configure the watchdog |
| 46 | + wdt.configure(WDT_16HZ, 160, 240); // 16 Hz clock, 10-second interrupt period, 15-second reset period |
| 47 | + |
| 48 | + // Start the watchdog |
| 49 | + wdt.start(); |
| 50 | +} |
| 51 | + |
| 52 | +void loop() |
| 53 | +{ |
| 54 | + // Check for alarm interrupt |
| 55 | + if (alarmFlag) |
| 56 | + { |
| 57 | + Serial.print("Alarm interrupt: "); |
| 58 | + printDateTime(); // Print RTC's date and time |
| 59 | + alarmFlag = false; |
| 60 | + |
| 61 | + wdt.restart(); // "Pet" the dog |
| 62 | + watchdogInterrupt = 0; // Reset watchdog interrupt counter |
| 63 | + } |
| 64 | + |
| 65 | + // Check for watchdog interrupt |
| 66 | + if (watchdogFlag) |
| 67 | + { |
| 68 | + Serial.print("Watchdog interrupt: "); |
| 69 | + printDateTime(); // Print RTC's date and time |
| 70 | + watchdogFlag = false; // Clear watchdog flag |
| 71 | + } |
| 72 | + |
| 73 | + goToSleep(); // Enter deep sleep |
| 74 | +} |
| 75 | + |
| 76 | +// Print the RTC's current date and time |
| 77 | +void printDateTime() |
| 78 | +{ |
| 79 | + rtc.getTime(); |
| 80 | + Serial.printf("20%02d-%02d-%02d %02d:%02d:%02d.%02d\n", |
| 81 | + rtc.year, rtc.month, rtc.dayOfMonth, |
| 82 | + rtc.hour, rtc.minute, rtc.seconds, rtc.hundredths); |
| 83 | +} |
| 84 | + |
| 85 | +// Power down gracefully |
| 86 | +void goToSleep() |
| 87 | +{ |
| 88 | + // Disable UART |
| 89 | + Serial.end(); |
| 90 | + |
| 91 | + // Disable ADC |
| 92 | + power_adc_disable(); |
| 93 | + |
| 94 | + // Force the peripherals off |
| 95 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM0); |
| 96 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM1); |
| 97 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM2); |
| 98 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM3); |
| 99 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM4); |
| 100 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM5); |
| 101 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_ADC); |
| 102 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_UART0); |
| 103 | + am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_UART1); |
| 104 | + |
| 105 | + // Disable all pads |
| 106 | + for (int x = 0 ; x < 50 ; x++) |
| 107 | + am_hal_gpio_pinconfig(x, g_AM_HAL_GPIO_DISABLE); |
| 108 | + |
| 109 | + //Power down Flash, SRAM, cache |
| 110 | + am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_CACHE); // Turn off CACHE |
| 111 | + am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_FLASH_512K); // Turn off everything but lower 512k |
| 112 | + am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_SRAM_64K_DTCM); // Turn off everything but lower 64k |
| 113 | + //am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_ALL); //Turn off all memory (doesn't recover) |
| 114 | + |
| 115 | + // Keep the 32kHz clock running for RTC |
| 116 | + am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); |
| 117 | + am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ); |
| 118 | + |
| 119 | + am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP); // Sleep forever |
| 120 | + |
| 121 | + // And we're back! |
| 122 | + wakeUp(); |
| 123 | +} |
| 124 | + |
| 125 | +// Power up gracefully |
| 126 | +void wakeUp() |
| 127 | +{ |
| 128 | + // Power up SRAM, turn on entire Flash |
| 129 | + am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_MAX); |
| 130 | + |
| 131 | + // Go back to using the main clock |
| 132 | + am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); |
| 133 | + am_hal_stimer_config(AM_HAL_STIMER_HFRC_3MHZ); |
| 134 | + |
| 135 | + // Enable ADC |
| 136 | + ap3_adc_setup(); |
| 137 | + |
| 138 | + // Enable Serial |
| 139 | + Serial.begin(115200); |
| 140 | +} |
| 141 | + |
| 142 | +// Interrupt handler for the RTC |
| 143 | +extern "C" void am_rtc_isr(void) |
| 144 | +{ |
| 145 | + // Clear the RTC alarm interrupt |
| 146 | + am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM); |
| 147 | + |
| 148 | + alarmFlag = true; // Set alarm flag |
| 149 | +} |
| 150 | + |
| 151 | +// Interrupt handler for the watchdog. |
| 152 | +extern "C" void am_watchdog_isr(void) |
| 153 | +{ |
| 154 | + // Clear the watchdog interrupt |
| 155 | + wdt.clear(); |
| 156 | + |
| 157 | + // Perform system reset after 10 watchdog interrupts (should not occur) |
| 158 | + if ( watchdogInterrupt < 10 ) |
| 159 | + { |
| 160 | + wdt.restart(); // "Pet" the dog |
| 161 | + } |
| 162 | + else { |
| 163 | + digitalWrite(LED_BUILTIN, HIGH); // Visual indication of system reset trigger |
| 164 | + } |
| 165 | + |
| 166 | + watchdogFlag = true; // Set the watchdog flag |
| 167 | + watchdogInterrupt++; // Increment watchdog interrupt counter |
| 168 | +} |
0 commit comments