1.项目根目录新建components文件夹

2.使用git拉取arduino

git clone https://github.com/espressif/arduino-esp32.git arduino

注意idf版本号和arduino版本号是否兼容

3.配置根目录CMakeLists.txt

# 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.5)


if(DEFINED ENV{ROOT_PARH})
  set(ROOT_PARH $ENV{ROOT_PARH})
else()
  set(ROOT_PARH ${CMAKE_CURRENT_LIST_DIR})
endif(DEFINED ENV{ROOT_PARH})
set(EXTRA_COMPONENT_DIRS
${ROOT_PARH}/components
${ROOT_PARH}/components/arduino)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(xxx)

4.添加main.cpp文件,默认为app_main.c

#include "Arduino.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"

extern "C" void app_main()
{
    initArduino();
    Serial.begin(115200);
    while (!Serial)
    {
        // 等待串口初始化完成
    }
    // pinMode(4, OUTPUT);
    // digitalWrite(4, HIGH);
    while (true)
    {
        Serial.print("Hello ");
        printf("World\n");
        ESP_LOGI("TAG", "Hello arduino");

        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

5.修改CMakeLists.txt

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

6.修改sdkconfig

#修改为1000
#CONFIG_FREERTOS_HZ=100
CONFIG_FREERTOS_HZ=1000