add WatchDogTimeout exception

This adds an exception to be raised when the WatchDogTimer times out.

Note that this currently causes a HardFault, and it's not clear why it's
not behaving properly.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-05-21 20:45:03 +08:00
parent 0169ea5155
commit ae950bc050
6 changed files with 29 additions and 19 deletions

View File

@ -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);

View File

@ -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) },

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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;