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 <damien@micropython.org>
This commit is contained in:
Damien George
2023-10-10 18:19:03 +11:00
parent 2590a34ed7
commit 60929ec7e2
38 changed files with 235 additions and 304 deletions

View File

@@ -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 \

View File

@@ -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
);

View File

@@ -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) },

View File

@@ -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);

View File

@@ -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)