Skip to content

Commit

Permalink
codal_port/modpower: Suppress run_every events if run_every=False.
Browse files Browse the repository at this point in the history
Fixes issue #131.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Sep 26, 2022
1 parent de38138 commit 769a90d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
18 changes: 10 additions & 8 deletions src/codal_port/drv_softtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/codal_port/drv_softtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions src/codal_port/modpower.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down

0 comments on commit 769a90d

Please sign in to comment.