Installing Rust#
Refer to Rust Installation/Update/Packaging
Switching Rust to nightly#
rustup install nightly
rustup default nightly
Installing ESP32 Tools#
There are two ways to develop ESP32 with Rust:
- Develop using the
std
library - Develop using the
core
library (no_std
) for bare-metal development
ldproxy#
This must be installed when building applications using the Rust standard library std
cargo install ldproxy
espflash#
Tool for flashing ESP devices
# cargo-espflash is used for cross-compiling and flashing
cargo install cargo-espflash
cargo install espflash
espmonitor#
Serial monitor for esp32 and esp8266
# cargo-espmonitor is used for serial monitoring of esp32 and esp8266
cargo install cargo-espmonitor
cargo install espmonitor
Only for RISC-V targets#
To build Rust applications for Espressif chips based on the RISC-V
architecture, follow these steps:
- Install the
nightly
toolchain andrust-src
component
rustup toolchain install nightly --component rust-src
rustup default nightly
- Set the target
- For
no_std
bare-metal applications, run
# For ESP32-C2 and ESP32-C3
rustup target add riscv32imc-unknown-none-elf
# For ESP32-C6 and ESP32-M2
rustup target add riscv32imac-unknown-none-elf
Creating a New Project#
Install cargo-generate:
cargo install cargo-generate
Once the above tools are installed, you can use the corresponding templates to generate projects. There are two templates available:
- esp-template - Bare-metal
no_std
project template - esp-idf-template - Template with
std
support
You can clone both templates to your local machine so that you don't have to download them from GitHub in the future:
mkdir ~/.espressif
cd ~/.espressif
git clone https://github.com/esp-rs/esp-template
git clone https://github.com/esp-rs/esp-idf-template.git
Then, create a project based on a template. For example, create a project using the esp-idf-template:
cargo generate ~/.espressif/esp-idf-template/
Configuring the Project#
Open .cargo/config.toml
and add the following lines to the bottom of the [env]
section. Keep the rest of the content unchanged.
cd xxx
vi .cargo/config.toml
[env]
ESP_IDF_TOOLS_INSTALL_DIR = { value = "global" }
Running the Project#
# Cross-compile the project
cargo build --release
# Cross-compile and run the project
cargo run --release
Checking Hardware#
If you encounter any issues, refer to Checking Hardware
Simulation#
Official Website#
https://wokwi.com/, wokwi is a web-based online simulation platform.
To write code locally, you can install the web simulation:
cargo install wokwi-server
- Create a new project
- Take note of the id of the web URL
- Then, locally enter
# Compile
cargo build --release
# Run
wokwi-server -c esp32c3 -i 网页端的id target/riscv32imac-esp-espidf/release/espidf-demo
Tutorials#
- https://dev.to/apollolabsbin/embassy-on-esp-getting-started-27fi
- https://dev.to/apollolabsbin/embassy-on-esp-gpio-5594
References#
- https://esp-rs.github.io/book/introduction.html
- https://narukara.github.io/std-training-zh-cn/
- ESP32-C6-DevKitM-1 Development Board Documentation
- ESP32-C3-DevKitC-02 Development Board Documentation
- Compiling openocd-JTAG debugging for ESP32C6 on macOS