ESP-IDF

ESP-IDF 是乐鑫官方提供的物联网开发框架,该框架基于 C/C++ 语言提供统一的编程接口,适用于乐鑫 ESP32、ESP32-S 和 ESP32-C 全系列 SoC。

Note

ESP-IDF是国内芯片领域,厂商提供SDK的一个模范,经历长期迭代已经非常成熟,是其芯产品的核心竞争力之一

Chip

Architecture

CoreMark

SRAM/ROM

Wireless

ETH/USB/CAN

Audio IF

ESP32

Xtensa LX6

994.2(2)

520KB/448KB

WiFi&BT4.2

100Mbps/X/√

I2S(2)/DAC/BT

ESP32-S2

Xtensa LX7

613.8

320KB/128KB

WiFi

X /1.1 OTG/ √

I2S/DAC

ESP32-S3

Xtensa LX7

1181.6(2)

512KB/384KB

WiFi&BLE5.0

X /1.1 OTG/ √

I2S(2)

ESP32-C3

RISC-V

407.22

400KB/384KB

WiFi&BLE5.0

X / X / √

I2S

ESP32-C2

RISC-V

272KB/576KB

WiFi&BLE5.0

X / X / √

I2S

ESP32-H2

RISC-V

400KB/384KB

Matter&BLE5.2

X / X / √

I2S

各芯片对比

../_images/idf-v.png ../_images/support-periods.svg

Hint

ESP-IDF 对于开发者而言,最大的痛点在于更新快版本多,对于git、Linux等技能要求高,初学的难度高,掌握后需要持续跟进。

ESP-IDF v5.0

ESP32-C2 ESP32-H2

  • MicroPython 开始支持使用最新的 IDF v5 构建 ESP32

ESP-IDF v4.4

ESP32-S3

安装配置

ESP-IDFv4.4 和之前的版本相比,占用的heap空间更大

ESP-IDF v4.3

ESP32-C3

安装配置

The general project configuration (default optimization level, bootloader configuration partition tables, etc) is set in a single file called sdkconfig in the root folder of the project. This configuration file can be modified via a special target called menuconfig (PlatformIO v4.3.0 greater is required):

pio run -t menuconfig

Hint

If menuconfig is not showed properly in the integrated VS Code terminal try changing the default terminal shell by clicking the dropdown menu on the top-right of the terminal panel and selectiing the Select Default Shell option.

Project Structure

The ESP-IDF framework requires an unusual project structure because most of the framework configuration is performed by the native for the ESP-IDF build system called CMake.

A typical PlatformIO project for the ESP-IDF framework must have the following structure:

project_dir
├── include
├── src
│    ├── CMakeLists.txt
│    └── main.c
├── CMakeLists.txt
└── platformio.ini

.. code-block:: ini

    [platformio]
    src_dir = main

    [env:esp32dev]
    platform = espressif32
    framework = espidf
    board = esp32dev
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16.0)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(project-name)
idf_component_register(SRCS "foo.c" "bar.c")

The files specified using idf_component_register are used ONLY for generating build configurations, but it’s highly recommended to specify all application source files in order to keep the project compatible with the usual ESP-IDF workflow.

Warning

By default PlatformIO expects source files to be located in the src folder. At the same time, the default location for source files within the ESP-IDF build system is a special folder with the name main. Renaming the main component may require users to manually specify additional dependencies:

idf_component_register(SRCS "main.c" REQUIRES idf::mbedtls)

More details in the official ESP-IDF documentation - Renaming main component.

Due to the current limitations of CMake file-based API, there is no way of generating build configuration for source files written in various programming languages if they are not specified in idf_component_register command. If your project contains libraries written in languages that differ from the language used for the main application you need to create an empty file with the desired extension (e.g. *.cpp for C++) in order to force CMake generate build configuration for this language.

Note

Build configuration generated for source files specified in idf_component_register is also used as the base build environment for project sources (including libraries).

ULP coprocessor programming

If you want to take measurements using ADC, internal temperature sensor or external I2C sensors, while the main processors are in deep sleep mode you need to use ULP coprocessor. At the moment ULP can be used only with the ESP-IDF.

All ULP code, usually written in assembly in files with .S extension, must be placed into a separate directory with the name ulp in the root folder of your project. So your project structure should look like this:

project_dir
├── include
├── src
│    ├── CMakeLists.txt
│    └── main.c
├── ulp
│    └── ulp_code.S
├── CMakeLists.txt
└── platformio.ini

Since PlatformIO uses the code model generated by CMake it’s mandatory to specify ULP source files in CMakeLists.txt as well. An example of typical CMakeLists.txt for ULP:

idf_component_register(SRCS "ulp_adc_example_main.c")
#
# ULP support additions to component CMakeLists.txt.
#
# 1. The ULP app name must be "ulp_main"
set(ulp_app_name ulp_main)
#
# 2. Specify all assembly source files.
#    Paths are relative because ULP files are placed into a special directory "ulp"
#    in the root of the project
set(ulp_s_sources "../ulp/adc.S")
#
# 3. List all the component source files which include automatically
#    generated ULP export file, ${ulp_app_name}.h:
set(ulp_exp_dep_srcs "ulp_adc_example_main.c")
#
# 4. Call function to build ULP binary and embed in project using the argument
#    values above.
ulp_embed_binary(${ulp_app_name} ${ulp_s_sources} ${ulp_exp_dep_srcs})

See full examples with ULP coprocessor programming:

More details are located in the official ESP-IDF documentation - ULP coprocessor programming.

Limitations

At the moment several limitations are present:

  • No whitespace characters allowed in project paths. This limitation is imposed by the native ESP-IDF build system. This affects users that have a whitespace in their username or added a whitespace to the project name. As a workaround. For example:

    [platformio]
    core_dir = C:/.platformio
    
    [env:esp32dev]
    platform = espressif32
    framework = espidf
    board = esp32dev
    
  • The src_filter option cannot be used. It’s done to preserve compatibility with existing ESP-IDF projects. List of source files is specified in the project CMakeLists.txt file.

工程示例

examples
├── bluetooth
│    ├── bluedroid
│    ├── blufi
│    ├── esp_ble_mesh
│    ├── esp_hid_device
│    ├── esp_hid_host
│    ├── hci
│    └── nimble
├── mesh
│    ├── internal_communication
│    ├── ip_internal_network
│    └── manual_networking
└── README.md

bluedroid