测试环境

Ubuntu 22.04

1
2
3
4
5
6
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.5 LTS
Release:        22.04
Codename:       jammy

安装 ESP-IDF

  • 安装 EIM(ESP-IDF 安装管理器)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    # APT 源列表添加 EIM 仓库
    $ echo "deb [trusted=yes] https://dl.espressif.com/dl/eim/apt/ stable main" | sudo tee /etc/apt/sources.list.d/espressif.list
    
    $ sudo apt update
    
    # GUI 和 CLI
    $ sudo apt install eim
    
    # 可以仅安装 CLI
    $ sudo apt install eim-cli
    
  • 安装 ESP-IDF

    当前最新的 ESP-IDF 稳定版本是 v6.0.1,所以我们安装该版本。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    $ sudo eim install -i v6.0.1
    ...
    2026-05-30 22:20:42 - 10 - 05 - INFO - Wizard result: Ok
    2026-05-30 22:20:42 - 10 - 05 - INFO - Successfully installed IDF
    2026-05-30 22:20:42 - 10 - 05 - INFO - Now you can start using IDF tools
    
    $ eim list
    2026-05-30 22:21:25 - 10 - 05 - INFO - Listing installed versions...
    Installed versions:
    - v6.0.1 (selected) [/home/zeepunt/.espressif/v6.0.1/esp-idf]
    

    默认情况下,ESP-IDF 及其相关组件是安装到 ~/.espressif 目录下。

  • 查看 ESP-IDF 的启动脚本

    1
    2
    3
    4
    5
    6
    7
    8
    
    $ eim select
    Available versions:
    ✔ Which version do you want to select? · v6.0.1
    Selected version: v6.0.1
    ===========================================
    To activate this environment, run the following command in your terminal:
    source "/home/zeepunt/.espressif/tools/activate_idf_v6.0.1.sh"
    ===========================================
    

安装依赖

1
$ sudo apt install libbsd-dev

构建和编译项目

假设当前并不存在 ESP-IDF 项目,所以我们要新建一个。

1
2
3
4
5
6
# 启动 ESP-IDF 环境
$ source ~/.espressif/tools/activate_idf_v6.0.1.sh

# 创建项目
$ idf.py create-project esp_linux
$ cd esp_linux

启动 ESP-IDF 环境后,可以使用命令 deactivate 退出。

修改该项目根目录下的 CMakeLists.txt 文件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ vim CMakeLists.txt

# The following five 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.22)

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

set(COMPONENTS main) # 添加这行, 而且必须放在 project 前面

project(esp_linux)

将目标芯片设为 linux。

1
2
3
4
5
6
$ idf.py --preview set-target linux

# 查看是否配置成功
$ cat sdkconfig | grep CONFIG_IDF_TARGET
CONFIG_IDF_TARGET="linux"
CONFIG_IDF_TARGET_LINUX=y

编译。

1
$ idf.py build

运行。

1
2
3
4
5
6
7
8
$ idf.py monitor

Executing action: monitor
Running idf_monitor in directory /home/zeepunt/share/project/esp_linux
Executing "/home/zeepunt/.espressif/tools/python/v6.0.1/venv/bin/python /home/zeepunt/.espressif/v6.0.1/esp-idf/tools/idf_monitor.py --toolchain-prefix  --target linux --revision 0 /home/zeepunt/share/project/esp_linux/build/esp_linux.elf -m '/home/zeepunt/.espressif/tools/python/v6.0.1/venv/bin/python' '/home/zeepunt/.espressif/v6.0.1/esp-idf/tools/idf.py'"...
--- esp-idf-monitor 1.9.0 on linux
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
I (13595519) port: Starting scheduler.

版权声明

本文为「Zeepunt 日常随笔」的原创文章,遵循 CC BY-NC-ND 4.0 许可协议。允许在署名作者、注明原文链接且不作任何更改的前提下非商业性地分享本文。

原文链接:https://zeepunt.github.io/article/esp32/esp32%E5%9C%A8-linux-%E7%8E%AF%E5%A2%83%E4%B8%8B%E8%BF%90%E8%A1%8C%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F/