summaryrefslogtreecommitdiff
path: root/ports
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 /ports
parent589cb1af6de04a01e1cfda1082fea4f6fbc4440c (diff)
Revert "add WatchDogTimeout exception"
This reverts commit 561e7e619095869f58fc728d428f3ff20e8bfc40.
Diffstat (limited to 'ports')
-rw-r--r--ports/nrf/common-hal/watchdog/WatchDogTimer.c112
-rw-r--r--ports/nrf/common-hal/watchdog/__init__.h8
2 files changed, 108 insertions, 12 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