Skip to content

Fix linking to make initVariant() work #6809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2022
Merged

Conversation

maxgerhardt
Copy link
Contributor

@maxgerhardt maxgerhardt commented May 26, 2022

Description of Change

The builder script compiles the sources of the variant folder to a .a library (env.BuildLibary()) which is then linked into the final .elf firmware in the order <variant .a> <core .a>.

-Wl,--start-group .pio\build\adafruit_feather_esp32_v2\libFrameworkArduinoVariant.a .pio\build\adafruit_feather_esp32_v2\libFrameworkArduino.a

This however prevents the _weak initVariant() function to be linked properly and be executed. This effectively means that the initVariant() code of some boards, which is very critical for e.g. powering up the I2C bus or Neopixels on some Adafruit boards is not executed, and so the same sketch code behaves differently between the Arduino IDE and PlatformIO. This was noticed in the I2C case in platformio/platform-espressif32#815.

The Arduino IDE links the variant sources as an object file as can be seen in the verbose compile commands

-Wl,--start-group "C:\Users\Max\AppData\Local\Temp\arduino_build_77852\sketch\sketch_may26a.ino.cpp.o" "C:\Users\Max\AppData\Local\Temp\arduino_build_77852\core\variant.cpp.o" "C:\Users\Max\AppData\Local\Temp\arduino_build_77852\core\core.a"

With this fix applied, i.e., using env.BuildSources() instead of env.BuildLibrary(), the PlatformIO linker process now uses object files too

.pio\build\adafruit_feather_esp32_v2\FrameworkArduinoVariant\variant.cpp.o .pio\build\adafruit_feather_esp32_v2\src\main.cpp.o [...] -Wl,--start-group .pio\build\adafruit_feather_esp32_v2\libFrameworkArduino.a

And the initVariant() function is called again properly.

Tests scenarios

I have tested my Pull Request on Arduino-esp32 core v2.0.3 with configuration

[env:adafruit_feather_esp32_v2]
platform = espressif32
board = adafruit_feather_esp32_v2
framework = arduino
monitor_speed = 115200
board_upload.flash_size = 4MB

on a regular ESP32Dev board. The code

#include <Arduino.h>

void setup() {
    Serial.begin(115200);
}

void loop() {
    Serial.print("I2C power pin state: ");
    Serial.println(digitalRead(NEOPIXEL_I2C_POWER));
    delay(3000);
}

prints "0" before the fix, indicating that the initVariant code for this boardw as not executed since the Neopixel I2C power pin is still at LOW.

Prints "1" after the fix, indicating initVariant() is now properly called and the pin is now HIGH.

Related links

Actually resolves issue platformio/platform-espressif32#815.

@me-no-dev
Copy link
Member

Thanks @maxgerhardt :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants