eslavko
Posts: 73
Joined: Fri Dec 06, 2013 6:10 pm

GPIO fastest way to switch from OUTPUT to INPUT

Wed Jul 01, 2020 7:33 am

Hello...

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?

Heater
Posts: 15832
Joined: Tue Jul 17, 2012 3:02 pm

Re: GPIO fastest way to switch from OUTPUT to INPUT

Wed Jul 01, 2020 10:40 am

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.
Memory in C++ is a leaky abstraction .

eslavko
Posts: 73
Joined: Fri Dec 06, 2013 6:10 pm

Re: GPIO fastest way to switch from OUTPUT to INPUT

Wed Jul 01, 2020 12:32 pm

Yes the application runs in python but as script I mean script for pigpiod daemon. http://abyz.me.uk/rpi/pigpio/python.html#run_script 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.

Return to “Interfacing (DSI, CSI, I2C, etc.)”