Skip to content

Commit f221ace

Browse files
FRASTMfpistm
authored andcommitted
chare(examples): configure RTC alarm in BIN mode to wakeup from sleep
In this sketch the RTC is configured in BINary only mode (for the stm32 devices that supports it) An Alarm A is set to wakeup the system from low power deep sleep. Signed-off-by: Francois Ramu <[email protected]>
1 parent 2ae8cf6 commit f221ace

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
binAlarmTimedWakeup
3+
4+
This sketch demonstrates the usage of Internal Interrupts to wakeup a chip in deep sleep mode.
5+
when the RTC is configured in BINary mode (BIN_ONLY)
6+
7+
In this sketch:
8+
- RTC in BINARY only for the stm32 that supports this mode
9+
- Alarm is set to wake up the processor each 'atime' and called a custom alarm callback
10+
which increment a value and reload alarm with 'atime' offset.
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include "STM32LowPower.h"
16+
#include <STM32RTC.h>
17+
18+
/* Get the rtc object */
19+
STM32RTC& rtc = STM32RTC::getInstance();
20+
21+
/* Change this value to set alarm match offset in millisecond */
22+
static uint32_t atime = 600;
23+
static uint32_t time_ts; /* value to get the binary time */
24+
25+
// Declare it volatile since it's incremented inside an interrupt
26+
volatile int alarmMatch_counter = 0;
27+
28+
void setup() {
29+
// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
30+
// By default the LSI is selected as source.
31+
// rtc.setClockSource(STM32RTC::LSE_CLOCK);
32+
// Select the STM32RTC::MODE_BIN
33+
rtc.setBinaryMode(STM32RTC::MODE_BIN);
34+
rtc.begin(true); /* reset the RTC else the mode is not changed */
35+
36+
pinMode(LED_BUILTIN, OUTPUT);
37+
38+
Serial.begin(115200);
39+
while (!Serial) {}
40+
Serial.println(" Start !");
41+
42+
// Configure low power
43+
LowPower.begin();
44+
LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime);
45+
46+
rtc.getEpoch(&time_ts);
47+
48+
// Configure first alarm in 2 seconds then it will be done in the rtc callback
49+
rtc.setAlarmEpoch(0, STM32RTC::MATCH_SUBSEC, (time_ts + 2000), STM32RTC::ALARM_A);
50+
}
51+
52+
void loop() {
53+
Serial.print("Alarm Match: ");
54+
Serial.print(alarmMatch_counter);
55+
Serial.println(" times.");
56+
Serial.flush();
57+
digitalWrite(LED_BUILTIN, HIGH);
58+
LowPower.deepSleep();
59+
digitalWrite(LED_BUILTIN, LOW);
60+
LowPower.deepSleep();
61+
}
62+
63+
void alarmMatch(void* data) {
64+
// This function will be called once on device wakeup
65+
// You can do some little operations here (like changing variables which will be used in the loop)
66+
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
67+
uint32_t _millis = 1000;
68+
69+
if (data != NULL) {
70+
_millis = *(uint32_t*)data;
71+
}
72+
73+
rtc.getEpoch(&time_ts);
74+
alarmMatch_counter++;
75+
rtc.setAlarmEpoch(0, STM32RTC::MATCH_SUBSEC, (time_ts + _millis), STM32RTC::ALARM_A);
76+
}

0 commit comments

Comments
 (0)