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:
@@ -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
|
||||
|
||||
@@ -25,16 +25,11 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#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
|
||||
);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user