Q on gpio_set_direction()

apuder
Posts: 53
Joined: Mon Mar 26, 2018 7:27 pm

Q on gpio_set_direction()

Postby apuder » Tue Apr 03, 2018 10:36 pm

looking at the implementation of gpio_set_direction():
https://github.com/espressif/esp-idf/bl ... pio.c#L245

It seems to be possible to configure a GPIO pin as both input and output at the same time. Is this permissible? What are the electrical characteristics of a GPIO pin configured both as input and output? What if you first want to write and then later read from such a pin?

TIA,
AP

xiangcheng
Posts: 29
Joined: Mon Feb 20, 2017 2:27 am

Re: Q on gpio_set_direction()

Postby xiangcheng » Wed Apr 04, 2018 3:11 am

seems doesn't make sense. OUTPUT and INPUT function for GPIO are different. you have to change the IO direction although some GPIO pins are multi-functional. Any special user case for it?

apuder
Posts: 53
Joined: Mon Mar 26, 2018 7:27 pm

Re: Q on gpio_set_direction()

Postby apuder » Wed Apr 04, 2018 3:43 am

as a followup to my own question, it seems that the combined input/output for a GPIO pin is indeed supported:
https://github.com/espressif/esp-idf/bl ... pio.h#L194

Can someone explain how this is supposed to work? How do you do read/write operations to such a pin?

TIA,
AP

meowsqueak
Posts: 151
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: Q on gpio_set_direction()

Postby meowsqueak » Wed Apr 04, 2018 4:03 am

Usually on microcontrollers/CPUs there are two separate electrical paths on the pin's hardware driver, the output path and the input path. They are connected to each other at the pin.

When set to input-only, the output is put into a high-impedance tri-state, so it does not drive the pin. Depending on the architecture, the input side is either always connected (so you can read from it even if not actually set as an input pin) or it's electrically disconnected via a transistor so that it does not source or sink any current, in which case a read may produce a fixed value, or an SDK error - depends on the system.

So, setting a pin to both output and input simply lets the CPU read whatever the current state of the pin is. This could also be handy if you want the pin to trigger an interrupt at the time it is toggled, maybe for setting up a timing reference, or to just read its current state as a input, perhaps if a second task is driving it as an output.

Oh, and to answer the last question: just write to and read from it normally.

apuder
Posts: 53
Joined: Mon Mar 26, 2018 7:27 pm

Re: Q on gpio_set_direction()

Postby apuder » Wed Apr 04, 2018 4:21 am

thanks for your response. OK, so if I get this right, an INPUT/OUTPUT pin is really just an output pin where you can use a read operation on the same pin that will simply return the value you have written. It doesn't mean that you can read from an external device connected to that pin. Is this correct?

AP

meowsqueak
Posts: 151
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: Q on gpio_set_direction()

Postby meowsqueak » Wed Apr 04, 2018 4:39 am

apuder wrote:thanks for your response. OK, so if I get this right, an INPUT/OUTPUT pin is really just an output pin where you can use a read operation on the same pin that will simply return the value you have written. It doesn't mean that you can read from an external device connected to that pin. Is this correct?
Well, if you have an external device driving that pin, then it will conflict with the ESP32 driver and cause electrical issues. So no, you wouldn't want to configure a pin as a push-pull (i.e. "typical") output and then try to drive it externally.

There's a type of output called an open-drain output that only drives in one direction - sometimes that can work with an external driver, in which case the pin can be configured as both input and output at the same time. You can even have multiple external drivers on such a pin, as long as they don't all try to drive at the same time.

Sometimes connections are bi-directional and you might want an external device to drive that pin sometimes, with the ESP32 driving it at other times. In that situation, you might have the pin set always as an input, but occasionally as an output too (when the ESP32 needs to drive it). When that happens, it's both input and output at the same time. When the ESP32 is not required to drive it, the pin is set as input only (i.e. output disabled) and that effectively disconnects the output branch.

I hope this makes sense? Maybe this helps?

https://stratifylabs.co/embedded%20desi ... put-Modes/

meowsqueak
Posts: 151
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: Q on gpio_set_direction()

Postby meowsqueak » Wed Apr 04, 2018 4:52 am

@apuder: I'm not sure how much you already know, but some background such as this this might help too: https://www.electronics-tutorials.ws/lo ... istor.html

My apologies if this is all old news to you.

apuder
Posts: 53
Joined: Mon Mar 26, 2018 7:27 pm

Re: Q on gpio_set_direction()

Postby apuder » Wed Apr 04, 2018 4:57 am

thanks a bunch. That all makes sense and confirms what I expected. I am trying to connect an ESP32 to the 8-bit I/O bus of a vintage Z80-based machine. The ESP needs to read and write to the same I/O bus. The Z80 indicates if it wants to read or write. Since the data bus is 8-bits wide, I need 8 GPIO pins. I guess I have no choice but to keep the 8 pins as input by default and whenever the Z80 indicates it wants to read, switch those 8 pins to output during the read cycle. I was looking for a way to efficiently make that switch from input to output and back again for all 8 pins. With the AVRs this is just one instruction but since the ESPs use several bits to configure one pin, it is a lot more code to run through. Since this happens for every single byte that the Z80 wants to read or write, I'm concerned about performance. Well, anyways, I think I know what to do. Thanks again for your explanations.

AP

Who is online

Users browsing this forum: Baidu [Spider] and 267 guests