Read pin configuration on ESP32 or Arduino

Hello,

Is it possible to read a complete pin configuration (pull mode, direction, etc) by means of Arduino API? By ESP-IDF? I looked into ESP-IDF gpio_dump_io_configuration() but it uses some internal API & structures which are not accessible from user code without modifying and recompiling IDF.

I need this to implement "save pin" and "load pin" logic in my code. Need to be able to save full pin state and then restore it to configure the pin exactly as it was before

Thanks!

You could write a function to save the required pin configuration to EEPROM or Preferences and load it at startup

I think you would have to go to "bare metal."
For example, I did this code for Atmel/Microchip SAMD: Duino-hacks/SAMD_Explorer at master · WestfW/Duino-hacks · GitHub
And this one for the ARM core: Duino-hacks/ARM_Explorer/ARM_Explorer.ino at master · WestfW/Duino-hacks · GitHub

(although, IIRC, those were primarily exercises in turning ARM ".svd" files into C structures...)
I believe SAMD_Explorer was specifically structured to be able to 'add' to some sketch, though it was never made into a library like it ought to be.

I found the solution:. But it uses a low-level API which is often changed :frowning:

#include <soc/gpio_struct.h>
#include <hal/gpio_ll.h>

extern gpio_dev_t GPIO;

bool pd,      //PULLUP
pu,           //PULLDOWN
ie,           //INPUT
oe,           //OUTPUT
od,           //OPEN_DRAIN
slp_sel;      //sleep mode magic

uint32_t
 drv,          // drive capability
fun_sel,       // iomux & gpio matrix magic 
sig_out;       // iomux & gpio matrix magic 

// read pin 21 configuration
gpio_ll_get_io_config(&GPIO, 21 ,&pu, &pd, &ie, &oe, &od,&drv, &fun_sel, &sig_out, &slp_sel);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.