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] Allow any GPIO pin for RS485 direction pin #6330

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
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
39 changes: 34 additions & 5 deletions ports/raspberrypi/common-hal/busio/UART.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_raise_ValueError(translate("Invalid buffer size"));
}

if ((rs485_dir != NULL) || (rs485_invert)) {
mp_raise_NotImplementedError(translate("RS485 Not yet supported on this device"));
}

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

if (uart_status[uart_id] != STATUS_FREE) {
Expand All @@ -126,6 +122,29 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
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 (rs485_dir != NULL) {
uint8_t pin = rs485_dir->number;
self->rs485_dir_pin = pin;
self->rs485_invert = rs485_invert;

gpio_init(pin);

claim_pin(rs485_dir);

gpio_disable_pulls(pin);

// Turn on "strong" pin driving (more current available).
hw_write_masked(&padsbank0_hw->io[pin],
PADS_BANK0_GPIO0_DRIVE_VALUE_12MA << PADS_BANK0_GPIO0_DRIVE_LSB,
PADS_BANK0_GPIO0_DRIVE_BITS);

gpio_put(self->rs485_dir_pin, rs485_invert);
gpio_set_dir(self->rs485_dir_pin, GPIO_OUT);
} else {
self->rs485_dir_pin = NO_PIN;
}

uart_status[uart_id] = STATUS_BUSY;


Expand Down Expand Up @@ -179,10 +198,12 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
reset_pin_number(self->rx_pin);
reset_pin_number(self->cts_pin);
reset_pin_number(self->rts_pin);
reset_pin_number(self->rs485_dir_pin);
self->tx_pin = NO_PIN;
self->rx_pin = NO_PIN;
self->cts_pin = NO_PIN;
self->rts_pin = NO_PIN;
self->rs485_dir_pin = NO_PIN;
}

// Write characters.
Expand All @@ -191,6 +212,11 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
mp_raise_ValueError(translate("No TX pin"));
}

if (self->rs485_dir_pin != NO_PIN) {
uart_tx_wait_blocking(self->uart);
gpio_put(self->rs485_dir_pin, !self->rs485_invert);
}

size_t left_to_write = len;
while (left_to_write > 0) {
while (uart_is_writable(self->uart) && left_to_write > 0) {
Expand All @@ -201,7 +227,10 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
}
RUN_BACKGROUND_TASKS;
}

if (self->rs485_dir_pin != NO_PIN) {
uart_tx_wait_blocking(self->uart);
gpio_put(self->rs485_dir_pin, self->rs485_invert);
}
return len;
}

Expand Down
2 changes: 2 additions & 0 deletions ports/raspberrypi/common-hal/busio/UART.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ typedef struct {
uint8_t tx_pin;
uint8_t rx_pin;
uint8_t cts_pin;
uint8_t rs485_dir_pin;
bool rs485_invert;
uint8_t rts_pin;
uint8_t uart_id;
uint8_t uart_irq_id;
Expand Down