Skip to content

Commit 609a43c

Browse files
committed
Update example to properly use millisecond
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 2abf36f commit 609a43c

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

examples/AlarmTimedWakeup/AlarmTimedWakeup.ino

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

20+
#if defined(STM32_RTC_VERSION) && (STM32_RTC_VERSION >= 0x01010000)
21+
/* Change this value to set alarm match offset in millisecond */
22+
/* Note that STM32F1xx does not manage subsecond only second */
23+
static uint32_t atime = 567;
24+
#else
2025
// Time in second between blink
2126
static uint32_t atime = 1;
27+
#endif
2228

2329
// Declare it volatile since it's incremented inside an interrupt
2430
volatile int alarmMatch_counter = 0;
@@ -41,21 +47,21 @@ void setup() {
4147
pinMode(LED_BUILTIN, OUTPUT);
4248

4349
Serial.begin(9600);
44-
while(!Serial) {}
50+
while (!Serial) {}
4551

4652
// Configure low power
4753
LowPower.begin();
4854
LowPower.enableWakeupFrom(&rtc, alarmMatch, &atime);
4955

5056
// Configure first alarm in 2 second then it will be done in the rtc callback
51-
rtc.setAlarmEpoch( rtc.getEpoch() + 2 );
57+
rtc.setAlarmEpoch( rtc.getEpoch() + 2);
5258
}
5359

5460
void loop() {
5561
Serial.print("Alarm Match: ");
5662
Serial.print(alarmMatch_counter);
5763
Serial.println(" times.");
58-
delay(100);
64+
Serial.flush();
5965
digitalWrite(LED_BUILTIN, HIGH);
6066
LowPower.deepSleep();
6167
digitalWrite(LED_BUILTIN, LOW);
@@ -67,6 +73,41 @@ void alarmMatch(void* data)
6773
// This function will be called once on device wakeup
6874
// You can do some little operations here (like changing variables which will be used in the loop)
6975
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
76+
#if defined(STM32_RTC_VERSION) && (STM32_RTC_VERSION >= 0x01010000)
77+
uint32_t epoc;
78+
uint32_t epoc_ms;
79+
uint32_t sec = 0;
80+
uint32_t _millis = 1000;
81+
82+
if (data != NULL) {
83+
_millis = *(uint32_t*)data;
84+
// Minimum is 1 second
85+
if (sec == 0) {
86+
sec = 1;
87+
}
88+
}
89+
90+
sec = _millis / 1000;
91+
#ifdef STM32F1xx
92+
// Minimum is 1 second
93+
if (sec == 0) {
94+
sec = 1;
95+
}
96+
epoc = rtc.getEpoch(&epoc_ms);
97+
#else
98+
_millis = _millis % 1000;
99+
epoc = rtc.getEpoch(&epoc_ms);
100+
101+
//Update epoch_ms - might need to add a second to epoch
102+
epoc_ms += _millis;
103+
if (epoc_ms >= 1000) {
104+
sec ++;
105+
epoc_ms -= 1000;
106+
}
107+
#endif
108+
alarmMatch_counter++;
109+
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
110+
#else
70111
uint32_t sec = 1;
71112
if(data != NULL) {
72113
sec = *(uint32_t*)data;
@@ -77,5 +118,5 @@ void alarmMatch(void* data)
77118
}
78119
alarmMatch_counter++;
79120
rtc.setAlarmEpoch( rtc.getEpoch() + sec);
121+
#endif
80122
}
81-

0 commit comments

Comments
 (0)