diff options
| -rw-r--r-- | py/circuitpy_defns.mk | 7 | ||||
| -rw-r--r-- | py/circuitpy_mpconfig.h | 8 | ||||
| -rw-r--r-- | py/circuitpy_mpconfig.mk | 4 | ||||
| -rw-r--r-- | shared-bindings/wdt/WDT.c | 128 | ||||
| -rw-r--r-- | shared-bindings/wdt/WDT.h | 47 | ||||
| -rw-r--r-- | shared-bindings/wdt/__init__.c | 62 | ||||
| -rw-r--r-- | shared-bindings/wdt/__init__.h | 34 |
7 files changed, 289 insertions, 1 deletions
diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 9e36f2e6a..be04dba48 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -241,6 +241,9 @@ endif ifeq ($(CIRCUITPY_USTACK),1) SRC_PATTERNS += ustack/% endif +ifeq ($(CIRCUITPY_WDT),1) +SRC_PATTERNS += wdt/% +endif ifeq ($(CIRCUITPY_PEW),1) SRC_PATTERNS += _pew/% endif @@ -301,7 +304,9 @@ SRC_COMMON_HAL_ALL = \ rtc/RTC.c \ rtc/__init__.c \ supervisor/Runtime.c \ - supervisor/__init__.c + supervisor/__init__.c \ + wdt/__init__.c \ + wdt/WDT.c \ SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL)) diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 5f2605c92..50f1139d7 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -630,6 +630,13 @@ extern const struct _mp_obj_module_t ustack_module; #define RE_MODULE #endif +#if CIRCUITPY_WDT +extern const struct _mp_obj_module_t wdt_module; +#define WDT_MODULE { MP_ROM_QSTR(MP_QSTR_wdt), MP_ROM_PTR(&wdt_module) }, +#else +#define WDT_MODULE +#endif + // Define certain native modules with weak links so they can be replaced with Python // implementations. This list may grow over time. #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ @@ -700,6 +707,7 @@ extern const struct _mp_obj_module_t ustack_module; USB_HID_MODULE \ USB_MIDI_MODULE \ USTACK_MODULE \ + WDT_MODULE \ // If weak links are enabled, just include strong links in the main list of modules, // and also include the underscore alternate names. diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 0801540bc..1b74f45c3 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -228,6 +228,10 @@ CFLAGS += -DCIRCUITPY_SERIAL_UART=$(CIRCUITPY_SERIAL_UART) CIRCUITPY_ULAB ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_ULAB=$(CIRCUITPY_ULAB) +# watchdog hardware support +CIRCUITPY_WDT ?= 0 +CFLAGS += -DCIRCUITPY_WDT=$(CIRCUITPY_WDT) + # Enabled micropython.native decorator (experimental) CIRCUITPY_ENABLE_MPY_NATIVE ?= 0 CFLAGS += -DCIRCUITPY_ENABLE_MPY_NATIVE=$(CIRCUITPY_ENABLE_MPY_NATIVE) diff --git a/shared-bindings/wdt/WDT.c b/shared-bindings/wdt/WDT.c new file mode 100644 index 000000000..edfac897e --- /dev/null +++ b/shared-bindings/wdt/WDT.c @@ -0,0 +1,128 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Paul Sokolovsky + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <string.h> + +#include "py/obj.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/wdt/__init__.h" +#include "shared-bindings/wdt/WDT.h" + +static wdt_wdt_obj_t *wdt_singleton; + +//| 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 wdt_wdt_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_ms, ARG_sleep }; + static const mp_arg_t allowed_args[] = { + {MP_QSTR_timeout_ms, MP_ARG_INT | MP_ARG_REQUIRED}, + {MP_QSTR_sleep, MP_ARG_BOOL, {.u_bool = false}}, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + + if (wdt_singleton) + return wdt_singleton; + + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), + allowed_args, args); + + if (args[ARG_timeout_ms].u_int <= 0) { + mp_raise_ValueError(translate("watchdog timeout must be greater than 0")); + } + + wdt_wdt_obj_t *self = m_new_obj(wdt_wdt_obj_t); + self->base.type = &wdt_wdt_type; + self->timeout = args[ARG_timeout_ms].u_int; + self->sleep = args[ARG_sleep].u_bool; + + common_hal_wdt_init(self->timeout, self->sleep); + wdt_singleton = self; + return MP_OBJ_FROM_PTR(self); +} + +//| def feed(self): +//| """Feed the watchdog timer. This must be called regularly, otherwise +//| the system will reset.""" +//| ... +//| +STATIC mp_obj_t wdt_wdt_feed(mp_obj_t self_in) { + (void)self_in; + common_hal_wdt_feed(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_feed_obj, wdt_wdt_feed); + +//| def deinit(self): +//| """Stop the watchdog timer. This may raise an error if the watchdog +//| timer cannot be disabled on this platform.""" +//| ... +//| +STATIC mp_obj_t wdt_wdt_deinit(mp_obj_t self_in) { + (void)self_in; + common_hal_wdt_disable(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_deinit_obj, wdt_wdt_deinit); + +//| timeout: int = ... +//| """The maximum number of milliseconds that can elapse between calls +//| to feed()""" +//| +STATIC mp_obj_t wdt_wdt_obj_get_timeout(mp_obj_t self_in) { + wdt_wdt_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_int(self->timeout); +} +MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_obj_get_timeout_obj, wdt_wdt_obj_get_timeout); + +const mp_obj_property_t wdt_wdt_timeout_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&wdt_wdt_obj_get_timeout_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC const mp_rom_map_elem_t wdt_wdt_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&wdt_wdt_feed_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&wdt_wdt_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&wdt_wdt_timeout_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(wdt_wdt_locals_dict, wdt_wdt_locals_dict_table); + +const mp_obj_type_t wdt_wdt_type = { + { &mp_type_type }, + .name = MP_QSTR_WDT, + .make_new = wdt_wdt_make_new, + .locals_dict = (mp_obj_dict_t*)&wdt_wdt_locals_dict, +}; diff --git a/shared-bindings/wdt/WDT.h b/shared-bindings/wdt/WDT.h new file mode 100644 index 000000000..bfd83d482 --- /dev/null +++ b/shared-bindings/wdt/WDT.h @@ -0,0 +1,47 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Noralf Trønnes + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H + +#include <stdint.h> +#include <stdbool.h> + +extern void common_hal_wdt_init(uint32_t duration, bool pause_during_sleep); +extern void common_hal_wdt_feed(void); +extern void common_hal_wdt_disable(void); + +extern const mp_obj_type_t wdt_wdt_type; + +typedef struct _wdt_wdt_obj_t { + mp_obj_base_t base; + uint32_t timeout; + bool sleep; +} wdt_wdt_obj_t; + +extern const wdt_wdt_obj_t wdt_wdt_obj; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H diff --git a/shared-bindings/wdt/__init__.c b/shared-bindings/wdt/__init__.c new file mode 100644 index 000000000..a95fa2c53 --- /dev/null +++ b/shared-bindings/wdt/__init__.c @@ -0,0 +1,62 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Paul Sokolovsky + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <string.h> + +#include "py/runtime.h" +#include "shared-bindings/wdt/__init__.h" +#include "shared-bindings/wdt/WDT.h" + +//| """Watchdog Timer +//| +//| The `wdt` module provides support for a Watchdog Timer. This timer will reset the device +//| if it hasn't been fed after a specified amount of time. This is useful to ensure the board +//| has not crashed or locked up. You can enable thw watchdog timer using :class:`wdt.WDT`. +//| Note that the watchdog timer cannot be disabled once it has been enabled. +//| +//| The WDT is used to restart the system when the application crashes and ends +//| up into a non recoverable state. Once started it cannot be stopped or +//| reconfigured in any way. After enabling, the application must "feed" the +//| watchdog periodically to prevent it from expiring and resetting the system. +//| +//| Example usage:: +//| +//| from machine import WDT +//| wdt = WDT(timeout=2000) # enable it with a timeout of 2s +//| wdt.feed()""" +//| + +STATIC const mp_rom_map_elem_t wdt_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_wdt) }, + { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&wdt_wdt_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(wdt_module_globals, wdt_module_globals_table); + +const mp_obj_module_t wdt_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&wdt_module_globals, +}; diff --git a/shared-bindings/wdt/__init__.h b/shared-bindings/wdt/__init__.h new file mode 100644 index 000000000..b27452ca5 --- /dev/null +++ b/shared-bindings/wdt/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H + +#include <stdint.h> +#include <stdbool.h> + + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H |
