Hello,
I need to implement half-duplex UART over a single wire and configure alternatively the same pin for both UART TX and UART RX.
The idea it to remap the pin (attach/detach) using the GPIO matrix.
Can somebody help me to correct switchToTX and switchToRX below that are supposed (but still not working...) to do RX/TX switching of the single wire.
For the tests, I use two ESP32 boards connected together throught the GPIO17.
Thank you,
Michel
#define SingleWire_pin 17
HardwareSerial MySerial(1);
void setup() {
//begins with the UART1 standard pins: 17 and 16. The pin 16 is definitively realeased bc no more needed.
MySerial.begin(115200, SERIAL_8N1, SingleWire_pin, 16);
pinMatrixOutDetach(16, false, false);
...
}
void switchToTX(){
pinMatrixOutDetach(SingleWire_pin, false, false);
pinMode(SingleWire_pin, OUTPUT);
pinMatrixOutAttach(SingleWire_pin, U1TXD_OUT_IDX, false, false);
}
void switchToRX(){
pinMatrixOutDetach(SingleWire_pin, false, false);
pinMode(SingleWire_pin, INPUT);
pinMatrixInAttach(SingleWire_pin, U1RXD_IN_IDX, false);
}
void loop() {
//before writing over MySerial: switchToTX
switchToTX();
...
//before reading over MySerial: switchToRX
switchToRX();
...
}
thank you DrDiettrich for your response.
I only have 1 remaining GPIO and can't use RS-485 for some reasons.
Yes, ESP32 also have open collector outputs. What's the relationship with my request?
You can connect o.c. outputs of both controllers and switch the usage of the pin between RX and TX without switching between input and output mode. At least this will reduce problems if both controllers try to send at the same time.
But if you want to use the hardware UART this may not help, I'm not familiar with the ESP32.
Shouldn't there be a pullup resistor somewhere (internal or external, device1 or device2)? If both devices are in RX mode, it's probably expected that the signal would be pulled high.