Framework = espidf, arduino

When I want to use framework = espidf, arduino

How do I add/use espidf libraries in Arduino frame work?

Do I just need to add a appropriate .h file and that is it?

How to configure the compiler?

I am planning to switch to platform io from esp idf, can anyone post a step by step guide to use ESP32 Arduino core as a component of ESP-IDF

Documentation is available on the Espressif32 platform, the ESP-IDF framework (including how to add ESP-IDF components / libraries to the project. Also an ESP-IDF + Arduino example is available.

1 Like

Hello there @maxgerhardt ,

I’ve read a ton of posts in this issue (using Arduino as a component for ESP-IDF framework) and it seems the best place to start is with pio examples, but even trying to compile it ‘as-is’ is giving me some trouble.

I’m trying to build the espidf-arduino-blink example available from platformio and it cannot find Arduino.h, do you have any recommendations about that?

Edit: If I use platform-espressif32/examples/espidf-arduino-wifiscan at develop · platformio/platform-espressif32 · GitHub as an example, will I still be able to use menuconfig via ‘pio run -t menuconfig’?

Thank you.

yes, you can use ‘pio run -t menuconfig’

@kherrmann ,

Sure, but I still cannot compile it due do Arduino.h not being found :frowning:

What is the exact platformio.ini being used here?

[platformio]
default_envs = debug

[env]
platform = espressif32
framework = arduino, espidf
platform_packages =
  ; use a special branch
  framework-arduinoespressif32 @ https://github.com/marcovannoord/arduino-esp32.git#idf-release/v4.0
board = featheresp32
upload_protocol = esptool
upload_port = /dev/ttyUSB_ESP32_Feather
upload_speed = 115200
monitor_port = /dev/ttyUSB_ESP32_Feather
monitor_speed = 115200

			
[env:release]
build_type = release
build_flags = -DBOARD_HAS_PSRAM
	-mfix-esp32-psram-cache-issue
	-O2

[env:debug]
build_type = debug
debug_init_break = tbreak app_main
debug_tool = esp-prog
build_flags = -DBOARD_HAS_PSRAM
	-mfix-esp32-psram-cache-issue
	-Wall -Wno-unused-but-set-variable -Wno-unused-variable
	-O0 -g

The platformio.ini is as follows:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200

The components folder in my project contains the GitHub - espressif/arduino-esp32: Arduino core for the ESP32 repository.

Edit: I’m getting the following error as I try to run ‘pio run -t menuconfig’

Running menuconfig...
[0/1] Re-running CMake...
fatal: not a git repository (or any of the parent directories): .git
-- Project is not inside a git repository, or git repository has no commits; will not use 'git describe' to determine PROJECT_VER.
-- Building ESP-IDF components for target esp32
CMake Error at C:/Users/IOT/.platformio/packages/framework-espidf/tools/cmake/build.cmake:186 (message):
  Failed to resolve component 'main'.
Call Stack (most recent call first):
  C:/Users/IOT/.platformio/packages/framework-espidf/tools/cmake/build.cmake:217 (__build_resolve_and_add_req)
  C:/Users/IOT/.platformio/packages/framework-espidf/tools/cmake/build.cmake:426 (__build_expand_requirements)
  C:/Users/IOT/.platformio/packages/framework-espidf/tools/cmake/project.cmake:395 (idf_build_process)
  CMakeLists.txt:3 (project)


-- Configuring incomplete, errors occurred!
See also "C:/Users/IOT/Documents/PlatformIO/Projects/helloworld-espidf/.pio/build/esp32dev/CMakeFiles/CMakeOutput.log".
FAILED: build.ninja
C:\Users\IOT\.platformio\packages\tool-cmake\bin\cmake.exe -SC:\Users\IOT\Documents\PlatformIO\Projects\helloworld-espidf -BC:\Users\IOT\Documents\PlatformIO\Projects\helloworld-espidf\.pio\build\esp32dev
ninja: error: rebuilding 'build.ninja': subcommand failed
Error: Couldn't execute 'menuconfig' target.
scons: *** [menuconfig] Explicit exit, status 1

Edit 2: By the way, the espidf-arduino-blink example compiles and flashes, but gives a similar error as described above when I try to run ‘pio run -t menuconfig’.

I’ve seen this error when a CMakeLists.txt requires the main component. An coincidentally…

this component is created when the main source folder is called main. PlatformIO calls it src by default (and thus there is a component called src). Try renaming the src folder of the project to main and do a clean build / retry menuconfig, or modify the CMakeLists.txt of Arduino-ESP32 locally in the above referenced line.

1 Like

Hello there,

So, I tried both solutions:

When renaming the src folder to main, the compiler no longer knew how to compile the project, even after running pio -t clean.

When I tried to change the line #164 to src instead of main, the following error has shown:

PS C:\Users\IOT\Documents\PlatformIO\Projects\helloworld-espidf> pio run -t build
Processing esp32dev (platform: espressif32; board: esp32dev; framework: espidf)
----------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via -v, --verbose option
CONFIGURATION: Redirecting...
PLATFORM: Espressif 32 (3.2.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:

  • framework-espidf 3.40200.210118 (4.2.0)
  • tool-cmake 3.16.4
  • tool-esptoolpy 1.30000.201119 (3.0.0)
  • tool-idf 1.0.1
  • tool-mconf 1.4060000.20190628 (406.0.0)
  • tool-ninja 1.9.0
  • toolchain-esp32ulp 1.22851.191205 (2.28.51)
  • toolchain-xtensa32 2.80400.210211 (8.4.0)
    Reading CMake configuration…
    Generating assembly for certificate bundle…
    LDF: Library Dependency Finder → Library Dependency Finder (LDF) — PlatformIO latest documentation
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 0 compatible libraries
    Scanning dependencies…
    No dependencies
    Building in release mode
    *** Do not know how to make File target `build’ (C:\Users\IOT\Documents\PlatformIO\Projects\helloworld-espidf\build). Stop.

Maybe there is another change that should be made?

Edit: When clicking the build icon instead of running through CLI then it seems some components no longer built:

In file included from components\arduino\cores\esp32\esp32-hal.h:74,
from components\arduino\cores\esp32\esp32-hal-adc.h:27,
from components\arduino\cores\esp32\esp32-hal-adc.c:15:
components\arduino\cores\esp32\esp32-hal-gpio.h:78:44: error: ‘SOC_GPIO_PIN_COUNT’ undeclared here (not in a function); did you mean ‘GPIO_PIN_COUNT’?
extern const esp32_gpioMux_t esp32_gpioMux[SOC_GPIO_PIN_COUNT];
^~~~~~~~~~~~~~~~~~
GPIO_PIN_COUNT
In file included from components\arduino\cores\esp32\esp32-hal.h:74,
from components\arduino\cores\esp32\esp32-hal-bt.h:18,
from components\arduino\cores\esp32\esp32-hal-bt.c:15:
components\arduino\cores\esp32\esp32-hal-gpio.h:78:44: error: ‘SOC_GPIO_PIN_COUNT’ undeclared here (not in a function); did you mean ‘GPIO_PIN_COUNT’?
extern const esp32_gpioMux_t esp32_gpioMux[SOC_GPIO_PIN_COUNT];
^~~~~~~~~~~~~~~~~~
GPIO_PIN_COUNT
*** [.pio\build\esp32dev\components\arduino\cores\esp32\esp32-hal-bt.o] Error 1
*** [.pio\build\esp32dev\components\arduino\cores\esp32\esp32-hal-adc.o] Error 1
In file included from components\arduino\cores\esp32\esp32-hal.h:74,
from components\arduino\cores\esp32\Arduino.h:36,
from components\arduino\cores\esp32\base64.cpp:25:
components\arduino\cores\esp32\esp32-hal-gpio.h:78:44: error: ‘SOC_GPIO_PIN_COUNT’ was not declared in this scope
extern const esp32_gpioMux_t esp32_gpioMux[SOC_GPIO_PIN_COUNT];
^~~~~~~~~~~~~~~~~~
components\arduino\cores\esp32\esp32-hal-gpio.h:78:44: note: suggested alternative: ‘GPIO_PIN_COUNT’
extern const esp32_gpioMux_t esp32_gpioMux[SOC_GPIO_PIN_COUNT];
^~~~~~~~~~~~~~~~~~
GPIO_PIN_COUNT
*** [.pio\build\esp32dev\components\arduino\cores\esp32\base64.o] Error 1
In file included from components\arduino\cores\esp32\esp32-hal.h:74,
from components\arduino\cores\esp32\esp32-hal-cpu.c:25:
components\arduino\cores\esp32\esp32-hal-gpio.h:78:44: error: ‘SOC_GPIO_PIN_COUNT’ undeclared here (not in a function); did you mean ‘GPIO_PIN_COUNT’?
extern const esp32_gpioMux_t esp32_gpioMux[SOC_GPIO_PIN_COUNT];
^~~~~~~~~~~~~~~~~~
GPIO_PIN_COUNT
components\arduino\cores\esp32\esp32-hal-cpu.c: In function ‘calculateApb’:
components\arduino\cores\esp32\esp32-hal-cpu.c:151:21: error: ‘MHZ’ undeclared (first use in this function)
return 80 * MHZ;
^~~
components\arduino\cores\esp32\esp32-hal-cpu.c:151:21: note: each undeclared identifier is reported only once for each function it appears in
components\arduino\cores\esp32\esp32-hal-cpu.c: In function ‘setCpuFrequencyMhz’:
components\arduino\cores\esp32\esp32-hal-cpu.c:226:46: error: ‘MHZ’ undeclared (first use in this function)
esp_timer_impl_update_apb_freq(apb / MHZ);
^~~
components\arduino\cores\esp32\esp32-hal-cpu.c: In function ‘calculateApb’:
components\arduino\cores\esp32\esp32-hal-cpu.c:155:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
cc1.exe: some warnings being treated as errors
*** [.pio\build\esp32dev\components\arduino\cores\esp32\esp32-hal-cpu.o] Error 1

Thanks in advance.

Oh actually there’s a second part to that, add

src_dir = main

in the platformio.ini (docs). But I guess that just leads to the second compilation error.

Is build a valid target? Building is just done with pio run, no arguments.

There may be a mismatched between the ESP-IDF used in the project and the ESP-IDF version the Arduino-ESP32 core is compatible with in the version you downloaded into the project.

is what PIO uses. What Arduino-ESP32 version are you using?

Sure, I will try that and see if it leads to the second compilation error.

I butchered the command over there, but then I used the build button on the left-hand side to do it, it should do the build the same as if I ran a CLI command, right?

I believe I’m using the latest Arduino-ESP32, since I just cloned the github repo at its master branch: GitHub - espressif/arduino-esp32: Arduino core for the ESP32.

If I read the commit messages correctly (example), it’s currently referring to literally the latest commit in the master branch of ESP-IDF at this time, see here. So ESP-IDF v4.2 is too old for that and one would need to go through the troubles of teaching PlatformIO to use the latest bleeding-edge ESP-IDF version. A start in that would be to fork ESP-IDF, put a package.json in it (based off <user home>\.platformio\packages\framework-espidf\package.json with updated version info) and platform_packages refering to that fork as platform_packages = framework-espidf @ <git fork link>. However, if the current ESP-IDF builder integration code does not work for bleeding-edge ESP-IDF, this may not work without additional adjustments.

As a quick test you should try and replace the Arduino-ESP32 component with https://github.com/marcovannoord/arduino-esp32/archive/refs/heads/idf-release/v4.2.zip, a branch that archives a version targeted for ESP-IDF v4.2, which is what PlatformIO is using.

1 Like

I’ve performed this quick test, unfortunately the exact same error occurs :frowning:

The same error being:

from components\arduino\cores\esp32\esp32-hal-adc.c:15:
components\arduino\cores\esp32\esp32-hal-gpio.h:78:44: error: ‘SOC_GPIO_PIN_COUNT’ undeclared here (not in a function); did you mean ‘GPIO_PIN_COUNT’?
extern const esp32_gpioMux_t esp32_gpioMux[SOC_GPIO_PIN_COUNT];

I think my request may be simpler, since I want for the moment to just build a blink project, so it’s far from the need of bleeding-edge ESP-IDF.

I’ve seen quite a lot of issues and topics about working with Arduino as an ESP-IDF component, which is basically what I want to do.

As I said before, I managed to build the espidf-arduino-blink examples but I know for sure that in the future I’ll need to add modifications to the sdk (to work with AWS OTA) and the precompiled ESP-IDF won’t suffice.

I’ll keep trying to see what I find, if there’s any direction you could point me to, I’d be very grateful.

But, if you have managed to get that example working, you do have the ESP-IDF part configurable already. That is basically ESP-IDF plus Arduino as a component.

I’ve looked further into this and it is definitely a mismatch of Arduino-ESP32 with ESP-IDF. In the used ESP-IDF version, the macro SOC_GPIO_PIN_COUNT does not exist, only GPIO_PIN_COUNT. One can get it to build further by adding

build_flags =
   -DSOC_GPIO_PIN_COUNT=GPIO_PIN_COUNT
   -DMHZ=1000000

but then LitteFS breaks the compilation with a non-found header file. Given that there was this recent commit which says that it fixes building Arduino-ESP32 as a component, I will be focusing on getting the latest ESP-IDF and Arduino-ESP32 working instead.

EDIT: I’ve made some progress with using the latest Arduino-ESP32 and ESP-IDF version, but PlatformIO’s linking logic fails at the very end of the compilation. Due to ESP-IDF shuffling around linker fragment files in esp32: move common fragment definitions · espressif/esp-idf@bbc5994 · GitHub and esp32: move system libs · espressif/esp-idf@a202a60 · GitHub the logic in

fails.

I’m currently trying a variety of things with

    result["lf_files"].extend(
        [
            os.path.join( FRAMEWORK_DIR, "components", "esp_common", "common.lf"),
            os.path.join( FRAMEWORK_DIR, "components", "esp_common", "soc.lf"),
            os.path.join(FRAMEWORK_DIR, "components", "newlib", "newlib.lf"),
            os.path.join(FRAMEWORK_DIR, "components", "newlib", "system_libs.lf")
        ]
    )

but it won’t work yet, with that esptool.py fails.

1 Like

Hello there @maxgerhardt ,

It seems you’re really going the extra mile for that one, thank you for that!

I managed to compiled Arduino as ESP-IDF component, but I’m not sure if it is the correct way of doing it, anyways, I think it would be a good thing to share on this topic.

So after googling around I found this Github issue: Successfully built arduino-esp32 as component on esp-idf v4.2 release · Issue #4702 · espressif/arduino-esp32 · GitHub and decided to give it a try, so I started follwing these steps arduino-esp32/docs/esp-idf_component.md at mod-idf-v4.2 · summivox/arduino-esp32 · GitHub.

Since I’m using Windows, make menuconfig does not work, I had to use these instead

cd %userprofile%\esp\hello_world
idf.py set-target esp32
idf.py menuconfig

So with the Arduino component inside the hello-world ESP-IDF sample project I managed to build it inside ESP-IDF, I still had to move it to platformio. What I did then was placing a platformio.ini file inside the project root folder and then running ‘pio run -t menuconfig’ to set the AUTOSTART ARDUINO parameter.

I also had to change the main folder from ‘main’ to ‘src’ and change the base CMakeLists.txt to register the source code file (changed from hello_world_main.c to main.cpp).

Then after a quite long build process I managed to compile Arduino as a ESP-IDF component inside platformio, and I’m quite happy about that!

I already built a simple serial loop code, but I’ll try adding some other code to see if I can progress any further.

Thanks.

Interesting, that one uses --branch mod-idf-v4.2 https://github.com/summivox/arduino-esp32.git though from a fork and lags behind main Arduino-ESP32 by 176 commits – a slightler older version but if it works, okay. I’ll see if I can get mainline Arduino-ESP32 running which requires a bleeding edge ESP-IDF.

1 Like

Hello there @maxgerhardt,

Any progress towards getting Arduino-ESP32 and bleeding edge ESP-IDF together?

By the way, I took some time to learn the very basics of CMake, it’s helping me a lot with these ‘adaptations’ from one framework to the other as well as better understanding ESP-IDF itself, I’d recommend to fellow members of the pio community to do such as well, because it helped me tremendously being a beginner as I am!

So, my next step is to download an Amazon FreeRTOS sample from AWS website (which downloads a fraction of FreeRTOS as opposed to cloning the github repo) and integrate it with this ESP-IDF+Arduino as a component program.

I expect to find a lot of mismatches and cmake related stuff to do in order to integrate both codes, but I’ll keep my progress posted.

Thank you.

Support for ESP-IDF v4.3 was recently released (Releases · platformio/platform-espressif32 · GitHub) with the accompanying changes to the builder script – maybe those are already the changes needed to get it working for also v4.4. I’ll test if I can find time.