Skip to content

Commit 872fff7

Browse files
committed
simplify system_on_idle_until_alarm()
1 parent 8bd77b7 commit 872fff7

File tree

1 file changed

+35
-82
lines changed

1 file changed

+35
-82
lines changed

ports/nrf/common-hal/alarm/__init__.c

Lines changed: 35 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,6 @@ bool common_hal_alarm_woken_from_sleep(void) {
118118
|| cause == NRF_SLEEP_WAKEUP_TOUCHPAD);
119119
}
120120

121-
nrf_sleep_source_t alarm_woken_from_sleep_2(void) {
122-
nrf_sleep_source_t cause = _get_wakeup_cause();
123-
if (cause == NRF_SLEEP_WAKEUP_GPIO ||
124-
cause == NRF_SLEEP_WAKEUP_TIMER ||
125-
cause == NRF_SLEEP_WAKEUP_TOUCHPAD) {
126-
return cause;
127-
}
128-
else {
129-
return NRF_SLEEP_WAKEUP_UNDEFINED;
130-
}
131-
}
132-
133121
STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
134122
nrf_sleep_source_t cause = _get_wakeup_cause();
135123
switch (cause) {
@@ -163,7 +151,7 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t
163151
// TODO: this handles all possible types of wakeup, which is redundant with main.
164152
// revise to extract all parts essential to enabling sleep wakeup, but leave the
165153
// alarm/non-alarm sorting to the existing main loop.
166-
nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t prescaler) {
154+
void system_on_idle_until_alarm(int64_t timediff_ms, uint32_t prescaler) {
167155
bool have_timeout = false;
168156
uint64_t start_tick = 0, end_tick = 0;
169157
int64_t tickdiff;
@@ -197,7 +185,6 @@ nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t pres
197185
#endif
198186

199187
int64_t remaining;
200-
nrf_sleep_source_t wakeup_cause = NRF_SLEEP_WAKEUP_UNDEFINED;
201188
sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_NONE;
202189
sleepmem_wakeup_pin = WAKEUP_PIN_UNDEF;
203190

@@ -210,30 +197,29 @@ nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t pres
210197
#endif
211198

212199
while(1) {
213-
if (mp_hal_is_interrupted()) {
214-
WAKEUP_REASON('I');
215-
break;
216-
}
200+
if (mp_hal_is_interrupted()) {
201+
WAKEUP_REASON('I');
202+
break;
203+
}
217204
if (serial_connected() && serial_bytes_available()) {
218-
WAKEUP_REASON('S');
219-
break;
220-
}
205+
WAKEUP_REASON('S');
206+
break;
207+
}
221208
RUN_BACKGROUND_TASKS;
222-
wakeup_cause = alarm_woken_from_sleep_2();
223-
if (wakeup_cause != NRF_SLEEP_WAKEUP_UNDEFINED) {
224-
WAKEUP_REASON('0'+wakeup_cause);
225-
break;
226-
}
227-
if (have_timeout) {
228-
remaining = end_tick - port_get_raw_ticks(NULL);
229-
// We break a bit early so we don't risk setting the alarm before the time when we call
230-
// sleep.
231-
if (remaining < 1) {
232-
WAKEUP_REASON('t');
209+
if (common_hal_alarm_woken_from_sleep()) {
210+
WAKEUP_REASON('W');
233211
break;
234212
}
235-
port_interrupt_after_ticks(remaining);
236-
}
213+
if (have_timeout) {
214+
remaining = end_tick - port_get_raw_ticks(NULL);
215+
// We break a bit early so we don't risk setting the alarm before the time when we call
216+
// sleep.
217+
if (remaining < 1) {
218+
WAKEUP_REASON('t');
219+
break;
220+
}
221+
port_interrupt_after_ticks(remaining);
222+
}
237223
// Idle until an interrupt happens.
238224
port_idle_until_interrupt();
239225
#ifdef NRF_DEBUG_PRINT
@@ -242,16 +228,15 @@ nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t pres
242228
--ct;
243229
}
244230
#endif
245-
if (have_timeout) {
246-
remaining = end_tick - port_get_raw_ticks(NULL);
247-
if (remaining <= 0) {
248-
wakeup_cause = NRF_SLEEP_WAKEUP_TIMER;
249-
sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_TIMER;
250-
WAKEUP_REASON('T');
251-
break;
231+
if (have_timeout) {
232+
remaining = end_tick - port_get_raw_ticks(NULL);
233+
if (remaining <= 0) {
234+
sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_TIMER;
235+
WAKEUP_REASON('T');
236+
break;
237+
}
252238
}
253239
}
254-
}
255240
#ifdef NRF_DEBUG_PRINT
256241
dbg_printf("%c\r\n", reason);
257242
#endif
@@ -271,12 +256,10 @@ nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t pres
271256
}
272257
dbg_printf("lapse %6.1f sec\r\n", sec);
273258
#endif
274-
275-
return wakeup_cause;
276259
}
277260

278261
mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
279-
mp_obj_t r_obj = mp_const_none;
262+
mp_obj_t wake_alarm;
280263
alarm_time_timealarm_clear_wakeup_time();
281264
_setup_sleep_alarms(false, n_alarms, alarms);
282265

@@ -285,25 +268,16 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj
285268
#endif
286269

287270
int64_t timediff_ms = alarm_time_timealarm_get_wakeup_timediff_ms();
288-
nrf_sleep_source_t cause = system_on_idle_until_alarm(timediff_ms, 0);
289-
(void)cause;
290-
291-
#ifdef NRF_DEBUG_PRINT
292-
//dbg_printf("wakeup! ");
293-
print_wakeup_cause(cause);
294-
#endif
271+
system_on_idle_until_alarm(timediff_ms, 0);
295272

296273
if (mp_hal_is_interrupted()) {
297-
#ifdef NRF_DEBUG_PRINT
298-
dbg_printf("mp_hal_is_interrupted\r\n");
299-
#endif
300-
r_obj = mp_const_none;
274+
wake_alarm = mp_const_none;
301275
}
302276
else {
303-
r_obj = _get_wake_alarm(n_alarms, alarms);
277+
wake_alarm = _get_wake_alarm(n_alarms, alarms);
304278
}
305279
alarm_reset();
306-
return r_obj;
280+
return wake_alarm;
307281
}
308282

309283
void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms) {
@@ -325,14 +299,9 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {
325299
#ifdef NRF_DEBUG_PRINT
326300
dbg_check_RTCprescaler(); //XXX
327301
#endif
328-
nrf_sleep_source_t cause;
329-
cause = system_on_idle_until_alarm(timediff_ms,
330-
PRESCALER_VALUE_IN_DEEP_SLEEP);
331-
(void)cause;
302+
system_on_idle_until_alarm(timediff_ms, PRESCALER_VALUE_IN_DEEP_SLEEP);
332303

333304
#ifdef NRF_DEBUG_PRINT
334-
dbg_printf("wakeup! ");
335-
print_wakeup_cause(cause);
336305
dbg_printf("RESET...\r\n\r\n");
337306
#endif
338307

@@ -370,25 +339,9 @@ void common_hal_alarm_pretending_deep_sleep(void) {
370339
#endif
371340

372341
int64_t timediff_ms = alarm_time_timealarm_get_wakeup_timediff_ms();
373-
nrf_sleep_source_t cause = system_on_idle_until_alarm(timediff_ms, 0);
374-
(void)cause;
342+
system_on_idle_until_alarm(timediff_ms, 0);
375343

376-
#ifdef NRF_DEBUG_PRINT
377-
dbg_printf("wakeup! ");
378-
print_wakeup_cause(cause);
379-
#endif
380-
381-
// alarm_reset();
382-
383-
#if 0
384-
// if one of Alarm event occurred, reset myself
385-
if (cause == NRF_SLEEP_WAKEUP_GPIO ||
386-
cause == NRF_SLEEP_WAKEUP_TIMER ||
387-
cause == NRF_SLEEP_WAKEUP_TOUCHPAD) {
388-
reset_cpu();
389-
}
390-
// else, just return and go into REPL
391-
#endif
344+
alarm_reset();
392345
}
393346

394347
void common_hal_alarm_gc_collect(void) {

0 commit comments

Comments
 (0)