Skip to content

Commit aac5a4f

Browse files
committed
watchdog: use common_hal_watchdog_* pattern
This pulls all common functionality into `shared-bindings` and keeps platform-specific code inside `nrf`. Additionally, this performs most validation in the `shared-bindings` site. The only validation that occurs inside platform-specific `common-hal` code is related to timeout limits that are platform-specific. Additionally, all documentation is now inside the `shared-bindings` directory. Signed-off-by: Sean Cross <[email protected]>
1 parent dbf1bef commit aac5a4f

File tree

7 files changed

+269
-207
lines changed

7 files changed

+269
-207
lines changed

ports/nrf/common-hal/microcontroller/__init__.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
123123
.type = &watchdog_watchdogtimer_type,
124124
},
125125
.timeout = 0.0f,
126-
.sleep = false,
127126
.mode = WATCHDOGMODE_NONE,
128127
};
129128
#endif

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

Lines changed: 52 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -69,88 +69,46 @@ STATIC void watchdogtimer_timer_event_handler(nrf_timer_event_t event_type, void
6969
#endif
7070
}
7171

72-
// This function is called if the timer expires. The system will reboot in 1/16384 of a second.
73-
// Issue a reboot ourselves so we can do any cleanup necessary.
72+
static void timer_free(void) {
73+
timer_refcount--;
74+
if (timer_refcount == 0) {
75+
nrf_peripherals_free_timer(timer);
76+
timer = NULL;
77+
}
78+
}
79+
80+
// This function is called if the timer expires. The system will reboot
81+
// in 1/16384 of a second. Issue a reboot ourselves so we can do any
82+
// cleanup necessary.
7483
STATIC void watchdogtimer_watchdog_event_handler(void) {
7584
reset_cpu();
7685
}
7786

78-
//| def feed(self):
79-
//| """Feed the watchdog timer. This must be called regularly, otherwise
80-
//| the timer will expire."""
81-
//| ...
82-
//|
83-
STATIC mp_obj_t watchdog_watchdogtimer_feed(mp_obj_t self_in) {
84-
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
85-
87+
void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
8688
if (self->mode == WATCHDOGMODE_RESET) {
8789
nrfx_wdt_feed(&wdt);
8890
} else if (self->mode == WATCHDOGMODE_RAISE) {
8991
nrfx_timer_clear(timer);
90-
} else if (self->mode == WATCHDOGMODE_NONE) {
91-
mp_raise_ValueError(translate("WatchDogTimer is not currently running"));
9292
}
93-
return mp_const_none;
9493
}
95-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_feed_obj, watchdog_watchdogtimer_feed);
96-
97-
//| def deinit(self):
98-
//| """Stop the watchdog timer. This may raise an error if the watchdog
99-
//| timer cannot be disabled on this platform."""
100-
//| ...
101-
//|
102-
STATIC mp_obj_t watchdog_watchdogtimer_deinit(mp_obj_t self_in) {
103-
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
10494

105-
if (self->mode == WATCHDOGMODE_RAISE) {
106-
timer_refcount--;
107-
if (timer_refcount == 0) {
108-
nrf_peripherals_free_timer(timer);
109-
timer = NULL;
110-
}
111-
self->mode = WATCHDOGMODE_NONE;
112-
} else if (self->mode == WATCHDOGMODE_RESET) {
113-
mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
95+
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
96+
if (timer) {
97+
timer_free();
11498
}
115-
116-
return mp_const_none;
99+
self->mode = WATCHDOGMODE_NONE;
117100
}
118-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_deinit_obj, watchdog_watchdogtimer_deinit);
119101

120102
void watchdog_reset(void) {
121-
if (common_hal_mcu_watchdogtimer_obj.mode == WATCHDOGMODE_RAISE) {
122-
common_hal_mcu_watchdogtimer_obj.mode = WATCHDOGMODE_NONE;
123-
timer_refcount--;
124-
if (timer_refcount == 0) {
125-
nrf_peripherals_free_timer(timer);
126-
timer = NULL;
127-
}
128-
}
103+
common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
129104
}
130105

131-
//| timeout: float = ...
132-
//| """The maximum number of seconds that can elapse between calls
133-
//| to feed()"""
134-
//|
135-
STATIC mp_obj_t watchdog_watchdogtimer_obj_get_timeout(mp_obj_t self_in) {
136-
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
137-
return mp_obj_new_float(self->timeout);
106+
mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
107+
return self->timeout;
138108
}
139-
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_timeout_obj, watchdog_watchdogtimer_obj_get_timeout);
140-
141-
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout(mp_obj_t self_in, mp_obj_t timeout_obj) {
142-
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
143-
mp_float_t timeout = mp_obj_get_float(timeout_obj);
144-
145-
if (timeout <= 0) {
146-
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
147-
}
148109

149-
if (self->mode == WATCHDOGMODE_RESET) {
150-
// If the WatchDogTimer is already running in "RESET" mode, raise an error
151-
// since the mode cannot be changed once started.
152-
mp_raise_TypeError(translate("cannot change the timeout once mode is WatchDogMode.RESET"));
153-
} else if (self->mode == WATCHDOGMODE_RAISE) {
110+
void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t timeout) {
111+
if (self->mode == WATCHDOGMODE_RAISE) {
154112
// If the WatchDogTimer is already running in "RAISE" mode, reset the timer
155113
// with the new value.
156114
uint64_t ticks = timeout * 31250ULL;
@@ -162,108 +120,42 @@ STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout(mp_obj_t self_in, mp_obj_
162120
}
163121

164122
self->timeout = timeout;
165-
return mp_const_none;
166123
}
167-
MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_timeout_obj, watchdog_watchdogtimer_obj_set_timeout);
168-
169-
const mp_obj_property_t watchdog_watchdogtimer_timeout_obj = {
170-
.base.type = &mp_type_property,
171-
.proxy = {(mp_obj_t)&watchdog_watchdogtimer_get_timeout_obj,
172-
(mp_obj_t)&watchdog_watchdogtimer_set_timeout_obj,
173-
(mp_obj_t)&mp_const_none_obj},
174-
};
175124

176-
//| mode: watchdog.WatchDogMode = ...
177-
//| """The current operating mode of the WatchDogTimer `watchdog.WatchDogMode`.
178-
//|
179-
//| Setting a WatchDogMode activates the WatchDog::
180-
//|
181-
//| import microcontroller
182-
//| import watchdog
183-
//|
184-
//| w = microcontroller.watchdog
185-
//| w.timeout = 5
186-
//| w.mode = watchdog.WatchDogMode.RAISE
187-
//|
188-
//|
189-
//| Once set, the WatchDogTimer will perform the specified action if the timer expires.
190-
//|
191-
STATIC mp_obj_t watchdog_watchdogtimer_obj_get_mode(mp_obj_t self_in) {
192-
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
193-
switch (self->mode) {
194-
case WATCHDOGMODE_NONE: default: return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_none_obj);
195-
case WATCHDOGMODE_RAISE: return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_raise_obj);
196-
case WATCHDOGMODE_RESET: return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_reset_obj);
197-
}
125+
watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
126+
return self->mode;
198127
}
199-
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_mode_obj, watchdog_watchdogtimer_obj_get_mode);
200128

201-
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t mode_obj) {
202-
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
203-
watchdog_watchdogmode_obj_t *mode = MP_OBJ_TO_PTR(mode_obj);
204-
if (mode == MP_ROM_PTR(&watchdog_watchdogmode_none_obj)) {
205-
if (self->mode == WATCHDOGMODE_RESET) {
206-
mp_raise_TypeError(translate("WatchDogTimer mode cannot be changed once set to WatchDogMode.RESET"));
207-
}
208-
else if (self->mode == WATCHDOGMODE_RAISE) {
209-
timer_refcount--;
210-
if (timer_refcount == 0) {
211-
nrf_peripherals_free_timer(timer);
212-
timer = NULL;
213-
}
214-
}
215-
self->mode = WATCHDOGMODE_NONE;
129+
void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
130+
watchdog_watchdogmode_t current_mode = self->mode;
216131

217-
} else if (mode == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
218-
if (self->timeout <= 0) {
219-
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
220-
}
221-
if (self->mode == WATCHDOGMODE_RESET) {
222-
mp_raise_ValueError(translate("WatchDogTimer mode cannot be changed once set to WatchDogMode.RESET"));
132+
if (new_mode == WATCHDOGMODE_RAISE) {
133+
if (timer_refcount == 0) {
134+
timer = nrf_peripherals_allocate_timer_or_throw();
223135
}
224-
else if (self->mode == WATCHDOGMODE_NONE || self->mode == WATCHDOGMODE_RAISE) {
225-
if (timer_refcount == 0) {
226-
timer = nrf_peripherals_allocate_timer_or_throw();
227-
}
228-
if (timer == NULL) {
229-
mp_raise_RuntimeError(translate("timer was null"));
230-
}
231-
timer_refcount++;
232-
233-
nrfx_timer_config_t timer_config = {
234-
.frequency = NRF_TIMER_FREQ_31250Hz,
235-
.mode = NRF_TIMER_MODE_TIMER,
236-
.bit_width = NRF_TIMER_BIT_WIDTH_32,
237-
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
238-
.p_context = self,
239-
};
240-
241-
nrfx_timer_init(timer, &timer_config, &watchdogtimer_timer_event_handler);
136+
timer_refcount++;
137+
138+
nrfx_timer_config_t timer_config = {
139+
.frequency = NRF_TIMER_FREQ_31250Hz,
140+
.mode = NRF_TIMER_MODE_TIMER,
141+
.bit_width = NRF_TIMER_BIT_WIDTH_32,
142+
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
143+
.p_context = self,
144+
};
242145

243-
uint64_t ticks = nrfx_timer_ms_to_ticks(timer, self->timeout * 1000);
244-
if (ticks > UINT32_MAX) {
245-
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
246-
}
146+
nrfx_timer_init(timer, &timer_config, &watchdogtimer_timer_event_handler);
247147

248-
// true enables interrupt.
249-
nrfx_timer_clear(timer);
250-
nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true);
251-
nrfx_timer_resume(timer);
148+
uint64_t ticks = nrfx_timer_ms_to_ticks(timer, self->timeout * 1000);
149+
if (ticks > UINT32_MAX) {
150+
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
252151
}
253-
self->mode = WATCHDOGMODE_RAISE;
254152

255-
} else if (mode == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
256-
if (self->timeout <= 0) {
257-
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
258-
}
259-
if (self->mode == WATCHDOGMODE_RAISE) {
260-
timer_refcount--;
261-
if (timer_refcount == 0) {
262-
nrf_peripherals_free_timer(timer);
263-
timer = NULL;
264-
}
265-
}
153+
// true enables interrupt.
154+
nrfx_timer_clear(timer);
155+
nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true);
156+
nrfx_timer_resume(timer);
266157

158+
} else if (new_mode == WATCHDOGMODE_RESET) {
267159
uint64_t ticks = self->timeout * 1000.0f;
268160
if (ticks > UINT32_MAX) {
269161
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
@@ -286,31 +178,12 @@ STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t m
286178
}
287179
nrfx_wdt_enable(&wdt);
288180
nrfx_wdt_feed(&wdt);
289-
self->mode = WATCHDOGMODE_RESET;
290181
}
291182

292-
return mp_const_none;
293-
}
294-
MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_mode_obj, watchdog_watchdogtimer_obj_set_mode);
295-
296-
const mp_obj_property_t watchdog_watchdogtimer_mode_obj = {
297-
.base.type = &mp_type_property,
298-
.proxy = {(mp_obj_t)&watchdog_watchdogtimer_get_mode_obj,
299-
(mp_obj_t)&watchdog_watchdogtimer_set_mode_obj,
300-
(mp_obj_t)&mp_const_none_obj},
301-
};
302-
303-
STATIC const mp_rom_map_elem_t watchdog_watchdogtimer_locals_dict_table[] = {
304-
{ MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&watchdog_watchdogtimer_feed_obj) },
305-
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&watchdog_watchdogtimer_deinit_obj) },
306-
{ MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&watchdog_watchdogtimer_timeout_obj) },
307-
{ MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&watchdog_watchdogtimer_mode_obj) },
308-
};
309-
STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogtimer_locals_dict, watchdog_watchdogtimer_locals_dict_table);
183+
// If we just switched away from RAISE, disable the timmer.
184+
if (current_mode == WATCHDOGMODE_RAISE && new_mode != WATCHDOGMODE_RAISE) {
185+
timer_free();
186+
}
310187

311-
const mp_obj_type_t watchdog_watchdogtimer_type = {
312-
{ &mp_type_type },
313-
.name = MP_QSTR_WatchDogTimer,
314-
// .make_new = watchdog_watchdogtimer_make_new,
315-
.locals_dict = (mp_obj_dict_t*)&watchdog_watchdogtimer_locals_dict,
316-
};
188+
self->mode = new_mode;
189+
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@
2828
#define MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
2929

3030
#include "py/obj.h"
31-
#include "shared-bindings/watchdog/WatchDogTimer.h"
3231
#include "shared-bindings/watchdog/WatchDogMode.h"
32+
#include "shared-bindings/watchdog/WatchDogTimer.h"
3333

34-
typedef struct _watchdog_watchdogtimer_obj_t {
35-
mp_obj_base_t base;
36-
mp_float_t timeout;
37-
bool sleep;
38-
watchdog_watchdogmode_t mode;
39-
} watchdog_watchdogtimer_obj_t;
34+
struct _watchdog_watchdogtimer_obj_t {
35+
mp_obj_base_t base;
36+
mp_float_t timeout;
37+
watchdog_watchdogmode_t mode;
38+
};
4039

40+
// This needs to be called in order to disable the watchdog if it's set to
41+
// "RAISE". If set to "RESET", then the watchdog cannot be reset.
4142
void watchdog_reset(void);
4243

43-
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
44+
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H

shared-bindings/watchdog/WatchDogMode.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
//| def __init__(self, ):
3333
//| """Enum-like class to define the run mode of the watchdog timer."""
3434
//|
35-
//| NONE: Any = ...
36-
//| """Take no action if the watchdog timer expires.
37-
//|
38-
//| :type watchdog.WatchDogMode:"""
39-
//|
4035
//| RAISE: Any = ...
4136
//| """Raise an exception when the WatchDogTimer expires.
4237
//|
@@ -49,10 +44,6 @@
4944
//|
5045
const mp_obj_type_t watchdog_watchdogmode_type;
5146

52-
const watchdog_watchdogmode_obj_t watchdog_watchdogmode_none_obj = {
53-
{ &watchdog_watchdogmode_type },
54-
};
55-
5647
const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj = {
5748
{ &watchdog_watchdogmode_type },
5849
};
@@ -61,8 +52,29 @@ const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj = {
6152
{ &watchdog_watchdogmode_type },
6253
};
6354

55+
watchdog_watchdogmode_t watchdog_watchdogmode_obj_to_type(mp_obj_t obj) {
56+
if (obj == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
57+
return WATCHDOGMODE_RAISE;
58+
} else if (obj == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
59+
return WATCHDOGMODE_RESET;
60+
}
61+
return WATCHDOGMODE_NONE;
62+
}
63+
64+
mp_obj_t watchdog_watchdogmode_type_to_obj(watchdog_watchdogmode_t mode) {
65+
switch (mode) {
66+
case WATCHDOGMODE_RAISE:
67+
return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_raise_obj);
68+
case WATCHDOGMODE_RESET:
69+
return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_reset_obj);
70+
case WATCHDOGMODE_NONE:
71+
default:
72+
return (mp_obj_t)MP_ROM_PTR(&mp_const_none_obj);
73+
}
74+
}
75+
6476
STATIC const mp_rom_map_elem_t watchdog_watchdogmode_locals_dict_table[] = {
65-
{MP_ROM_QSTR(MP_QSTR_NONE), MP_ROM_PTR(&watchdog_watchdogmode_none_obj)},
77+
{MP_ROM_QSTR(MP_QSTR_NONE), MP_ROM_PTR(&mp_const_none_obj)},
6678
{MP_ROM_QSTR(MP_QSTR_RAISE), MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)},
6779
{MP_ROM_QSTR(MP_QSTR_RESET), MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)},
6880
};

shared-bindings/watchdog/WatchDogMode.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ typedef enum {
3737

3838
const mp_obj_type_t watchdog_watchdogmode_type;
3939

40+
watchdog_watchdogmode_t watchdog_watchdogmode_obj_to_type(mp_obj_t obj);
41+
mp_obj_t watchdog_watchdogmode_type_to_obj(watchdog_watchdogmode_t mode);
42+
4043
typedef struct {
4144
mp_obj_base_t base;
4245
} watchdog_watchdogmode_obj_t;
43-
extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_none_obj;
4446
extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj;
4547
extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj;
4648

0 commit comments

Comments
 (0)