[RP2040] Allow any GPIO pin for RS485 direction pin
As I mentioned in issue #6310 while investigating that the Teensy port did not support RS485_dir pin on normal GPIO pins, I found that it was not implemented either as well on some other ports. So was curious to implement it for RP2040 using same approach as I did for the MIMXRT in the Pull Request #6328 That is I setup the specified pin as a normal GPIO pin in output mode and then when you do a write operation it sets the GPIO pin logically high, and when the write completes I set it logically low. Note: knowing when I can set it low can be tricky, as you need to make sure the full output has completed otherwise the data will be corrupted. I am using: uart_tx_wait_blocking(self->uart); Which looks like it is supposed to wait until the busy status is no longer set, which the Reference manual mentioned, but this is leaving the line logically set longer than I would like. however I have tried running it with my hacked up version of the Python Robotis DynamixelSDK and was able to talk to some AX servos. I did have to change the library slightly for the RP2040, as the library was erroring out when you did something like uart.read(5) and it timed out without receiving anything. The RP2040 returned None whereas I think the Teensy returned an empty set, which is what it looks like the PySerial original code expects. Not sure if anyone is interested in this, but thought i would put it out as PR and see.
This commit is contained in:
parent
b598ec0ddb
commit
1fad381513
@ -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) {
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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.
|
||||
@ -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) {
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user