mirror of
https://github.com/micropython/micropython.git
synced 2025-12-16 01:40:14 +01:00
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:
@@ -104,7 +104,6 @@ SRC_C += \
|
||||
machine_rtc.c \
|
||||
machine_spi.c \
|
||||
machine_uart.c \
|
||||
machine_wdt.c \
|
||||
main.c \
|
||||
modmachine.c \
|
||||
modsamd.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
|
||||
);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user