From 60929ec7e2c19b896487491192361e44f7cee0d9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 10 Oct 2023 18:19:03 +1100 Subject: [PATCH] extmod/machine_wdt: Factor ports' WDT Python bindings to common code. There are currently 7 ports that implement machine.WDT and a lot of code is duplicated across these implementations. This commit factors the common parts of all these implementations to a single location in extmod/machine_wdt.c. This common code provides the top-level Python bindings (class and method wrappers), and then each port implements the back end specific to that port. With this refactor the ports remain functionally the same except for: - The esp8266 WDT constructor now takes keyword arguments, and accepts the "timeout" argument but raises an exception if it's not the default value (this port doesn't support changing the timeout). - The mimxrt and samd ports now interpret the argument to WDT.timeout_ms() as signed and if it's negative truncate it to the minimum timeout (rather than it being unsigned and a negative value truncating to the maximum timeout). Signed-off-by: Damien George --- extmod/extmod.cmake | 1 + extmod/extmod.mk | 1 + extmod/machine_wdt.c | 95 +++++++++++++++++++ ports/stm32/wdt.h => extmod/modmachine.h | 22 ++++- ports/cc3200/application.mk | 1 - ports/cc3200/mods/{pybwdt.c => machine_wdt.c} | 66 ++++--------- ports/cc3200/mods/modmachine.c | 4 +- ports/cc3200/mods/pybwdt.h | 4 +- ports/cc3200/mpconfigport.h | 2 + ports/esp32/esp32_common.cmake | 1 - ports/esp32/machine_wdt.c | 44 ++------- ports/esp32/modmachine.c | 3 + ports/esp32/modmachine.h | 1 - ports/esp32/mpconfigport.h | 2 + ports/esp8266/Makefile | 1 - ports/esp8266/machine_wdt.c | 38 ++------ ports/esp8266/modmachine.c | 7 +- ports/esp8266/mpconfigport.h | 2 + ports/mimxrt/Makefile | 1 - ports/mimxrt/machine_wdt.c | 67 ++++--------- ports/mimxrt/modmachine.c | 3 + ports/mimxrt/modmachine.h | 1 - ports/mimxrt/mpconfigport.h | 3 + ports/renesas-ra/modmachine.h | 1 - ports/rp2/CMakeLists.txt | 1 - ports/rp2/machine_wdt.c | 40 ++------ ports/rp2/modmachine.c | 3 + ports/rp2/modmachine.h | 1 - ports/rp2/mpconfigport.h | 2 + ports/samd/Makefile | 1 - ports/samd/machine_wdt.c | 52 ++-------- ports/samd/modmachine.c | 3 + ports/samd/modmachine.h | 1 - ports/samd/mpconfigport.h | 3 + ports/stm32/Makefile | 1 - ports/stm32/{wdt.c => machine_wdt.c} | 52 +++------- ports/stm32/modmachine.c | 6 +- ports/stm32/mpconfigport.h | 2 + 38 files changed, 235 insertions(+), 304 deletions(-) create mode 100644 extmod/machine_wdt.c rename ports/stm32/wdt.h => extmod/modmachine.h (63%) rename ports/cc3200/mods/{pybwdt.c => machine_wdt.c} (68%) rename ports/stm32/{wdt.c => machine_wdt.c} (60%) diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index c8756a682e..063df70f5f 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -14,6 +14,7 @@ set(MICROPY_SOURCE_EXTMOD ${MICROPY_EXTMOD_DIR}/machine_pwm.c ${MICROPY_EXTMOD_DIR}/machine_signal.c ${MICROPY_EXTMOD_DIR}/machine_spi.c + ${MICROPY_EXTMOD_DIR}/machine_wdt.c ${MICROPY_EXTMOD_DIR}/modbluetooth.c ${MICROPY_EXTMOD_DIR}/modframebuf.c ${MICROPY_EXTMOD_DIR}/modlwip.c diff --git a/extmod/extmod.mk b/extmod/extmod.mk index f776a7f694..a6c7826867 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -11,6 +11,7 @@ SRC_EXTMOD_C += \ extmod/machine_signal.c \ extmod/machine_spi.c \ extmod/machine_timer.c \ + extmod/machine_wdt.c \ extmod/modasyncio.c \ extmod/modbinascii.c \ extmod/modbluetooth.c \ diff --git a/extmod/machine_wdt.c b/extmod/machine_wdt.c new file mode 100644 index 0000000000..a49484ed80 --- /dev/null +++ b/extmod/machine_wdt.c @@ -0,0 +1,95 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020-2023 Damien P. George + * + * 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/runtime.h" + +#if MICROPY_PY_MACHINE_WDT + +#include "extmod/modmachine.h" + +// The port must provide implementations of these low-level WDT functions. +STATIC machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms); +STATIC void mp_machine_wdt_feed(machine_wdt_obj_t *self); +#if MICROPY_PY_MACHINE_WDT_TIMEOUT_MS +STATIC void mp_machine_wdt_timeout_ms_set(machine_wdt_obj_t *self_in, mp_int_t timeout_ms); +#endif + +// The port provides implementations of the above in this file. +#include MICROPY_PY_MACHINE_WDT_INCLUDEFILE + +STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_timeout }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, + }; + + // Parse the arguments. + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // Create WDT instance. + machine_wdt_obj_t *self = mp_machine_wdt_make_new_instance(args[ARG_id].u_int, args[ARG_timeout].u_int); + + return MP_OBJ_FROM_PTR(self); +} + +// WDT.feed() +STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { + machine_wdt_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_machine_wdt_feed(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed); + +#if MICROPY_PY_MACHINE_WDT_TIMEOUT_MS +// WDT.timeout_ms(timeout) +STATIC mp_obj_t machine_wdt_timeout_ms(mp_obj_t self_in, mp_obj_t timeout_in) { + machine_wdt_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_int_t timeout_ms = mp_obj_get_int(timeout_in); + mp_machine_wdt_timeout_ms_set(self, timeout_ms); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_wdt_timeout_ms_obj, machine_wdt_timeout_ms); +#endif + +STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) }, + #if MICROPY_PY_MACHINE_WDT_TIMEOUT_MS + { MP_ROM_QSTR(MP_QSTR_timeout_ms), MP_ROM_PTR(&machine_wdt_timeout_ms_obj) }, + #endif +}; +STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + machine_wdt_type, + MP_QSTR_WDT, + MP_TYPE_FLAG_NONE, + make_new, machine_wdt_make_new, + locals_dict, &machine_wdt_locals_dict + ); + +#endif // MICROPY_PY_MACHINE_WDT diff --git a/ports/stm32/wdt.h b/extmod/modmachine.h similarity index 63% rename from ports/stm32/wdt.h rename to extmod/modmachine.h index d60bb7e9e6..794c62cde2 100644 --- a/ports/stm32/wdt.h +++ b/extmod/modmachine.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Damien P. George + * Copyright (c) 2023 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,9 +23,21 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32_WDT_H -#define MICROPY_INCLUDED_STM32_WDT_H -extern const mp_obj_type_t pyb_wdt_type; +#ifndef MICROPY_INCLUDED_EXTMOD_MODMACHINE_H +#define MICROPY_INCLUDED_EXTMOD_MODMACHINE_H -#endif // MICROPY_INCLUDED_STM32_WDT_H +#include "py/obj.h" + +// A port must provide these types, but they are otherwise opaque. +typedef struct _machine_wdt_obj_t machine_wdt_obj_t; + +// These classes correspond to machine.Type entries in the machine module. +// Their Python bindings are implemented in extmod, and their implementation +// is provided by a port. +extern const mp_obj_type_t machine_i2c_type; +extern const mp_obj_type_t machine_spi_type; +extern const mp_obj_type_t machine_timer_type; +extern const mp_obj_type_t machine_wdt_type; + +#endif // MICROPY_INCLUDED_EXTMOD_MODMACHINE_H diff --git a/ports/cc3200/application.mk b/ports/cc3200/application.mk index f4487d69d4..deee201eef 100644 --- a/ports/cc3200/application.mk +++ b/ports/cc3200/application.mk @@ -93,7 +93,6 @@ APP_MODS_SRC_C = $(addprefix mods/,\ pybspi.c \ pybtimer.c \ pybuart.c \ - pybwdt.c \ ) APP_CC3100_SRC_C = $(addprefix drivers/cc3100/src/,\ diff --git a/ports/cc3200/mods/pybwdt.c b/ports/cc3200/mods/machine_wdt.c similarity index 68% rename from ports/cc3200/mods/pybwdt.c rename to ports/cc3200/mods/machine_wdt.c index 589f53cf12..b20ef7eb93 100644 --- a/ports/cc3200/mods/pybwdt.c +++ b/ports/cc3200/mods/machine_wdt.c @@ -24,11 +24,9 @@ * THE SOFTWARE. */ -#include +// This file is never compiled standalone, it's included directly from +// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE. -#include "py/mpconfig.h" -#include "py/obj.h" -#include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" #include "inc/hw_types.h" @@ -40,8 +38,6 @@ #include "prcm.h" #include "utils.h" #include "pybwdt.h" -#include "mperror.h" - /****************************************************************************** DECLARE CONSTANTS @@ -52,18 +48,18 @@ /****************************************************************************** DECLARE TYPES ******************************************************************************/ -typedef struct { +typedef struct _machine_wdt_obj_t { mp_obj_base_t base; bool servers; bool servers_sleeping; bool simplelink; bool running; -} pyb_wdt_obj_t; +} machine_wdt_obj_t; /****************************************************************************** DECLARE PRIVATE DATA ******************************************************************************/ -STATIC pyb_wdt_obj_t pyb_wdt_obj = {.servers = false, .servers_sleeping = false, .simplelink = false, .running = false}; +STATIC machine_wdt_obj_t machine_wdt_obj = {.servers = false, .servers_sleeping = false, .simplelink = false, .running = false}; /****************************************************************************** DEFINE PUBLIC FUNCTIONS @@ -74,39 +70,28 @@ void pybwdt_init0 (void) { } void pybwdt_srv_alive (void) { - pyb_wdt_obj.servers = true; + machine_wdt_obj.servers = true; } void pybwdt_srv_sleeping (bool state) { - pyb_wdt_obj.servers_sleeping = state; + machine_wdt_obj.servers_sleeping = state; } void pybwdt_sl_alive (void) { - pyb_wdt_obj.simplelink = true; + machine_wdt_obj.simplelink = true; } /******************************************************************************/ // MicroPython bindings -STATIC const mp_arg_t pyb_wdt_init_args[] = { - { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, // 5 s -}; -STATIC mp_obj_t pyb_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - // check the arguments - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args); - mp_arg_val_t args[MP_ARRAY_SIZE(pyb_wdt_init_args)]; - mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_wdt_init_args, args); - - if (args[0].u_obj != mp_const_none && mp_obj_get_int(args[0].u_obj) > 0) { +STATIC machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { + if (id != 0) { mp_raise_OSError(MP_ENODEV); } - uint timeout_ms = args[1].u_int; if (timeout_ms < PYBWDT_MIN_TIMEOUT_MS) { mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s) value")); } - if (pyb_wdt_obj.running) { + if (machine_wdt_obj.running) { mp_raise_OSError(MP_EPERM); } @@ -116,10 +101,10 @@ STATIC mp_obj_t pyb_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_ // Unlock to be able to configure the registers MAP_WatchdogUnlock(WDT_BASE); -#ifdef DEBUG + #ifdef DEBUG // make the WDT stall when the debugger stops on a breakpoint MAP_WatchdogStallEnable (WDT_BASE); -#endif + #endif // set the watchdog timer reload value // the WDT trigger a system reset after the second timeout @@ -128,33 +113,16 @@ STATIC mp_obj_t pyb_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_ // start the timer. Once it's started, it cannot be disabled. MAP_WatchdogEnable(WDT_BASE); - pyb_wdt_obj.base.type = &pyb_wdt_type; - pyb_wdt_obj.running = true; + machine_wdt_obj.base.type = &machine_wdt_type; + machine_wdt_obj.running = true; - return (mp_obj_t)&pyb_wdt_obj; + return &machine_wdt_obj; } -STATIC mp_obj_t pyb_wdt_feed(mp_obj_t self_in) { - pyb_wdt_obj_t *self = self_in; +STATIC void mp_machine_wdt_feed(machine_wdt_obj_t *self) { if ((self->servers || self->servers_sleeping) && self->simplelink && self->running) { self->servers = false; self->simplelink = false; MAP_WatchdogIntClear(WDT_BASE); } - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_feed_obj, pyb_wdt_feed); - -STATIC const mp_rom_map_elem_t pybwdt_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&pyb_wdt_feed_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(pybwdt_locals_dict, pybwdt_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - pyb_wdt_type, - MP_QSTR_WDT, - MP_TYPE_FLAG_NONE, - make_new, pyb_wdt_make_new, - locals_dict, &pybwdt_locals_dict - ); - diff --git a/ports/cc3200/mods/modmachine.c b/ports/cc3200/mods/modmachine.c index bdf963c31a..107efaef16 100644 --- a/ports/cc3200/mods/modmachine.c +++ b/ports/cc3200/mods/modmachine.c @@ -30,6 +30,7 @@ #include "py/runtime.h" #include "py/mphal.h" +#include "extmod/modmachine.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "inc/hw_ints.h" @@ -51,7 +52,6 @@ #include "pybadc.h" #include "pybi2c.h" #include "pybsd.h" -#include "pybwdt.h" #include "pybsleep.h" #include "pybspi.h" #include "pybtimer.h" @@ -189,7 +189,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) }, - { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) }, + { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, { MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sd_type) }, // class constants diff --git a/ports/cc3200/mods/pybwdt.h b/ports/cc3200/mods/pybwdt.h index 275c49435c..e106ae30ea 100644 --- a/ports/cc3200/mods/pybwdt.h +++ b/ports/cc3200/mods/pybwdt.h @@ -26,9 +26,7 @@ #ifndef MICROPY_INCLUDED_CC3200_MODS_PYBWDT_H #define MICROPY_INCLUDED_CC3200_MODS_PYBWDT_H -#include "py/obj.h" - -extern const mp_obj_type_t pyb_wdt_type; +#include void pybwdt_init0 (void); void pybwdt_srv_alive (void); diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index b6b412f178..6b2dc7ca09 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -120,6 +120,8 @@ #define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1) #define MICROPY_PY_TIME_TIME_TIME_NS (1) #define MICROPY_PY_TIME_INCLUDEFILE "ports/cc3200/mods/modtime.c" +#define MICROPY_PY_MACHINE_WDT (1) +#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/cc3200/mods/machine_wdt.c" #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0) diff --git a/ports/esp32/esp32_common.cmake b/ports/esp32/esp32_common.cmake index f5219048f3..e1dd546ea3 100644 --- a/ports/esp32/esp32_common.cmake +++ b/ports/esp32/esp32_common.cmake @@ -86,7 +86,6 @@ list(APPEND MICROPY_SOURCE_PORT esp32_ulp.c modesp32.c machine_hw_spi.c - machine_wdt.c mpthreadport.c machine_rtc.c machine_sdcard.c diff --git a/ports/esp32/machine_wdt.c b/ports/esp32/machine_wdt.c index bf924c35e0..cee761f265 100644 --- a/ports/esp32/machine_wdt.c +++ b/ports/esp32/machine_wdt.c @@ -25,16 +25,11 @@ * THE SOFTWARE. */ -#include - -#include "py/nlr.h" -#include "py/obj.h" -#include "py/runtime.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE. #include "esp_task_wdt.h" -const mp_obj_type_t machine_wdt_type; - typedef struct _machine_wdt_obj_t { mp_obj_base_t base; esp_task_wdt_user_handle_t twdt_user_handle; @@ -44,25 +39,17 @@ STATIC machine_wdt_obj_t wdt_default = { {&machine_wdt_type}, 0 }; -STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_id, ARG_timeout }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} } - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - if (args[ARG_id].u_int != 0) { +STATIC machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { + if (id != 0) { mp_raise_ValueError(NULL); } - if (args[ARG_timeout].u_int <= 0) { + if (timeout_ms <= 0) { mp_raise_ValueError(MP_ERROR_TEXT("WDT timeout too short")); } esp_task_wdt_config_t config = { - .timeout_ms = args[ARG_timeout].u_int, + .timeout_ms = timeout_ms, .idle_core_mask = 0, .trigger_panic = true, }; @@ -81,25 +68,10 @@ STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args return &wdt_default; } -STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { - (void)self_in; +STATIC void mp_machine_wdt_feed(machine_wdt_obj_t *self) { + (void)self; mp_int_t rs_code = esp_task_wdt_reset_user(wdt_default.twdt_user_handle); if (rs_code != ESP_OK) { mp_raise_OSError(rs_code); } - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed); - -STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_wdt_type, - MP_QSTR_WDT, - MP_TYPE_FLAG_NONE, - make_new, machine_wdt_make_new, - locals_dict, &machine_wdt_locals_dict - ); diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 31fa589aed..f8567973b0 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -47,6 +47,7 @@ #include "extmod/machine_pwm.h" #include "extmod/machine_i2c.h" #include "extmod/machine_spi.h" +#include "extmod/modmachine.h" #include "modmachine.h" #include "machine_rtc.h" @@ -297,7 +298,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, + #if MICROPY_PY_MACHINE_WDT { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, + #endif #if MICROPY_HW_ENABLE_SDCARD { MP_ROM_QSTR(MP_QSTR_SDCard), MP_ROM_PTR(&machine_sdcard_type) }, #endif diff --git a/ports/esp32/modmachine.h b/ports/esp32/modmachine.h index 24417f6107..24f27101a8 100644 --- a/ports/esp32/modmachine.h +++ b/ports/esp32/modmachine.h @@ -10,7 +10,6 @@ typedef enum { } wake_type_t; extern const mp_obj_type_t machine_timer_type; -extern const mp_obj_type_t machine_wdt_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_touchpad_type; extern const mp_obj_type_t machine_adc_type; diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 04a745262d..5d39323c58 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -115,6 +115,8 @@ #ifndef MICROPY_PY_MACHINE_I2S #define MICROPY_PY_MACHINE_I2S (1) #endif +#define MICROPY_PY_MACHINE_WDT (1) +#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/esp32/machine_wdt.c" #define MICROPY_PY_NETWORK (1) #ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT #if CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 75b4076338..5982987d7b 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -116,7 +116,6 @@ SRC_C = \ machine_rtc.c \ machine_adc.c \ machine_uart.c \ - machine_wdt.c \ machine_hspi.c \ modesp.c \ network_wlan.c \ diff --git a/ports/esp8266/machine_wdt.c b/ports/esp8266/machine_wdt.c index 39a5d51119..074ee56598 100644 --- a/ports/esp8266/machine_wdt.c +++ b/ports/esp8266/machine_wdt.c @@ -24,27 +24,22 @@ * THE SOFTWARE. */ -#include +// This file is never compiled standalone, it's included directly from +// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE. -#include "py/runtime.h" #include "user_interface.h" -#include "etshal.h" #include "ets_alt_task.h" -const mp_obj_type_t esp_wdt_type; - typedef struct _machine_wdt_obj_t { mp_obj_base_t base; } machine_wdt_obj_t; -STATIC machine_wdt_obj_t wdt_default = {{&esp_wdt_type}}; +STATIC machine_wdt_obj_t wdt_default = {{&machine_wdt_type}}; -STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 0, 1, false); - - mp_int_t id = 0; - if (n_args > 0) { - id = mp_obj_get_int(args[0]); +STATIC machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { + // The timeout on ESP8266 is fixed, so raise an exception if the argument is not the default. + if (timeout_ms != 5000) { + mp_raise_ValueError(NULL); } switch (id) { @@ -57,22 +52,7 @@ STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args } } -STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { - (void)self_in; +STATIC void mp_machine_wdt_feed(machine_wdt_obj_t *self) { + (void)self; system_soft_wdt_feed(); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed); - -STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - esp_wdt_type, - MP_QSTR_WDT, - MP_TYPE_FLAG_NONE, - make_new, machine_wdt_make_new, - locals_dict, &machine_wdt_locals_dict - ); diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index 64346b4deb..eee5f3c683 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -43,6 +43,7 @@ #include "extmod/machine_pwm.h" #include "extmod/machine_i2c.h" #include "extmod/machine_spi.h" +#include "extmod/modmachine.h" #include "modmachine.h" #include "xtirq.h" @@ -58,8 +59,6 @@ // #define MACHINE_WAKE_SLEEP (0x02) #define MACHINE_WAKE_DEEPSLEEP (0x04) -extern const mp_obj_type_t esp_wdt_type; - STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { // get @@ -423,7 +422,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&esp_timer_type) }, - { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&esp_wdt_type) }, + #if MICROPY_PY_MACHINE_WDT + { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pyb_pin_type) }, { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 18b2e47e29..2a51f8bcbb 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -71,6 +71,8 @@ #define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) +#define MICROPY_PY_MACHINE_WDT (1) +#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/esp8266/machine_wdt.c" #define MICROPY_PY_NETWORK (1) #ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-esp8266" diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 3521f92288..d29b38e040 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -205,7 +205,6 @@ SRC_C += \ machine_sdcard.c \ machine_spi.c \ machine_uart.c \ - machine_wdt.c \ main.c \ mbedtls/mbedtls_port.c \ mimxrt_flash.c \ diff --git a/ports/mimxrt/machine_wdt.c b/ports/mimxrt/machine_wdt.c index f5f14f9ef7..176e114f72 100644 --- a/ports/mimxrt/machine_wdt.c +++ b/ports/mimxrt/machine_wdt.c @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#include "py/runtime.h" -#include "modmachine.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE. #include "fsl_wdog.h" @@ -38,19 +38,8 @@ typedef struct _machine_wdt_obj_t { STATIC const machine_wdt_obj_t machine_wdt = {{&machine_wdt_type}}; -STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_id, ARG_timeout }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, - }; - - // Parse the arguments. - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - +STATIC machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { // Verify the WDT id. - mp_int_t id = args[ARG_id].u_int; if (id != 0) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), id); } @@ -58,51 +47,33 @@ STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, s // Start the watchdog (timeout is in milliseconds). wdog_config_t config; WDOG_GetDefaultConfig(&config); - uint32_t timeout = args[ARG_timeout].u_int; // confine to the valid range - if (timeout < MIN_TIMEOUT) { - timeout = MIN_TIMEOUT; - } else if (timeout > MAX_TIMEOUT) { - timeout = MAX_TIMEOUT; + if (timeout_ms < MIN_TIMEOUT) { + timeout_ms = MIN_TIMEOUT; + } else if (timeout_ms > MAX_TIMEOUT) { + timeout_ms = MAX_TIMEOUT; } - config.timeoutValue = (timeout / 500) - 1; + config.timeoutValue = (timeout_ms / 500) - 1; WDOG_Init(WDOG1, &config); - return MP_OBJ_FROM_PTR(&machine_wdt); + return (machine_wdt_obj_t *)&machine_wdt; } -STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { - (void)self_in; +STATIC void mp_machine_wdt_feed(machine_wdt_obj_t *self) { + (void)self; WDOG_Refresh(WDOG1); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed); -STATIC mp_obj_t machine_wdt_timeout_ms(mp_obj_t self_in, mp_obj_t timout_in) { - uint32_t timeout = mp_obj_get_int(timout_in); +STATIC void mp_machine_wdt_timeout_ms_set(machine_wdt_obj_t *self, mp_int_t timeout_ms) { + (void)self; + // confine to the valid range - if (timeout < MIN_TIMEOUT) { - timeout = MIN_TIMEOUT; - } else if (timeout > MAX_TIMEOUT) { - timeout = MAX_TIMEOUT; + if (timeout_ms < MIN_TIMEOUT) { + timeout_ms = MIN_TIMEOUT; + } else if (timeout_ms > MAX_TIMEOUT) { + timeout_ms = MAX_TIMEOUT; } - timeout = (timeout / 500) - 1; + uint32_t timeout = (timeout_ms / 500) - 1; WDOG_SetTimeoutValue(WDOG1, (uint16_t)timeout); WDOG_Refresh(WDOG1); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_wdt_timeout_ms_obj, machine_wdt_timeout_ms); - -STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) }, - { MP_ROM_QSTR(MP_QSTR_timeout_ms), MP_ROM_PTR(&machine_wdt_timeout_ms_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_wdt_type, - MP_QSTR_WDT, - MP_TYPE_FLAG_NONE, - make_new, machine_wdt_make_new, - locals_dict, &machine_wdt_locals_dict - ); diff --git a/ports/mimxrt/modmachine.c b/ports/mimxrt/modmachine.c index 16eae22029..c2f3559cf8 100644 --- a/ports/mimxrt/modmachine.c +++ b/ports/mimxrt/modmachine.c @@ -33,6 +33,7 @@ #include "extmod/machine_pulse.h" #include "extmod/machine_signal.h" #include "extmod/machine_spi.h" +#include "extmod/modmachine.h" #include "shared/runtime/pyexec.h" #include "led.h" #include "pin.h" @@ -204,7 +205,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, + #if MICROPY_PY_MACHINE_WDT { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) }, diff --git a/ports/mimxrt/modmachine.h b/ports/mimxrt/modmachine.h index 478b1c7736..cf40505ea5 100644 --- a/ports/mimxrt/modmachine.h +++ b/ports/mimxrt/modmachine.h @@ -38,7 +38,6 @@ extern const mp_obj_type_t machine_sdcard_type; extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_timer_type; extern const mp_obj_type_t machine_uart_type; -extern const mp_obj_type_t machine_wdt_type; void machine_adc_init(void); void machine_pin_irq_deinit(void); diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index feb6cb2660..1eb3b17112 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -94,6 +94,9 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) #define MICROPY_PY_MACHINE_TIMER (1) +#define MICROPY_PY_MACHINE_WDT (1) +#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/mimxrt/machine_wdt.c" +#define MICROPY_PY_MACHINE_WDT_TIMEOUT_MS (1) #define MICROPY_SOFT_TIMER_TICKS_MS systick_ms #define MICROPY_PY_ONEWIRE (1) diff --git a/ports/renesas-ra/modmachine.h b/ports/renesas-ra/modmachine.h index 5dbda6bda8..ec494b798c 100644 --- a/ports/renesas-ra/modmachine.h +++ b/ports/renesas-ra/modmachine.h @@ -30,7 +30,6 @@ #include "py/obj.h" extern const mp_obj_type_t machine_timer_type; -extern const mp_obj_type_t machine_wdt_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_touchpad_type; extern const mp_obj_type_t machine_adc_type; diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index fae4cda756..b8cf4d7bec 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -126,7 +126,6 @@ set(MICROPY_SOURCE_PORT machine_spi.c machine_timer.c machine_uart.c - machine_wdt.c main.c modmachine.c modrp2.c diff --git a/ports/rp2/machine_wdt.c b/ports/rp2/machine_wdt.c index 6574a6180f..58c372176c 100644 --- a/ports/rp2/machine_wdt.c +++ b/ports/rp2/machine_wdt.c @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#include "py/runtime.h" -#include "modmachine.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE. #include "hardware/watchdog.h" @@ -38,49 +38,23 @@ typedef struct _machine_wdt_obj_t { STATIC const machine_wdt_obj_t machine_wdt = {{&machine_wdt_type}}; -STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_id, ARG_timeout }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, - }; - - // Parse the arguments. - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - +STATIC machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { // Verify the WDT id. - mp_int_t id = args[ARG_id].u_int; if (id != 0) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), id); } // Start the watchdog (timeout is in milliseconds). - uint32_t timeout = args[ARG_timeout].u_int; + uint32_t timeout = timeout_ms; if (timeout > WDT_TIMEOUT_MAX) { mp_raise_ValueError(MP_ERROR_TEXT("timeout exceeds " MP_STRINGIFY(WDT_TIMEOUT_MAX))); } watchdog_enable(timeout, false); - return MP_OBJ_FROM_PTR(&machine_wdt); + return (machine_wdt_obj_t *)&machine_wdt; } -STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { - (void)self_in; +STATIC void mp_machine_wdt_feed(machine_wdt_obj_t *self) { + (void)self; watchdog_update(); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed); - -STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_wdt_type, - MP_QSTR_WDT, - MP_TYPE_FLAG_NONE, - make_new, machine_wdt_make_new, - locals_dict, &machine_wdt_locals_dict - ); diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index 35c938b54b..eedeca1e3b 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -35,6 +35,7 @@ #include "extmod/machine_pwm.h" #include "extmod/machine_signal.h" #include "extmod/machine_spi.h" +#include "extmod/modmachine.h" #include "modmachine.h" #include "uart.h" @@ -266,7 +267,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, + #if MICROPY_PY_MACHINE_WDT { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(RP2_RESET_PWRON) }, { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(RP2_RESET_WDT) }, diff --git a/ports/rp2/modmachine.h b/ports/rp2/modmachine.h index 45ccd49e8c..559fb34814 100644 --- a/ports/rp2/modmachine.h +++ b/ports/rp2/modmachine.h @@ -11,7 +11,6 @@ extern const mp_obj_type_t machine_rtc_type; extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_timer_type; extern const mp_obj_type_t machine_uart_type; -extern const mp_obj_type_t machine_wdt_type; void machine_pin_init(void); void machine_pin_deinit(void); diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 056e2df4fe..bf43b5ce7f 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -122,6 +122,8 @@ #define MICROPY_PY_MACHINE_SPI_MSB (SPI_MSB_FIRST) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_LSB_FIRST) #define MICROPY_PY_MACHINE_SOFTSPI (1) +#define MICROPY_PY_MACHINE_WDT (1) +#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/rp2/machine_wdt.c" #define MICROPY_PY_ONEWIRE (1) #define MICROPY_VFS (1) #define MICROPY_VFS_LFS2 (1) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 6dd71bd93f..8e4327073c 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -104,7 +104,6 @@ SRC_C += \ machine_rtc.c \ machine_spi.c \ machine_uart.c \ - machine_wdt.c \ main.c \ modmachine.c \ modsamd.c \ diff --git a/ports/samd/machine_wdt.c b/ports/samd/machine_wdt.c index d7dcfcf54e..d6fadfd123 100644 --- a/ports/samd/machine_wdt.c +++ b/ports/samd/machine_wdt.c @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#include "py/runtime.h" -#include "modmachine.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE. #include "sam.h" @@ -76,27 +76,15 @@ STATIC void set_timeout(uint32_t timeout) { #endif } -STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_id, ARG_timeout, ARG_lock }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, - }; - - // Parse the arguments. - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - +STATIC machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { #if defined(MCU_SAMD51) // Verify the WDT id. SAMD51 only, saving a few bytes for SAMD21 - mp_int_t id = args[ARG_id].u_int; if (id != 0) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), id); } #endif // Start the watchdog (timeout is in milliseconds). - uint32_t timeout = args[ARG_timeout].u_int; // Configure the WDT #if defined(MCU_SAMD21) @@ -112,37 +100,17 @@ STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type, size_t n_args, s #endif - set_timeout(timeout); + set_timeout(timeout_ms); - return MP_OBJ_FROM_PTR(&machine_wdt); + return (machine_wdt_obj_t *)&machine_wdt; } -STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { - (void)self_in; +STATIC void mp_machine_wdt_feed(machine_wdt_obj_t *self) { + (void)self; WDT->CLEAR.reg = 0xa5; - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed); -STATIC mp_obj_t machine_wdt_timeout_ms(mp_obj_t self_in, mp_obj_t timout_in) { - uint32_t timeout = mp_obj_get_int(timout_in); - - set_timeout(timeout); - - return mp_const_none; +STATIC void mp_machine_wdt_timeout_ms_set(machine_wdt_obj_t *self, mp_int_t timeout_ms) { + (void)self; + set_timeout(timeout_ms); } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_wdt_timeout_ms_obj, machine_wdt_timeout_ms); - -STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) }, - { MP_ROM_QSTR(MP_QSTR_timeout_ms), MP_ROM_PTR(&machine_wdt_timeout_ms_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_wdt_type, - MP_QSTR_WDT, - MP_TYPE_FLAG_NONE, - make_new, machine_wdt_make_new, - locals_dict, &machine_wdt_locals_dict - ); diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 9d5b7c2e27..97718401a3 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -31,6 +31,7 @@ #include "extmod/machine_i2c.h" #include "extmod/machine_signal.h" #include "extmod/machine_spi.h" +#include "extmod/modmachine.h" #include "drivers/dht/dht.h" #include "shared/runtime/pyexec.h" #include "modmachine.h" @@ -261,7 +262,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #if MICROPY_PY_MACHINE_UART { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, #endif + #if MICROPY_PY_MACHINE_WDT { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, + #endif #if MICROPY_PY_MACHINE_RTC { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, #endif diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index b3ba44039a..d62b14a0aa 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -49,7 +49,6 @@ extern const mp_obj_type_t machine_timer_type; #if MICROPY_PY_MACHINE_UART extern const mp_obj_type_t machine_uart_type; #endif -extern const mp_obj_type_t machine_wdt_type; #if MICROPY_PY_MACHINE_RTC extern const mp_obj_type_t machine_rtc_type; #endif diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 011119fd11..3505d8db1e 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -127,6 +127,9 @@ #endif #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_DHT_READINTO (1) +#define MICROPY_PY_MACHINE_WDT (1) +#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/samd/machine_wdt.c" +#define MICROPY_PY_MACHINE_WDT_TIMEOUT_MS (1) #define MICROPY_PY_ONEWIRE (1) #define MICROPY_PY_PLATFORM (1) #define MICROPY_PLATFORM_VERSION "ASF4" diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index dcfc8dfd54..589ef4cdbb 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -329,7 +329,6 @@ SRC_C += \ fdcan.c \ pyb_can.c \ usb.c \ - wdt.c \ eth.c \ gccollect.c \ help.c \ diff --git a/ports/stm32/wdt.c b/ports/stm32/machine_wdt.c similarity index 60% rename from ports/stm32/wdt.c rename to ports/stm32/machine_wdt.c index e541e4d36b..e0605751d1 100644 --- a/ports/stm32/wdt.c +++ b/ports/stm32/machine_wdt.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Damien P. George + * Copyright (c) 2016-2023 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,40 +24,28 @@ * THE SOFTWARE. */ -#include +// This file is never compiled standalone, it's included directly from +// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE. -#include "py/runtime.h" -#include "wdt.h" +#include "py/mphal.h" #if defined(STM32H7) #define IWDG (IWDG1) #endif -typedef struct _pyb_wdt_obj_t { +typedef struct _machine_wdt_obj_t { mp_obj_base_t base; -} pyb_wdt_obj_t; +} machine_wdt_obj_t; -STATIC const pyb_wdt_obj_t pyb_wdt = {{&pyb_wdt_type}}; +STATIC const machine_wdt_obj_t machine_wdt = {{&machine_wdt_type}}; -STATIC mp_obj_t pyb_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - // parse arguments - enum { ARG_id, ARG_timeout }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - mp_int_t id = args[ARG_id].u_int; +STATIC machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { if (id != 0) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), id); } - // timeout is in milliseconds - mp_int_t timeout = args[ARG_timeout].u_int; - // compute prescaler + int32_t timeout = timeout_ms; uint32_t prescaler; for (prescaler = 0; prescaler < 6 && timeout >= 512; ++prescaler, timeout /= 2) { } @@ -86,26 +74,10 @@ STATIC mp_obj_t pyb_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_ // start the watch dog IWDG->KR = 0xcccc; - return MP_OBJ_FROM_PTR(&pyb_wdt); + return (machine_wdt_obj_t *)&machine_wdt; } -STATIC mp_obj_t pyb_wdt_feed(mp_obj_t self_in) { - (void)self_in; +STATIC void mp_machine_wdt_feed(machine_wdt_obj_t *self) { + (void)self; IWDG->KR = 0xaaaa; - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_feed_obj, pyb_wdt_feed); - -STATIC const mp_rom_map_elem_t pyb_wdt_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&pyb_wdt_feed_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(pyb_wdt_locals_dict, pyb_wdt_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - pyb_wdt_type, - MP_QSTR_WDT, - MP_TYPE_FLAG_NONE, - make_new, pyb_wdt_make_new, - locals_dict, &pyb_wdt_locals_dict - ); diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 69fe8f4f54..926dc30413 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -38,6 +38,7 @@ #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" #include "extmod/machine_spi.h" +#include "extmod/modmachine.h" #include "shared/runtime/pyexec.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" @@ -56,7 +57,6 @@ #include "i2c.h" #include "spi.h" #include "uart.h" -#include "wdt.h" #if defined(STM32G0) // G0 has BOR and POR combined @@ -447,7 +447,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) }, #endif { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, - { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) }, + #if MICROPY_PY_MACHINE_WDT + { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, #if 0 { MP_ROM_QSTR(MP_QSTR_HeartBeat), MP_ROM_PTR(&pyb_heartbeat_type) }, diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 2d2d81486b..0e9df54ff4 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -122,6 +122,8 @@ #define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB) #define MICROPY_PY_MACHINE_SOFTSPI (1) #define MICROPY_PY_MACHINE_TIMER (1) +#define MICROPY_PY_MACHINE_WDT (1) +#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/stm32/machine_wdt.c" #endif #define MICROPY_HW_SOFTSPI_MIN_DELAY (0) #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (HAL_RCC_GetSysClockFreq() / 48)