|
| 1 | +/* |
| 2 | + mixRTCAlarmSleep |
| 3 | +
|
| 4 | + This sketch shows how to configure the RTC alarm in MIX mode and go to sleep |
| 5 | +
|
| 6 | + Creation 12 Dec 2017 |
| 7 | + by Wi6Labs |
| 8 | + Modified 03 Jul 2020 |
| 9 | + by Frederic Pillon for STMicroelectronics |
| 10 | + Modified 03 Jul 2023 |
| 11 | + by Francois Ramu for STMicroelectronics |
| 12 | +
|
| 13 | + This example code is in the public domain. |
| 14 | +
|
| 15 | + https://github.com/stm32duino/STM32RTC |
| 16 | +*/ |
| 17 | + |
| 18 | +#include "STM32LowPower.h" |
| 19 | +#include <STM32RTC.h> |
| 20 | + |
| 21 | +/* Get the rtc object */ |
| 22 | +STM32RTC& rtc = STM32RTC::getInstance(); |
| 23 | + |
| 24 | +/* Change this value to set alarm match offset */ |
| 25 | +/* Note that STM32F1xx does not manage subsecond only second */ |
| 26 | +static uint32_t atime = 148; /* in MIX mode, this is 148 ticks * 3.9ms/tick = 578 ms */ |
| 27 | + |
| 28 | +// Declare it volatile since it's incremented inside an interrupt |
| 29 | +volatile int alarmMatch_counter = 0; |
| 30 | + |
| 31 | +/* Change these values to set the current initial time */ |
| 32 | +const byte seconds = 05; |
| 33 | +const byte minutes = 14; |
| 34 | +const byte hours = 16; |
| 35 | + |
| 36 | +/* Change these values to set the current initial date */ |
| 37 | +const byte day = 25; |
| 38 | +const byte month = 9; |
| 39 | +const byte year = 15; |
| 40 | + |
| 41 | +uint32_t timeout; |
| 42 | + |
| 43 | +void setup() |
| 44 | +{ |
| 45 | + Serial.begin(115200); |
| 46 | + |
| 47 | + // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK. |
| 48 | + // By default the LSI is selected as source : choose LSE |
| 49 | + rtc.setClockSource(STM32RTC::LSE_CLOCK); |
| 50 | +#if defined(RTC_BINARY_NONE) |
| 51 | + rtc.setBinaryMode(STM32RTC::MODE_MIX); |
| 52 | + rtc.begin(true); /* reset the RTC else the binary mode is not changed */ |
| 53 | +#else |
| 54 | + rtc.begin(); |
| 55 | +#endif /* RTC_BINARY_NONE */ |
| 56 | + // Configure low power |
| 57 | + LowPower.begin(); |
| 58 | + |
| 59 | + rtc.setTime(hours, minutes, seconds); |
| 60 | + rtc.setDate(day, month, year); |
| 61 | + |
| 62 | + /* wait for a while */ |
| 63 | + delay(200); |
| 64 | + |
| 65 | + pinMode(LED_BUILTIN, OUTPUT); |
| 66 | + |
| 67 | + LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime); |
| 68 | + rtc.disableAlarm(STM32RTC::ALARM_A); |
| 69 | + /* program Alarm in mix mode |
| 70 | + * Configure first alarm in 2 second : 2000ms is 512 ticks |
| 71 | + * then it will be done in the rtc callback |
| 72 | + */ |
| 73 | + timeout = rtc.getSubSeconds() + 512; |
| 74 | + |
| 75 | + rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_A); |
| 76 | + rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_A); |
| 77 | + |
| 78 | + Serial.printf("set Alarm at = %d\r\n", timeout); |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +void loop() |
| 83 | +{ |
| 84 | + Serial.print("Alarm Match: "); |
| 85 | + Serial.print(alarmMatch_counter); |
| 86 | + Serial.println(" times."); |
| 87 | + Serial.flush(); |
| 88 | + digitalWrite(LED_BUILTIN, HIGH); |
| 89 | + LowPower.deepSleep(); |
| 90 | + digitalWrite(LED_BUILTIN, LOW); |
| 91 | + LowPower.deepSleep(); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +void alarmMatch(void *data) |
| 96 | +{ |
| 97 | + // This function will be called once on device wakeup |
| 98 | + // You can do some little operations here (like changing variables which will be used in the loop) |
| 99 | + // Remember to avoid calling delay() and long running functions since this functions executes in interrupt context |
| 100 | + uint32_t epoc; |
| 101 | + uint32_t _millis = 1000; |
| 102 | + |
| 103 | + if (data != NULL) { |
| 104 | + _millis = *(uint32_t*)data; |
| 105 | + } |
| 106 | + |
| 107 | +#ifdef STM32F1xx |
| 108 | + uint32_t epoc_ms; |
| 109 | + uint32_t sec = 0; |
| 110 | + |
| 111 | + sec = _millis / 1000; |
| 112 | + // Minimum is 1 second |
| 113 | + if (sec == 0) { |
| 114 | + sec = 1; |
| 115 | + } |
| 116 | + epoc = rtc.getEpoch(&epoc_ms); |
| 117 | +#else |
| 118 | + epoc = rtc.getSubSeconds() + _millis; //in ms |
| 119 | +#endif |
| 120 | + alarmMatch_counter++; |
| 121 | + rtc.setAlarmSubSeconds(epoc, STM32RTC::ALARM_A); |
| 122 | +} |
| 123 | + |
0 commit comments