esp32_adc2gpio not declared in pins_arduino.h for M5StickCPlus



  • The file pins_arduino.h in the 2.0.5 board files for the M5Stick-C-Plus contains a macro definition that refers to an array esp32_adc2gpio that appears to be undefined and causes a compile error. I first ran into the problem when trying to compile a Blynk Quickstart example.

    Comparing this with the pins_arduino.h files for other M5Stack devices including Atom, Stick-C, and Core2, I noticed that they do not refer to esp32_adc2gpio, but instead call a function analogChannelToDigitalPin that compiles properly. Is this a bug for Stick-C-Plus and should it call the same function as the Stick-C and other devices? Any insights/comments would be appreciated!

    Here's a bunch more detail on what I've found.

    Here's the macro definition from the file packages\m5stack\hardware\esp32\2.0.5\variants\m5stick_c_plus\pins_arduino.h

    #define analogInputToDigitalPin(p)  (((p)<20)?(esp32_adc2gpio[(p)]):-1)
    

    Note that the same macro is also used in the pins_arduino.h files for several other device variants, including m5stack_paper and m5stack_tough.

    Here's a simple sketch that causes a compile error related that uses the macro:

    void setup() {
      analogInputToDigitalPin(5);
    }
    
    void loop() {
    }
    

    Here's the error I get:

    C:\Users\xxx\AppData\Local\Arduino15\packages\m5stack\hardware\esp32\2.0.5\variants\m5stick_c_plus/pins_arduino.h:10:48: error: 'esp32_adc2gpio' was not declared in this scope
     #define analogInputToDigitalPin(p)  (((p)<20)?(esp32_adc2gpio[(p)]):-1)
    

    When I searched the entire m5stack for esp32_adc2gpio using grep -r, it wasn't anywhere to be found.

    If I change the device to M5Stack-Atom, it compiles fine.

    I looked at the pins_arduino.h files for a few other device variants and saw that they use a different definition for the macro. For variants m5stack_atom and m5stick_c, the macro analogInputToDigitalPin is defined as:

    #define analogInputToDigitalPin(p)  (((p)<20)?(analogChannelToDigitalPin(p)):-1)
    

    The function analogChannelToDigitalPin is defined in packages\m5stack\hardware\esp32\2.0.5\cores\esp32\esp32-hal-gpio.c This function refers to another array called adc_channel_io_map that does appear to be defined in the esp32 library archive files according to grep:

    (base) PS C:\Users\xxx\Downloads\m5stack-2.0.5\m5stack-2.0.5> grep -r adc_channel_io_map .
    ./cores/esp32/esp32-hal-gpio.c:                if (adc_channel_io_map[i][j] == pin) {
    ./cores/esp32/esp32-hal-gpio.c:    return adc_channel_io_map[adc_unit][adc_chan];
    ./tools/sdk/esp32/include/soc/include/soc/adc_periph.h:extern const int adc_channel_io_map[SOC_ADC_PERIPH_NUM][SOC_ADC_MAX_CHANNEL_NUM];
    Binary file ./tools/sdk/esp32/lib/libdriver.a matches
    Binary file ./tools/sdk/esp32/lib/libhal.a matches
    Binary file ./tools/sdk/esp32/lib/libsoc.a matches
    

    Perhaps the version of the macro analogInputToDigitalPin for the Stick-C-Plus is obsolete and should replaced with the version that calls analogChannelToDigitalPin? Or maybe I'm missing something else?



  • I replaced the macro analogInputToDigitalPin(p) in the file packages\m5stack\hardware\esp32\2.0.5\variants\m5stick_c_plus\pins_arduino.h and it seemed to work:

    //#define analogInputToDigitalPin(p)  (((p)<20)?(esp32_adc2gpio[(p)]):-1)
    #define analogInputToDigitalPin(p)  (((p)<20)?(analogChannelToDigitalPin(p)):-1)
    

    I tested it with the following sketch, which printed the correct sequence of mappings (36, 37, 38, 39, 32, 33, 34, 35, -1, -1, 4, 0, 2, 15, 13, 12, 14, 27, 25, 26):

    #include <M5StickCPlus.h>
    void setup() {
      M5.begin();
      M5.Lcd.fillScreen(RED);
      for (uint8_t i = 0;  i < 20;  i++) {
        Serial.println(analogInputToDigitalPin(i));
      }
    }
    
    void loop() {
    }
    

    With this change, I was able to get the Blynk Quickstart example working with my M5StickCPlus, which is what led me down this rabbit hole in the first place!