Skip to content

Commit 5c214f4

Browse files
fprfpistm
fpr
authored andcommitted
Add examples
Signed-off-by: fpr <[email protected]>
1 parent ea29ec0 commit 5c214f4

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
ExternalWakeup
3+
4+
This sketch demonstrates the usage of External Interrupts (on pins) to wakeup
5+
a chip in sleep mode. Sleep modes allow a significant drop in the power usage
6+
of a board while it does nothing waiting for an event to happen.
7+
Battery powered application can take advantage of these modes to enhance
8+
battery life significantly.
9+
10+
In this sketch, pressing a pushbutton attached to pin will wake up the board.
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include "STM32LowPower.h"
16+
17+
// Blink sequence number
18+
// Declare it volatile since it's incremented inside an interrupt
19+
volatile int repetitions = 1;
20+
21+
// Pin used to trigger a wakeup
22+
const int pin = USER_BTN;
23+
24+
void setup() {
25+
pinMode(LED_BUILTIN, OUTPUT);
26+
// Set pin as INPUT_PULLUP to avoid spurious wakeup
27+
pinMode(pin, INPUT_PULLUP);
28+
29+
// Configure low power
30+
LowPower.begin();
31+
// Attach a wakeup interrupt on pin, calling repetitionsIncrease when the device is woken up
32+
LowPower.attachInterruptWakeup(pin, repetitionsIncrease, RISING);
33+
}
34+
35+
void loop() {
36+
for (int i = 0; i < repetitions; i++) {
37+
digitalWrite(LED_BUILTIN, HIGH);
38+
delay(500);
39+
digitalWrite(LED_BUILTIN, LOW);
40+
delay(500);
41+
}
42+
// Triggers an infinite sleep (the device will be woken up only by the registered wakeup sources)
43+
// The power consumption of the chip will drop consistently
44+
LowPower.sleep();
45+
}
46+
47+
void repetitionsIncrease() {
48+
// This function will be called once on device wakeup
49+
// You can do some little operations here (like changing variables which will be used in the loop)
50+
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
51+
repetitions ++;
52+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
SerialDeepSleep
3+
4+
This sketch demonstrates the usage of Serial Interrupts to wakeup a chip
5+
in deep sleep mode.
6+
7+
This sketch is compatible only with board supporting uart peripheral in
8+
stop mode.
9+
10+
This example code is in the public domain.
11+
*/
12+
13+
#include "STM32LowPower.h"
14+
15+
// Declare it volatile since it's incremented inside an interrupt
16+
volatile int wakeup_counter = 0;
17+
18+
void setup() {
19+
Serial.begin(9600);
20+
// initialize digital pin LED_BUILTIN as an output.
21+
pinMode(LED_BUILTIN, OUTPUT);
22+
// Configure low power
23+
LowPower.begin();
24+
// Enable UART in Low Power mode wakeup source
25+
LowPower.enableWakeupFrom(&Serial, SerialWakeup);
26+
Serial.println("Start deep sleep wakeup from Serial");
27+
}
28+
29+
void loop() {
30+
digitalWrite(LED_BUILTIN, HIGH);
31+
delay(500);
32+
digitalWrite(LED_BUILTIN, LOW);
33+
delay(500);
34+
// Triggers an infinite deep sleep
35+
// (the device will be woken up only by the registered wakeup sources)
36+
// The power consumption of the chip will drop consistently
37+
LowPower.deepSleep();
38+
39+
Serial.print(wakeup_counter);
40+
Serial.println(" wake up");
41+
42+
// Empty Serial Rx
43+
while(Serial.available()) {
44+
char c = Serial.read();
45+
Serial.print(c);
46+
}
47+
Serial.println();
48+
}
49+
50+
void SerialWakeup() {
51+
// This function will be called once on device wakeup
52+
// You can do some little operations here (like changing variables
53+
// which will be used in the loop)
54+
// Remember to avoid calling delay() and long running functions
55+
// since this functions executes in interrupt context
56+
wakeup_counter++;
57+
}

examples/TimedWakeup/TimedWakeup.ino

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
TimedWakeup
3+
4+
This sketch demonstrates the usage of Internal Interrupts to wakeup a chip
5+
in deep sleep mode.
6+
7+
In this sketch, the internal RTC will wake up the processor every second.
8+
9+
This example code is in the public domain.
10+
*/
11+
12+
#include "STM32LowPower.h"
13+
14+
void setup() {
15+
pinMode(LED_BUILTIN, OUTPUT);
16+
// Configure low power
17+
LowPower.begin();
18+
}
19+
20+
void loop() {
21+
digitalWrite(LED_BUILTIN, HIGH);
22+
LowPower.deepSleep(1000);
23+
digitalWrite(LED_BUILTIN, LOW);
24+
LowPower.deepSleep(1000);
25+
}

0 commit comments

Comments
 (0)