Skip to content

Commit a70fe2a

Browse files
committed
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 <[email protected]>
1 parent 8d6f1a4 commit a70fe2a

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

ports/nrf/common-hal/watchdog/WatchDogTimer.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,6 @@ STATIC uint8_t timer_refcount = 0;
4747
#define WATCHDOG_RELOAD_COUNT 2
4848
STATIC nrfx_timer_t *timer = NULL;
4949

50-
const mp_obj_type_t mp_type_WatchDogTimeout = {
51-
{ &mp_type_type },
52-
.name = MP_QSTR_WatchDogTimeout,
53-
.make_new = mp_obj_exception_make_new,
54-
.attr = mp_obj_exception_attr,
55-
.parent = &mp_type_Exception,
56-
};
57-
58-
static mp_obj_exception_t mp_watchdog_timeout_exception = {
59-
.base.type = &mp_type_WatchDogTimeout,
60-
.traceback_alloc = 0,
61-
.traceback_len = 0,
62-
.traceback_data = NULL,
63-
.args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj,
64-
};
65-
66-
6750
STATIC void watchdogtimer_hardware_init(mp_float_t duration, bool pause_during_sleep) {
6851
unsigned int channel;
6952
nrf_wdt_behaviour_t behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP_HALT;
@@ -93,6 +76,10 @@ STATIC void watchdogtimer_hardware_feed(void) {
9376
}
9477
}
9578

79+
NORETURN void mp_raise_WatchDogTimeout(void) {
80+
nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception)));
81+
}
82+
9683
STATIC void watchdogtimer_event_handler(nrf_timer_event_t event_type, void *p_context) {
9784
(void)p_context;
9885
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
10289

10390
// If the timer hits without being cleared, pause the timer and raise an exception.
10491
nrfx_timer_pause(timer);
105-
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception));
106-
MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception;
92+
MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception));
10793
#if MICROPY_ENABLE_SCHEDULER
10894
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
10995
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_
139125
{MP_QSTR_hardware, MP_ARG_BOOL, {.u_bool = false}},
140126
};
141127
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
128+
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception)));
142129

143130
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
144131
allowed_args, args);

py/modbuiltins.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,9 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
719719
{ MP_ROM_QSTR(MP_QSTR_IndexError), MP_ROM_PTR(&mp_type_IndexError) },
720720
{ MP_ROM_QSTR(MP_QSTR_KeyboardInterrupt), MP_ROM_PTR(&mp_type_KeyboardInterrupt) },
721721
{ MP_ROM_QSTR(MP_QSTR_ReloadException), MP_ROM_PTR(&mp_type_ReloadException) },
722+
#if CIRCUITPY_WATCHDOG
723+
{ MP_ROM_QSTR(MP_QSTR_WatchDogTimeout), MP_ROM_PTR(&mp_type_WatchDogTimeout) },
724+
#endif
722725
{ MP_ROM_QSTR(MP_QSTR_KeyError), MP_ROM_PTR(&mp_type_KeyError) },
723726
{ MP_ROM_QSTR(MP_QSTR_LookupError), MP_ROM_PTR(&mp_type_LookupError) },
724727
{ MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_MemoryError) },

py/mpstate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ typedef struct _mp_state_vm_t {
141141
// exception object of type ReloadException
142142
mp_obj_exception_t mp_reload_exception;
143143

144+
#if CIRCUITPY_WATCHDOG
145+
// exception object of type WatchdogTimeout
146+
mp_obj_exception_t mp_watchdog_exception;
147+
#endif
148+
144149
// dictionary with loaded modules (may be exposed as sys.modules)
145150
mp_obj_dict_t mp_loaded_modules_dict;
146151

py/obj.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ extern const mp_obj_type_t mp_type_IndentationError;
596596
extern const mp_obj_type_t mp_type_IndexError;
597597
extern const mp_obj_type_t mp_type_KeyboardInterrupt;
598598
extern const mp_obj_type_t mp_type_ReloadException;
599+
#if CIRCUITPY_WATCHDOG
600+
extern const mp_obj_type_t mp_type_WatchDogTimeout;
601+
#endif
599602
extern const mp_obj_type_t mp_type_KeyError;
600603
extern const mp_obj_type_t mp_type_LookupError;
601604
extern const mp_obj_type_t mp_type_MemoryError;

py/objexcept.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ const mp_obj_type_t mp_type_BaseException = {
256256
MP_DEFINE_EXCEPTION(SystemExit, BaseException)
257257
MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
258258
MP_DEFINE_EXCEPTION(ReloadException, BaseException)
259+
#if CIRCUITPY_WATCHDOG
260+
MP_DEFINE_EXCEPTION(WatchDogTimeout, BaseException)
261+
#endif
259262
MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
260263
MP_DEFINE_EXCEPTION(Exception, BaseException)
261264
#if MICROPY_PY_ASYNC_AWAIT

py/runtime.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ void mp_init(void) {
8686
MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj;
8787
#endif
8888

89+
#if CIRCUITPY_WATCHDOG
90+
// initialise the exception object for raising WatchDogTimeout
91+
MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_WatchDogTimeout;
92+
MP_STATE_VM(mp_kbd_exception).traceback_alloc = 0;
93+
MP_STATE_VM(mp_kbd_exception).traceback_len = 0;
94+
MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
95+
MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj;
96+
#endif
97+
8998
MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException;
9099
MP_STATE_VM(mp_reload_exception).traceback_alloc = 0;
91100
MP_STATE_VM(mp_reload_exception).traceback_len = 0;

0 commit comments

Comments
 (0)