Skip to content

Commit 769a90d

Browse files
committed
codal_port/modpower: Suppress run_every events if run_every=False.
Fixes issue #131. Signed-off-by: Damien George <[email protected]>
1 parent de38138 commit 769a90d

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/codal_port/drv_softtimer.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ void microbit_soft_timer_deinit(void) {
4343
microbit_soft_timer_paused = false;
4444
}
4545

46-
void microbit_soft_timer_handler_run(void) {
46+
static void microbit_soft_timer_handler_run(bool run_callbacks) {
4747
uint32_t ticks_ms = mp_hal_ticks_ms();
4848
microbit_soft_timer_entry_t *heap = MP_STATE_PORT(soft_timer_heap);
4949
while (heap != NULL && TICKS_DIFF(heap->expiry_ms, ticks_ms) <= 0) {
5050
microbit_soft_timer_entry_t *entry = heap;
5151
heap = (microbit_soft_timer_entry_t *)mp_pairheap_pop(microbit_soft_timer_lt, &heap->pairheap);
52-
if (entry->flags & MICROBIT_SOFT_TIMER_FLAG_PY_CALLBACK) {
53-
mp_sched_schedule(entry->py_callback, MP_OBJ_FROM_PTR(entry));
54-
} else {
55-
entry->c_callback(entry);
52+
if (run_callbacks) {
53+
if (entry->flags & MICROBIT_SOFT_TIMER_FLAG_PY_CALLBACK) {
54+
mp_sched_schedule(entry->py_callback, MP_OBJ_FROM_PTR(entry));
55+
} else {
56+
entry->c_callback(entry);
57+
}
5658
}
5759
if (entry->mode == MICROBIT_SOFT_TIMER_MODE_PERIODIC) {
5860
entry->expiry_ms += entry->delta_ms;
@@ -65,7 +67,7 @@ void microbit_soft_timer_handler_run(void) {
6567
// This function can be executed at interrupt priority.
6668
void microbit_soft_timer_handler(void) {
6769
if (!microbit_soft_timer_paused) {
68-
microbit_soft_timer_handler_run();
70+
microbit_soft_timer_handler_run(true);
6971
}
7072
}
7173

@@ -77,10 +79,10 @@ void microbit_soft_timer_insert(microbit_soft_timer_entry_t *entry, uint32_t ini
7779
MICROPY_END_ATOMIC_SECTION(atomic_state);
7880
}
7981

80-
void microbit_soft_timer_set_pause(bool paused) {
82+
void microbit_soft_timer_set_pause(bool paused, bool run_callbacks) {
8183
if (microbit_soft_timer_paused && !paused) {
8284
// Explicitly run the soft timer before unpausing, to catch up on any queued events.
83-
microbit_soft_timer_handler_run();
85+
microbit_soft_timer_handler_run(run_callbacks);
8486
}
8587
microbit_soft_timer_paused = paused;
8688
}

src/codal_port/drv_softtimer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern bool microbit_outer_nlr_will_handle_soft_timer_exceptions;
5151
void microbit_soft_timer_deinit(void);
5252
void microbit_soft_timer_handler(void);
5353
void microbit_soft_timer_insert(microbit_soft_timer_entry_t *entry, uint32_t initial_delta_ms);
54-
void microbit_soft_timer_set_pause(bool paused);
54+
void microbit_soft_timer_set_pause(bool paused, bool run_callbacks);
5555
uint32_t microbit_soft_timer_get_ms_to_next_expiry(void);
5656

5757
#endif // MICROPY_INCLUDED_CODAL_PORT_DRV_SOFTTIMER_H

src/codal_port/modpower.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map
9393
bool wake = wake_on_ms;
9494
uint32_t ms = remain_ms;
9595

96+
// Pause the soft timer.
97+
microbit_soft_timer_set_pause(true, true);
98+
9699
// If run_every is true then check if any soft timers will expire and need to wake the device.
97100
if (args[ARG_run_every].u_bool) {
98-
microbit_soft_timer_set_pause(true);
99101
uint32_t soft_timer_ms = microbit_soft_timer_get_ms_to_next_expiry();
100102
if (soft_timer_ms != UINT32_MAX) {
101103
// 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
109111
// Enter low power state.
110112
bool interrupted = microbit_hal_power_deep_sleep(wake, ms);
111113

112-
// Resume soft timer (doesn't hurt to resume even if it wasn't paused).
113-
microbit_soft_timer_set_pause(false);
114+
// Resume the soft timer, and run outstanding events if run_every=True.
115+
microbit_soft_timer_set_pause(false, args[ARG_run_every].u_bool);
114116

115117
// Run all outstanding scheduled functions.
116118
while (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {

0 commit comments

Comments
 (0)