Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By btidey
#92073 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.
User avatar
By mikekgr
#92074 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.