WIP RP RTC alarm
@@ -110,6 +110,9 @@ | ||
110 | 110 | while ((rtcp->rtc->CTRL & RTC_CTRL_RTC_ACTIVE_BITS) != 0) |
111 | 111 | ; |
112 | 112 | |
113 | + /* Entering a reentrant critical zone.*/ | |
114 | + syssts_t sts = osalSysGetStatusAndLockX(); | |
115 | + | |
113 | 116 | /* Write setup to pre-load registers. */ |
114 | 117 | rtcp->rtc->SETUP0 = |
115 | 118 | ((timespec->year + 1980) << RTC_SETUP_0_YEAR_LSB) | |
@@ -126,6 +129,10 @@ | ||
126 | 129 | |
127 | 130 | /* Enable RTC and wait for active. */ |
128 | 131 | rtcp->rtc->CTRL = RTC_CTRL_RTC_ENABLE_BITS; |
132 | + | |
133 | + /* Leaving a reentrant critical zone.*/ | |
134 | + osalSysRestoreStatusX(sts); | |
135 | + | |
129 | 136 | while ((rtcp->rtc->CTRL & RTC_CTRL_RTC_ACTIVE_BITS) == 0) |
130 | 137 | ; |
131 | 138 | } |
@@ -141,10 +148,16 @@ | ||
141 | 148 | */ |
142 | 149 | void rtc_lld_get_time(RTCDriver *rtcp, RTCDateTime *timespec) { |
143 | 150 | |
151 | + /* Entering a reentrant critical zone.*/ | |
152 | + syssts_t sts = osalSysGetStatusAndLockX(); | |
153 | + | |
144 | 154 | /* Read RTC0 first then RTC1. */ |
145 | 155 | uint32_t rtc_0 = rtcp->rtc->RTC0; |
146 | 156 | uint32_t rtc_1 = rtcp->rtc->RTC1; |
147 | 157 | |
158 | + /* Leaving a reentrant critical zone.*/ | |
159 | + osalSysRestoreStatusX(sts); | |
160 | + | |
148 | 161 | /* Calculate and set milliseconds since midnight field. */ |
149 | 162 | timespec->millisecond = |
150 | 163 | ((((rtc_0 & RTC_RTC_0_HOUR_BITS) >> RTC_RTC_0_HOUR_LSB) * 3600) |
@@ -179,9 +192,49 @@ | ||
179 | 192 | rtcalarm_t alarm, |
180 | 193 | const RTCAlarm *alarmspec) { |
181 | 194 | |
182 | - (void)rtcp; | |
183 | 195 | (void)alarm; |
184 | - (void)alarmspec; | |
196 | + RTCDateTime *t = &alarmspec->alarm; | |
197 | + uint32_t sec = (uint32_t)t->millisecond / 1000; | |
198 | + uint32_t hour = sec / 3600; | |
199 | + sec %= 3600; | |
200 | + uint32_t min = sec / 60; | |
201 | + sec %= 60; | |
202 | + | |
203 | + rtc_disable_alarm(); | |
204 | + | |
205 | + // Only add to setup if it isn't -1 | |
206 | + rtcp->rtc->IRQSETUP0 = ((t->year < 0) ? 0 : (((uint)t->year) << RTC_IRQ_SETUP_0_YEAR_LSB )) | | |
207 | + ((t->month < 0) ? 0 : (((uint)t->month) << RTC_IRQ_SETUP_0_MONTH_LSB)) | | |
208 | + ((t->day < 0) ? 0 : (((uint)t->day) << RTC_IRQ_SETUP_0_DAY_LSB )); | |
209 | + rtcp->rtc->IRQSETUP1 = ((t->dotw == 0) ? 0 : (((uint)t->dotw) << RTC_IRQ_SETUP_1_DOTW_LSB)) | | |
210 | + (t->hour << RTC_IRQ_SETUP_1_HOUR_LSB)) | | |
211 | + (t->min << RTC_IRQ_SETUP_1_MIN_LSB )) | | |
212 | + (t->sec << RTC_IRQ_SETUP_1_SEC_LSB )); | |
213 | + | |
214 | + // Set the match enable bits for things we care about | |
215 | + if (t->year >= 0) hw_set_bits(&rtc_hw->irq_setup_0, RTC_IRQ_SETUP_0_YEAR_ENA_BITS); | |
216 | + if (t->month >= 0) hw_set_bits(&rtc_hw->irq_setup_0, RTC_IRQ_SETUP_0_MONTH_ENA_BITS); | |
217 | + if (t->day >= 0) hw_set_bits(&rtc_hw->irq_setup_0, RTC_IRQ_SETUP_0_DAY_ENA_BITS); | |
218 | + if (t->dotw >= 0) hw_set_bits(&rtc_hw->irq_setup_1, RTC_IRQ_SETUP_1_DOTW_ENA_BITS); | |
219 | + if (t->hour >= 0) hw_set_bits(&rtc_hw->irq_setup_1, RTC_IRQ_SETUP_1_HOUR_ENA_BITS); | |
220 | + if (t->min >= 0) hw_set_bits(&rtc_hw->irq_setup_1, RTC_IRQ_SETUP_1_MIN_ENA_BITS); | |
221 | + if (t->sec >= 0) hw_set_bits(&rtc_hw->irq_setup_1, RTC_IRQ_SETUP_1_SEC_ENA_BITS); | |
222 | + | |
223 | + // Does it repeat? I.e. do we not match on any of the bits | |
224 | + _alarm_repeats = rtc_alarm_repeats(t); | |
225 | + | |
226 | + // Store function pointer we can call later | |
227 | + _callback = user_callback; | |
228 | + | |
229 | + irq_set_exclusive_handler(RTC_IRQ, rtc_irq_handler); | |
230 | + | |
231 | + // Enable the IRQ at the peri | |
232 | + rtc_hw->inte = RTC_INTE_RTC_BITS; | |
233 | + | |
234 | + // Enable the IRQ at the proc | |
235 | + irq_set_enabled(RTC_IRQ, true); | |
236 | + | |
237 | + rtc_enable_alarm(); | |
185 | 238 | } |
186 | 239 | |
187 | 240 | /** |
@@ -95,7 +95,7 @@ | ||
95 | 95 | */ |
96 | 96 | typedef struct { |
97 | 97 | /* End of the mandatory fields.*/ |
98 | - uint32_t dummy; | |
98 | + RTCDateTime alarm; | |
99 | 99 | } RTCAlarm; |
100 | 100 | |
101 | 101 | /** |