-->
Page 1 of 1

How to get the pinmode of a gpio

PostPosted: Sat Aug 14, 2021 2:25 am
by mikekgr
Dear Sirs,
Is there any way to get the current pinmode of a gpio pin in ESP8266 using Arduino IDE?
Your help will be appreciated. Thanks.

Re: How to get the pinmode of a gpio

PostPosted: Sat Aug 14, 2021 6:59 am
by btidey
The short answer is no.

The longer one is that it depends on what you are trying to achieve.

pinMode manipulates several hardware registers to achieve what is specified in the mode. These include registers that control the pin muxing, function, direction and items like pull up/down, open drain etc.

code is in core_esp8266_wiring_digital.cpp

So when you issue a pinMode call then these registers are set appropriately but there is nothing directly remembering the mode associated with that pin.

In addition GPIO16 is special and has a more limited set of possible modes.

In principle one could write a routine that interpreted all the registers associated with a pin and figure out what mode that pin had been set to but it would be quite complex.

So there are a couple of suggestions depending on what you are trying to achieve.

If it is something simple like figuring out whether a pin is input or output then that can be achieved fairly simply by looking at the GPE (enable register).

If you really want to retrieve the full mode then the simplest way is to write a shell function (e.g. pinModeEx) that stores the mode for a pin in an array and calls the standard pinMode function.

Another thing to take from this is that if you are manipulating pinMode to say switch a pin from input to output repeatedly then there can be some benefit in setting it once via pinMode and then doing further switches by using the GPE register directly. This is much faster than going through the complex logic in pinMode. This is particularly beneficial if muxing a pin from input to output in an interrupt service routine.

Re: How to get the pinmode of a gpio

PostPosted: Sat Aug 14, 2021 7:21 am
by mikekgr
Dear @btidey
many thanks for your detailed answer. Actually what I want to achieve is to share the same GPIO pin between DHT22 peripheral and as led driving pin (to turn on or off a LED when is not used for the communication with DHT22). I am not giving you more details because are irelevant ... I understood your "way to go" approach but on my software skills I can't implement it.
Anyway, many thanks again.

btidey wrote:The short answer is no.

The longer one is that it depends on what you are trying to achieve.

pinMode manipulates several hardware registers to achieve what is specified in the mode. These include registers that control the pin muxing, function, direction and items like pull up/down, open drain etc.

code is in core_esp8266_wiring_digital.cpp

So when you issue a pinMode call then these registers are set appropriately but there is nothing directly remembering the mode associated with that pin.

In addition GPIO16 is special and has a more limited set of possible modes.

In principle one could write a routine that interpreted all the registers associated with a pin and figure out what mode that pin had been set to but it would be quite complex.

So there are a couple of suggestions depending on what you are trying to achieve.

If it is something simple like figuring out whether a pin is input or output then that can be achieved fairly simply by looking at the GPE (enable register).

If you really want to retrieve the full mode then the simplest way is to write a shell function (e.g. pinModeEx) that stores the mode for a pin in an array and calls the standard pinMode function.

Another thing to take from this is that if you are manipulating pinMode to say switch a pin from input to output repeatedly then there can be some benefit in setting it once via pinMode and then doing further switches by using the GPE register directly. This is much faster than going through the complex logic in pinMode. This is particularly beneficial if muxing a pin from input to output in an interrupt service routine.