@@ -36,9 +36,9 @@ typedef struct {
36
36
bool alarm_a;
37
37
} cb_data_t ;
38
38
39
- static cb_data_t atime = { 2222 , true };
39
+ static cb_data_t atime = { 2222 , true };
40
40
#ifdef RTC_ALARM_B
41
- static cb_data_t btime = { 3333 , false };
41
+ static cb_data_t btime = { 3333 , false };
42
42
#endif
43
43
static byte seconds = 0 ;
44
44
static byte minutes = 0 ;
@@ -64,7 +64,7 @@ static uint8_t conv2d(const char* p) {
64
64
}
65
65
66
66
// sample input: date = "Dec 26 2009", time = "12:34:56"
67
- void initDateTime (void ) {
67
+ void initDateTime (void ) {
68
68
Serial.printf (" Build date & time %s, %s\n " , mydate, mytime);
69
69
70
70
year = conv2d (mydate + 9 );
@@ -89,15 +89,15 @@ void initDateTime (void) {
89
89
seconds = conv2d (mytime + 6 );
90
90
}
91
91
92
- void setup ()
93
- {
92
+ void setup () {
94
93
pinMode (USER_BTN, INPUT_PULLUP);
95
94
int32_t default_state = digitalRead (USER_BTN);
96
-
97
- Serial. begin ( 9600 );
98
- while (!Serial) ;
95
+ Serial. begin ( 115200 );
96
+ while (!Serial)
97
+ ;
99
98
// Wait user input to start
100
- while (digitalRead (USER_BTN) == default_state);
99
+ while (digitalRead (USER_BTN) == default_state)
100
+ ;
101
101
// Convenient function to init date and time variables
102
102
initDateTime ();
103
103
@@ -110,7 +110,7 @@ void setup()
110
110
#ifdef RTC_ALARM_B
111
111
rtc.attachInterrupt (alarmMatch, &btime, STM32RTC::ALARM_B);
112
112
#endif
113
- rtc.begin (); // Initialize RTC 24H format
113
+ rtc.begin (); // Initialize RTC 24H format
114
114
if (!rtc.isTimeSet ()) {
115
115
Serial.printf (" RTC time not set\n Set it.\n " );
116
116
// Set the time
@@ -129,6 +129,10 @@ void setup()
129
129
} else {
130
130
// RTC already initialized
131
131
time_t epoc, alarm_epoc;
132
+ rtc.getTime (&hours, &minutes, &seconds, &subSeconds, &period);
133
+ year = rtc.getYear ();
134
+ month = rtc.getMonth ();
135
+ day = rtc.getDay ();
132
136
if (rtc.isAlarmEnabled ()) {
133
137
rtc.enableAlarm (rtc.MATCH_DHHMMSS );
134
138
alarm_epoc = rtc.getAlarmEpoch ();
@@ -156,16 +160,28 @@ void setup()
156
160
#endif
157
161
Serial.printf (" RTC time already set\n " );
158
162
}
159
- Serial.printf (" Alarm A enable status: %s\n " , (rtc.isAlarmEnabled (STM32RTC::ALARM_A)) ? " True" : " False" );
163
+ // For STM32F1xx series, alarm is always disabled after a reset.
164
+ bool alarmA = rtc.isAlarmEnabled (STM32RTC::ALARM_A);
165
+ Serial.printf (" Alarm A enable status: %s\n " , (alarmA) ? " True" : " False" );
166
+ if (!alarmA) {
167
+ rtc.setAlarmDay (day);
168
+ rtc.setAlarmTime (hours, minutes, seconds + 5 , 567 );
169
+ rtc.enableAlarm (rtc.MATCH_DHHMMSS );
170
+ }
160
171
#ifdef RTC_ALARM_B
161
- Serial.printf (" Alarm B enable status: %s\n " , (rtc.isAlarmEnabled (STM32RTC::ALARM_B)) ? " True" : " False" );
172
+ bool alarmB = rtc.isAlarmEnabled (STM32RTC::ALARM_B);
173
+ Serial.printf (" Alarm B enable status: %s\n " , (alarmB) ? " True" : " False" );
174
+ if (!alarmB) {
175
+ rtc.setAlarmDay (day, STM32RTC::ALARM_B);
176
+ rtc.setAlarmTime (hours, minutes, seconds + 5 , 567 , STM32RTC::ALARM_B);
177
+ rtc.enableAlarm (rtc.MATCH_DHHMMSS , STM32RTC::ALARM_B);
178
+ }
162
179
#else
163
180
Serial.println (" Alarm B not available." );
164
181
#endif
165
182
}
166
183
167
- void loop ()
168
- {
184
+ void loop () {
169
185
rtc.getTime (&hours, &minutes, &seconds, &subSeconds, &period);
170
186
// Print current date & time
171
187
Serial.printf (" \n %02d/%02d/%02d %02d:%02d:%02d.%03d\n " , rtc.getDay (), rtc.getMonth (), rtc.getYear (), hours, minutes, seconds, subSeconds);
@@ -177,13 +193,12 @@ void loop()
177
193
delay (1000 );
178
194
}
179
195
180
- void alarmMatch (void *data)
181
- {
196
+ void alarmMatch (void * data) {
182
197
time_t epoc;
183
198
uint32_t epoc_ms;
184
199
uint32_t sec = 0 ;
185
200
uint32_t _millis = 1000 ;
186
- cb_data_t cbdata = {.next = 1000 , .alarm_a = true };
201
+ cb_data_t cbdata = { .next = 1000 , .alarm_a = true };
187
202
if (data != NULL ) {
188
203
cbdata.next = ((cb_data_t *)data)->next ;
189
204
cbdata.alarm_a = ((cb_data_t *)data)->alarm_a ;
@@ -204,7 +219,7 @@ void alarmMatch(void *data)
204
219
// Update epoch_ms - might need to add a second to epoch
205
220
epoc_ms += _millis;
206
221
if (epoc_ms >= 1000 ) {
207
- sec ++;
222
+ sec++;
208
223
epoc_ms -= 1000 ;
209
224
}
210
225
#endif
0 commit comments