Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rp2040: uart: check all pins before claiming any #9425

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ports/raspberrypi/common-hal/busio/UART.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ void never_reset_uart(uint8_t num) {
uart_status[num] = STATUS_NEVER_RESET;
}

static uint8_t pin_init(const uint8_t uart, const mcu_pin_obj_t *pin, const uint8_t pin_type) {
static void pin_check(const uint8_t uart, const mcu_pin_obj_t *pin, const uint8_t pin_type) {
if (pin == NULL) {
return NO_PIN;
return;
}
if (!(((pin->number % 4) == pin_type) && ((((pin->number + 4) / 8) % NUM_UARTS) == uart))) {
jepler marked this conversation as resolved.
Show resolved Hide resolved
raise_ValueError_invalid_pins();
}
}

static uint8_t pin_init(const uint8_t uart, const mcu_pin_obj_t *pin, const uint8_t pin_type) {
if (pin == NULL) {
return NO_PIN;
}
claim_pin(pin);
gpio_set_function(pin->number, GPIO_FUNC_UART);
return pin->number;
Expand Down Expand Up @@ -90,10 +96,15 @@ 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;

pin_check(uart_id, tx, 0);
pin_check(uart_id, rx, 1);
pin_check(uart_id, cts, 2);
pin_check(uart_id, rts, 3);

if (uart_status[uart_id] != STATUS_FREE) {
mp_raise_ValueError(MP_ERROR_TEXT("UART peripheral 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);
Expand Down