Skip to content

Commit f945ab8

Browse files
committed
Update rtc.c
- use enum for multi-state value - code formatting
1 parent 61309a6 commit f945ab8

File tree

1 file changed

+30
-28
lines changed
  • src/rp2_common/hardware_rtc

1 file changed

+30
-28
lines changed

src/rp2_common/hardware_rtc/rtc.c

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313

1414
// Set this when setting an alarm
1515
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;
1724

1825
bool rtc_running(void) {
1926
return (rtc_hw->ctrl & RTC_CTRL_RTC_ACTIVE_BITS);
@@ -115,11 +122,11 @@ void rtc_enable_alarm(void) {
115122
}
116123

117124
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+
}
123130
}
124131

125132
static void rtc_irq_handler(void) {
@@ -131,9 +138,9 @@ static void rtc_irq_handler(void) {
131138
if (_alarm_repeats) {
132139
// If it is a repeatable alarm, re enable the alarm.
133140
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);
137144
}
138145
rtc_enable_alarm();
139146
}
@@ -148,9 +155,11 @@ static void rtc_irq_handler(void) {
148155
static int8_t rtc_alarm_repeats(const datetime_t *t) {
149156
// If any value is set to -1 then we don't match on that value
150157
// 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;
154163
}
155164

156165
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) {
165174
return false;
166175

167176
// 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
184186
datetime_t tNew;
185187
rtc_get_datetime(&tNew);
186188
s1 = RTC_IRQ_SETUP_1_SEC_ENA_BITS | ((((uint)tNew.sec + 1) % 60) << RTC_IRQ_SETUP_1_SEC_LSB);

0 commit comments

Comments
 (0)