Skip to content

Commit 9b3f858

Browse files
committed
wdt: add watchdog support
This adds shared bindings for a watchdog timer, based on the API provided by micropython. Signed-off-by: Sean Cross <[email protected]>
1 parent 2585953 commit 9b3f858

File tree

7 files changed

+289
-1
lines changed

7 files changed

+289
-1
lines changed

py/circuitpy_defns.mk

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ endif
241241
ifeq ($(CIRCUITPY_USTACK),1)
242242
SRC_PATTERNS += ustack/%
243243
endif
244+
ifeq ($(CIRCUITPY_WDT),1)
245+
SRC_PATTERNS += wdt/%
246+
endif
244247
ifeq ($(CIRCUITPY_PEW),1)
245248
SRC_PATTERNS += _pew/%
246249
endif
@@ -301,7 +304,9 @@ SRC_COMMON_HAL_ALL = \
301304
rtc/RTC.c \
302305
rtc/__init__.c \
303306
supervisor/Runtime.c \
304-
supervisor/__init__.c
307+
supervisor/__init__.c \
308+
wdt/__init__.c \
309+
wdt/WDT.c \
305310

306311
SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL))
307312

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,13 @@ extern const struct _mp_obj_module_t ustack_module;
630630
#define RE_MODULE
631631
#endif
632632

633+
#if CIRCUITPY_WDT
634+
extern const struct _mp_obj_module_t wdt_module;
635+
#define WDT_MODULE { MP_ROM_QSTR(MP_QSTR_wdt), MP_ROM_PTR(&wdt_module) },
636+
#else
637+
#define WDT_MODULE
638+
#endif
639+
633640
// Define certain native modules with weak links so they can be replaced with Python
634641
// implementations. This list may grow over time.
635642
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
@@ -700,6 +707,7 @@ extern const struct _mp_obj_module_t ustack_module;
700707
USB_HID_MODULE \
701708
USB_MIDI_MODULE \
702709
USTACK_MODULE \
710+
WDT_MODULE \
703711

704712
// If weak links are enabled, just include strong links in the main list of modules,
705713
// and also include the underscore alternate names.

py/circuitpy_mpconfig.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ CFLAGS += -DCIRCUITPY_SERIAL_UART=$(CIRCUITPY_SERIAL_UART)
228228
CIRCUITPY_ULAB ?= $(CIRCUITPY_FULL_BUILD)
229229
CFLAGS += -DCIRCUITPY_ULAB=$(CIRCUITPY_ULAB)
230230

231+
# watchdog hardware support
232+
CIRCUITPY_WDT ?= 0
233+
CFLAGS += -DCIRCUITPY_WDT=$(CIRCUITPY_WDT)
234+
231235
# Enabled micropython.native decorator (experimental)
232236
CIRCUITPY_ENABLE_MPY_NATIVE ?= 0
233237
CFLAGS += -DCIRCUITPY_ENABLE_MPY_NATIVE=$(CIRCUITPY_ENABLE_MPY_NATIVE)

shared-bindings/wdt/WDT.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Paul Sokolovsky
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <string.h>
28+
29+
#include "py/obj.h"
30+
#include "py/objproperty.h"
31+
#include "py/runtime.h"
32+
#include "shared-bindings/wdt/__init__.h"
33+
#include "shared-bindings/wdt/WDT.h"
34+
35+
static wdt_wdt_obj_t *wdt_singleton;
36+
37+
//| class WDT:
38+
//| """Watchdog Timer"""
39+
//|
40+
//| def __init__(self, ):
41+
//| """This class represents the system's Watchdog Timer. It is a
42+
//| singleton and will always return the same instance."""
43+
//| ...
44+
//|
45+
STATIC mp_obj_t wdt_wdt_make_new(const mp_obj_type_t *type, size_t n_args,
46+
const mp_obj_t *pos_args,
47+
mp_map_t *kw_args) {
48+
enum { ARG_timeout_ms, ARG_sleep };
49+
static const mp_arg_t allowed_args[] = {
50+
{MP_QSTR_timeout_ms, MP_ARG_INT | MP_ARG_REQUIRED},
51+
{MP_QSTR_sleep, MP_ARG_BOOL, {.u_bool = false}},
52+
};
53+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
54+
55+
if (wdt_singleton)
56+
return wdt_singleton;
57+
58+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
59+
allowed_args, args);
60+
61+
if (args[ARG_timeout_ms].u_int <= 0) {
62+
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
63+
}
64+
65+
wdt_wdt_obj_t *self = m_new_obj(wdt_wdt_obj_t);
66+
self->base.type = &wdt_wdt_type;
67+
self->timeout = args[ARG_timeout_ms].u_int;
68+
self->sleep = args[ARG_sleep].u_bool;
69+
70+
common_hal_wdt_init(self->timeout, self->sleep);
71+
wdt_singleton = self;
72+
return MP_OBJ_FROM_PTR(self);
73+
}
74+
75+
//| def feed(self):
76+
//| """Feed the watchdog timer. This must be called regularly, otherwise
77+
//| the system will reset."""
78+
//| ...
79+
//|
80+
STATIC mp_obj_t wdt_wdt_feed(mp_obj_t self_in) {
81+
(void)self_in;
82+
common_hal_wdt_feed();
83+
return mp_const_none;
84+
}
85+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_feed_obj, wdt_wdt_feed);
86+
87+
//| def deinit(self):
88+
//| """Stop the watchdog timer. This may raise an error if the watchdog
89+
//| timer cannot be disabled on this platform."""
90+
//| ...
91+
//|
92+
STATIC mp_obj_t wdt_wdt_deinit(mp_obj_t self_in) {
93+
(void)self_in;
94+
common_hal_wdt_disable();
95+
return mp_const_none;
96+
}
97+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_deinit_obj, wdt_wdt_deinit);
98+
99+
//| timeout: int = ...
100+
//| """The maximum number of milliseconds that can elapse between calls
101+
//| to feed()"""
102+
//|
103+
STATIC mp_obj_t wdt_wdt_obj_get_timeout(mp_obj_t self_in) {
104+
wdt_wdt_obj_t *self = MP_OBJ_TO_PTR(self_in);
105+
return mp_obj_new_int(self->timeout);
106+
}
107+
MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_obj_get_timeout_obj, wdt_wdt_obj_get_timeout);
108+
109+
const mp_obj_property_t wdt_wdt_timeout_obj = {
110+
.base.type = &mp_type_property,
111+
.proxy = {(mp_obj_t)&wdt_wdt_obj_get_timeout_obj,
112+
(mp_obj_t)&mp_const_none_obj,
113+
(mp_obj_t)&mp_const_none_obj},
114+
};
115+
116+
STATIC const mp_rom_map_elem_t wdt_wdt_locals_dict_table[] = {
117+
{ MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&wdt_wdt_feed_obj) },
118+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&wdt_wdt_deinit_obj) },
119+
{ MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&wdt_wdt_timeout_obj) },
120+
};
121+
STATIC MP_DEFINE_CONST_DICT(wdt_wdt_locals_dict, wdt_wdt_locals_dict_table);
122+
123+
const mp_obj_type_t wdt_wdt_type = {
124+
{ &mp_type_type },
125+
.name = MP_QSTR_WDT,
126+
.make_new = wdt_wdt_make_new,
127+
.locals_dict = (mp_obj_dict_t*)&wdt_wdt_locals_dict,
128+
};

shared-bindings/wdt/WDT.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Noralf Trønnes
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H
29+
30+
#include <stdint.h>
31+
#include <stdbool.h>
32+
33+
extern void common_hal_wdt_init(uint32_t duration, bool pause_during_sleep);
34+
extern void common_hal_wdt_feed(void);
35+
extern void common_hal_wdt_disable(void);
36+
37+
extern const mp_obj_type_t wdt_wdt_type;
38+
39+
typedef struct _wdt_wdt_obj_t {
40+
mp_obj_base_t base;
41+
uint32_t timeout;
42+
bool sleep;
43+
} wdt_wdt_obj_t;
44+
45+
extern const wdt_wdt_obj_t wdt_wdt_obj;
46+
47+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H

shared-bindings/wdt/__init__.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Paul Sokolovsky
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <string.h>
28+
29+
#include "py/runtime.h"
30+
#include "shared-bindings/wdt/__init__.h"
31+
#include "shared-bindings/wdt/WDT.h"
32+
33+
//| """Watchdog Timer
34+
//|
35+
//| The `wdt` module provides support for a Watchdog Timer. This timer will reset the device
36+
//| if it hasn't been fed after a specified amount of time. This is useful to ensure the board
37+
//| has not crashed or locked up. You can enable thw watchdog timer using :class:`wdt.WDT`.
38+
//| Note that the watchdog timer cannot be disabled once it has been enabled.
39+
//|
40+
//| The WDT is used to restart the system when the application crashes and ends
41+
//| up into a non recoverable state. Once started it cannot be stopped or
42+
//| reconfigured in any way. After enabling, the application must "feed" the
43+
//| watchdog periodically to prevent it from expiring and resetting the system.
44+
//|
45+
//| Example usage::
46+
//|
47+
//| from machine import WDT
48+
//| wdt = WDT(timeout=2000) # enable it with a timeout of 2s
49+
//| wdt.feed()"""
50+
//|
51+
52+
STATIC const mp_rom_map_elem_t wdt_module_globals_table[] = {
53+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_wdt) },
54+
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&wdt_wdt_type) },
55+
};
56+
57+
STATIC MP_DEFINE_CONST_DICT(wdt_module_globals, wdt_module_globals_table);
58+
59+
const mp_obj_module_t wdt_module = {
60+
.base = { &mp_type_module },
61+
.globals = (mp_obj_dict_t*)&wdt_module_globals,
62+
};

shared-bindings/wdt/__init__.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H
29+
30+
#include <stdint.h>
31+
#include <stdbool.h>
32+
33+
34+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H

0 commit comments

Comments
 (0)