Skip to content

Commit 0e8a355

Browse files
fprfpistm
fpr
authored andcommitted
Add Low Power API
Signed-off-by: fpr <[email protected]>
1 parent 9a20b89 commit 0e8a355

File tree

2 files changed

+303
-0
lines changed

2 files changed

+303
-0
lines changed

src/STM32LowPower.cpp

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/**
2+
******************************************************************************
3+
* @file STM32LowPower.cpp
4+
* @author WI6LABS
5+
* @version V1.0.0
6+
* @date 11-December-2017
7+
* @brief Provides a STM32 Low Power interface with Arduino
8+
*
9+
******************************************************************************
10+
* @attention
11+
*
12+
* <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
13+
*
14+
* Redistribution and use in source and binary forms, with or without modification,
15+
* are permitted provided that the following conditions are met:
16+
* 1. Redistributions of source code must retain the above copyright notice,
17+
* this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright notice,
19+
* this list of conditions and the following disclaimer in the documentation
20+
* and/or other materials provided with the distribution.
21+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
22+
* may be used to endorse or promote products derived from this software
23+
* without specific prior written permission.
24+
*
25+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*
36+
******************************************************************************
37+
*/
38+
39+
#include "STM32LowPower.h"
40+
41+
STM32LowPower LowPower;
42+
43+
44+
STM32LowPower::STM32LowPower()
45+
{
46+
_configured = false;
47+
_serial = NULL;
48+
}
49+
50+
/**
51+
* @brief Initializes the low power mode
52+
* @param None
53+
* @retval None
54+
*/
55+
void STM32LowPower::begin(void)
56+
{
57+
LowPower_init();
58+
_configured = true;
59+
}
60+
61+
/**
62+
* @brief Enable the idle low power mode (STM32 sleep). Exit this mode on
63+
* interrupt or in n milliseconds.
64+
* @param millis: optional delay before leave the idle mode (default: 0).
65+
* @retval None
66+
*/
67+
void STM32LowPower::idle(uint32_t millis)
68+
{
69+
if(millis > 0) {
70+
programRtcWakeUp(millis);
71+
}
72+
LowPower_sleep(PWR_MAINREGULATOR_ON);
73+
}
74+
75+
/**
76+
* @brief Enable the sleep low power mode (STM32 sleep). Exit this mode on
77+
* interrupt or in n milliseconds.
78+
* @param millis: optional delay before leave the sleep mode (default: 0).
79+
* @retval None
80+
*/
81+
void STM32LowPower::sleep(uint32_t millis)
82+
{
83+
if(millis > 0) {
84+
programRtcWakeUp(millis);
85+
}
86+
LowPower_sleep(PWR_LOWPOWERREGULATOR_ON);
87+
}
88+
89+
/**
90+
* @brief Enable the deepsleep low power mode (STM32 stop). Exit this mode on
91+
* interrupt or in n milliseconds.
92+
* @param millis: optional delay before leave the deepSleep mode (default: 0).
93+
* @retval None
94+
*/
95+
void STM32LowPower::deepSleep(uint32_t millis)
96+
{
97+
if(millis > 0) {
98+
programRtcWakeUp(millis);
99+
}
100+
LowPower_stop(_serial);
101+
}
102+
103+
/**
104+
* @brief Enable the shutdown low power mode (STM32 shutdown or standby mode).
105+
* Exit this mode on interrupt or in n milliseconds.
106+
* @param millis: optional delay before leave the shutdown mode (default: 0).
107+
* @retval None
108+
*/
109+
void STM32LowPower::shutdown(uint32_t millis)
110+
{
111+
if(millis > 0) {
112+
programRtcWakeUp(millis);
113+
}
114+
LowPower_shutdown();
115+
}
116+
117+
/**
118+
* @brief Enable GPIO pin in interrupt mode. If the pin is a wakeup pin, it is
119+
* configured as wakeup source.
120+
* @param pin: pin number
121+
* @param callback: pointer to callback function.
122+
* @param mode: pin interrupt mode (HIGH, LOW, RISING, FALLING or CHANGE)
123+
* @retval None
124+
*/
125+
void STM32LowPower::attachInterruptWakeup(uint32_t pin, voidFuncPtrVoid callback, uint32_t mode)
126+
{
127+
// all GPIO for idle (smt32 sleep) and sleep (stm32 stop)
128+
attachInterrupt(pin, callback, mode);
129+
130+
// If Gpio is a Wake up pin activate it for deepSleep (standby stm32) and shutdown
131+
LowPower_EnableWakeUpPin(pin, mode);
132+
}
133+
134+
/**
135+
* @brief Enable a serial interface as a wakeup source.
136+
* @param serial: pointer to a HardwareSerial
137+
* @param callback: pointer to callback function called when leave the low power
138+
* mode.
139+
* @retval None
140+
*/
141+
void STM32LowPower::enableWakeupFrom(HardwareSerial *serial, voidFuncPtrVoid callback)
142+
{
143+
if(serial != NULL) {
144+
_serial = &(serial->_serial);
145+
// Reconfigure serial for low power mode (using HSI as clock source)
146+
serial->configForLowPower();
147+
LowPower_EnableWakeUpUart(_serial, callback);
148+
}
149+
}
150+
151+
/**
152+
* @brief Attach a callback to a RTC alarm.
153+
* @param rtc: pointer to a STM32RTC
154+
* @param callback: pointer to callback function called when leave the low power
155+
* mode.
156+
* @retval None
157+
*/
158+
void STM32LowPower::enableWakeupFrom(STM32RTC *rtc, voidFuncPtr callback)
159+
{
160+
rtc->attachInterrupt(callback);
161+
}
162+
163+
/**
164+
* @brief Configure the RTC alarm
165+
* @param millis: time of the alarm in milliseconds. At least 1000ms.
166+
* @retval None
167+
*/
168+
void STM32LowPower::programRtcWakeUp(uint32_t millis)
169+
{
170+
int epoc;
171+
uint32_t sec;
172+
STM32RTC& rtc = STM32RTC::getInstance();
173+
174+
if(millis > 0) {
175+
if (!rtc.isConfigured()){
176+
// LSE must be selected as clock source to wakeup the device from shutdown mode
177+
rtc.setClockSource(STM32RTC::RTC_LSE_CLOCK);
178+
//Enable RTC
179+
rtc.begin();
180+
} else {
181+
if (rtc.getClockSource() != STM32RTC::RTC_LSE_CLOCK) {
182+
// Save current config
183+
STM32RTC::RTC_AM_PM period;
184+
uint32_t subSeconds;
185+
uint8_t seconds, minutes, hours, weekDay, day, month, years;
186+
rtc.getDate(&weekDay, &day, &month, &years);
187+
rtc.getTime(&seconds, &minutes, &hours, &subSeconds, &period);
188+
rtc.end();
189+
// LSE must be selected as clock source to wakeup the device from shutdown mode
190+
rtc.setClockSource(STM32RTC::RTC_LSE_CLOCK);
191+
//Enable RTC
192+
rtc.begin(period);
193+
// Restore config
194+
rtc.setTime(seconds, minutes, hours, subSeconds, period);
195+
rtc.setDate(weekDay, day, month, years);
196+
}
197+
}
198+
199+
// convert millisecond to second
200+
sec = millis / 1000;
201+
// Minimum is 1 second
202+
if (sec == 0){
203+
sec = 1;
204+
}
205+
206+
epoc = rtc.getEpoch();
207+
rtc.setAlarmEpoch( epoc + sec );
208+
}
209+
210+
}

src/STM32LowPower.h

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
******************************************************************************
3+
* @file STM32LowPower.h
4+
* @author WI6LABS
5+
* @version V1.0.0
6+
* @date 11-December-2017
7+
* @brief Provides a STM32 Low Power interface with Arduino
8+
*
9+
******************************************************************************
10+
* @attention
11+
*
12+
* <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
13+
*
14+
* Redistribution and use in source and binary forms, with or without modification,
15+
* are permitted provided that the following conditions are met:
16+
* 1. Redistributions of source code must retain the above copyright notice,
17+
* this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright notice,
19+
* this list of conditions and the following disclaimer in the documentation
20+
* and/or other materials provided with the distribution.
21+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
22+
* may be used to endorse or promote products derived from this software
23+
* without specific prior written permission.
24+
*
25+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*
36+
******************************************************************************
37+
*/
38+
39+
#ifndef _STM32_LOW_POWER_H_
40+
#define _STM32_LOW_POWER_H_
41+
42+
#include <Arduino.h>
43+
44+
// Check if PWR HAL enable in variants/board_name/stm32yzxx_hal_conf.h
45+
#ifndef HAL_PWR_MODULE_ENABLED
46+
#error "PWR configuration is missing. Check flag HAL_PWR_MODULE_ENABLED in variants/board_name/stm32yzxx_hal_conf.h"
47+
#endif
48+
49+
#include "STM32RTC.h"
50+
#include "Wire.h"
51+
52+
typedef void (*voidFuncPtrVoid)( void ) ;
53+
54+
class STM32LowPower {
55+
public:
56+
STM32LowPower();
57+
58+
void begin(void);
59+
60+
void idle(uint32_t millis = 0);
61+
void idle(int millis) {
62+
idle((uint32_t)millis);
63+
}
64+
65+
void sleep(uint32_t millis = 0);
66+
void sleep(int millis) {
67+
sleep((uint32_t)millis);
68+
}
69+
70+
void deepSleep(uint32_t millis = 0);
71+
void deepSleep(int millis) {
72+
deepSleep((uint32_t)millis);
73+
}
74+
75+
void shutdown(uint32_t millis = 0);
76+
void shutdown(int millis) {
77+
shutdown((uint32_t)millis);
78+
}
79+
80+
void attachInterruptWakeup(uint32_t pin, voidFuncPtrVoid callback, uint32_t mode);
81+
82+
void enableWakeupFrom(HardwareSerial *serial, voidFuncPtrVoid callback);
83+
void enableWakeupFrom(STM32RTC *rtc, voidFuncPtr callback);
84+
85+
private:
86+
bool _configured; /* Low Power mode initialization status */
87+
serial_t *_serial; /* Serial for wakeup from deep sleep */
88+
void programRtcWakeUp(uint32_t millis);
89+
};
90+
91+
extern STM32LowPower LowPower;
92+
93+
#endif // _STM32_LOW_POWER_H_

0 commit comments

Comments
 (0)