17
17
/* Get the rtc object */
18
18
STM32RTC& rtc = STM32RTC::getInstance();
19
19
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
20
25
// Time in second between blink
21
26
static uint32_t atime = 1 ;
27
+ #endif
22
28
23
29
// Declare it volatile since it's incremented inside an interrupt
24
30
volatile int alarmMatch_counter = 0 ;
@@ -41,21 +47,21 @@ void setup() {
41
47
pinMode (LED_BUILTIN, OUTPUT);
42
48
43
49
Serial.begin (9600 );
44
- while (!Serial) {}
50
+ while (!Serial) {}
45
51
46
52
// Configure low power
47
53
LowPower.begin ();
48
54
LowPower.enableWakeupFrom (&rtc, alarmMatch, &atime);
49
55
50
56
// 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 );
52
58
}
53
59
54
60
void loop () {
55
61
Serial.print (" Alarm Match: " );
56
62
Serial.print (alarmMatch_counter);
57
63
Serial.println (" times." );
58
- delay ( 100 );
64
+ Serial. flush ( );
59
65
digitalWrite (LED_BUILTIN, HIGH);
60
66
LowPower.deepSleep ();
61
67
digitalWrite (LED_BUILTIN, LOW);
@@ -67,6 +73,41 @@ void alarmMatch(void* data)
67
73
// This function will be called once on device wakeup
68
74
// You can do some little operations here (like changing variables which will be used in the loop)
69
75
// 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
70
111
uint32_t sec = 1 ;
71
112
if (data != NULL ) {
72
113
sec = *(uint32_t *)data;
@@ -77,5 +118,5 @@ void alarmMatch(void* data)
77
118
}
78
119
alarmMatch_counter++;
79
120
rtc.setAlarmEpoch ( rtc.getEpoch () + sec);
121
+ #endif
80
122
}
81
-
0 commit comments