Skip to content

Commit f411a8d

Browse files
fprfpistm
fpr
authored andcommitted
Add examples
Signed-off-by: fpr <[email protected]>
1 parent 0e8a355 commit f411a8d

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
+52
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+
}
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

+25
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)