summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Cross <sean@xobs.io>2020-05-22 17:43:05 +0800
committerSean Cross <sean@xobs.io>2020-05-27 11:28:49 +0800
commitbd086a102e515765b55b50db9f3e45a23164b840 (patch)
tree38cb8848460ae4e77b43f0c781a89dbaae4338da
parent589cb1af6de04a01e1cfda1082fea4f6fbc4440c (diff)
Revert "add WatchDogTimeout exception"
This reverts commit 561e7e619095869f58fc728d428f3ff20e8bfc40.
-rw-r--r--ports/nrf/common-hal/watchdog/WatchDogTimer.c112
-rw-r--r--ports/nrf/common-hal/watchdog/__init__.h8
-rw-r--r--py/modbuiltins.c3
-rw-r--r--py/mpstate.h5
-rw-r--r--py/obj.h3
-rw-r--r--py/objexcept.c3
-rw-r--r--py/runtime.c9
7 files changed, 108 insertions, 35 deletions
diff --git a/ports/nrf/common-hal/watchdog/WatchDogTimer.c b/ports/nrf/common-hal/watchdog/WatchDogTimer.c
index 358fab798..3820fdcce 100644
--- a/ports/nrf/common-hal/watchdog/WatchDogTimer.c
+++ b/ports/nrf/common-hal/watchdog/WatchDogTimer.c
@@ -49,9 +49,21 @@ STATIC nrfx_timer_t *timer = NULL;
STATIC nrfx_wdt_t wdt = NRFX_WDT_INSTANCE(0);
STATIC nrfx_wdt_channel_id wdt_channel_id;
-NORETURN void mp_raise_WatchDogTimeout(void) {
- nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception)));
-}
+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_timer_event_handler(nrf_timer_event_t event_type, void *p_context) {
(void)p_context;
@@ -62,7 +74,8 @@ STATIC void watchdogtimer_timer_event_handler(nrf_timer_event_t event_type, void
// If the timer hits without being cleared, pause the timer and raise an exception.
nrfx_timer_pause(timer);
- MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception));
+ mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception));
+ MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception;
#if MICROPY_ENABLE_SCHEDULER
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
@@ -84,6 +97,97 @@ void watchdog_watchdogtimer_reset(void) {
timer_refcount = 0;
}
+<<<<<<< HEAD
+=======
+//| class WDT:
+//| """Watchdog Timer"""
+//|
+//| def __init__(self, ):
+//| """This class represents the system's Watchdog Timer. It is a
+//| singleton and will always return the same instance.
+//|
+//| """
+//| ...
+//|
+STATIC mp_obj_t watchdog_watchdogtimer_make_new(const mp_obj_type_t *type, size_t n_args,
+ const mp_obj_t *pos_args,
+ mp_map_t *kw_args) {
+ enum { ARG_timeout, ARG_sleep, ARG_hardware };
+ static const mp_arg_t allowed_args[] = {
+ {MP_QSTR_timeout, MP_ARG_OBJ | MP_ARG_REQUIRED},
+ {MP_QSTR_sleep, MP_ARG_BOOL, {.u_bool = false}},
+ {MP_QSTR_hardware, MP_ARG_BOOL, {.u_bool = false}},
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+
+ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
+ allowed_args, args);
+ mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
+ bool hardware = args[ARG_hardware].u_bool;
+ bool sleep = args[ARG_sleep].u_bool;
+
+ // If the hardware timer is already running, return that timer.
+ // If the parameters have changed, then ignore them, but print
+ // an error.
+ if (wdt_singleton && hardware) {
+ if ((sleep != wdt_singleton->sleep)
+ || (hardware != wdt_singleton->hardware)
+ || fabsf(timeout - wdt_singleton->timeout) > 0.01f) {
+ // Print a warning indicating things aren't quite right
+ // mp_printf(&mp_stderr_print, translate("warning: hardware timer was already running"));
+ }
+ watchdogtimer_hardware_feed();
+ return wdt_singleton;
+ }
+
+ if (timeout <= 0) {
+ mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
+ }
+
+ watchdog_watchdogtimer_obj_t *self = m_new_obj(watchdog_watchdogtimer_obj_t);
+ self->base.type = &watchdog_watchdogtimer_type;
+ self->timeout = timeout;
+ self->sleep = sleep;
+ self->hardware = hardware;
+
+ if (hardware) {
+ watchdogtimer_hardware_init(self->timeout, self->sleep);
+ wdt_singleton = self;
+ } else {
+ uint64_t ticks = timeout * 31250ULL;
+ if (ticks > UINT32_MAX) {
+ mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
+ }
+
+ if (timer_refcount == 0) {
+ timer = nrf_peripherals_allocate_timer_or_throw();
+ }
+ timer_refcount++;
+
+ nrfx_timer_config_t timer_config = {
+ .frequency = NRF_TIMER_FREQ_31250Hz,
+ .mode = NRF_TIMER_MODE_TIMER,
+ .bit_width = NRF_TIMER_BIT_WIDTH_32,
+ .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
+ .p_context = self,
+ };
+
+ nrfx_timer_init(timer, &timer_config, &watchdogtimer_event_handler);
+
+ // true enables interrupt.
+ nrfx_timer_clear(timer);
+ nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true);
+ nrfx_timer_resume(timer);
+ }
+
+ // Feed the watchdog, in case there's a timer that's already running
+ // and it's only partially finished.
+ mp_obj_t *self_obj = MP_OBJ_FROM_PTR(self);
+ watchdog_watchdogtimer_feed(self_obj);
+ return self_obj;
+}
+
+>>>>>>> parent of 561e7e619... add WatchDogTimeout exception
//| def feed(self):
//| """Feed the watchdog timer. This must be called regularly, otherwise
//| the timer will expire."""
diff --git a/ports/nrf/common-hal/watchdog/__init__.h b/ports/nrf/common-hal/watchdog/__init__.h
index fbfb98eff..de19bdae4 100644
--- a/ports/nrf/common-hal/watchdog/__init__.h
+++ b/ports/nrf/common-hal/watchdog/__init__.h
@@ -27,12 +27,4 @@
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG___INIT___H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG___INIT___H
-#include "py/obj.h"
-#include "shared-bindings/watchdog/__init__.h"
-
-typedef struct _watchdog_obj_t {
- mp_obj_base_t base;
- mp_rom_obj_t *watchdogtimer;
-} watchdog_obj_t;
-
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG___INIT___H
diff --git a/py/modbuiltins.c b/py/modbuiltins.c
index b031ce9fe..e764f1987 100644
--- a/py/modbuiltins.c
+++ b/py/modbuiltins.c
@@ -719,9 +719,6 @@ 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 5d3b7709b..a5815776a 100644
--- a/py/mpstate.h
+++ b/py/mpstate.h
@@ -141,11 +141,6 @@ 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 cabe5498d..fa315d12f 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -596,9 +596,6 @@ 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 db09d5153..b7a536c5e 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -256,9 +256,6 @@ 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 02352ea6c..59dcbc7a1 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -86,15 +86,6 @@ 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;