17
17
/* Get the rtc object */
18
18
STM32RTC& rtc = STM32RTC::getInstance();
19
19
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 ;
22
23
23
24
// Declare it volatile since it's incremented inside an interrupt
24
25
volatile int alarmMatch_counter = 0 ;
@@ -41,21 +42,21 @@ void setup() {
41
42
pinMode (LED_BUILTIN, OUTPUT);
42
43
43
44
Serial.begin (9600 );
44
- while (!Serial) {}
45
+ while (!Serial) {}
45
46
46
47
// Configure low power
47
48
LowPower.begin ();
48
49
LowPower.enableWakeupFrom (&rtc, alarmMatch, &atime);
49
50
50
51
// 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 );
52
53
}
53
54
54
55
void loop () {
55
56
Serial.print (" Alarm Match: " );
56
57
Serial.print (alarmMatch_counter);
57
58
Serial.println (" times." );
58
- delay ( 100 );
59
+ Serial. flush ( );
59
60
digitalWrite (LED_BUILTIN, HIGH);
60
61
LowPower.deepSleep ();
61
62
digitalWrite (LED_BUILTIN, LOW);
@@ -67,15 +68,37 @@ void alarmMatch(void* data)
67
68
// This function will be called once on device wakeup
68
69
// You can do some little operations here (like changing variables which will be used in the loop)
69
70
// 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;
73
78
// Minimum is 1 second
74
- if (sec == 0 ){
79
+ if (sec == 0 ) {
75
80
sec = 1 ;
76
81
}
77
82
}
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
78
102
alarmMatch_counter++;
79
- rtc.setAlarmEpoch ( rtc. getEpoch () + sec);
103
+ rtc.setAlarmEpoch (epoc + sec, STM32RTC::MATCH_SS, epoc_ms );
80
104
}
81
-
0 commit comments