ESP-IDF rust学习
ESP-IDF
介绍
ESP32是乐鑫信息科技(Espressif Systems)推出的一款高性能、低功耗的双核微控制器(MCU),主打物联网(IoT)、无线通信场景。对比STM32来说,前者直接携带无线通信协议,且为国产,后者更适应于工业级。
ESP-IDF为乐鑫官方原生框架(基于 FreeRTOS),支持 C/C++ 开发,适合深度定制。
在乐鑫芯片上使用 Rust 开发,有以下两种方式(请参考这里):
- 使用 std 库,即标准库。
- 使用 core 库(no_std),即裸机开发。
我们这里的直接使用IDF开发,即使用std库开发模式。
本文硬件使用ESP32-C6-DEVKIT-N16-M开发板(购买链接),芯片为ESP32-C6-WROOM。
环境搭建(Linux+Rust+RustRover)
参考链接
基本程序安装
-
安装rust及相关镜像源配置 (省略)
-
安装rust的esp相关工具
安装cargo下载(可选):
cargo install cargo-binstall。如果安装此工具,以下安装替换为cargo binstall xxxx,该工具直接从github下载release,无需再次本机编译。-
cargo install esp-generate --locked(no_std) 可不安装 -
cargo install espflash --locked -
cargo binstall probe-rs-tools -
cargo install esp-config --features=tui --locked -
cargo install ldproxy -
cargo install cargo-generate
-
ESP_IDF环境搭建(参考)
# 1. 设置环境变量,国内镜像使用
export IDF_GITHUB_ASSETS="dl.espressif.com/github_assets"
# 2. 前期必要的软件包
sudo apt-get install git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
# 3. clone 仓库
cd ~/.local/share
# 官方文档这里是 git clone --single-branch --recursive https://github.com/espressif/esp-idf.git
git clone -b v5.3.3 --recursive https://github.com/espressif/esp-idf.git
# 4. 自定义工具安装
mkdir ~/.local/share/espressif/
export IDF_TOOLS_PATH="~/.local/share/espressif/"
# 5. 安装工具
# 注意fish的shell为: ./install.fish esp32c6
./install.sh esp32c6
# 6. 设置烧录权限 https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32c6/get-started/establish-serial-connection.html
sudo usermod -a -G dialout $USER
生成项目代码
基于模板生成项目: cargo generate esp-rs/esp-idf-template cargo
使用本机刚创建好的ESP-IDF
编辑.cargo/config.toml
[build]
target = "riscv32imac-esp-espidf"
[target.riscv32imac-esp-espidf]
linker = "ldproxy"
runner = "espflash flash --monitor"
rustflags = [ "--cfg", "espidf_time64"]
[unstable]
build-std = ["std", "panic_abort"]
[env]
# 可以参考esp-idf-sys的文档 https://github.com/esp-rs/esp-idf-template
MCU="esp32c6"
ESP_IDF_VERSION = { value = "tag:v5.3.3" }
# ESP_IDF_SDKCONFIG_DEFAULTS = { value = "./sdkconfig.defaults", relative = true }
# 确保使用本机环境 https://github.com/esp-rs/esp-idf-sys?tab=readme-ov-file#customizing-how-the-esp-idf-sdk-is-built
IDF_PATH = "/home/nsfoxer/.local/share/esp-idf/"
IDF_TOOLS_PATH = "/home/nsfoxer/.local/share/espressif/"
ESP_IDF_TOOLS_INSTALL_DIR = { value = "custom:/home/nsfoxer/.local/share/espressif/" }
最终的项目结构为:
~/桌/s/e/my (master|✔) $ tree .
.
├── build.rs
├── Cargo.lock
├── Cargo.toml
├── rust-toolchain.toml
├── sdkconfig.defaults
└── src
└── main.rs
2 directories, 6 files
连接单片机,执行cargo run --release即可,出现Hello World表示成功。
# 输出
[2025-12-23T14:44:10Z INFO ] Serial port: '/dev/ttyACM0'
[2025-12-23T14:44:10Z INFO ] Connecting...
[2025-12-23T14:44:11Z INFO ] Using flash stub
Chip type: esp32c6 (revision v0.2)
Crystal frequency: 40 MHz
Flash size: 16MB
Features: WiFi 6, BT 5
MAC address: 58:8c:81:3c:9c:ec
App/part. size: 390,064/16,384,000 bytes, 2.38%
[00:00:00] [========================================] 14/14 0x0 Skipped! (checksum matches) [00:00:00] [========================================] 1/1 0x8000 Skipped! (checksum matches) [00:00:21] [========================================] 204/204 0x10000 Verifying... OK! [2025-12-23T14:44:32Z INFO ] Flashing has completed!
Commands:
CTRL+R Reset chip
CTRL+C Exit
ESP-ROM:esp32c6-20220919
Build:Sep 19 2022
rst:0x1 (POWERON),boot:0x1c (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x40875730,len:0x175c
load:0x4086b910,len:0xec8
load:0x4086e610,len:0x31c4
entry 0x4086b91a
I (23) boot: ESP-IDF v5.5.1-838-gd66ebb86d2e 2nd stage bootloader
I (24) boot: compile time Nov 27 2025 09:46:10
I (24) boot: chip revision: v0.2
I (25) boot: efuse block revision: v0.3
I (29) boot.esp32c6: SPI Speed : 80MHz
I (32) boot.esp32c6: SPI Mode : DIO
I (36) boot.esp32c6: SPI Flash Size : 16MB
I (40) boot: Enabling RNG early entropy source...
I (45) boot: Partition Table:
I (47) boot: ## Label Usage Type ST Offset Length
I (54) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (60) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (67) boot: 2 factory factory app 00 00 00010000 00fa0000
I (73) boot: End of partition table
I (76) esp_image: segment 0: paddr=00010020 vaddr=42040020 size=11f10h ( 73488) map
I (98) esp_image: segment 1: paddr=00021f38 vaddr=40800000 size=0985ch ( 39004) load
I (107) esp_image: segment 2: paddr=0002b79c vaddr=40809860 size=01d74h ( 7540) load
I (109) esp_image: segment 3: paddr=0002d518 vaddr=00000000 size=02b00h ( 11008)
I (113) esp_image: segment 4: paddr=00030020 vaddr=42000020 size=3f36ch (258924) map
I (170) boot: Loaded app from partition at offset 0x10000
I (170) boot: Disabling RNG early entropy source...
I (180) cpu_start: Unicore app
I (188) cpu_start: Pro cpu start user code
I (189) cpu_start: cpu freq: 160000000 Hz
I (189) app_init: Application information:
I (191) app_init: Project name: libespidf
I (196) app_init: App version: 1
I (201) app_init: Compile time: Dec 23 2025 22:43:51
I (207) app_init: ELF file SHA256: 000000000...
I (212) app_init: ESP-IDF: v5.3.3
I (217) efuse_init: Min chip rev: v0.0
I (222) efuse_init: Max chip rev: v0.99
I (227) efuse_init: Chip rev: v0.2
I (231) heap_init: Initializing. RAM available for dynamic allocation:
I (239) heap_init: At 4080C900 len 0006FD10 (447 KiB): RAM
I (245) heap_init: At 4087C610 len 00002F54 (11 KiB): RAM
I (251) heap_init: At 50000000 len 00003FE8 (15 KiB): RTCRAM
I (258) spi_flash: detected chip: generic
I (262) spi_flash: flash io: dio
W (266) timer_group: legacy driver is deprecated, please migrate to `driver/gptimer.h`
I (274) sleep: Configure to isolate all GPIO pins in sleep state
I (281) sleep: Enable automatic switching of GPIO sleep configuration
I (288) coexist: coex firmware version: 85a8de8
I (307) coexist: coexist rom version 5b8dcfa
I (308) main_task: Started on CPU0
I (308) main_task: Calling app_main()
I (308) gpio: GPIO[15]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (318) my: Hello, world!
[!NOTE]
如果项目一直不使用本地的ESP-IDF环境,可以试着在项目里执行cargo clean,可能是最开始没有配置项目下的.cargo/config.toml的ENV,有旧的缓存导致的。

浙公网安备 33010602011771号