How to use the Arduino-ESP32 Library as an ESP-IDF Component

这篇具有很好参考价值的文章主要介绍了How to use the Arduino-ESP32 Library as an ESP-IDF Component。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Related Documentation

  • arduino-esp32 SDK
  • ESP-IDF SDK
  • ESP-IDF Environment Setup Guide
  • Arduino Environment Setup Guide
  • Arduino as an ESP-IDF component

Prepare Environment

Currently, the latest Master version of the arduino-esp32 SDK requires the usage of ESP-IDF SDK environment version v4.4.

  • For the different versions of the arduino-esp32 SDK and their corresponding ESP-IDF SDK versions, please refer to the “ESP32 Arduino Release” documentation.
  • You can find the released versions of the arduino-esp32 SDK under the arduino-esp32 SDK management directory as follows:
    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件
  • ESP-IDF Compilation Environment
    • If you are using the Windows environment, setting up the ESP-IDF SDK compilation environment is straightforward. You only need to use the offline version of the “ESP-IDF Windows Installer” to install the required ESP-IDF SDK version.

      For detailed instructions, you can refer to the guide titled “Set up the ESP-IDF SDK compilation environment + Visual Studio Code software programming environment” Additionally, you may find a video tutorial “Setting Up ESP-IDF Development Environment (Windows) Using One-Click Installation Tool” helpful.

    • If you are using the Ubuntu environment, please refer to the “Standard Setup of Toolchain for Linux” documentation for instructions. You can also refer to the “How to set up the software development environment ESP-IDF for ESP32-S3” guide.


Next, we will demonstrate how to use the arduino-esp32 library as an ESP-IDF SDK component on a Windows environment. This includes:

  • Using the arduino-esp32 library as a component in the project
  • Using the arduino-esp32 library as a component in the ESP-IDF SDK libraries

1、 Using the arduino-esp32 library as a component in the project:

  • Create a custom project
  • Create a component folder for the current project
  • Clone the arduino-esp32 library as a component for the current project
  • Make modifications to the project file names
  • Make modifications to the project configuration options
  • Compile and flash the current project for testing

1.1 Creating a custom project:

You can based on the ESP-IDF SDK to copy a project for testing. For example, copy the hello-world project. and rename the project name as hello-world_Arduino.
How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

1.2 Create a component folder for the current project

You can use the following command to create a component folder for the current project:

cd hello-world_Arduino
mkdir components

How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

1.3 Clone the arduino-esp32 library as a component for the current project

  • Goto the components directory, running the following commands to clone the arduino-esp32 library into the components directory
cd components

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

How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

  • After completing the cloning of the arduino-esp32 SDK, goto the “arduino-esp32” directory, and running the following command to clone the submodules of the arduino-esp32 library.
cd arduino-esp32

git submodule update --init --recursive

How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件
After completing the above steps, the project structure will be as follows:
How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件


1.4 Make modifications to the project file names

We will use Arduino’s setup() and loop() functions within the hello-world_Arduino project to demonstrate.

  • In the “hello-world_Arduino” project directory, rename the file “main.c” to “main.cpp” . As follows:

    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

  • In the main folder within the project directory, open the file CMakeLists.txt and change the name of the file main.c to main.cpp . As follows:
    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

  • In the hello-world_Arduino project, you can write test code based on the Arduino library in the hello_world_main.cpp file as follows:

#include "Arduino.h"

#define RGB_BUILTIN 26

void setup() {
  // No need to initialize the RGB LED
  Serial.begin(115200);
  pinMode(RGB_BUILTIN, OUTPUT);
  Serial.printf("GPIO is %d \r\n", RGB_BUILTIN);
}

// the loop function runs over and over again forever
void loop() {
#ifdef RGB_BUILTIN
  digitalWrite(RGB_BUILTIN, HIGH);
   Serial.printf("Light on \r\n ");
  delay(1000);
  
  digitalWrite(RGB_BUILTIN, LOW);   // Turn the RGB LED off
  Serial.printf("Light off \r\n");
  delay(1000);

#endif
}

How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件


1.5 Make modifications to the project configuration options

  • Modify the CONFIG_FREERTOS_HZ configuration in the sdkconfig file to 1000. The default value is 100.

    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

  • In the project directory, run the command idf.py menuconfig to enter the project configuration options interface. Enable the Autostart Arduino setup and loop on boot configuration option.

    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

    idf.py menuconfig → Arduino Configuration [*] Autostart Arduino setup and loop on boot

    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

1.6 Compile and flash the current project for testing

  • In the current project directory, run the following command to compile the project:

    idf.py build
    

    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

    After the firmware compilation is completed, the following log will be printed, indicating the compiled firmware and its corresponding download address.

How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

  • In the current project directory, run the following command to download the firmware and print the firmware running logs.

    idf.py -p COM4 flash monitor
    

    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件


2、Using the Arduino-ESP32 Library as an ESP-IDF Component

  • Create the components-Arduino folder in the esp-idf SDK directory
  • Clone the arduino-esp32 SDK into the components-Arduino folder
  • In the CMakeLists.txt file located in the project directory, add the path to the arduino-esp32 component

2.1 Open the esp-idf CMD environment and create the components-Arduino folder in the esp-idf SDK directory

mkdir components-Arduino

How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

2.2 Clone the arduino-esp32 SDK into the components-Arduino folder

cd components-Arduino

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

cd arduino-esp32

git submodule update --init --recursive

How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

2.3 In the CMakeLists.txt file located in the project directory, add the path to the arduino-esp32 component

To include the arduino-esp32 library as a component based on the esp-idf SDK directory, add the path to the arduino-esp32 component in the CMakeLists.txt file of the project directory, as follows:

set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/components-Arduino/arduino-esp32)

How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件文章来源地址https://www.toymoban.com/news/detail-614708.html

Other steps are exactly the same as Step 1.


【Note】

  • If you need to switch the chip environment, please running the following command in the project directory:
 idf.py set-target esp32s3
  • If you need to use app_main() from ESP-IDF to run the code and call Arduino library API functions, the project file must be named main.cpp. In addition, you need to disable the Autostart Arduino setup and loop on boot configuration option and define app_main() using extern "C" void app_main(), as shown in the example test code below:
#include "Arduino.h"
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"

#define RGB_BUILTIN 21

extern "C" void app_main()
{

      // ESP-IDF API Usage
 printf("Hello world!\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    uint32_t flash_size;
    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
           CONFIG_IDF_TARGET,
           chip_info.cores,
           (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
           (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
           (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
           (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");

    unsigned major_rev = chip_info.revision / 100;
    unsigned minor_rev = chip_info.revision % 100;
    printf("silicon revision v%d.%d, ", major_rev, minor_rev);
    if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
        printf("Get flash size failed");
        return;
    }

    printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
           (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());

    for (int i = 5; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

    // Arduino-like setup()
  Serial.begin(115200);
   pinMode(RGB_BUILTIN, OUTPUT);
  Serial.printf("GPIO is %d \r\n", RGB_BUILTIN);
 
  // Arduino-like loop()
  while(true){
    #ifdef RGB_BUILTIN
  digitalWrite(RGB_BUILTIN, HIGH);
   Serial.printf("Light on \r\n ");
  delay(1000);
  
  digitalWrite(RGB_BUILTIN, LOW);   // Turn the RGB LED off
  Serial.printf("Light off \r\n");
  delay(1000);

#endif
    
    }

}
  • The setup() function in Arduino is called only once within app_main() and does not require the while(!Serial){} loop.
  • The loop() function in Arduino, when used within app_main(), must be implemented with while(true){} or while(1){} to create an infinite loop.
  • 固件运行日志:
    How to use the Arduino-ESP32 Library as an ESP-IDF Component,英文指南 (English Guide),环境搭建 (Environment Setup),芯片基础 (Chip Foundation),单片机,stm32,嵌入式硬件

到了这里,关于How to use the Arduino-ESP32 Library as an ESP-IDF Component的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • How to fix the problem that Raspberry Pi cannot use the root user for SSH login All In One

    如何修复树莓派无法使用 root 用户进行 SSH 登录的问题 修改树莓派默认的 pi 用户名和密码后,需要使用 root 用户进行 SSH 登录; 对 pi/home 文件夹进行 备份 ,复制到新用户下 xgqfrms/home 备份后,要 删除 pi 用户, 必须切换到其他用户,毕竟 pi 用户不能自己删除自己呀!⚠️ 给

    2024年02月07日
    浏览(53)
  • 解决运行js代码报错—Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs

    目录 ❌ 报错信息 🎈 解决方案 ✔️ 执行结果 vscode运行js代码报错:(node:20452) Warning: To load an ES module, set \\\"type\\\": \\\"module\\\" in the package.json or use the .mjs extension.  同类型报错的情况,都可以使用该方案解决!

    2024年02月08日
    浏览(41)
  • How to Use Glslang

    Execution of Standalone Wrapper 要使用独立的二进制形式,请执行glslang,它将打印一条使用语句。基本操作是给它一个包含着色器的文件,它会打印出警告/错误以及可选的 AST。 应用的特定于阶段的规则基于文件扩展名: vert 顶点着色器 tesc 曲面细分控制着色器 tese 曲面细分评估着

    2024年02月14日
    浏览(29)
  • how to use git sub modules

    To use Git submodules in your project, follow these steps: 1,Initialize a new Git repository for your main project if you haven’t already done so: 2,Add the submodule to your project by using the git submodule add command followed by the URL of the submodule repository and the path where you want it to be located within your project: For example, if yo

    2024年03月08日
    浏览(30)
  • How to use notebook in Ubuntu 22.04

    这个时候,系统会自动打开浏览器,浏览器会自动跳转到页面http://localhost:8888/tree,如下图所示: 如果我们希望停止服务运行,可以在终端窗口中按Ctrl+C,这个时候,终端窗口命令行会出现如下变化 我们再来观察notebook浏览器画面,发现没有任何变化。

    2024年02月10日
    浏览(31)
  • How to use jupyterlab in Ubuntu 22.04

    这个时候,系统会自动打开浏览器,页面会自动跳转到http://localhost:8888/lab页面。 在终端窗口中按Ctrl+C 切换到浏览器,我们将会看到下面的画面

    2024年02月11日
    浏览(35)
  • Lost in the Middle: How Language Models Use Long Contexts

    本文是LLM系列文章,针对《Lost in the Middle: How Language Models Use Long Contexts》的翻译。 虽然最近的语言模型能够将长上下文作为输入,但人们对它们使用长上下文的情况知之甚少。我们分析了语言模型在两项任务中的性能,这两项任务需要在输入上下文中识别相关信息:多文档问

    2024年02月09日
    浏览(33)
  • How to Implement a cascader using Web Component

    To implement a cascader using Web Components, you can create a custom element that encapsulates the cascader functionality. Here’s an example: In the above example, we define a custom element called cascader using the customElements.define() method. Inside the Cascader class, we extend the HTMLElement class to create our custom element. In the constructor,

    2024年02月19日
    浏览(30)
  • How to configure Qlik Sense to use a dedicated PostgreSQL database

    The Qlik Sense Repository Database (QSR) can be moved to a dedicated standalone PostgreSQL instance not hosted on the same machine as other Qlik Sense Services. It is also possible to move an already existing QSR from a local Sense install, to a dedicated PostgresSQL database. Content: Set up a postgresSQL database Simplified checklist of steps: Moving the Q

    2024年02月07日
    浏览(28)
  • 论文解读: 2023-Lost in the Middle: How Language Models Use Long Contexts

    大模型使用的关键在于Prompt,然而大模型存在幻觉现象,如何减少这种现象的发生成为迫切解决的问题。外部知识库+LLM的方法可以缓解大模型幻觉,但是如何撰写Prompt才能发挥LLM的性能。下面介绍这篇论文说明上下文信息出现在Prompt什么位置使模型表现最佳,以及上下文文本

    2024年02月17日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包