summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Webb <chris@arachsys.com>2025-08-25 14:02:32 +0100
committerDamien George <damien.p.george@gmail.com>2025-09-25 23:59:24 +1000
commit955b6a907cdaf15135470d2c71bcede8620eaa6f (patch)
tree59b9510746c4c10ec0e1a7521185df10378d40ce
parentad11df7f7ac73b1d9d0c69274a5e56140c621401 (diff)
extmod/machine_timer: Support hard IRQ soft timer callbacks.
machine.Timer() has inconsistent behaviour between ports: some run callbacks in hard IRQ context whereas others schedule them like soft IRQs. As on the rp2 port, add support to the generic software timer for a hard= argument to explicitly choose between these, setting the default to False to match the existing behaviour. This enables hard timer callbacks for the alif, mimxrt and samd ports. Signed-off-by: Chris Webb <chris@arachsys.com>
-rw-r--r--extmod/machine_timer.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/extmod/machine_timer.c b/extmod/machine_timer.c
index 665be82ce..7029f7dde 100644
--- a/extmod/machine_timer.c
+++ b/extmod/machine_timer.c
@@ -42,13 +42,14 @@ static void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_pr
}
static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_mode, ARG_callback, ARG_period, ARG_tick_hz, ARG_freq, };
+ enum { ARG_mode, ARG_callback, ARG_period, ARG_tick_hz, ARG_freq, ARG_hard, };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SOFT_TIMER_MODE_PERIODIC} },
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
{ MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
+ { MP_QSTR_hard, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
};
// Parse args
@@ -81,6 +82,12 @@ static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar
self->py_callback = args[ARG_callback].u_obj;
}
+ if (args[ARG_hard].u_bool) {
+ self->flags |= SOFT_TIMER_FLAG_HARD_CALLBACK;
+ } else {
+ self->flags &= ~SOFT_TIMER_FLAG_HARD_CALLBACK;
+ }
+
if (self->py_callback != mp_const_none) {
soft_timer_insert(self, self->delta_ms);
}