Skip to content

Commit fcb9ef9

Browse files
committed
new example to 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 <francois.ramu@st.com>
1 parent 1dd547b commit fcb9ef9

File tree

1 file changed

+78
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)