summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorScott Shawcroft <scott@adafruit.com>2020-06-02 10:33:24 -0700
committerGitHub <noreply@github.com>2020-06-02 10:33:24 -0700
commitdb04fd1725d375d7f9110c35f9346b32cb99b19e (patch)
tree652ffb11481a29bd424de8de05a8ab42ab042b99 /shared-bindings
parentaae7a459f37c284445eb68b5d691238f212a12a4 (diff)
parent7e632c9414073a36db0b74d891ee0408a2fdb565 (diff)
Merge pull request #2933 from simmel-project/wdt-nrf
Add Watchdog timer and add support for NRF watchdog
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/microcontroller/__init__.c5
-rw-r--r--shared-bindings/microcontroller/__init__.h5
-rw-r--r--shared-bindings/watchdog/WatchDogMode.c99
-rw-r--r--shared-bindings/watchdog/WatchDogMode.h49
-rw-r--r--shared-bindings/watchdog/WatchDogTimer.c186
-rw-r--r--shared-bindings/watchdog/WatchDogTimer.h48
-rw-r--r--shared-bindings/watchdog/__init__.c81
-rw-r--r--shared-bindings/watchdog/__init__.h34
8 files changed, 506 insertions, 1 deletions
diff --git a/shared-bindings/microcontroller/__init__.c b/shared-bindings/microcontroller/__init__.c
index 21e1c90a9..88fe9c224 100644
--- a/shared-bindings/microcontroller/__init__.c
+++ b/shared-bindings/microcontroller/__init__.c
@@ -167,6 +167,11 @@ STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
#else
{ MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&mp_const_none_obj) },
#endif
+ #if CIRCUITPY_WATCHDOG
+ { MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&common_hal_mcu_watchdogtimer_obj) },
+ #else
+ { MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&mp_const_none_obj) },
+ #endif
{ MP_ROM_QSTR(MP_QSTR_RunMode), MP_ROM_PTR(&mcu_runmode_type) },
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&mcu_pin_type) },
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&mcu_pin_module) },
diff --git a/shared-bindings/microcontroller/__init__.h b/shared-bindings/microcontroller/__init__.h
index e1487c555..8abdff763 100644
--- a/shared-bindings/microcontroller/__init__.h
+++ b/shared-bindings/microcontroller/__init__.h
@@ -49,10 +49,13 @@ extern const mcu_processor_obj_t common_hal_mcu_processor_obj;
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
-
#include "common-hal/nvm/ByteArray.h"
extern const nvm_bytearray_obj_t common_hal_mcu_nvm_obj;
+#endif
+#if CIRCUITPY_WATCHDOG
+#include "common-hal/watchdog/WatchDogTimer.h"
+extern watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj;
#endif
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H
diff --git a/shared-bindings/watchdog/WatchDogMode.c b/shared-bindings/watchdog/WatchDogMode.c
new file mode 100644
index 000000000..369454c11
--- /dev/null
+++ b/shared-bindings/watchdog/WatchDogMode.c
@@ -0,0 +1,99 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Sean Cross 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.
+ */
+
+#include "shared-bindings/watchdog/WatchDogMode.h"
+
+//| class WatchDogMode:
+//| """run state of the watchdog timer"""
+//|
+//| def __init__(self, ):
+//| """Enum-like class to define the run mode of the watchdog timer."""
+//|
+//| RAISE: Any = ...
+//| """Raise an exception when the WatchDogTimer expires.
+//|
+//| :type watchdog.WatchDogMode:"""
+//|
+//| RESET: Any = ...
+//| """Reset the system if the WatchDogTimer expires.
+//|
+//| :type watchdog.WatchDogMode:"""
+//|
+const mp_obj_type_t watchdog_watchdogmode_type;
+
+const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj = {
+ { &watchdog_watchdogmode_type },
+};
+
+const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj = {
+ { &watchdog_watchdogmode_type },
+};
+
+watchdog_watchdogmode_t watchdog_watchdogmode_obj_to_type(mp_obj_t obj) {
+ if (obj == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
+ return WATCHDOGMODE_RAISE;
+ } else if (obj == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
+ return WATCHDOGMODE_RESET;
+ }
+ return WATCHDOGMODE_NONE;
+}
+
+mp_obj_t watchdog_watchdogmode_type_to_obj(watchdog_watchdogmode_t mode) {
+ switch (mode) {
+ case WATCHDOGMODE_RAISE:
+ return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_raise_obj);
+ case WATCHDOGMODE_RESET:
+ return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_reset_obj);
+ case WATCHDOGMODE_NONE:
+ default:
+ return (mp_obj_t)MP_ROM_PTR(&mp_const_none_obj);
+ }
+}
+
+STATIC const mp_rom_map_elem_t watchdog_watchdogmode_locals_dict_table[] = {
+ {MP_ROM_QSTR(MP_QSTR_RAISE), MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)},
+ {MP_ROM_QSTR(MP_QSTR_RESET), MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)},
+};
+STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogmode_locals_dict, watchdog_watchdogmode_locals_dict_table);
+
+STATIC void watchdog_watchdogmode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ qstr runmode = MP_QSTR_None;
+ if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
+ runmode = MP_QSTR_RAISE;
+ }
+ else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
+ runmode = MP_QSTR_RESET;
+ }
+ mp_printf(print, "%q.%q.%q", MP_QSTR_watchdog, MP_QSTR_WatchDogMode,
+ runmode);
+}
+
+const mp_obj_type_t watchdog_watchdogmode_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_WatchDogMode,
+ .print = watchdog_watchdogmode_print,
+ .locals_dict = (mp_obj_t)&watchdog_watchdogmode_locals_dict,
+};
diff --git a/shared-bindings/watchdog/WatchDogMode.h b/shared-bindings/watchdog/WatchDogMode.h
new file mode 100644
index 000000000..68022671f
--- /dev/null
+++ b/shared-bindings/watchdog/WatchDogMode.h
@@ -0,0 +1,49 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Sean Cross 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_WATCHDOG_WATCHDOGMODE_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
+
+#include "py/obj.h"
+
+typedef enum {
+ WATCHDOGMODE_NONE,
+ WATCHDOGMODE_RAISE,
+ WATCHDOGMODE_RESET,
+} watchdog_watchdogmode_t;
+
+const mp_obj_type_t watchdog_watchdogmode_type;
+
+watchdog_watchdogmode_t watchdog_watchdogmode_obj_to_type(mp_obj_t obj);
+mp_obj_t watchdog_watchdogmode_type_to_obj(watchdog_watchdogmode_t mode);
+
+typedef struct {
+ mp_obj_base_t base;
+} watchdog_watchdogmode_obj_t;
+extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj;
+extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
diff --git a/shared-bindings/watchdog/WatchDogTimer.c b/shared-bindings/watchdog/WatchDogTimer.c
new file mode 100644
index 000000000..52bda4c77
--- /dev/null
+++ b/shared-bindings/watchdog/WatchDogTimer.c
@@ -0,0 +1,186 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Nick Moore 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.
+ */
+
+
+#include <math.h>
+#include <string.h>
+
+#include "py/obj.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+#include "common-hal/watchdog/WatchDogTimer.h"
+
+#include "shared-bindings/microcontroller/__init__.h"
+#include "shared-bindings/watchdog/__init__.h"
+#include "shared-bindings/watchdog/WatchDogTimer.h"
+
+#include "supervisor/port.h"
+
+//| class WatchDogTimer:
+//| """Timer that is used to detect code lock ups and automatically reset the microcontroller
+//| when one is detected.
+//|
+//| A lock up is detected when the watchdog hasn't been fed after a given duration. So, make
+//| sure to call `feed` within the timeout.
+//| """
+//|
+
+//| def __init__(self, ):
+//| """Not currently dynamically supported. Access the sole instance through `microcontroller.watchdog`."""
+//| ...
+//|
+
+//| def feed(self):
+//| """Feed the watchdog timer. This must be called regularly, otherwise
+//| the timer will expire."""
+//| ...
+//|
+STATIC mp_obj_t watchdog_watchdogtimer_feed(mp_obj_t self_in) {
+ watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ watchdog_watchdogmode_t current_mode = common_hal_watchdog_get_mode(self);
+
+ if (current_mode == WATCHDOGMODE_NONE) {
+ mp_raise_ValueError(translate("WatchDogTimer is not currently running"));
+ }
+ common_hal_watchdog_feed(self);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_feed_obj, watchdog_watchdogtimer_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 watchdog_watchdogtimer_deinit(mp_obj_t self_in) {
+ watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ watchdog_watchdogmode_t current_mode = common_hal_watchdog_get_mode(self);
+
+ if (current_mode == WATCHDOGMODE_RESET) {
+ mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
+ }
+
+ common_hal_watchdog_deinit(self);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_deinit_obj, watchdog_watchdogtimer_deinit);
+
+//| timeout: float = ...
+//| """The maximum number of seconds that can elapse between calls
+//| to feed()"""
+//|
+STATIC mp_obj_t watchdog_watchdogtimer_obj_get_timeout(mp_obj_t self_in) {
+ watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return mp_obj_new_float(common_hal_watchdog_get_timeout(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_timeout_obj, watchdog_watchdogtimer_obj_get_timeout);
+
+STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout(mp_obj_t self_in, mp_obj_t timeout_obj) {
+ watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ mp_float_t timeout = mp_obj_get_float(timeout_obj);
+
+ if (timeout <= 0) {
+ mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
+ }
+
+ common_hal_watchdog_set_timeout(self, timeout);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_timeout_obj, watchdog_watchdogtimer_obj_set_timeout);
+
+const mp_obj_property_t watchdog_watchdogtimer_timeout_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&watchdog_watchdogtimer_get_timeout_obj,
+ (mp_obj_t)&watchdog_watchdogtimer_set_timeout_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| mode: watchdog.WatchDogMode = ...
+//| """The current operating mode of the WatchDogTimer `watchdog.WatchDogMode`.
+//|
+//| Setting a WatchDogMode activates the WatchDog::
+//|
+//| import microcontroller
+//| import watchdog
+//|
+//| w = microcontroller.watchdog
+//| w.timeout = 5
+//| w.mode = watchdog.WatchDogMode.RAISE
+//|
+//|
+//| Once set, the WatchDogTimer will perform the specified action if the timer expires."""
+//|
+STATIC mp_obj_t watchdog_watchdogtimer_obj_get_mode(mp_obj_t self_in) {
+ watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return watchdog_watchdogmode_type_to_obj(common_hal_watchdog_get_mode(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_mode_obj, watchdog_watchdogtimer_obj_get_mode);
+
+STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t mode_obj) {
+ watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ watchdog_watchdogmode_t current_mode = common_hal_watchdog_get_mode(self);
+ watchdog_watchdogmode_t new_mode = watchdog_watchdogmode_obj_to_type(mode_obj);
+ mp_float_t current_timeout = common_hal_watchdog_get_timeout(self);
+
+ // When setting the mode, the timeout value must be greater than zero
+ if (new_mode == WATCHDOGMODE_RESET || new_mode == WATCHDOGMODE_RAISE) {
+ if (current_timeout <= 0) {
+ mp_raise_ValueError(translate("WatchDogTimer.timeout must be greater than 0"));
+ }
+ }
+
+ // Don't allow changing the mode once the watchdog timer has been started
+ if (current_mode == WATCHDOGMODE_RESET && new_mode != WATCHDOGMODE_RESET) {
+ mp_raise_TypeError(translate("WatchDogTimer.mode cannot be changed once set to WatchDogMode.RESET"));
+ }
+
+ common_hal_watchdog_set_mode(self, new_mode);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_mode_obj, watchdog_watchdogtimer_obj_set_mode);
+
+const mp_obj_property_t watchdog_watchdogtimer_mode_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&watchdog_watchdogtimer_get_mode_obj,
+ (mp_obj_t)&watchdog_watchdogtimer_set_mode_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+STATIC const mp_rom_map_elem_t watchdog_watchdogtimer_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&watchdog_watchdogtimer_feed_obj) },
+ { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&watchdog_watchdogtimer_deinit_obj) },
+ { MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&watchdog_watchdogtimer_timeout_obj) },
+ { MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&watchdog_watchdogtimer_mode_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogtimer_locals_dict, watchdog_watchdogtimer_locals_dict_table);
+
+const mp_obj_type_t watchdog_watchdogtimer_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_WatchDogTimer,
+ // .make_new = watchdog_watchdogtimer_make_new,
+ .locals_dict = (mp_obj_dict_t*)&watchdog_watchdogtimer_locals_dict,
+};
diff --git a/shared-bindings/watchdog/WatchDogTimer.h b/shared-bindings/watchdog/WatchDogTimer.h
new file mode 100644
index 000000000..48044748a
--- /dev/null
+++ b/shared-bindings/watchdog/WatchDogTimer.h
@@ -0,0 +1,48 @@
+/*
+ * 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_WATCHDOG_WATCHDOGTIMER_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGTIMER_H
+
+#include <py/obj.h>
+#include "shared-bindings/watchdog/WatchDogMode.h"
+
+typedef struct _watchdog_watchdogtimer_obj_t watchdog_watchdogtimer_obj_t;
+
+extern void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self);
+
+extern void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t);
+extern watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self);
+
+extern void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t timeout);
+extern mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self);
+
+extern void common_hal_watchdog_enable(watchdog_watchdogtimer_obj_t *self);
+extern void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self);
+
+extern const mp_obj_type_t watchdog_watchdogtimer_type;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGTIMER_H
diff --git a/shared-bindings/watchdog/__init__.c b/shared-bindings/watchdog/__init__.c
new file mode 100644
index 000000000..76e631729
--- /dev/null
+++ b/shared-bindings/watchdog/__init__.c
@@ -0,0 +1,81 @@
+/*
+ * 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/watchdog/__init__.h"
+#include "shared-bindings/watchdog/WatchDogMode.h"
+
+//| """Watchdog Timer
+//|
+//| The `watchdog` 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. Note that on some platforms the watchdog timer cannot be disabled
+//| once it has been enabled.
+//|
+//| The `WatchDogTimer` 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 microcontroller import watchdog as w
+//| from watchdog import WatchDogMode
+//| w.timeout=2.5 # Set a timeout of 2.5 seconds
+//| w.mode = WatchDogMode.RAISE
+//| w.feed()"""
+//|
+
+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,
+};
+
+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 const mp_rom_map_elem_t watchdog_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_watchdog) },
+ { MP_ROM_QSTR(MP_QSTR_WatchDogMode), MP_ROM_PTR(&watchdog_watchdogmode_type) },
+ { MP_ROM_QSTR(MP_QSTR_WatchDogTimeout), MP_ROM_PTR(&mp_type_WatchDogTimeout) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(watchdog_module_globals, watchdog_module_globals_table);
+
+const mp_obj_module_t watchdog_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&watchdog_module_globals,
+};
diff --git a/shared-bindings/watchdog/__init__.h b/shared-bindings/watchdog/__init__.h
new file mode 100644
index 000000000..b5a0ad71d
--- /dev/null
+++ b/shared-bindings/watchdog/__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_WATCHDOG___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H
+
+extern const mp_obj_module_t watchdog_module;
+extern mp_obj_exception_t mp_watchdog_timeout_exception;
+extern const mp_obj_type_t mp_type_WatchDogTimeout;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H