3

I am using the ESP32 Arduino Framework and I have some GPIO expanders attached via I²C. They work fine and I now want to map some unused pin numbers to them so I can used my custom pin numbers in pinMode, digitalRead and digitalWrite.

Luckily, the Arduino core links these functions weakly.

extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias("__pinMode")));

I defined my own functions like so:

void pinMode(uint8_t pin, uint8_t mode) {
    if(pin<0x80)
        // call conventional handler
    else
        // communicate with GPIO expander via I²C
}

Unfortunately, calling any of the functions will not result in the linker picking my implementation but the linker picks the default implementation.

Any ideas how to override the weakly linked pinMode, digitalWrite and digitalRead?

Ilka
  • 69
  • 6

1 Answers1

3

I found the issue: I ran platformio run --target clean instead of platformio run --target cleanall. It seems that only the latter command cleans all build artifacts. The next time I compiled and uploaded, it worked as expected.

Another issue occurs only with Adafruit libraries: Some libraries do not use uint8_t for ports but int8_t and they do checks like

if(pin_number >=0)  {
    // code that only works with pin numbers between 0x01 and 0x80
}

Take the Adafruit GFX library as an example. As a workaround, I will map my GPIO expander ports to 0x40, 0x41, ...

Ilka
  • 69
  • 6