watchdog: rename module from wdt and move to microcontroller

This also places it under the `microcontroller` object.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-05-21 13:47:23 +08:00
parent c23f151b6b
commit 595f6387c2
18 changed files with 384 additions and 222 deletions

View File

@ -24,7 +24,7 @@ CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 1 CIRCUITPY_RTC = 1
CIRCUITPY_TOUCHIO = 0 CIRCUITPY_TOUCHIO = 0
CIRCUITPY_ULAB = 0 CIRCUITPY_ULAB = 0
CIRCUITPY_WDT = 1 CIRCUITPY_WATCHDOG = 1
# Enable micropython.native # Enable micropython.native
#CIRCUITPY_ENABLE_MPY_NATIVE = 1 #CIRCUITPY_ENABLE_MPY_NATIVE = 1

View File

@ -106,7 +106,6 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
}; };
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0 #if CIRCUITPY_INTERNAL_NVM_SIZE > 0
// The singleton nvm.ByteArray object. // The singleton nvm.ByteArray object.
const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = { const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
.base = { .base = {
@ -117,6 +116,16 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
}; };
#endif #endif
#if CIRCUITPY_WATCHDOG
// The singleton nvm.WatchDogTimer object.
const watchdog_obj_t common_hal_mcu_watchdog_obj = {
.base = {
.type = &watchdog_type,
},
.watchdogtimer = (mp_obj_t)&mp_const_none_obj,
};
#endif
STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = { STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_P0_00), MP_ROM_PTR(&pin_P0_00) }, { MP_ROM_QSTR(MP_QSTR_P0_00), MP_ROM_PTR(&pin_P0_00) },
{ MP_ROM_QSTR(MP_QSTR_P0_01), MP_ROM_PTR(&pin_P0_01) }, { MP_ROM_QSTR(MP_QSTR_P0_01), MP_ROM_PTR(&pin_P0_01) },

View File

@ -0,0 +1,197 @@
/*
* 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 "py/obj.h"
#include "py/runtime.h"
#include "common-hal/watchdog/WatchDogTimer.h"
#include "nrf_wdt.h"
#define WATCHDOG_RELOAD_COUNT 2
void common_hal_watchdog_init(uint32_t duration, bool pause_during_sleep) {
unsigned int channel;
nrf_wdt_behaviour_t behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP_HALT;
if (pause_during_sleep) {
behaviour = NRF_WDT_BEHAVIOUR_PAUSE_SLEEP_HALT;
}
nrf_wdt_behaviour_set(NRF_WDT, behaviour);
uint64_t ticks = (duration * 32768ULL) / 1000;
if (ticks > UINT32_MAX) {
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
}
nrf_wdt_reload_value_set(NRF_WDT, (uint32_t) ticks);
for (channel = 0; channel < WATCHDOG_RELOAD_COUNT; channel++) {
nrf_wdt_reload_request_enable(NRF_WDT, channel);
}
nrf_wdt_task_trigger(NRF_WDT, NRF_WDT_TASK_START);
}
void common_hal_watchdog_feed(void) {
unsigned int channel;
for (channel = 0; channel < WATCHDOG_RELOAD_COUNT; channel++) {
nrf_wdt_reload_request_set(NRF_WDT, (nrf_wdt_rr_register_t)(NRF_WDT_RR0 + channel));
}
}
void common_hal_watchdog_disable(void) {
// mp_raise_ValueError(translate("Watchdog timer cannot be disabled -- board will reset shortly"));
}
/*
* 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/watchdog/__init__.h"
#include "shared-bindings/watchdog/WatchDogTimer.h"
static watchdog_watchdogtimer_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 watchdog_watchdogtimer_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"));
}
watchdog_watchdogtimer_obj_t *self = m_new_obj(watchdog_watchdogtimer_obj_t);
self->base.type = &watchdog_watchdogtimer_type;
self->timeout = args[ARG_timeout_ms].u_int;
self->sleep = args[ARG_sleep].u_bool;
common_hal_watchdog_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 watchdog_watchdogtimer_feed(mp_obj_t self_in) {
(void)self_in;
common_hal_watchdog_feed();
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) {
(void)self_in;
common_hal_watchdog_disable();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_deinit_obj, watchdog_watchdogtimer_deinit);
//| timeout: int = ...
//| """The maximum number of milliseconds 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_int(self->timeout);
}
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_obj_get_timeout_obj, watchdog_watchdogtimer_obj_get_timeout);
const mp_obj_property_t watchdog_watchdogtimer_timeout_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&watchdog_watchdogtimer_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 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) },
};
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,
};

View File

@ -24,24 +24,16 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H #ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H #define MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
#include <stdint.h> #include "py/obj.h"
#include <stdbool.h> #include "shared-bindings/watchdog/WatchDogTimer.h"
extern void common_hal_wdt_init(uint32_t duration, bool pause_during_sleep); typedef struct _watchdog_watchdogtimer_obj_t {
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; mp_obj_base_t base;
uint32_t timeout; uint32_t timeout;
bool sleep; bool sleep;
} wdt_wdt_obj_t; } watchdog_watchdogtimer_obj_t;
extern const wdt_wdt_obj_t wdt_wdt_obj; #endif // MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WDT_WDT_H

View File

@ -0,0 +1,42 @@
/*
* 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 "py/obj.h"
#include "common-hal/watchdog/__init__.h"
#include "common-hal/watchdog/WatchDogTimer.h"
STATIC const mp_rom_map_elem_t watchdog_locals_dict_table[] = {
{MP_ROM_QSTR(MP_QSTR_WatchDogTimer), MP_ROM_PTR(&watchdog_watchdogtimer_type) },
};
STATIC MP_DEFINE_CONST_DICT(watchdog_locals_dict, watchdog_locals_dict_table);
const mp_obj_type_t watchdog_type = {
{ &mp_type_type },
.name = MP_QSTR_watchdog,
// .make_new = watchdog_watchdogtimer_make_new,
.locals_dict = (mp_obj_dict_t*)&watchdog_locals_dict,
};

View File

@ -0,0 +1,38 @@
/*
* 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_NRF_COMMON_HAL_WATCHDOG___INIT___H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG___INIT___H
#include "py/obj.h"
#include "shared-bindings/watchdog/__init__.h"
typedef struct _watchdog_obj_t {
mp_obj_base_t base;
mp_rom_obj_t *watchdogtimer;
} watchdog_obj_t;
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_WATCHDOG___INIT___H

View File

@ -1,40 +0,0 @@
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/rtc/__init__.h"
#include "nrf_wdt.h"
#define WDT_RELOAD_COUNT 2
void common_hal_wdt_init(uint32_t duration, bool pause_during_sleep) {
unsigned int channel;
nrf_wdt_behaviour_t behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP_HALT;
if (pause_during_sleep) {
behaviour = NRF_WDT_BEHAVIOUR_PAUSE_SLEEP_HALT;
}
nrf_wdt_behaviour_set(NRF_WDT, behaviour);
uint64_t ticks = (duration * 32768ULL) / 1000;
if (ticks > UINT32_MAX) {
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
}
nrf_wdt_reload_value_set(NRF_WDT, (uint32_t) ticks);
for (channel = 0; channel < WDT_RELOAD_COUNT; channel++) {
nrf_wdt_reload_request_enable(NRF_WDT, channel);
}
nrf_wdt_task_trigger(NRF_WDT, NRF_WDT_TASK_START);
}
void common_hal_wdt_feed(void) {
unsigned int channel;
for (channel = 0; channel < WDT_RELOAD_COUNT; channel++) {
nrf_wdt_reload_request_set(NRF_WDT, (nrf_wdt_rr_register_t)(NRF_WDT_RR0 + channel));
}
}
void common_hal_wdt_disable(void) {
// mp_raise_ValueError(translate("Watchdog timer cannot be disabled -- board will reset shortly"));
}

View File

@ -37,7 +37,7 @@ CIRCUITPY_RGBMATRIX ?= 1
CIRCUITPY_FRAMEBUFFERIO ?= 1 CIRCUITPY_FRAMEBUFFERIO ?= 1
CIRCUITPY_COUNTIO = 0 CIRCUITPY_COUNTIO = 0
CIRCUITPY_WDT ?= 1 CIRCUITPY_WATCHDOG ?= 1
# nRF52840-specific # nRF52840-specific

View File

@ -241,8 +241,8 @@ endif
ifeq ($(CIRCUITPY_USTACK),1) ifeq ($(CIRCUITPY_USTACK),1)
SRC_PATTERNS += ustack/% SRC_PATTERNS += ustack/%
endif endif
ifeq ($(CIRCUITPY_WDT),1) ifeq ($(CIRCUITPY_WATCHDOG),1)
SRC_PATTERNS += wdt/% SRC_PATTERNS += watchdog/%
endif endif
ifeq ($(CIRCUITPY_PEW),1) ifeq ($(CIRCUITPY_PEW),1)
SRC_PATTERNS += _pew/% SRC_PATTERNS += _pew/%
@ -305,8 +305,8 @@ SRC_COMMON_HAL_ALL = \
rtc/__init__.c \ rtc/__init__.c \
supervisor/Runtime.c \ supervisor/Runtime.c \
supervisor/__init__.c \ supervisor/__init__.c \
wdt/__init__.c \ watchdog/__init__.c \
wdt/WDT.c \ watchdog/WatchDogTimer.c \
SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL)) SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL))

View File

@ -630,11 +630,9 @@ extern const struct _mp_obj_module_t ustack_module;
#define RE_MODULE #define RE_MODULE
#endif #endif
#if CIRCUITPY_WDT // This is not a top-level module; it's microcontroller.watchdog.
extern const struct _mp_obj_module_t wdt_module; #if CIRCUITPY_WATCHDOG
#define WDT_MODULE { MP_ROM_QSTR(MP_QSTR_wdt), MP_ROM_PTR(&wdt_module) }, extern const struct _mp_obj_module_t watchdog_module;
#else
#define WDT_MODULE
#endif #endif
// Define certain native modules with weak links so they can be replaced with Python // Define certain native modules with weak links so they can be replaced with Python
@ -707,7 +705,6 @@ extern const struct _mp_obj_module_t wdt_module;
USB_HID_MODULE \ USB_HID_MODULE \
USB_MIDI_MODULE \ USB_MIDI_MODULE \
USTACK_MODULE \ USTACK_MODULE \
WDT_MODULE \
// If weak links are enabled, just include strong links in the main list of modules, // If weak links are enabled, just include strong links in the main list of modules,
// and also include the underscore alternate names. // and also include the underscore alternate names.

View File

@ -229,8 +229,8 @@ CIRCUITPY_ULAB ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_ULAB=$(CIRCUITPY_ULAB) CFLAGS += -DCIRCUITPY_ULAB=$(CIRCUITPY_ULAB)
# watchdog hardware support # watchdog hardware support
CIRCUITPY_WDT ?= 0 CIRCUITPY_WATCHDOG ?= 0
CFLAGS += -DCIRCUITPY_WDT=$(CIRCUITPY_WDT) CFLAGS += -DCIRCUITPY_WATCHDOG=$(CIRCUITPY_WATCHDOG)
# Enabled micropython.native decorator (experimental) # Enabled micropython.native decorator (experimental)
CIRCUITPY_ENABLE_MPY_NATIVE ?= 0 CIRCUITPY_ENABLE_MPY_NATIVE ?= 0

View File

@ -167,6 +167,11 @@ STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
#else #else
{ MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&mp_const_none_obj) }, { MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&mp_const_none_obj) },
#endif #endif
#if CIRCUITPY_WATCHDOG
{ MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&common_hal_mcu_watchdog_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_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_type) },
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&mcu_pin_module) }, { MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&mcu_pin_module) },

View File

@ -49,10 +49,13 @@ extern const mcu_processor_obj_t common_hal_mcu_processor_obj;
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0 #if CIRCUITPY_INTERNAL_NVM_SIZE > 0
#include "common-hal/nvm/ByteArray.h" #include "common-hal/nvm/ByteArray.h"
extern const nvm_bytearray_obj_t common_hal_mcu_nvm_obj; extern const nvm_bytearray_obj_t common_hal_mcu_nvm_obj;
#endif
#if CIRCUITPY_WATCHDOG
#include "common-hal/watchdog/__init__.h"
extern const watchdog_obj_t common_hal_mcu_watchdog_obj;
#endif #endif
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H

View File

@ -0,0 +1,45 @@
/*
* 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>
// 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);
// typedef struct _wdt_wdt_obj_t {
// mp_obj_base_t base;
// uint32_t timeout;
// bool sleep;
// } wdt_wdt_obj_t;
extern const mp_obj_type_t watchdog_watchdogtimer_type;
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGTIMER_H

View File

@ -27,36 +27,40 @@
#include <string.h> #include <string.h>
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/wdt/__init__.h" #include "shared-bindings/watchdog/__init__.h"
#include "shared-bindings/wdt/WDT.h" // #include "shared-bindings/wdt/WDT.h"
//| """Watchdog Timer //| """Watchdog Timer
//| //|
//| The `wdt` module provides support for a Watchdog Timer. This timer will reset the device //| 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 //| 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`. //| has not crashed or locked up. You can enable the watchdog timer using :class:`wdt.WDT`.
//| Note that the watchdog timer cannot be disabled once it has been enabled. //| Note that on some platforms 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 //| 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 //| up into a non recoverable state. Once started it cannot be stopped or
//| reconfigured in any way. After enabling, the application must "feed" the //| reconfigured in any way. After enabling, the application must "feed" the
//| watchdog periodically to prevent it from expiring and resetting the system. //| watchdog periodically to prevent it from expiring and resetting the system.
//| //|
//| Note that this module can't be imported and used directly. The sole
//| instance of :class:`WatchDogTimer` is available at
//| :attr:`microcontroller.watchdog`."""
//|
//| Example usage:: //| Example usage::
//| //|
//| from machine import WDT //| from microcontroller.watchdog import WatchDogTimer
//| wdt = WDT(timeout=2000) # enable it with a timeout of 2s //| wdt = WatchDogTimer(timeout=2.5) # enable it with a timeout of 2.5 seconds
//| wdt.feed()""" //| wdt.feed()"""
//| //|
STATIC const mp_rom_map_elem_t wdt_module_globals_table[] = { // 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___name__), MP_ROM_QSTR(MP_QSTR_wdt) },
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&wdt_wdt_type) }, // { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&wdt_wdt_type) },
}; // };
STATIC MP_DEFINE_CONST_DICT(wdt_module_globals, wdt_module_globals_table); // STATIC MP_DEFINE_CONST_DICT(wdt_module_globals, wdt_module_globals_table);
const mp_obj_module_t wdt_module = { // const mp_obj_module_t wdt_module = {
.base = { &mp_type_module }, // .base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&wdt_module_globals, // .globals = (mp_obj_dict_t*)&wdt_module_globals,
}; // };

View File

@ -24,11 +24,9 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H #define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H
#include <stdint.h> extern const mp_obj_type_t watchdog_type;
#include <stdbool.h>
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WDT___INIT___H

View File

@ -1,128 +0,0 @@
/*
* 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,
};