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 <robert@hammelrath.com>
This commit is contained in:
robert-hh
2023-06-07 15:10:11 +02:00
committed by Damien George
parent 3819ee4a6f
commit 29e9573de7
5 changed files with 24 additions and 33 deletions

View File

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