Skip to content

Commit a200626

Browse files
committed
Update example to properly use millisecond
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent bc67d17 commit a200626

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

examples/AlarmTimedWakeup/AlarmTimedWakeup.ino

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
/* Get the rtc object */
1818
STM32RTC& rtc = STM32RTC::getInstance();
1919

20-
// Time in second between blink
21-
static uint32_t atime = 1;
20+
/* Change this value to set alarm match offset in millisecond */
21+
/* Note that STM32F1xx does not manage subsecond only second */
22+
static uint32_t atime = 567;
2223

2324
// Declare it volatile since it's incremented inside an interrupt
2425
volatile int alarmMatch_counter = 0;
@@ -41,21 +42,21 @@ void setup() {
4142
pinMode(LED_BUILTIN, OUTPUT);
4243

4344
Serial.begin(9600);
44-
while(!Serial) {}
45+
while (!Serial) {}
4546

4647
// Configure low power
4748
LowPower.begin();
4849
LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime);
4950

5051
// Configure first alarm in 2 second then it will be done in the rtc callback
51-
rtc.setAlarmEpoch( rtc.getEpoch() + 2 );
52+
rtc.setAlarmEpoch( rtc.getEpoch() + 2, rtc.MATCH_DHHMMSS, atime );
5253
}
5354

5455
void loop() {
5556
Serial.print("Alarm Match: ");
5657
Serial.print(alarmMatch_counter);
5758
Serial.println(" times.");
58-
delay(100);
59+
Serial.flush();
5960
digitalWrite(LED_BUILTIN, HIGH);
6061
LowPower.deepSleep();
6162
digitalWrite(LED_BUILTIN, LOW);
@@ -67,15 +68,37 @@ void alarmMatch(void* data)
6768
// This function will be called once on device wakeup
6869
// You can do some little operations here (like changing variables which will be used in the loop)
6970
// 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;
71+
uint32_t epoc;
72+
uint32_t epoc_ms;
73+
uint32_t sec = 0;
74+
uint32_t _millis = 1000;
75+
76+
if (data != NULL) {
77+
_millis = *(uint32_t*)data;
7378
// Minimum is 1 second
74-
if (sec == 0){
79+
if (sec == 0) {
7580
sec = 1;
7681
}
7782
}
83+
84+
sec = _millis / 1000;
85+
#ifdef STM32F1xx
86+
// Minimum is 1 second
87+
if (sec == 0) {
88+
sec = 1;
89+
}
90+
epoc = rtc.getEpoch(&epoc_ms);
91+
#else
92+
_millis = _millis % 1000;
93+
epoc = rtc.getEpoch(&epoc_ms);
94+
95+
//Update epoch_ms - might need to add a second to epoch
96+
epoc_ms += _millis;
97+
if (epoc_ms >= 1000) {
98+
sec ++;
99+
epoc_ms -= 1000;
100+
}
101+
#endif
78102
alarmMatch_counter++;
79-
rtc.setAlarmEpoch( rtc.getEpoch() + sec);
103+
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
80104
}
81-

0 commit comments

Comments
 (0)