Is it possible to use MISO as GPIO?


System: ESP32 with HSPI module

  1. My goal is: Use MISO (IO12) as a GPIO for another purpose
  2. My actions are: In config_schema the initialization parameters are
- ["spi.enable", true]
- ["spi.unit_no", 2]
- ["spi.mosi_gpio", 13]
- ["spi.miso_gpio", -1] #IO12 assigned as DC(GPIO) for LCD data/command 
- ["spi.sclk_gpio", 14]
- ["spi.cs0_gpio",  -1] #CS controlled by GPIO
- ["mydev", "o", {title: "mydev interface parameters"}]
- ["mydev.cs_index", "i", 0, {title: "spi.cs*_gpio index, 0, 1 or 2"}]
- ["mydev.spi_freq", "i", 40000000, {title: "SPI frequency"}]
- ["mydev.dc_pin", "i", 12, {title: "DC pin"}]
- ["mydev.cs_pin", "i", 15, {title: "CS pin"}]
- ["mydev.io26_pin", "i", 26, {title: "IO26 pin"}] #IO26 as a control experiment

IO12 configured as gpio in initialization section.

  1. The result I see is: IO12 is out of control. When I write a byte (e.g. 0x10) with MOSI, I try to output IO12 high but the result is like a clock output.
    Snippet below shows the
//private fuction
static void lpm013_spi_write8_data(uint8_t byte) {
    mgos_gpio_write(mgos_sys_config_get_mydev_dc_pin(), 1);
    mgos_gpio_write(mgos_sys_config_get_mydev_io26_pin(), 1);
    lpm013_spi_write(&byte, 1);

//...code to write a single byte 0x01 from mosi
lpm013_spi_cs_lo(); lpm013_spi_write8_data(0x10);lpm013_spi_cs_hi();

See Screen capture from a logic analyzer below

4. My expectation & question is:
I expected IO12, when it is not used as MISO it can be used for another purpose like DC by GPIO. It turns out that it is out of control.

Any idea why?


I have used GPIO12 as I/O on an ESP32 WROOM module just by using GPIO.set_mode(); in mJS.
Did you initialize the pin as an I/O ? Did you connect an oscilloscope ? perhaps you are just seeing noise.
Besides that, I’m not sure about the ‘-1’ there, it seems to be passed verbatim to the manufacturer setup routine, here, but I did not check further down the call chain.
Other thing to try is to assign it an unused (but selectable) pin for that same function. Did you enable debug and check the log ?


Thank you, scaprile.
It turns out that the logic analyzer was measuring the cross-talk noises. The code works too. Number ‘-1’ shows MISO by hardware is not used so it can be assigned as gpio.

Did you enable debug and check the log ?

How to do it?


yeah, logic analyzers are great for inventing nonexisting data… been there.
For “enable debug” I meant raising the log level, libraries log at different levels (here) and default logging level is 2. if you set the config key to 3, you will get lots of messages (but not as verbose as 4) telling you what is going on. Then you connect to the device with mos console (text) or just mos. If the library has enough logging built in, it might help. They usually do.
mos config-set debug.level=3 when connected or set it in your mos.yml - ["debug.level", 3]
Watch out for level 4, it is really verbose and might clog the pipe to a stall.