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