@@ -39,7 +39,7 @@ void rtc_init(void) {
39
39
rtc_hw -> clkdiv_m1 = rtc_freq ;
40
40
}
41
41
42
- static bool valid_datetime (datetime_t * t ) {
42
+ static bool valid_datetime (const datetime_t * t ) {
43
43
// Valid ranges taken from RTC doc. Note when setting an RTC alarm
44
44
// these values are allowed to be -1 to say "don't match this value"
45
45
if (!(t -> year >= 0 && t -> year <= 4095 )) return false;
@@ -52,7 +52,7 @@ static bool valid_datetime(datetime_t *t) {
52
52
return true;
53
53
}
54
54
55
- bool rtc_set_datetime (datetime_t * t ) {
55
+ bool rtc_set_datetime (const datetime_t * t ) {
56
56
if (!valid_datetime (t )) {
57
57
return false;
58
58
}
@@ -114,6 +114,14 @@ void rtc_enable_alarm(void) {
114
114
}
115
115
}
116
116
117
+ void rtc_disable_alarm (void ) {
118
+ // Disable matching and wait for it to stop being active
119
+ hw_clear_bits (& rtc_hw -> irq_setup_0 , RTC_IRQ_SETUP_0_MATCH_ENA_BITS );
120
+ while (rtc_hw -> irq_setup_0 & RTC_IRQ_SETUP_0_MATCH_ACTIVE_BITS ) {
121
+ tight_loop_contents ();
122
+ }
123
+ }
124
+
117
125
static void rtc_irq_handler (void ) {
118
126
// Always disable the alarm to clear the current IRQ.
119
127
// Even if it is a repeatable alarm, we don't want it to keep firing.
@@ -131,7 +139,7 @@ static void rtc_irq_handler(void) {
131
139
}
132
140
}
133
141
134
- static bool rtc_alarm_repeats (datetime_t * t ) {
142
+ static bool rtc_alarm_repeats (const datetime_t * t ) {
135
143
// If any value is set to -1 then we don't match on that value
136
144
// hence the alarm will eventually repeat
137
145
if (t -> year < 0 ) return true;
@@ -144,9 +152,15 @@ static bool rtc_alarm_repeats(datetime_t *t) {
144
152
return false;
145
153
}
146
154
147
- void rtc_set_alarm (datetime_t * t , rtc_callback_t user_callback ) {
155
+ bool rtc_set_alarm (datetime_t * t , rtc_callback_t user_callback ) {
148
156
rtc_disable_alarm ();
149
157
158
+ // Does it repeat? I.e. do we not match on any of the bits
159
+ _alarm_repeats = rtc_alarm_repeats (t );
160
+
161
+ if ( (!valid_datetime (t ) && !_alarm_repeats ) || !user_callback ) // none of the parameters is valid
162
+ return false;
163
+
150
164
// Only add to setup if it isn't -1
151
165
rtc_hw -> irq_setup_0 = ((t -> year < 0 ) ? 0 : (((uint )t -> year ) << RTC_IRQ_SETUP_0_YEAR_LSB )) |
152
166
((t -> month < 0 ) ? 0 : (((uint )t -> month ) << RTC_IRQ_SETUP_0_MONTH_LSB )) |
@@ -180,12 +194,5 @@ void rtc_set_alarm(datetime_t *t, rtc_callback_t user_callback) {
180
194
irq_set_enabled (RTC_IRQ , true);
181
195
182
196
rtc_enable_alarm ();
183
- }
184
-
185
- void rtc_disable_alarm (void ) {
186
- // Disable matching and wait for it to stop being active
187
- hw_clear_bits (& rtc_hw -> irq_setup_0 , RTC_IRQ_SETUP_0_MATCH_ENA_BITS );
188
- while (rtc_hw -> irq_setup_0 & RTC_IRQ_SETUP_0_MATCH_ACTIVE_BITS ) {
189
- tight_loop_contents ();
190
- }
197
+ return true;
191
198
}
0 commit comments