From 29e9573de7200cc64654a77c2c20a57f89d11082 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 7 Jun 2023 15:10:11 +0200 Subject: [PATCH] esp32: Use always machine_pin_get_id for getting a Pin id. This applies to all machine modules which have pins as arguments. Since machine_pin_get_id() calls pin_find(), these pin arguments may be at the moment either integer objects or Pin objects. That allows for instance to write uart = UART(1, tx=Pin(4), rx=Pin(5)) instead of uart = UART(1, tx=4, rx=5) which is consistent with other ports. Since this handling is done at a single place in the code, extending that scheme to accept strings for named pins is easy. Signed-off-by: robert-hh --- ports/esp32/machine_i2c.c | 4 ++-- ports/esp32/machine_i2s.c | 6 +++--- ports/esp32/machine_sdcard.c | 11 +---------- ports/esp32/machine_uart.c | 35 ++++++++++++++++++----------------- ports/esp32/mphalport.h | 1 - 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index e3b7647790..d498aa058d 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -185,10 +185,10 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_ // Set SCL/SDA pins if given if (args[ARG_scl].u_obj != MP_OBJ_NULL) { - self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj); + self->scl = machine_pin_get_id(args[ARG_scl].u_obj); } if (args[ARG_sda].u_obj != MP_OBJ_NULL) { - self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj); + self->sda = machine_pin_get_id(args[ARG_sda].u_obj); } // Initialise the I2C peripheral diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index 59b24ae70c..95751c87c3 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -392,9 +392,9 @@ STATIC void machine_i2s_init_helper(machine_i2s_obj_t *self, size_t n_pos_args, // // are Pins valid? - int8_t sck = args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : mp_hal_get_pin_obj(args[ARG_sck].u_obj); - int8_t ws = args[ARG_ws].u_obj == MP_OBJ_NULL ? -1 : mp_hal_get_pin_obj(args[ARG_ws].u_obj); - int8_t sd = args[ARG_sd].u_obj == MP_OBJ_NULL ? -1 : mp_hal_get_pin_obj(args[ARG_sd].u_obj); + int8_t sck = args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sck].u_obj); + int8_t ws = args[ARG_ws].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_ws].u_obj); + int8_t sd = args[ARG_sd].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sd].u_obj); // is Mode valid? i2s_mode_t mode = args[ARG_mode].u_int; diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index a2d133442f..bc4aaf30c6 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -131,18 +131,9 @@ static const sdspi_device_config_t spi_dev_defaults[2] = { SDSPI_DEVICE_CONFIG_DEFAULT(), // HSPI (ESP32) / SPI2 (ESP32S3) }; -STATIC gpio_num_t pin_or_int(const mp_obj_t arg) { - if (mp_obj_is_small_int(arg)) { - return MP_OBJ_SMALL_INT_VALUE(arg); - } else { - // This raises a value error if the argument is not a Pin. - return machine_pin_get_id(arg); - } -} - #define SET_CONFIG_PIN(config, pin_var, arg_id) \ if (arg_vals[arg_id].u_obj != mp_const_none) \ - config.pin_var = pin_or_int(arg_vals[arg_id].u_obj) + config.pin_var = machine_pin_get_id(arg_vals[arg_id].u_obj) STATIC esp_err_t sdcard_ensure_card_init(sdcard_card_obj_t *self, bool force) { if (force || !(self->flags & SDCARD_CARD_FLAGS_CARD_INIT_DONE)) { diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 7a2dc2b323..e7b2b8376b 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -34,6 +34,7 @@ #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" +#include "py/mphal.h" #include "modmachine.h" #include "uart.h" @@ -58,10 +59,10 @@ typedef struct _machine_uart_obj_t { uint8_t bits; uint8_t parity; uint8_t stop; - int8_t tx; - int8_t rx; - int8_t rts; - int8_t cts; + gpio_num_t tx; + gpio_num_t rx; + gpio_num_t rts; + gpio_num_t cts; uint16_t txbuf; uint16_t rxbuf; uint16_t timeout; // timeout waiting for first char (in ms) @@ -133,10 +134,10 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co { MP_QSTR_bits, MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_stop, MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_PIN_NO_CHANGE} }, - { MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_PIN_NO_CHANGE} }, - { MP_QSTR_rts, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_PIN_NO_CHANGE} }, - { MP_QSTR_cts, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_PIN_NO_CHANGE} }, + { MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_rts, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_cts, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_txbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, @@ -185,22 +186,22 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co } uart_get_baudrate(self->uart_num, &baudrate); - uart_set_pin(self->uart_num, args[ARG_tx].u_int, args[ARG_rx].u_int, args[ARG_rts].u_int, args[ARG_cts].u_int); - if (args[ARG_tx].u_int != UART_PIN_NO_CHANGE) { - self->tx = args[ARG_tx].u_int; + if (args[ARG_tx].u_obj != MP_OBJ_NULL) { + self->tx = machine_pin_get_id(args[ARG_tx].u_obj); } - if (args[ARG_rx].u_int != UART_PIN_NO_CHANGE) { - self->rx = args[ARG_rx].u_int; + if (args[ARG_rx].u_obj != MP_OBJ_NULL) { + self->rx = machine_pin_get_id(args[ARG_rx].u_obj); } - if (args[ARG_rts].u_int != UART_PIN_NO_CHANGE) { - self->rts = args[ARG_rts].u_int; + if (args[ARG_rts].u_obj != MP_OBJ_NULL) { + self->rts = machine_pin_get_id(args[ARG_rts].u_obj); } - if (args[ARG_cts].u_int != UART_PIN_NO_CHANGE) { - self->cts = args[ARG_cts].u_int; + if (args[ARG_cts].u_obj != MP_OBJ_NULL) { + self->cts = machine_pin_get_id(args[ARG_cts].u_obj); } + uart_set_pin(self->uart_num, self->tx, self->rx, self->rts, self->cts); // set data bits switch (args[ARG_bits].u_int) { diff --git a/ports/esp32/mphalport.h b/ports/esp32/mphalport.h index 4f25b3d4fd..566d6609f1 100644 --- a/ports/esp32/mphalport.h +++ b/ports/esp32/mphalport.h @@ -86,7 +86,6 @@ void mp_hal_wake_main_task_from_isr(void); #define mp_hal_pin_obj_t gpio_num_t mp_hal_pin_obj_t machine_pin_get_id(mp_obj_t pin_in); #define mp_hal_get_pin_obj(o) machine_pin_get_id(o) -#define mp_obj_get_pin(o) machine_pin_get_id(o) // legacy name; only to support esp8266/modonewire #define mp_hal_pin_name(p) (p) static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) { esp_rom_gpio_pad_select_gpio(pin);