I am trying to talk to an encoder from my Raspberry Pi 3B+ using a baud rate of 2000000. The signal that is coming out of the Pi is being converted from a RS-232 to an RS-485 signal using a chip that needs a chip select toggled to allow communication one way or the other. From RS-232 -> rs-485 the line needs to be high so i set it high and send the communication and that works just fine. The problem is that the encoder responds in ~4us and it seems that the write command is taking too long to allow the following digital write to toggle the chip select low to allow the response from RS-485 -> RS-232 by just a little bit so we are losing the first few bits. I am wondering if there is any way to speed up the write or toggle of the GPIO pin. Based off of tests of just the GPIO pin, I can toggle it in around .1us so that is why I believe it is the write command.
I have attempted to remove the library that I am using which is wiringPi and create the serial port myself to the same end result.
For the following picture, the yellow line is the transmit and receive line and everything is in hex. The 0x54 is the command that I am sending and the 0xD7 and 0x3A are the responses. As you can see the chip select goes low during the first few bits of the response, causing data loss.
int main(int argc, char** argv) {
wiringPiSetup();
pinMode(21, OUTPUT);
int sfd = serialOpen("/dev/serial0", 2000000);
digitalWrite(21, 1);
serialPutchar(sfd, 0x54);
digitalWrite(21, 0);
return 0;
}