diff --git a/ports/nrf/common-hal/watchdog/WatchDogTimer.c b/ports/nrf/common-hal/watchdog/WatchDogTimer.c index 300ac77122..148146b01f 100644 --- a/ports/nrf/common-hal/watchdog/WatchDogTimer.c +++ b/ports/nrf/common-hal/watchdog/WatchDogTimer.c @@ -47,23 +47,6 @@ STATIC uint8_t timer_refcount = 0; #define WATCHDOG_RELOAD_COUNT 2 STATIC nrfx_timer_t *timer = NULL; -const mp_obj_type_t mp_type_WatchDogTimeout = { - { &mp_type_type }, - .name = MP_QSTR_WatchDogTimeout, - .make_new = mp_obj_exception_make_new, - .attr = mp_obj_exception_attr, - .parent = &mp_type_Exception, -}; - -static mp_obj_exception_t mp_watchdog_timeout_exception = { - .base.type = &mp_type_WatchDogTimeout, - .traceback_alloc = 0, - .traceback_len = 0, - .traceback_data = NULL, - .args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj, -}; - - STATIC void watchdogtimer_hardware_init(mp_float_t duration, bool pause_during_sleep) { unsigned int channel; nrf_wdt_behaviour_t behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP_HALT; @@ -93,6 +76,10 @@ STATIC void watchdogtimer_hardware_feed(void) { } } +NORETURN void mp_raise_WatchDogTimeout(void) { + nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception))); +} + STATIC void watchdogtimer_event_handler(nrf_timer_event_t event_type, void *p_context) { (void)p_context; if (event_type != NRF_TIMER_EVENT_COMPARE0) { @@ -102,8 +89,7 @@ STATIC void watchdogtimer_event_handler(nrf_timer_event_t event_type, void *p_co // If the timer hits without being cleared, pause the timer and raise an exception. nrfx_timer_pause(timer); - mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception)); - MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception; + MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception)); #if MICROPY_ENABLE_SCHEDULER if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { MP_STATE_VM(sched_state) = MP_SCHED_PENDING; @@ -139,6 +125,7 @@ STATIC mp_obj_t watchdog_watchdogtimer_make_new(const mp_obj_type_t *type, size_ {MP_QSTR_hardware, MP_ARG_BOOL, {.u_bool = false}}, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception))); mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); diff --git a/py/modbuiltins.c b/py/modbuiltins.c index e764f1987e..b031ce9fe0 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -719,6 +719,9 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_IndexError), MP_ROM_PTR(&mp_type_IndexError) }, { MP_ROM_QSTR(MP_QSTR_KeyboardInterrupt), MP_ROM_PTR(&mp_type_KeyboardInterrupt) }, { MP_ROM_QSTR(MP_QSTR_ReloadException), MP_ROM_PTR(&mp_type_ReloadException) }, + #if CIRCUITPY_WATCHDOG + { MP_ROM_QSTR(MP_QSTR_WatchDogTimeout), MP_ROM_PTR(&mp_type_WatchDogTimeout) }, + #endif { MP_ROM_QSTR(MP_QSTR_KeyError), MP_ROM_PTR(&mp_type_KeyError) }, { MP_ROM_QSTR(MP_QSTR_LookupError), MP_ROM_PTR(&mp_type_LookupError) }, { MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_MemoryError) }, diff --git a/py/mpstate.h b/py/mpstate.h index a5815776a4..5d3b7709b3 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -141,6 +141,11 @@ typedef struct _mp_state_vm_t { // exception object of type ReloadException mp_obj_exception_t mp_reload_exception; + #if CIRCUITPY_WATCHDOG + // exception object of type WatchdogTimeout + mp_obj_exception_t mp_watchdog_exception; + #endif + // dictionary with loaded modules (may be exposed as sys.modules) mp_obj_dict_t mp_loaded_modules_dict; diff --git a/py/obj.h b/py/obj.h index fa315d12f7..cabe5498db 100644 --- a/py/obj.h +++ b/py/obj.h @@ -596,6 +596,9 @@ extern const mp_obj_type_t mp_type_IndentationError; extern const mp_obj_type_t mp_type_IndexError; extern const mp_obj_type_t mp_type_KeyboardInterrupt; extern const mp_obj_type_t mp_type_ReloadException; +#if CIRCUITPY_WATCHDOG +extern const mp_obj_type_t mp_type_WatchDogTimeout; +#endif extern const mp_obj_type_t mp_type_KeyError; extern const mp_obj_type_t mp_type_LookupError; extern const mp_obj_type_t mp_type_MemoryError; diff --git a/py/objexcept.c b/py/objexcept.c index b7a536c5e3..db09d51538 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -256,6 +256,9 @@ const mp_obj_type_t mp_type_BaseException = { MP_DEFINE_EXCEPTION(SystemExit, BaseException) MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException) MP_DEFINE_EXCEPTION(ReloadException, BaseException) +#if CIRCUITPY_WATCHDOG +MP_DEFINE_EXCEPTION(WatchDogTimeout, BaseException) +#endif MP_DEFINE_EXCEPTION(GeneratorExit, BaseException) MP_DEFINE_EXCEPTION(Exception, BaseException) #if MICROPY_PY_ASYNC_AWAIT diff --git a/py/runtime.c b/py/runtime.c index 59dcbc7a1c..02352ea6c7 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -86,6 +86,15 @@ void mp_init(void) { MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; #endif + #if CIRCUITPY_WATCHDOG + // initialise the exception object for raising WatchDogTimeout + MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_WatchDogTimeout; + MP_STATE_VM(mp_kbd_exception).traceback_alloc = 0; + MP_STATE_VM(mp_kbd_exception).traceback_len = 0; + MP_STATE_VM(mp_kbd_exception).traceback_data = NULL; + MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; + #endif + MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException; MP_STATE_VM(mp_reload_exception).traceback_alloc = 0; MP_STATE_VM(mp_reload_exception).traceback_len = 0;