From 2251d0f4b0692ab6c1104ec6b04ff513b3f028fe Mon Sep 17 00:00:00 2001 From: arturo182 Date: Tue, 11 Jan 2022 15:13:36 +0100 Subject: [PATCH 1/2] raspberrypi: Claim UART after checking pins If, for some reason, you mix up TX and RX when calling `busio.UART` (who would do that ;) ), you get `Invalid pins`. When you go to try again, you'll get `All UART peripherals are in use` because the interface was claimed as busy before pins are verified. This should fix that issue. --- ports/raspberrypi/common-hal/busio/UART.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/raspberrypi/common-hal/busio/UART.c b/ports/raspberrypi/common-hal/busio/UART.c index 14935f4046..e698c3701f 100644 --- a/ports/raspberrypi/common-hal/busio/UART.c +++ b/ports/raspberrypi/common-hal/busio/UART.c @@ -118,17 +118,17 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, uint8_t uart_id = ((((tx != NULL) ? tx->number : rx->number) + 4) / 8) % NUM_UARTS; + self->tx_pin = pin_init(uart_id, tx, 0); + self->rx_pin = pin_init(uart_id, rx, 1); + self->cts_pin = pin_init(uart_id, cts, 2); + self->rts_pin = pin_init(uart_id, rts, 3); + if (uart_status[uart_id] != STATUS_FREE) { mp_raise_RuntimeError(translate("All UART peripherals are in use")); } else { uart_status[uart_id] = STATUS_BUSY; } - self->tx_pin = pin_init(uart_id, tx, 0); - self->rx_pin = pin_init(uart_id, rx, 1); - self->cts_pin = pin_init(uart_id, cts, 2); - self->rts_pin = pin_init(uart_id, rts, 3); - self->uart = UART_INST(uart_id); self->uart_id = uart_id; self->baudrate = baudrate; From 9b825869c53fb6c6c57aef05bd3d9e1c5855b4ba Mon Sep 17 00:00:00 2001 From: arturo182 Date: Tue, 11 Jan 2022 22:02:47 +0100 Subject: [PATCH 2/2] raspberrypi : Check UART before claiming pins, claim it after Co-authored-by: Scott Shawcroft --- ports/raspberrypi/common-hal/busio/UART.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/raspberrypi/common-hal/busio/UART.c b/ports/raspberrypi/common-hal/busio/UART.c index e698c3701f..0e25c57297 100644 --- a/ports/raspberrypi/common-hal/busio/UART.c +++ b/ports/raspberrypi/common-hal/busio/UART.c @@ -118,16 +118,16 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, uint8_t uart_id = ((((tx != NULL) ? tx->number : rx->number) + 4) / 8) % NUM_UARTS; + if (uart_status[uart_id] != STATUS_FREE) { + mp_raise_RuntimeError(translate("All UART peripherals are in use")); + } + // These may raise exceptions if pins are already in use. self->tx_pin = pin_init(uart_id, tx, 0); self->rx_pin = pin_init(uart_id, rx, 1); self->cts_pin = pin_init(uart_id, cts, 2); self->rts_pin = pin_init(uart_id, rts, 3); + uart_status[uart_id] = STATUS_BUSY; - if (uart_status[uart_id] != STATUS_FREE) { - mp_raise_RuntimeError(translate("All UART peripherals are in use")); - } else { - uart_status[uart_id] = STATUS_BUSY; - } self->uart = UART_INST(uart_id); self->uart_id = uart_id;