As a follow up to my post about ESP32 with PlatformIO and Arduino, in this post I present how to use PlatformIO with the Espressif IDF (or ESPIDF, for short) in conjunction with C++ and Unity as a UnitTesting framework.
It proved to be much more difficult to get this running than with the Arduino framework.
Here are our requirements for development:
- Support (hardware and framework independent) unit tests to be run on the local dev machine (aka
env:native). - Support hardware and framework specific unit tests to be run on the actual microcontroller.
Here are some similarities and differences between ESPIDF and Arduino:
- Again, the
nativeenvironment will be compiled via SYS2/Mingw64 whereas the microcontroller environments are compiled by the compilers provided by the PlatformIO toolchain. - ESPIDF uses
app_main()instead of thesetup()/loop()construct in Arduino. - ESPIDF by default creates a
main.cinstead of amain.cppfile. We therefore have to useextern "C" { }to unmangle the symbols in our code. - For whatever reason the use of
#ifdef __cpluscplusalways evaluated tofalseand was therefore not usable. Thus, I usedextern "C"unconditionally in the code. - To detect the ESPIDF framework, I used the
ESP_PLATFORMsymbol (instead of theARDUINOsymbol). - All framework dependent
cppandhlibfiles are guarded with#if defined(ESP_PLATFORM). - All test code (
test_embeddedandtest_native) has to be surrounded withextern "C"as well (only the code and certinaly not the#includes). - Classes and code in
lib_dirshould not be surrounded withextern "C". - Also, I pretty much moved all the code to
lib_dir, so themain.cppis essentially only a stub. - We have to manually enable exceptions to support
throwetc viabuild_flags:-fexceptions. - (not unit test related) Reading out GPIO to get the state of an LED always returns
0.
The final result can be found here.
Summary
Again, it is quite quirky to setup the development environment. Plus, I could not find a single example out in the wild (PlatformIO in conjunction with C++, Unity with embedded and native testing, ESPIDF).
In the end, I now have a working environment where I can hopefully do what I want to do: sending and receiving CAN messages via the TWAI interface. We will find out …
