Skip to content

Commit 2233d79

Browse files
committed
codal_port/modpower: Implement deep_sleep's "run_every" parameter.
Signed-off-by: Damien George <[email protected]>
1 parent c9e5f7a commit 2233d79

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/codal_app/microbithal.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ void microbit_hal_power_clear_wake_sources(void) {
113113
for (size_t i = 0; i < HAL_ARRAY_SIZE(button_obj); ++i) {
114114
microbit_hal_power_wake_on_button(i, false);
115115
}
116-
// TODO: Clear the run_every wake up source when implemented
117116
}
118117

119118
void microbit_hal_power_wake_on_button(int button, bool wake_on_active) {

src/codal_port/modpower.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "py/runtime.h"
2828
#include "py/mphal.h"
29+
#include "drv_softtimer.h"
2930

3031
STATIC size_t get_array(mp_obj_t *src, mp_obj_t **items) {
3132
if (*src == mp_const_none) {
@@ -63,6 +64,14 @@ STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map
6364

6465
microbit_hal_power_clear_wake_sources();
6566

67+
// Configure wake-up time, if given.
68+
bool wake_on_ms = false;
69+
uint32_t wake_ms = UINT32_MAX;
70+
if (args[ARG_ms].u_obj != mp_const_none) {
71+
wake_on_ms = true;
72+
wake_ms = mp_obj_get_int(args[ARG_ms].u_obj);
73+
}
74+
6675
// Configure wake-up sources.
6776
mp_obj_t *items;
6877
size_t len = get_array(&args[ARG_wake_on].u_obj, &items);
@@ -77,15 +86,24 @@ STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map
7786
}
7887
}
7988

89+
// If run_every is true then check if any soft timers will expire and need to wake the device.
8090
if (args[ARG_run_every].u_bool) {
81-
mp_raise_ValueError(MP_ERROR_TEXT("run_every not implemented"));
91+
microbit_soft_timer_set_pause(true);
92+
uint32_t ms = microbit_soft_timer_get_ms_to_next_expiry();
93+
if (ms != UINT32_MAX) {
94+
// A soft timer will expire in "ms" milliseconds.
95+
wake_on_ms = true;
96+
if (ms < wake_ms) {
97+
wake_ms = ms;
98+
}
99+
}
82100
}
83101

84-
if (args[ARG_ms].u_obj == mp_const_none) {
85-
microbit_hal_power_deep_sleep(false, 0);
86-
} else {
87-
microbit_hal_power_deep_sleep(true, mp_obj_get_int(args[ARG_ms].u_obj));
88-
}
102+
// Enter low power state.
103+
microbit_hal_power_deep_sleep(wake_on_ms, wake_ms);
104+
105+
// Resume soft timer (doesn't hurt to resume even if it wasn't paused).
106+
microbit_soft_timer_set_pause(false);
89107

90108
return mp_const_none;
91109
}

0 commit comments

Comments
 (0)