1717/* Get the rtc object */
1818STM32RTC& 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
2126static uint32_t atime = 1 ;
27+ #endif
2228
2329// Declare it volatile since it's incremented inside an interrupt
2430volatile 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
5460void 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