Skip to content

Commit 3e7d28c

Browse files
committed
rp2040: uart: check all pins before claiming any
remove the comment that pin_init may raise an exception for an in-use pin; this is incorrect. The unclaimed status of all pins was verified in shared-bindings before calling construct.
1 parent 937cfa6 commit 3e7d28c

File tree

1 file changed

+12
-4
lines changed
  • ports/raspberrypi/common-hal/busio

1 file changed

+12
-4
lines changed

ports/raspberrypi/common-hal/busio/UART.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ void never_reset_uart(uint8_t num) {
4242
uart_status[num] = STATUS_NEVER_RESET;
4343
}
4444

45+
static void pin_check(const uint8_t uart, const mcu_pin_obj_t *pin, const uint8_t pin_type) {
46+
if (!(((pin->number % 4) == pin_type) && ((((pin->number + 4) / 8) % NUM_UARTS) == uart))) {
47+
raise_ValueError_invalid_pins();
48+
}
49+
}
50+
4551
static uint8_t pin_init(const uint8_t uart, const mcu_pin_obj_t *pin, const uint8_t pin_type) {
4652
if (pin == NULL) {
4753
return NO_PIN;
4854
}
49-
if (!(((pin->number % 4) == pin_type) && ((((pin->number + 4) / 8) % NUM_UARTS) == uart))) {
50-
raise_ValueError_invalid_pins();
51-
}
5255
claim_pin(pin);
5356
gpio_set_function(pin->number, GPIO_FUNC_UART);
5457
return pin->number;
@@ -90,10 +93,15 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
9093

9194
uint8_t uart_id = ((((tx != NULL) ? tx->number : rx->number) + 4) / 8) % NUM_UARTS;
9295

96+
pin_check(uart_id, tx, 0);
97+
pin_check(uart_id, rx, 1);
98+
pin_check(uart_id, cts, 2);
99+
pin_check(uart_id, rts, 3);
100+
93101
if (uart_status[uart_id] != STATUS_FREE) {
94102
mp_raise_ValueError(MP_ERROR_TEXT("UART peripheral in use"));
95103
}
96-
// These may raise exceptions if pins are already in use.
104+
97105
self->tx_pin = pin_init(uart_id, tx, 0);
98106
self->rx_pin = pin_init(uart_id, rx, 1);
99107
self->cts_pin = pin_init(uart_id, cts, 2);

0 commit comments

Comments
 (0)