espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.66k stars 7.29k forks source link

How do I convert a PlatformIO build environment to ESP-IDF? (IDFGH-8275) #9760

Closed RoscoeTheDog closed 1 year ago

RoscoeTheDog commented 2 years ago

I am using a ESP32-S2 variant for a project in PlatformIO. It seems like the support for this board definition via platformIO is always behind the latest ESPIDF framework updates which is frustrating. Right now it seems as though the basic example for the smartconfig feature no longer sends acknowledgements to the smartphone app. I am trying to rebuild the project purely in ESP-IDF using VScode and the esp-idf extension to see if I can get it working, but it uses a different build system than PlatformIO does and I can't build it.

I understand that CMake wants us to specify what is required dependencies for each component. This seems like a huge huge amount of trouble for me in my instance where I have like 10 headers and source files for each different peripheral on the board I am making use of previously in my platformIO project.

The simplist and sloppiest way I could think of getting everything migrated over was to throw the headers in the main ESP-IDF project folder and tell CMake to look at the same root path for headers like this:

idf_component_register(SRCS "main.c"
                    INCLUDE_DIRS ".")

main.c


/*
    Include C++ headers here.

    If you use C++ libraries, they should be included in this CPP scope OR explicitely wrapped as extern "C++" {} so the compiler knows what conventions to use
    and how to link them together.
*/

/*
    Tell the compiler to wrap main for C convention since most of the IDF framework in standard C but we may want to use C++ occasionally.
*/

#ifdef __cplusplus
extern "C" {
#endif
    // Include C-headers here.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <esp_timer.h>
    #include <freeRTOS/FreeRTOS.h>

    #include <configGpio.h>
    #include <configLedc.h>
    #include <configTimers.h>
    #include <configSteppers.h>
    #include <configWifi.h>
    #include <httpRequests.h>
    #include <esp_err.h>
    #include <string.h>
    #include <esp_pm.h>
    #include <esp_sleep.h>
    #include <esp_freertos_hooks.h>
    #include <freertos/task.h>
    #include <esp_sntp.h>
    #include <sys/time.h>
    #include <time.h>
    #include <globals.h>

    #include <cJSON.h>
    #include <cJSON_Utils.h>

    void app_main(void);

#ifdef __cplusplus
}
#endif

void app_main(void) {
    // configure the JSON library to use FREERTOS thread safe malloc / free methods
    cJSON_Hooks hooks;
    hooks.malloc_fn = pvPortMalloc;
    hooks.free_fn = vPortFree;
    cJSON_InitHooks(&hooks);

    // esp_pm_config_esp32s2_t cfg;
    // esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
    // cfg.light_sleep_enable = true;
    // cfg.max_freq_mhz = 240;
    // cfg.min_freq_mhz = 80;

    /* *************initialize peripherials and resources here****************/
    // configure gpio for peripiherials first
    vInitGpioConfig();                  // GPIO pin configuration
    // cold start go status red as soon as GPIO is initialized.
    setStatusLEDRed();
    // continue init...
    initialize_ledc_config_0();             // LED PWM generator
    vInitCurtainMotorConfig_0();        // Primary Motor Control    
    initializeTimerConfig();                // Button interrupts

    // pause for visual indication
    vTaskDelay(pdMS_TO_TICKS(2000));
    if (nvsRestoreSystemState() == ESP_OK) {
        // blink green twice if restored state sucessfully
        for (short i = 0; i < 2; ++i) {
            setStatusLEDGreen();
            vTaskDelay(pdMS_TO_TICKS(100));
            setStatusLEDRed();
            vTaskDelay(pdMS_TO_TICKS(100));
        }
        // flag sync will bypass the parsing of last command from server.
        SYS_SYNC = true;
    }
    vTaskDelay(pdMS_TO_TICKS(1000));

    // system hardware initialized sucessfully. go status yellow
    setStatusLEDYellow();

    // initialize rtos tasks
    initializeTasks();

    // pause for visual indication
    vTaskDelay(pdMS_TO_TICKS(2000));

    // status gets updated in wifi event handler
    initializeWifi();
}

OUTPUT:

[0/1] Re-running CMake...
-- Component directory C:/Users/admin/Documents/Github/ESP32-S2-Saola-Project/ESP32_S2_Saola1_ESPIDF/components/components does not contain a CMakeLists.txt file. No component will be added
-- ccache will be used for faster recompilation
-- Building ESP-IDF components for target esp32s2
-- Project sdkconfig file C:/Users/admin/Documents/Github/ESP32-S2-Saola-Project/ESP32_S2_Saola1_ESPIDF/sdkconfig
-- App "ESP32_S2_Saola1_ESPIDF" version: 0eda92a
-- Adding linker script C:/Users/admin/Documents/Github/ESP32-S2-Saola-Project/ESP32_S2_Saola1_ESPIDF/build/esp-idf/esp_system/ld/memory.ld
-- Adding linker script C:/Users/admin/esp/esp-idf/components/esp_system/ld/esp32s2/sections.ld.in
-- Adding linker script C:/Users/admin/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.ld
-- Adding linker script C:/Users/admin/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld
-- Adding linker script C:/Users/admin/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.libgcc.ld      
-- Adding linker script C:/Users/admin/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.newlib-funcs.ld
-- Adding linker script C:/Users/admin/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.newlib-data.ld 
-- Adding linker script C:/Users/admin/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.spiflash.ld   
-- Adding linker script C:/Users/admin/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.newlib-time.ld
-- Adding linker script C:/Users/admin/esp/esp-idf/components/soc/esp32s2/ld/esp32s2.peripherals.ld
-- Components: app_trace app_update asio bootloader bootloader_support bt cbor cmock coap console cxx driver efuse esp-tls esp32s2 esp_adc_cal esp_common esp_eth esp_event esp_gdbstub esp_hid esp_http_client esp_http_server esp_https_ota esp_https_server esp_hw_support esp_ipc esp_lcd esp_local_ctrl esp_netif esp_phy esp_pm esp_ringbuf esp_rom esp_serial_slave_link esp_system esp_timer esp_websocket_client esp_wifi espcoredump esptool_py expat fatfs freemodbus freertos hal heap idf_test ieee802154 jsmn json libsodium log lwip main mbedtls mdns mqtt newlib nghttp nvs_flash openssl openthread partition_table perfmon protobuf-c protocomm pthread sdmmc soc spi_flash spiffs tcp_transport tcpip_adapter tinyusb touch_element ulp unity usb vfs wear_levelling wifi_provisioning wpa_supplicant xtensa    
-- Component paths: C:/Users/admin/esp/esp-idf/components/app_trace C:/Users/admin/esp/esp-idf/components/app_update C:/Users/admin/esp/esp-idf/components/asio C:/Users/admin/esp/esp-idf/components/bootloader C:/Users/admin/esp/esp-idf/components/bootloader_support C:/Users/admin/esp/esp-idf/components/bt C:/Users/admin/esp/esp-idf/components/cbor C:/Users/admin/esp/esp-idf/components/cmock C:/Users/admin/esp/esp-idf/components/coap C:/Users/admin/esp/esp-idf/components/console C:/Users/admin/esp/esp-idf/components/cxx C:/Users/admin/esp/esp-idf/components/driver C:/Users/admin/esp/esp-idf/components/efuse C:/Users/admin/esp/esp-idf/components/esp-tls C:/Users/admin/esp/esp-idf/components/esp32s2 C:/Users/admin/esp/esp-idf/components/esp_adc_cal C:/Users/admin/esp/esp-idf/components/esp_common C:/Users/admin/esp/esp-idf/components/esp_eth C:/Users/admin/esp/esp-idf/components/esp_event C:/Users/admin/esp/esp-idf/components/esp_gdbstub C:/Users/admin/esp/esp-idf/components/esp_hid C:/Users/admin/esp/esp-idf/components/esp_http_client C:/Users/admin/esp/esp-idf/components/esp_http_server C:/Users/admin/esp/esp-idf/components/esp_https_ota C:/Users/admin/esp/esp-idf/components/esp_https_server C:/Users/admin/esp/esp-idf/components/esp_hw_support C:/Users/admin/esp/esp-idf/components/esp_ipc C:/Users/admin/esp/esp-idf/components/esp_lcd C:/Users/admin/esp/esp-idf/components/esp_local_ctrl C:/Users/admin/esp/esp-idf/components/esp_netif C:/Users/admin/esp/esp-idf/components/esp_phy C:/Users/admin/esp/esp-idf/components/esp_pm C:/Users/admin/esp/esp-idf/components/esp_ringbuf C:/Users/admin/esp/esp-idf/components/esp_rom C:/Users/admin/esp/esp-idf/components/esp_serial_slave_link C:/Users/admin/esp/esp-idf/components/esp_system C:/Users/admin/esp/esp-idf/components/esp_timer C:/Users/admin/esp/esp-idf/components/esp_websocket_client C:/Users/admin/esp/esp-idf/components/esp_wifi C:/Users/admin/esp/esp-idf/components/espcoredump C:/Users/admin/esp/esp-idf/components/esptool_py C:/Users/admin/esp/esp-idf/components/expat C:/Users/admin/esp/esp-idf/components/fatfs C:/Users/admin/esp/esp-idf/components/freemodbus C:/Users/admin/esp/esp-idf/components/freertos C:/Users/admin/esp/esp-idf/components/hal C:/Users/admin/esp/esp-idf/components/heap C:/Users/admin/esp/esp-idf/components/idf_test C:/Users/admin/esp/esp-idf/components/ieee802154 C:/Users/admin/esp/esp-idf/components/jsmn C:/Users/admin/esp/esp-idf/components/json C:/Users/admin/esp/esp-idf/components/libsodium C:/Users/admin/esp/esp-idf/components/log C:/Users/admin/esp/esp-idf/components/lwip C:/Users/admin/Documents/Github/ESP32-S2-Saola-Project/ESP32_S2_Saola1_ESPIDF/main C:/Users/admin/esp/esp-idf/components/mbedtls C:/Users/admin/esp/esp-idf/components/mdns C:/Users/admin/esp/esp-idf/components/mqtt C:/Users/admin/esp/esp-idf/components/newlib C:/Users/admin/esp/esp-idf/components/nghttp C:/Users/admin/esp/esp-idf/components/nvs_flash C:/Users/admin/esp/esp-idf/components/openssl C:/Users/admin/esp/esp-idf/components/openthread C:/Users/admin/esp/esp-idf/components/partition_table C:/Users/admin/esp/esp-idf/components/perfmon C:/Users/admin/esp/esp-idf/components/protobuf-c C:/Users/admin/esp/esp-idf/components/protocomm C:/Users/admin/esp/esp-idf/components/pthread C:/Users/admin/esp/esp-idf/components/sdmmc C:/Users/admin/esp/esp-idf/components/soc C:/Users/admin/esp/esp-idf/components/spi_flash C:/Users/admin/esp/esp-idf/components/spiffs C:/Users/admin/esp/esp-idf/components/tcp_transport C:/Users/admin/esp/esp-idf/components/tcpip_adapter C:/Users/admin/esp/esp-idf/components/tinyusb C:/Users/admin/esp/esp-idf/components/touch_element C:/Users/admin/esp/esp-idf/components/ulp C:/Users/admin/esp/esp-idf/components/unity C:/Users/admin/esp/esp-idf/components/usb C:/Users/admin/esp/esp-idf/components/vfs C:/Users/admin/esp/esp-idf/components/wear_levelling C:/Users/admin/esp/esp-idf/components/wifi_provisioning C:/Users/admin/esp/esp-idf/components/wpa_supplicant C:/Users/admin/esp/esp-idf/components/xtensa
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/admin/Documents/Github/ESP32-S2-Saola-Project/ESP32_S2_Saola1_ESPIDF/build
[1/10] Performing build step for 'bootloader'
[1/1] cmd.exe /C "cd /D C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build\bootloader\esp-idf\esptool_py && C:\Users\admin\.espressif\python_env\idf4.4_py3.8_env\Scripts\python.exe C:/Users/admin/esp/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 C:/Users/admin/Documents/Github/ESP32-S2-Saola-Project/ESP32_S2_Saola1_ESPIDF/build/bootloader/bootloader.bin"
Bootloader binary size 0x5060 bytes. 0x1fa0 bytes (28%) free.
[6/8] Linking CXX executable ESP32_S2_Saola1_ESPIDF.elf
FAILED: ESP32_S2_Saola1_ESPIDF.elf 
cmd.exe /C "cd . && C:\Users\admin\.espressif\tools\xtensa-esp32s2-elf\esp-2021r2-patch3-8.4.0\xtensa-esp32s2-elf\bin\xtensa-esp32s2-elf-g++.exe -mlongcalls  @CMakeFiles\ESP32_S2_Saola1_ESPIDF.elf.rsp 
-o ESP32_S2_Saola1_ESPIDF.elf  && cd ."
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x8): undefined reference to `SYS_SYNC'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x1c): undefined reference to `vInitGpioConfig'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x20): undefined reference to `setStatusLEDRed'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x24): undefined reference to `initialize_ledc_config_0'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x28): undefined reference to `vInitCurtainMotorConfig_0'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x2c): undefined reference to `initializeTimerConfig'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x30): undefined reference to `nvsRestoreSystemState'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x34): undefined reference to `setStatusLEDGreen'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x38): undefined reference to `setStatusLEDYellow'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x3c): undefined reference to `initializeTasks'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.app_main+0x40): undefined reference to `initializeWifi'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj): in function `app_main':
C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:72: undefined reference to `vInitGpioConfig'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:82: undefined reference to `setStatusLEDRed'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:84: undefined reference to `initialize_ledc_config_0'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:86: undefined reference to `vInitCurtainMotorConfig_0'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:87: undefined reference to `initializeTimerConfig'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:92: undefined reference to `nvsRestoreSystemState'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:93: undefined reference to `setStatusLEDGreen'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:97: undefined reference to `setStatusLEDRed'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:104: undefined reference to `setStatusLEDYellow'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:104: undefined reference to `initializeTasks'
c:/users/admin/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: C:\Users\admin\Documents\Github\ESP32-S2-Saola-Project\ESP32_S2_Saola1_ESPIDF\build/../main/main.c:117: undefined reference to `initializeWifi'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

 *  The terminal process "C:\Windows\System32\cmd.exe /d /c ninja " terminated with exit code: 1.
igrr commented 2 years ago

@RoscoeTheDog Could you please upload your project somewhere? As is, there is not enough information provided in this ticket for us to give you some help. It's not clear where the functions like initializeWifi are defined, whether they have C or C++ linkage, in which component they are located, and so on.

In general, you can also take a look at some of the example projects provided with ESP-IDF. They should demonstrate how to structure the project, splitting it into multiple components. https://github.com/espressif/esp-idf/tree/master/examples/system/console/advanced is one such example.

Alvin1Zhang commented 1 year ago

@RoscoeTheDog Thanks for reporting, would you please help share if any further updates for the issue? Thanks.

Alvin1Zhang commented 1 year ago

Thanks for reporting, will close due to short of feedback, feel free to reopen with more updates. Thanks.