From 7cbf826a9575e18ce1b7fe11b0f0997509153260 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Apr 2021 10:52:19 +1000 Subject: [PATCH] py/scheduler: Add mp_sched_exception() to schedule a pending exception. This helper is added to properly set a pending exception, to mirror mp_sched_schedule(), which schedules a function. Signed-off-by: Damien George --- ports/esp32/mpconfigport.h | 1 + ports/esp8266/mpconfigport.h | 1 + py/mpconfig.h | 4 ++++ py/runtime.h | 1 + py/scheduler.c | 14 +++++++++----- 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 8788963cb3..0c7d42210e 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -264,6 +264,7 @@ void *esp_native_code_commit(void *, size_t, void *); #endif // Functions that should go in IRAM +#define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) IRAM_ATTR f #define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) IRAM_ATTR f #define UINT_FMT "%u" diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 2c56b4188f..52028e833a 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -195,6 +195,7 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define MICROPY_PY_SYS_PLATFORM "esp8266" #define MP_FASTCODE(n) __attribute__((section(".iram0.text." #n))) n +#define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) MP_FASTCODE(f) #define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) MP_FASTCODE(f) #define MICROPY_WRAP_MP_SCHED_SCHEDULE(f) MP_FASTCODE(f) diff --git a/py/mpconfig.h b/py/mpconfig.h index 5c7212fd1f..2934f8ec89 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1504,6 +1504,10 @@ typedef double mp_float_t; /*****************************************************************************/ /* Hooks for a port to wrap functions with attributes */ +#ifndef MICROPY_WRAP_MP_SCHED_EXCEPTION +#define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) f +#endif + #ifndef MICROPY_WRAP_MP_KEYBOARD_INTERRUPT #define MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(f) f #endif diff --git a/py/runtime.h b/py/runtime.h index 0cbbb287ac..9ad4bc0242 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -64,6 +64,7 @@ extern const byte mp_binary_op_method_name[]; void mp_init(void); void mp_deinit(void); +void mp_sched_exception(mp_obj_t exc); void mp_keyboard_interrupt(void); void mp_handle_pending(bool raise_exc); void mp_handle_pending_tail(mp_uint_t atomic_state); diff --git a/py/scheduler.c b/py/scheduler.c index 6b138a631b..f37f8c3f86 100644 --- a/py/scheduler.c +++ b/py/scheduler.c @@ -28,17 +28,21 @@ #include "py/runtime.h" -#if MICROPY_KBD_EXCEPTION -// This function may be called asynchronously at any time so only do the bare minimum. -void MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(mp_keyboard_interrupt)(void) { - MP_STATE_VM(mp_kbd_exception).traceback_data = NULL; - MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)); +void mp_sched_exception(mp_obj_t exc) { + MP_STATE_VM(mp_pending_exception) = exc; #if MICROPY_ENABLE_SCHEDULER if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { MP_STATE_VM(sched_state) = MP_SCHED_PENDING; } #endif } + +#if MICROPY_KBD_EXCEPTION +// This function may be called asynchronously at any time so only do the bare minimum. +void MICROPY_WRAP_MP_KEYBOARD_INTERRUPT(mp_keyboard_interrupt)(void) { + MP_STATE_VM(mp_kbd_exception).traceback_data = NULL; + mp_sched_exception(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))); +} #endif #if MICROPY_ENABLE_SCHEDULER