|
| 1 | +/* |
| 2 | + AdvancedTimedWakeup |
| 3 | +
|
| 4 | + This sketch demonstrates the usage of Internal Interrupts to wakeup a chip in deep sleep mode. |
| 5 | +
|
| 6 | + In this sketch: |
| 7 | + - RTC date and time are configured. |
| 8 | + - Alarm is set to wake up the processor each 'atime' and called a custom alarm callback |
| 9 | + which increment a value and reload alarm with 'atime' offset. |
| 10 | +
|
| 11 | + This example code is in the public domain. |
| 12 | +*/ |
| 13 | + |
| 14 | +#include "STM32LowPower.h" |
| 15 | +#include <STM32RTC.h> |
| 16 | + |
| 17 | +/* Get the rtc object */ |
| 18 | +STM32RTC& rtc = STM32RTC::getInstance(); |
| 19 | + |
| 20 | +// Time in second between blink |
| 21 | +static uint32_t atime = 1; |
| 22 | + |
| 23 | +// Declare it volatile since it's incremented inside an interrupt |
| 24 | +volatile int alarmMatch_counter = 0; |
| 25 | + |
| 26 | +// Variables for RTC configurations |
| 27 | +static byte seconds = 0; |
| 28 | +static byte minutes = 0; |
| 29 | +static byte hours = 0; |
| 30 | + |
| 31 | +static byte weekDay = 1; |
| 32 | +static byte day = 1; |
| 33 | +static byte month = 1; |
| 34 | +static byte year = 18; |
| 35 | + |
| 36 | +void setup() { |
| 37 | + rtc.begin(); |
| 38 | + rtc.setTime(hours, minutes, seconds); |
| 39 | + rtc.setDate(weekDay, day, month, year); |
| 40 | + |
| 41 | + pinMode(LED_BUILTIN, OUTPUT); |
| 42 | + |
| 43 | + Serial.begin(9600); |
| 44 | + while(!Serial) {} |
| 45 | + |
| 46 | + // Configure low power |
| 47 | + LowPower.begin(); |
| 48 | + LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime); |
| 49 | + |
| 50 | + // Configure first alarm in 2 second then it will be done in the rtc callback |
| 51 | + rtc.setAlarmEpoch( rtc.getEpoch() + 2 ); |
| 52 | +} |
| 53 | + |
| 54 | +void loop() { |
| 55 | + Serial.print("Alarm Match: "); |
| 56 | + Serial.print(alarmMatch_counter); |
| 57 | + Serial.println(" times."); |
| 58 | + delay(100); |
| 59 | + digitalWrite(LED_BUILTIN, HIGH); |
| 60 | + LowPower.deepSleep(); |
| 61 | + digitalWrite(LED_BUILTIN, LOW); |
| 62 | + LowPower.deepSleep(); |
| 63 | +} |
| 64 | + |
| 65 | +void alarmMatch(void* data) |
| 66 | +{ |
| 67 | + // This function will be called once on device wakeup |
| 68 | + // You can do some little operations here (like changing variables which will be used in the loop) |
| 69 | + // Remember to avoid calling delay() and long running functions since this functions executes in interrupt context |
| 70 | + uint32_t sec = 1; |
| 71 | + if(data != NULL) { |
| 72 | + sec = *(uint32_t*)data; |
| 73 | + // Minimum is 1 second |
| 74 | + if (sec == 0){ |
| 75 | + sec = 1; |
| 76 | + } |
| 77 | + } |
| 78 | + alarmMatch_counter++; |
| 79 | + rtc.setAlarmEpoch( rtc.getEpoch() + sec); |
| 80 | +} |
| 81 | + |
0 commit comments