1

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;
}
Bjduf123
  • 43
  • 5
  • Google returned [this](http://d-fence.sytes.net/raspberry-pis-gpio-speed/). – Duck Dodgers Feb 15 '19 at 15:04
  • If you read my post, you will see that I said I can toggle the pins at .1us which I do not believe is my problem, I believe that it is the write that is taking too long. – Bjduf123 Feb 15 '19 at 15:43
  • I am afraid that the issue is a bit deeper. `serialPutchar` invokes `write`, which is a system call, with lots and lots of overhead. At the first glance, the safest bet is to write the device driver for your protocol converter, to handle writing _and_ chip select. – user58697 Feb 15 '19 at 20:42
  • Yeah I assumed that it was write, i originally used write directly and attempted this library after it was taking too long hoping for some sort of magic. I think I might switch to a different encoder and use SPI as I have never dealt with a device driver. – Bjduf123 Feb 15 '19 at 21:05

0 Answers0