Device description (for context, skip it if you don't feel comfortable with electronic):
For a simple device, the communication is done in half-duplex UART (TX and RX are on the same wire), in the following way:
When the write-mode
is in TX (writing), the RX
pin is in high-impedance and TX
in transmitting. While the write-mode
is in RX (reading), the TX
pin is in high-impedance and RX
receiving.
This is just for context, I do not expect electronic question/answers here.
WiringPI sample:
For this to happens, I have the following sample:
#include <wiringPi.h>
#include <wiringSerial.h>
int main()
{
wiringPiSetup ();
auto fd = serialOpen ("/dev/ttyAMA0", 115200);
pinMode(0, OUTPUT);
for(size_t i=0; i<10; ++i)
{
digitalWrite(0, HIGH);
serialPutchar(fd, '\x55');
digitalWrite(0, LOW);
delay(1000);
}
serialClose(fd);
}
Using an oscilloscope, I can clearly see that the write-mode
pin is reset before the UART end to send the data.
Obviously, I tried to add some "delay" or empty-loop to adjust, but this is not reliable for μs times (due to usual precision in timers on OS).
The question:
How to synchronize, so the write-mode
pin is reset just after the UART byte is sent? (No more than approximately 150μs later).
I see 2 ways to implement this:
1. I can't test this right now, but it seems you could use the
void serialFlush (int fd) ;
According to the docs "This discards all data received, or waiting to be send down the given device" see at http://wiringpi.com/reference/serial-library/
(Edit: after re-reading that sentence, it's clear that it would indeed flush also the data to be written, so this option is out...)
serialOpen()