Skip to content

Commit eaeedd7

Browse files
authored
Merge pull request #103 from FRASTM/rtc_mix_ms
sketch using the Rtc mix mode with value in milliseconds
2 parents fa8f64f + f221ace commit eaeedd7

File tree

2 files changed

+96
-8
lines changed

2 files changed

+96
-8
lines changed

Diff for: examples/AlarmTimedWakeup/AlarmTimedWakeup.ino

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/*
2-
AdvancedTimedWakeup
2+
AlarmTimedWakeup
33
4-
This sketch demonstrates the usage of Internal Interrupts to wakeup a chip in deep sleep mode.
4+
This sketch demonstrates the usage of Internal Interrupts to wakeup a chip in deep sleep mode,
5+
when the RTC is configured in BCD or MIX mode (BCD and BINARY)
56
67
In this sketch:
8+
- RTC is configured in BCD (default) or MIX mode (BCD and BINARY)
79
- RTC date and time are configured.
810
- Alarm is set to wake up the processor each 'atime' and called a custom alarm callback
911
which increment a value and reload alarm with 'atime' offset.
@@ -19,7 +21,7 @@ STM32RTC& rtc = STM32RTC::getInstance();
1921

2022
/* Change this value to set alarm match offset in millisecond */
2123
/* Note that STM32F1xx does not manage subsecond only second */
22-
static uint32_t atime = 567;
24+
static uint32_t atime = 600;
2325

2426
// Declare it volatile since it's incremented inside an interrupt
2527
volatile int alarmMatch_counter = 0;
@@ -35,21 +37,32 @@ static byte month = 1;
3537
static byte year = 18;
3638

3739
void setup() {
40+
#if defined(RTC_BINARY_NONE)
41+
// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
42+
// By default the LSI is selected as source.
43+
// rtc.setClockSource(STM32RTC::LSE_CLOCK);
44+
// Select the STM32RTC::MODE_BCD or STM32RTC::MODE_MIX
45+
// By default the STM32RTC::MODE_BCD is selected.
46+
// rtc.setBinaryMode(STM32RTC::MODE_BCD);
47+
rtc.begin(true); /* reset the RTC else the binary mode is not changed */
48+
#else
3849
rtc.begin();
50+
#endif /* RTC_BINARY_NONE */
3951
rtc.setTime(hours, minutes, seconds);
4052
rtc.setDate(weekDay, day, month, year);
4153

4254
pinMode(LED_BUILTIN, OUTPUT);
4355

44-
Serial.begin(9600);
56+
Serial.begin(115200);
4557
while (!Serial) {}
58+
Serial.println(" Start !");
4659

4760
// Configure low power
4861
LowPower.begin();
4962
LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime);
5063

5164
// Configure first alarm in 2 second then it will be done in the rtc callback
52-
rtc.setAlarmEpoch( rtc.getEpoch() + 2);
65+
rtc.setAlarmEpoch(rtc.getEpoch() + 2);
5366
}
5467

5568
void loop() {
@@ -63,8 +76,7 @@ void loop() {
6376
LowPower.deepSleep();
6477
}
6578

66-
void alarmMatch(void* data)
67-
{
79+
void alarmMatch(void* data) {
6880
// This function will be called once on device wakeup
6981
// You can do some little operations here (like changing variables which will be used in the loop)
7082
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
@@ -91,7 +103,7 @@ void alarmMatch(void* data)
91103
//Update epoch_ms - might need to add a second to epoch
92104
epoc_ms += _millis;
93105
if (epoc_ms >= 1000) {
94-
sec ++;
106+
sec++;
95107
epoc_ms -= 1000;
96108
}
97109
#endif

Diff for: examples/binAlarmTimedWakeup/binAlarmTimedWakeup.ino

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