Skip to content

Commit c494863

Browse files
authored
Merge pull request #2 from fpistm/pr1-review
STM32 Low Power library
2 parents 0c1fa59 + 0421115 commit c494863

File tree

9 files changed

+598
-0
lines changed

9 files changed

+598
-0
lines changed

README.md

+62
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,64 @@
11
# STM32LowPower
22
Arduino library to support Low Power
3+
4+
## API
5+
6+
* **`void begin()`**: configure the Low Power
7+
8+
* **`void idle(uint32_t millis)`**: enter in idle mode
9+
**param** millis (optional): number of milliseconds before to exit the mode. At least 1000 ms. The RTC is used in alarm mode to wakeup the chip in millis milliseconds.
10+
11+
* **`void sleep(uint32_t millis)`**: enter in sleep mode
12+
**param** millis (optional): number of milliseconds before to exit the mode. At least 1000 ms. The RTC is used in alarm mode to wakeup the chip in millis milliseconds.
13+
14+
* **`void deepSleep(uint32_t millis)`**: enter in deepSleep mode
15+
**param** millis (optional): number of milliseconds before to exit the mode. At least 1000 ms. The RTC is used in alarm mode to wakeup the chip in millis milliseconds.
16+
17+
* **`void shutdown(uint32_t millis)`**: enter in shutdown mode
18+
**param** millis (optional): number of milliseconds before to exit the mode. At least 1000 ms. The RTC is used in alarm mode to wakeup the board in millis milliseconds.
19+
20+
* **`void attachInterruptWakeup(uint32_t pin, voidFuncPtrVoid callback, uint32_t mode)`**: Enable GPIO pin in interrupt mode. If the pin is a wakeup pin, it is configured as wakeup source (see board documentation).
21+
**param** pin: pin number
22+
**param** callback: pointer to callback
23+
**param** mode: interrupt mode (HIGH, LOW, RISING, FALLING or CHANGE)
24+
25+
* **`void enableWakeupFrom(HardwareSerial *serial, voidFuncPtrVoid callback)`**: enable a UART peripheral in low power mode. See board documentation for low power mode compatibility.
26+
**param** serial: pointer to a UART
27+
**param** callback: pointer to a callback to call when the board is waked up.
28+
29+
* **`void enableWakeupFrom(TwoWire *wire, voidFuncPtrVoid callback)`**:
30+
enable an I2C peripheral in low power mode. See board documentation for low power mode compatibility.
31+
**param** wire: pointer to I2C
32+
**param** callback: pointer to a callback to call when the board is waked up.
33+
34+
* **`void enableWakeupFrom(STM32RTC *rtc, voidFuncPtr callback)`**
35+
attach a callback to the RTC peripheral.
36+
**param** rtc: pointer to RTC
37+
**param** callback: pointer to a callback to call when the board is waked up.
38+
39+
`Begin()` function must be called at least once before `idle()`, `sleep()`, `deepSleep()` or `shutdown()` functions.
40+
41+
`attachInterruptWakeup()` or `enableWakeupFrom()` functions should be called before `idle()`, `sleep()`, `deepSleep()` or `shutdown()` functions.
42+
43+
The board will restart when exit the deepSleep or shutdown mode.
44+
45+
## Hardware state
46+
47+
* **Idle mode**: low wake-up latency (µs range) (e.g. ARM WFI). Memories and
48+
voltage supplies are retained. Minimal power saving mainly on the core itself.
49+
50+
* **sleep mode**: low wake-up latency (µs range) (e.g. ARM WFI), Memories and
51+
voltage supplies are retained. Minimal power saving mainly on the core itself but
52+
higher than idle mode.
53+
54+
* **deep sleep mode**: medium latency (ms range), clocks are gated to reduced. Memories
55+
and voltage supplies are retained. If supported, Peripherals wake-up is possible (UART, I2C ...).
56+
57+
* **shutdown mode**: high wake-up latency (posible hundereds of ms or second
58+
timeframe), voltage supplies are cut except always-on domain, memory content
59+
are lost and system basically reboots.
60+
61+
## Source
62+
63+
You can find the source files at
64+
https://github.com/stm32duino/STM32LowPower
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+
+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+
}

keywords.txt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#######################################
2+
# Syntax Coloring Map For Energy Saving
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
ArduinoLowPower KEYWORD1
10+
LowPower KEYWORD1
11+
12+
#######################################
13+
# Methods and Functions (KEYWORD2)
14+
#######################################
15+
16+
begin KEYWORD2
17+
idle KEYWORD2
18+
sleep KEYWORD2
19+
deepSleep KEYWORD2
20+
shutdown KEYWORD2
21+
attachInterruptWakeup KEYWORD2
22+
enableWakeupFrom KEYWORD2
23+
24+
#######################################
25+
# Constants (LITERAL1)
26+
#######################################

library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=STM32duino Low Power
2+
version=1.0.0
3+
author=Wi6Labs
4+
maintainer=stm32duino
5+
sentence=Power save primitives features for STM32 boards
6+
paragraph=With this library you can manage the low power states of STM32 boards
7+
category=Device Control
8+
url=https://github.com/stm32duino/STM32LowPower
9+
architectures=stm32

0 commit comments

Comments
 (0)