Skip to content

Commit

Permalink
extmod/machine_wdt: Factor ports' WDT Python bindings to common code.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dpgeorge committed Oct 20, 2023
1 parent 2590a34 commit 60929ec
Show file tree
Hide file tree
Showing 38 changed files with 235 additions and 304 deletions.
1 change: 1 addition & 0 deletions extmod/extmod.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions extmod/extmod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
95 changes: 95 additions & 0 deletions extmod/machine_wdt.c
Original file line number Diff line number Diff line change
@@ -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
22 changes: 17 additions & 5 deletions ports/stm32/wdt.h → extmod/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion ports/cc3200/application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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/,\
Expand Down
66 changes: 17 additions & 49 deletions ports/cc3200/mods/pybwdt.c → ports/cc3200/mods/machine_wdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
* THE SOFTWARE.
*/

#include <stdint.h>
// 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"
Expand All @@ -40,8 +38,6 @@
#include "prcm.h"
#include "utils.h"
#include "pybwdt.h"
#include "mperror.h"


/******************************************************************************
DECLARE CONSTANTS
Expand All @@ -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
Expand All @@ -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);
}

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

4 changes: 2 additions & 2 deletions ports/cc3200/mods/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions ports/cc3200/mods/pybwdt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdbool.h>

void pybwdt_init0 (void);
void pybwdt_srv_alive (void);
Expand Down
2 changes: 2 additions & 0 deletions ports/cc3200/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/esp32_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 60929ec

Please sign in to comment.