Page 1 of 1

GPIO fastest way to switch from OUTPUT to INPUT

Posted: Wed Jul 01, 2020 7:33 am
by eslavko

I have some special serial transmission and that use single wire with half duplex.
So I need to switch gpio from output to input after transmission. And I need to do that fast!

For now I do transmission with gpio create_wave and encode data to it. And I have script where I switch pin from output to input when no wave is transmitted.
I tried to have two PI pins one for TX and other for RX and resistors to combine them but doesn't work. While transmitting the output need to be totem pole, and while receiving no pullups/pulldowns are allowed.
The my idea mostly work but I have somewhere around 10us to switch and I do that sometime to late. I observe signal on scope and indeed I got timing around 5 to 20us. Is it there a way to do even faster?

Re: GPIO fastest way to switch from OUTPUT to INPUT

Posted: Wed Jul 01, 2020 10:40 am
by Heater
When you say "script" I presume you are programming in Python or some such interpreted language.

Being interpreted they are slow, you may well need to use a compiled language like C, C++, Rust or Pascal.

If you are really sensitive to timing down to a handful of micro-seconds you will also have the Linux kernel process scheduler to contend with. That can introduce arbitrarily long delays at random.

To mitigate that you may need to isolate the kernel from one or more cores, see "isolcpus" kernel command line option, and run your program on the isolate core where it will not suffer from being rescheduled. See "taskset" command.

Re: GPIO fastest way to switch from OUTPUT to INPUT

Posted: Wed Jul 01, 2020 12:32 pm
by eslavko
Yes the application runs in python but as script I mean script for pigpiod daemon. so they should be fast. As I read using one core for time critical program is option but even there can be glitch longer than 10us and that is not good. Seems that this is not possible at all in simple way.
I see the solution but for now I'm not capable to do that.
The pigpiod use DMA to transfer data to GPIO. I assume that is possible to transfer some data over DMA to GPIO and when I need to switch to receiver the DMA itself should change direction of pin.
Afaik writing of GPIO or direction register is same thing. Only the address is different.