diff --git a/src/codal_port/drv_softtimer.c b/src/codal_port/drv_softtimer.c index b244008..e58ee26 100644 --- a/src/codal_port/drv_softtimer.c +++ b/src/codal_port/drv_softtimer.c @@ -43,16 +43,18 @@ void microbit_soft_timer_deinit(void) { microbit_soft_timer_paused = false; } -void microbit_soft_timer_handler_run(void) { +static void microbit_soft_timer_handler_run(bool run_callbacks) { uint32_t ticks_ms = mp_hal_ticks_ms(); microbit_soft_timer_entry_t *heap = MP_STATE_PORT(soft_timer_heap); while (heap != NULL && TICKS_DIFF(heap->expiry_ms, ticks_ms) <= 0) { microbit_soft_timer_entry_t *entry = heap; heap = (microbit_soft_timer_entry_t *)mp_pairheap_pop(microbit_soft_timer_lt, &heap->pairheap); - if (entry->flags & MICROBIT_SOFT_TIMER_FLAG_PY_CALLBACK) { - mp_sched_schedule(entry->py_callback, MP_OBJ_FROM_PTR(entry)); - } else { - entry->c_callback(entry); + if (run_callbacks) { + if (entry->flags & MICROBIT_SOFT_TIMER_FLAG_PY_CALLBACK) { + mp_sched_schedule(entry->py_callback, MP_OBJ_FROM_PTR(entry)); + } else { + entry->c_callback(entry); + } } if (entry->mode == MICROBIT_SOFT_TIMER_MODE_PERIODIC) { entry->expiry_ms += entry->delta_ms; @@ -65,7 +67,7 @@ void microbit_soft_timer_handler_run(void) { // This function can be executed at interrupt priority. void microbit_soft_timer_handler(void) { if (!microbit_soft_timer_paused) { - microbit_soft_timer_handler_run(); + microbit_soft_timer_handler_run(true); } } @@ -77,10 +79,10 @@ void microbit_soft_timer_insert(microbit_soft_timer_entry_t *entry, uint32_t ini MICROPY_END_ATOMIC_SECTION(atomic_state); } -void microbit_soft_timer_set_pause(bool paused) { +void microbit_soft_timer_set_pause(bool paused, bool run_callbacks) { if (microbit_soft_timer_paused && !paused) { // Explicitly run the soft timer before unpausing, to catch up on any queued events. - microbit_soft_timer_handler_run(); + microbit_soft_timer_handler_run(run_callbacks); } microbit_soft_timer_paused = paused; } diff --git a/src/codal_port/drv_softtimer.h b/src/codal_port/drv_softtimer.h index f178f78..c59d6ea 100644 --- a/src/codal_port/drv_softtimer.h +++ b/src/codal_port/drv_softtimer.h @@ -51,7 +51,7 @@ extern bool microbit_outer_nlr_will_handle_soft_timer_exceptions; void microbit_soft_timer_deinit(void); void microbit_soft_timer_handler(void); void microbit_soft_timer_insert(microbit_soft_timer_entry_t *entry, uint32_t initial_delta_ms); -void microbit_soft_timer_set_pause(bool paused); +void microbit_soft_timer_set_pause(bool paused, bool run_callbacks); uint32_t microbit_soft_timer_get_ms_to_next_expiry(void); #endif // MICROPY_INCLUDED_CODAL_PORT_DRV_SOFTTIMER_H diff --git a/src/codal_port/modpower.c b/src/codal_port/modpower.c index 5ca9add..a707e4a 100644 --- a/src/codal_port/modpower.c +++ b/src/codal_port/modpower.c @@ -93,9 +93,11 @@ STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map bool wake = wake_on_ms; uint32_t ms = remain_ms; + // Pause the soft timer. + microbit_soft_timer_set_pause(true, true); + // If run_every is true then check if any soft timers will expire and need to wake the device. if (args[ARG_run_every].u_bool) { - microbit_soft_timer_set_pause(true); uint32_t soft_timer_ms = microbit_soft_timer_get_ms_to_next_expiry(); if (soft_timer_ms != UINT32_MAX) { // A soft timer will expire in "ms" milliseconds. @@ -109,8 +111,8 @@ STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map // Enter low power state. bool interrupted = microbit_hal_power_deep_sleep(wake, ms); - // Resume soft timer (doesn't hurt to resume even if it wasn't paused). - microbit_soft_timer_set_pause(false); + // Resume the soft timer, and run outstanding events if run_every=True. + microbit_soft_timer_set_pause(false, args[ARG_run_every].u_bool); // Run all outstanding scheduled functions. while (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {