I started by using the following configuration to let the driver reset the RTS pin automatically after the UART Tx is completed:
Code: Select all
uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX);
Then I tried to do manual switching. I call the uart_write_bytes function with zero TX buffer so that it blocks until TX is done, and then I set the GPIO manually. This is faster than the RS485 mode, around 7us, but still too slow to keep up with the encoder and the first response byte is lost. The time delay is not always stable, sometimes I lose more than one byte.
I also tried to call the uart_write_bytes with a non-zero buffer, so that it doesn't block, and then I start a HW timer to wait for a few us before manually resetting the RTS pin in the timer alarm interrupt. This solution is also not stable because the interrupt sometimes triggers too soon, sometimes too late.
I have no other idea at the moment. What is the common way to handle this requirement? Why isn't the internal RS485 driver able to switch the RTS pin right away?