Skip to content

Commit 29e9573

Browse files
robert-hhdpgeorge
authored andcommitted
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>
1 parent 3819ee4 commit 29e9573

File tree

5 files changed

+24
-33
lines changed

5 files changed

+24
-33
lines changed

ports/esp32/machine_i2c.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_
185185

186186
// Set SCL/SDA pins if given
187187
if (args[ARG_scl].u_obj != MP_OBJ_NULL) {
188-
self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
188+
self->scl = machine_pin_get_id(args[ARG_scl].u_obj);
189189
}
190190
if (args[ARG_sda].u_obj != MP_OBJ_NULL) {
191-
self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
191+
self->sda = machine_pin_get_id(args[ARG_sda].u_obj);
192192
}
193193

194194
// Initialise the I2C peripheral

ports/esp32/machine_i2s.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ STATIC void machine_i2s_init_helper(machine_i2s_obj_t *self, size_t n_pos_args,
392392
//
393393

394394
// are Pins valid?
395-
int8_t sck = args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : mp_hal_get_pin_obj(args[ARG_sck].u_obj);
396-
int8_t ws = args[ARG_ws].u_obj == MP_OBJ_NULL ? -1 : mp_hal_get_pin_obj(args[ARG_ws].u_obj);
397-
int8_t sd = args[ARG_sd].u_obj == MP_OBJ_NULL ? -1 : mp_hal_get_pin_obj(args[ARG_sd].u_obj);
395+
int8_t sck = args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sck].u_obj);
396+
int8_t ws = args[ARG_ws].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_ws].u_obj);
397+
int8_t sd = args[ARG_sd].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sd].u_obj);
398398

399399
// is Mode valid?
400400
i2s_mode_t mode = args[ARG_mode].u_int;

ports/esp32/machine_sdcard.c

+1-10
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,9 @@ static const sdspi_device_config_t spi_dev_defaults[2] = {
131131
SDSPI_DEVICE_CONFIG_DEFAULT(), // HSPI (ESP32) / SPI2 (ESP32S3)
132132
};
133133

134-
STATIC gpio_num_t pin_or_int(const mp_obj_t arg) {
135-
if (mp_obj_is_small_int(arg)) {
136-
return MP_OBJ_SMALL_INT_VALUE(arg);
137-
} else {
138-
// This raises a value error if the argument is not a Pin.
139-
return machine_pin_get_id(arg);
140-
}
141-
}
142-
143134
#define SET_CONFIG_PIN(config, pin_var, arg_id) \
144135
if (arg_vals[arg_id].u_obj != mp_const_none) \
145-
config.pin_var = pin_or_int(arg_vals[arg_id].u_obj)
136+
config.pin_var = machine_pin_get_id(arg_vals[arg_id].u_obj)
146137

147138
STATIC esp_err_t sdcard_ensure_card_init(sdcard_card_obj_t *self, bool force) {
148139
if (force || !(self->flags & SDCARD_CARD_FLAGS_CARD_INIT_DONE)) {

ports/esp32/machine_uart.c

+18-17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/runtime.h"
3535
#include "py/stream.h"
3636
#include "py/mperrno.h"
37+
#include "py/mphal.h"
3738
#include "modmachine.h"
3839
#include "uart.h"
3940

@@ -58,10 +59,10 @@ typedef struct _machine_uart_obj_t {
5859
uint8_t bits;
5960
uint8_t parity;
6061
uint8_t stop;
61-
int8_t tx;
62-
int8_t rx;
63-
int8_t rts;
64-
int8_t cts;
62+
gpio_num_t tx;
63+
gpio_num_t rx;
64+
gpio_num_t rts;
65+
gpio_num_t cts;
6566
uint16_t txbuf;
6667
uint16_t rxbuf;
6768
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
133134
{ MP_QSTR_bits, MP_ARG_INT, {.u_int = 0} },
134135
{ MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
135136
{ MP_QSTR_stop, MP_ARG_INT, {.u_int = 0} },
136-
{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_PIN_NO_CHANGE} },
137-
{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_PIN_NO_CHANGE} },
138-
{ MP_QSTR_rts, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_PIN_NO_CHANGE} },
139-
{ MP_QSTR_cts, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_PIN_NO_CHANGE} },
137+
{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
138+
{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
139+
{ MP_QSTR_rts, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
140+
{ MP_QSTR_cts, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
140141
{ MP_QSTR_txbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
141142
{ MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
142143
{ 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
185186
}
186187
uart_get_baudrate(self->uart_num, &baudrate);
187188

188-
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);
189-
if (args[ARG_tx].u_int != UART_PIN_NO_CHANGE) {
190-
self->tx = args[ARG_tx].u_int;
189+
if (args[ARG_tx].u_obj != MP_OBJ_NULL) {
190+
self->tx = machine_pin_get_id(args[ARG_tx].u_obj);
191191
}
192192

193-
if (args[ARG_rx].u_int != UART_PIN_NO_CHANGE) {
194-
self->rx = args[ARG_rx].u_int;
193+
if (args[ARG_rx].u_obj != MP_OBJ_NULL) {
194+
self->rx = machine_pin_get_id(args[ARG_rx].u_obj);
195195
}
196196

197-
if (args[ARG_rts].u_int != UART_PIN_NO_CHANGE) {
198-
self->rts = args[ARG_rts].u_int;
197+
if (args[ARG_rts].u_obj != MP_OBJ_NULL) {
198+
self->rts = machine_pin_get_id(args[ARG_rts].u_obj);
199199
}
200200

201-
if (args[ARG_cts].u_int != UART_PIN_NO_CHANGE) {
202-
self->cts = args[ARG_cts].u_int;
201+
if (args[ARG_cts].u_obj != MP_OBJ_NULL) {
202+
self->cts = machine_pin_get_id(args[ARG_cts].u_obj);
203203
}
204+
uart_set_pin(self->uart_num, self->tx, self->rx, self->rts, self->cts);
204205

205206
// set data bits
206207
switch (args[ARG_bits].u_int) {

ports/esp32/mphalport.h

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ void mp_hal_wake_main_task_from_isr(void);
8686
#define mp_hal_pin_obj_t gpio_num_t
8787
mp_hal_pin_obj_t machine_pin_get_id(mp_obj_t pin_in);
8888
#define mp_hal_get_pin_obj(o) machine_pin_get_id(o)
89-
#define mp_obj_get_pin(o) machine_pin_get_id(o) // legacy name; only to support esp8266/modonewire
9089
#define mp_hal_pin_name(p) (p)
9190
static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) {
9291
esp_rom_gpio_pad_select_gpio(pin);

0 commit comments

Comments
 (0)