Skip to content

Commit dfbbbad

Browse files
authored
Merge pull request #9425 from jepler/issue9424
rp2040: uart: check all pins before claiming any
2 parents 937cfa6 + 598cad7 commit dfbbbad

File tree

1 file changed

+14
-3
lines changed
  • ports/raspberrypi/common-hal/busio

1 file changed

+14
-3
lines changed

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

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

45-
static uint8_t pin_init(const uint8_t uart, const mcu_pin_obj_t *pin, const uint8_t pin_type) {
45+
static void pin_check(const uint8_t uart, const mcu_pin_obj_t *pin, const uint8_t pin_type) {
4646
if (pin == NULL) {
47-
return NO_PIN;
47+
return;
4848
}
4949
if (!(((pin->number % 4) == pin_type) && ((((pin->number + 4) / 8) % NUM_UARTS) == uart))) {
5050
raise_ValueError_invalid_pins();
5151
}
52+
}
53+
54+
static uint8_t pin_init(const uint8_t uart, const mcu_pin_obj_t *pin, const uint8_t pin_type) {
55+
if (pin == NULL) {
56+
return NO_PIN;
57+
}
5258
claim_pin(pin);
5359
gpio_set_function(pin->number, GPIO_FUNC_UART);
5460
return pin->number;
@@ -90,10 +96,15 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
9096

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

99+
pin_check(uart_id, tx, 0);
100+
pin_check(uart_id, rx, 1);
101+
pin_check(uart_id, cts, 2);
102+
pin_check(uart_id, rts, 3);
103+
93104
if (uart_status[uart_id] != STATUS_FREE) {
94105
mp_raise_ValueError(MP_ERROR_TEXT("UART peripheral in use"));
95106
}
96-
// These may raise exceptions if pins are already in use.
107+
97108
self->tx_pin = pin_init(uart_id, tx, 0);
98109
self->rx_pin = pin_init(uart_id, rx, 1);
99110
self->cts_pin = pin_init(uart_id, cts, 2);

0 commit comments

Comments
 (0)