Skip to content

Commit b3fa3e6

Browse files
committed
- better const correctness
- reorder functions to context - check parameters for alarm
1 parent 6c4d25b commit b3fa3e6

File tree

2 files changed

+22
-14
lines changed
  • src/rp2_common/hardware_rtc

2 files changed

+22
-14
lines changed

src/rp2_common/hardware_rtc/include/hardware/rtc.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void rtc_init(void);
5050
* \param t Pointer to a \ref datetime_t structure contains time to set
5151
* \return true if set, false if the passed in datetime was invalid.
5252
*/
53-
bool rtc_set_datetime(datetime_t *t);
53+
bool rtc_set_datetime(const datetime_t *t);
5454

5555
/*! \brief Get the current time from the RTC
5656
* \ingroup hardware_rtc
@@ -71,8 +71,9 @@ bool rtc_running(void);
7171
*
7272
* \param t Pointer to a \ref datetime_t structure containing a time in the future to fire the alarm. Any values set to -1 will not be matched on.
7373
* \param user_callback pointer to a \ref rtc_callback_t to call when the alarm fires
74+
* \return false if parameters aren't valid
7475
*/
75-
void rtc_set_alarm(datetime_t *t, rtc_callback_t user_callback);
76+
bool rtc_set_alarm(const datetime_t *t, rtc_callback_t user_callback);
7677

7778
/*! \brief Enable the RTC alarm (if inactive)
7879
* \ingroup hardware_rtc

src/rp2_common/hardware_rtc/rtc.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void rtc_init(void) {
3939
rtc_hw->clkdiv_m1 = rtc_freq;
4040
}
4141

42-
static bool valid_datetime(datetime_t *t) {
42+
static bool valid_datetime(const datetime_t *t) {
4343
// Valid ranges taken from RTC doc. Note when setting an RTC alarm
4444
// these values are allowed to be -1 to say "don't match this value"
4545
if (!(t->year >= 0 && t->year <= 4095)) return false;
@@ -52,7 +52,7 @@ static bool valid_datetime(datetime_t *t) {
5252
return true;
5353
}
5454

55-
bool rtc_set_datetime(datetime_t *t) {
55+
bool rtc_set_datetime(const datetime_t *t) {
5656
if (!valid_datetime(t)) {
5757
return false;
5858
}
@@ -114,6 +114,14 @@ void rtc_enable_alarm(void) {
114114
}
115115
}
116116

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+
117125
static void rtc_irq_handler(void) {
118126
// Always disable the alarm to clear the current IRQ.
119127
// 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) {
131139
}
132140
}
133141

134-
static bool rtc_alarm_repeats(datetime_t *t) {
142+
static bool rtc_alarm_repeats(const datetime_t *t) {
135143
// If any value is set to -1 then we don't match on that value
136144
// hence the alarm will eventually repeat
137145
if (t->year < 0) return true;
@@ -144,9 +152,15 @@ static bool rtc_alarm_repeats(datetime_t *t) {
144152
return false;
145153
}
146154

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) {
148156
rtc_disable_alarm();
149157

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+
150164
// Only add to setup if it isn't -1
151165
rtc_hw->irq_setup_0 = ((t->year < 0) ? 0 : (((uint)t->year) << RTC_IRQ_SETUP_0_YEAR_LSB )) |
152166
((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) {
180194
irq_set_enabled(RTC_IRQ, true);
181195

182196
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;
191198
}

0 commit comments

Comments
 (0)