13
13
14
14
// Set this when setting an alarm
15
15
static rtc_callback_t _callback = NULL ;
16
- static int8_t _alarm_repeats = 0 ;
16
+
17
+ typedef enum {
18
+ NO_REPEAT = 0 ,
19
+ CONTINUOUS_REPEAT = 1 ,
20
+ CONTINUOUS_REPEAT_ON_SEC = 2 ,
21
+ } repeat_type ;
22
+
23
+ static repeat_type _alarm_repeats = NO_REPEAT ;
17
24
18
25
bool rtc_running (void ) {
19
26
return (rtc_hw -> ctrl & RTC_CTRL_RTC_ACTIVE_BITS );
@@ -115,11 +122,11 @@ void rtc_enable_alarm(void) {
115
122
}
116
123
117
124
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
- }
125
+ // Disable matching and wait for it to stop being active
126
+ hw_clear_bits (& rtc_hw -> irq_setup_0 , RTC_IRQ_SETUP_0_MATCH_ENA_BITS );
127
+ while (rtc_hw -> irq_setup_0 & RTC_IRQ_SETUP_0_MATCH_ACTIVE_BITS ) {
128
+ tight_loop_contents ();
129
+ }
123
130
}
124
131
125
132
static void rtc_irq_handler (void ) {
@@ -131,9 +138,9 @@ static void rtc_irq_handler(void) {
131
138
if (_alarm_repeats ) {
132
139
// If it is a repeatable alarm, re enable the alarm.
133
140
if (_alarm_repeats == -1 ) {
134
- datetime_t tNew ;
135
- rtc_get_datetime (& tNew );
136
- rtc_hw -> irq_setup_1 = RTC_IRQ_SETUP_1_SEC_ENA_BITS | ((((uint )tNew .sec + 1 ) % 60 ) << RTC_IRQ_SETUP_1_SEC_LSB );
141
+ datetime_t t ;
142
+ rtc_get_datetime (& t );
143
+ rtc_hw -> irq_setup_1 = RTC_IRQ_SETUP_1_SEC_ENA_BITS | ((((uint )t .sec + 1 ) % 60 ) << RTC_IRQ_SETUP_1_SEC_LSB );
137
144
}
138
145
rtc_enable_alarm ();
139
146
}
@@ -148,9 +155,11 @@ static void rtc_irq_handler(void) {
148
155
static int8_t rtc_alarm_repeats (const datetime_t * t ) {
149
156
// If any value is set to -1 then we don't match on that value
150
157
// hence the alarm will eventually repeat
151
- if (t -> year & t -> month & t -> day & t -> dotw & t -> hour & t -> min & t -> sec < 0 )
152
- return - 1 ;
153
- return ((t -> year | t -> month | t -> day | t -> dotw | t -> hour | t -> min | t -> sec ) < 0 );
158
+ if ((t -> year & t -> month & t -> day & t -> dotw & t -> hour & t -> min & t -> sec ) < 0 ) return CONTINUOUS_REPEAT_ON_SEC ;
159
+
160
+ return (t -> year < 0 || t -> month < 0 || t -> day < 0 || t -> dotw < 0
161
+ || t -> hour < 0 || t -> min < 0 || t -> sec < 0 )
162
+ ? CONTINUOUS_REPEAT : NO_REPEAT ;
154
163
}
155
164
156
165
bool rtc_set_alarm (const datetime_t * t , rtc_callback_t user_callback ) {
@@ -165,22 +174,15 @@ bool rtc_set_alarm(const datetime_t *t, rtc_callback_t user_callback) {
165
174
return false;
166
175
167
176
// Set the match enable bits for things we care about
168
- if (t -> year >= 0 )
169
- s0 |= RTC_IRQ_SETUP_0_YEAR_ENA_BITS | (((uint )t -> year ) << RTC_IRQ_SETUP_0_YEAR_LSB );
170
- if (t -> month >= 1 )
171
- s0 |= RTC_IRQ_SETUP_0_MONTH_ENA_BITS | (((uint )t -> month ) << RTC_IRQ_SETUP_0_MONTH_LSB );
172
- if (t -> day >= 1 )
173
- s0 |= RTC_IRQ_SETUP_0_DAY_ENA_BITS | (((uint )t -> day ) << RTC_IRQ_SETUP_0_DAY_LSB );
174
- if (t -> dotw >= 0 )
175
- s1 |= RTC_IRQ_SETUP_1_DOTW_ENA_BITS | (((uint )t -> dotw ) << RTC_IRQ_SETUP_1_DOTW_LSB );
176
- if (t -> hour >= 0 )
177
- s1 |= RTC_IRQ_SETUP_1_HOUR_ENA_BITS | (((uint )t -> hour ) << RTC_IRQ_SETUP_1_HOUR_LSB );
178
- if (t -> min >= 0 )
179
- s1 |= RTC_IRQ_SETUP_1_MIN_ENA_BITS | (((uint )t -> min ) << RTC_IRQ_SETUP_1_MIN_LSB );
180
- if (t -> sec >= 0 )
181
- s1 |= RTC_IRQ_SETUP_1_SEC_ENA_BITS | (((uint )t -> sec ) << RTC_IRQ_SETUP_1_SEC_LSB );
182
- else if (_alarm_repeats == -1 ) // repeatable every second! All entries are -1
183
- {
177
+ if (t -> year >= 0 ) s0 |= RTC_IRQ_SETUP_0_YEAR_ENA_BITS | (((uint )t -> year ) << RTC_IRQ_SETUP_0_YEAR_LSB );
178
+ if (t -> month >= 1 ) s0 |= RTC_IRQ_SETUP_0_MONTH_ENA_BITS | (((uint )t -> month ) << RTC_IRQ_SETUP_0_MONTH_LSB );
179
+ if (t -> day >= 1 ) s0 |= RTC_IRQ_SETUP_0_DAY_ENA_BITS | (((uint )t -> day ) << RTC_IRQ_SETUP_0_DAY_LSB );
180
+ if (t -> dotw >= 0 ) s1 |= RTC_IRQ_SETUP_1_DOTW_ENA_BITS | (((uint )t -> dotw ) << RTC_IRQ_SETUP_1_DOTW_LSB );
181
+ if (t -> hour >= 0 ) s1 |= RTC_IRQ_SETUP_1_HOUR_ENA_BITS | (((uint )t -> hour ) << RTC_IRQ_SETUP_1_HOUR_LSB );
182
+ if (t -> min >= 0 ) s1 |= RTC_IRQ_SETUP_1_MIN_ENA_BITS | (((uint )t -> min ) << RTC_IRQ_SETUP_1_MIN_LSB );
183
+ if (t -> sec >= 0 ) s1 |= RTC_IRQ_SETUP_1_SEC_ENA_BITS | (((uint )t -> sec ) << RTC_IRQ_SETUP_1_SEC_LSB );
184
+ else if (_alarm_repeats == CONTINUOUS_REPEAT_ON_SEC ) {
185
+ // repeatable every second! All entries are -1
184
186
datetime_t tNew ;
185
187
rtc_get_datetime (& tNew );
186
188
s1 = RTC_IRQ_SETUP_1_SEC_ENA_BITS | ((((uint )tNew .sec + 1 ) % 60 ) << RTC_IRQ_SETUP_1_SEC_LSB );
0 commit comments