diff options
author | Chris Webb <chris@arachsys.com> | 2025-08-25 14:04:45 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2025-09-25 23:59:24 +1000 |
commit | b7bf24e4812d2d27c61c8cb0dbcbbfc1a28a271c (patch) | |
tree | 1a1674efeecb083cb439865016016d8215c64550 | |
parent | 955b6a907cdaf15135470d2c71bcede8620eaa6f (diff) |
rp2/machine_timer: Use mp_irq_dispatch() to reduce duplication.
Now that mp_irq_dispatch() is available to dispatch arbitary hard/soft
callbacks, take advantage of this for rp2 machine.Timer. This should
slightly reduce binary size.
Signed-off-by: Chris Webb <chris@arachsys.com>
-rw-r--r-- | ports/rp2/machine_timer.c | 28 |
1 files changed, 5 insertions, 23 deletions
diff --git a/ports/rp2/machine_timer.c b/ports/rp2/machine_timer.c index ffb4c7024..276fa5e78 100644 --- a/ports/rp2/machine_timer.c +++ b/ports/rp2/machine_timer.c @@ -29,6 +29,7 @@ #include "py/mphal.h" #include "py/gc.h" #include "pico/time.h" +#include "shared/runtime/mpirq.h" #define ALARM_ID_INVALID (-1) #define TIMER_MODE_ONE_SHOT (0) @@ -49,29 +50,10 @@ const mp_obj_type_t machine_timer_type; static int64_t alarm_callback(alarm_id_t id, void *user_data) { machine_timer_obj_t *self = user_data; - if (self->ishard) { - // When executing code within a handler we must lock the scheduler to - // prevent any scheduled callbacks from running, and lock the GC to - // prevent any memory allocations. - mp_sched_lock(); - gc_lock(); - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - mp_call_function_1(self->callback, MP_OBJ_FROM_PTR(self)); - nlr_pop(); - } else { - // Uncaught exception; disable the callback so it doesn't run again. - self->mode = TIMER_MODE_ONE_SHOT; - mp_printf(MICROPY_ERROR_PRINTER, "uncaught exception in timer callback\n"); - mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(nlr.ret_val)); - } - gc_unlock(); - mp_sched_unlock(); - } else { - mp_sched_schedule(self->callback, MP_OBJ_FROM_PTR(self)); - } - - if (self->mode == TIMER_MODE_ONE_SHOT) { + if (mp_irq_dispatch(self->callback, MP_OBJ_FROM_PTR(self), self->ishard) < 0) { + // Uncaught exception; don't run the callback again. + return 0; + } else if (self->mode == TIMER_MODE_ONE_SHOT) { return 0; } else { return -self->delta_us; |