Skip to content

Commit bd086a1

Browse files
committed
Revert "add WatchDogTimeout exception"
This reverts commit 561e7e6.
1 parent 589cb1a commit bd086a1

File tree

7 files changed

+108
-35
lines changed

7 files changed

+108
-35
lines changed

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

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,21 @@ STATIC nrfx_timer_t *timer = NULL;
4949
STATIC nrfx_wdt_t wdt = NRFX_WDT_INSTANCE(0);
5050
STATIC nrfx_wdt_channel_id wdt_channel_id;
5151

52-
NORETURN void mp_raise_WatchDogTimeout(void) {
53-
nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception)));
54-
}
52+
const mp_obj_type_t mp_type_WatchDogTimeout = {
53+
{ &mp_type_type },
54+
.name = MP_QSTR_WatchDogTimeout,
55+
.make_new = mp_obj_exception_make_new,
56+
.attr = mp_obj_exception_attr,
57+
.parent = &mp_type_Exception,
58+
};
59+
60+
static mp_obj_exception_t mp_watchdog_timeout_exception = {
61+
.base.type = &mp_type_WatchDogTimeout,
62+
.traceback_alloc = 0,
63+
.traceback_len = 0,
64+
.traceback_data = NULL,
65+
.args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj,
66+
};
5567

5668
STATIC void watchdogtimer_timer_event_handler(nrf_timer_event_t event_type, void *p_context) {
5769
(void)p_context;
@@ -62,7 +74,8 @@ STATIC void watchdogtimer_timer_event_handler(nrf_timer_event_t event_type, void
6274

6375
// If the timer hits without being cleared, pause the timer and raise an exception.
6476
nrfx_timer_pause(timer);
65-
MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_watchdog_exception));
77+
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception));
78+
MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception;
6679
#if MICROPY_ENABLE_SCHEDULER
6780
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
6881
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
@@ -84,6 +97,97 @@ void watchdog_watchdogtimer_reset(void) {
8497
timer_refcount = 0;
8598
}
8699

100+
<<<<<<< HEAD
101+
=======
102+
//| class WDT:
103+
//| """Watchdog Timer"""
104+
//|
105+
//| def __init__(self, ):
106+
//| """This class represents the system's Watchdog Timer. It is a
107+
//| singleton and will always return the same instance.
108+
//|
109+
//| """
110+
//| ...
111+
//|
112+
STATIC mp_obj_t watchdog_watchdogtimer_make_new(const mp_obj_type_t *type, size_t n_args,
113+
const mp_obj_t *pos_args,
114+
mp_map_t *kw_args) {
115+
enum { ARG_timeout, ARG_sleep, ARG_hardware };
116+
static const mp_arg_t allowed_args[] = {
117+
{MP_QSTR_timeout, MP_ARG_OBJ | MP_ARG_REQUIRED},
118+
{MP_QSTR_sleep, MP_ARG_BOOL, {.u_bool = false}},
119+
{MP_QSTR_hardware, MP_ARG_BOOL, {.u_bool = false}},
120+
};
121+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
122+
123+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
124+
allowed_args, args);
125+
mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
126+
bool hardware = args[ARG_hardware].u_bool;
127+
bool sleep = args[ARG_sleep].u_bool;
128+
129+
// If the hardware timer is already running, return that timer.
130+
// If the parameters have changed, then ignore them, but print
131+
// an error.
132+
if (wdt_singleton && hardware) {
133+
if ((sleep != wdt_singleton->sleep)
134+
|| (hardware != wdt_singleton->hardware)
135+
|| fabsf(timeout - wdt_singleton->timeout) > 0.01f) {
136+
// Print a warning indicating things aren't quite right
137+
// mp_printf(&mp_stderr_print, translate("warning: hardware timer was already running"));
138+
}
139+
watchdogtimer_hardware_feed();
140+
return wdt_singleton;
141+
}
142+
143+
if (timeout <= 0) {
144+
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
145+
}
146+
147+
watchdog_watchdogtimer_obj_t *self = m_new_obj(watchdog_watchdogtimer_obj_t);
148+
self->base.type = &watchdog_watchdogtimer_type;
149+
self->timeout = timeout;
150+
self->sleep = sleep;
151+
self->hardware = hardware;
152+
153+
if (hardware) {
154+
watchdogtimer_hardware_init(self->timeout, self->sleep);
155+
wdt_singleton = self;
156+
} else {
157+
uint64_t ticks = timeout * 31250ULL;
158+
if (ticks > UINT32_MAX) {
159+
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
160+
}
161+
162+
if (timer_refcount == 0) {
163+
timer = nrf_peripherals_allocate_timer_or_throw();
164+
}
165+
timer_refcount++;
166+
167+
nrfx_timer_config_t timer_config = {
168+
.frequency = NRF_TIMER_FREQ_31250Hz,
169+
.mode = NRF_TIMER_MODE_TIMER,
170+
.bit_width = NRF_TIMER_BIT_WIDTH_32,
171+
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
172+
.p_context = self,
173+
};
174+
175+
nrfx_timer_init(timer, &timer_config, &watchdogtimer_event_handler);
176+
177+
// true enables interrupt.
178+
nrfx_timer_clear(timer);
179+
nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true);
180+
nrfx_timer_resume(timer);
181+
}
182+
183+
// Feed the watchdog, in case there's a timer that's already running
184+
// and it's only partially finished.
185+
mp_obj_t *self_obj = MP_OBJ_FROM_PTR(self);
186+
watchdog_watchdogtimer_feed(self_obj);
187+
return self_obj;
188+
}
189+
190+
>>>>>>> parent of 561e7e619... add WatchDogTimeout exception
87191
//| def feed(self):
88192
//| """Feed the watchdog timer. This must be called regularly, otherwise
89193
//| the timer will expire."""

ports/nrf/common-hal/watchdog/__init__.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,4 @@
2727
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG___INIT___H
2828
#define MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG___INIT___H
2929

30-
#include "py/obj.h"
31-
#include "shared-bindings/watchdog/__init__.h"
32-
33-
typedef struct _watchdog_obj_t {
34-
mp_obj_base_t base;
35-
mp_rom_obj_t *watchdogtimer;
36-
} watchdog_obj_t;
37-
3830
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG___INIT___H

py/modbuiltins.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,6 @@ 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
725722
{ MP_ROM_QSTR(MP_QSTR_KeyError), MP_ROM_PTR(&mp_type_KeyError) },
726723
{ MP_ROM_QSTR(MP_QSTR_LookupError), MP_ROM_PTR(&mp_type_LookupError) },
727724
{ MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_MemoryError) },

py/mpstate.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,6 @@ 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-
149144
// dictionary with loaded modules (may be exposed as sys.modules)
150145
mp_obj_dict_t mp_loaded_modules_dict;
151146

py/obj.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,6 @@ 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
602599
extern const mp_obj_type_t mp_type_KeyError;
603600
extern const mp_obj_type_t mp_type_LookupError;
604601
extern const mp_obj_type_t mp_type_MemoryError;

py/objexcept.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,6 @@ 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
262259
MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
263260
MP_DEFINE_EXCEPTION(Exception, BaseException)
264261
#if MICROPY_PY_ASYNC_AWAIT

py/runtime.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ 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-
9889
MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException;
9990
MP_STATE_VM(mp_reload_exception).traceback_alloc = 0;
10091
MP_STATE_VM(mp_reload_exception).traceback_len = 0;

0 commit comments

Comments
 (0)