A starter for ESP32-S2 projects, including tooling to build the "Cargo-first" way with embuild
.
This is the result of some work over the past few weeks getting into both Rust development, and working with the ESP32-S2 MCU. The end-goal is to build a few WiFi-connected air quality sensors to place around my house. That project aims to use 3,000mAh Li-Ion 18650 batteries and (hopefully!) year-plus operation, so ultra-low power usage is critical.
This project includes a buildable example that can be flashed to your ESP32-S2, including some boilerplate code to interact with the ULP coprocessor.
Please note that I am new to Cargo Workspaces, Rust, and the ESP32. Please open an issue if you notice anything wrong!
- Build both main and ULP binaries from one workspace
- Build the main binary using embuild's
native
features (e.g. no PlatformIO) - Allow flashing from the WSL2 environment
- Example ULP code to sleep for a specified interval, then wake-up the main processor
- Example code to comminicate between main and ULP processors
- Allow building via native
cargo build
- Install rustup from https://rustup.rs/
- Install the custom Rust ESP32 toolchain and LLVM Clang forks
- You probably want to do the following in
~/.espressif-rust
or/opt/rustup-esp
or similar curl -sSL -o install-rust-toolchain.sh https://github.com/esp-rs/rust-build/releases/download/v1.57.0.2/install-rust-toolchain.sh
sudo chmod u+x install-rust-toolchain.sh
./install-rust-toolchain.sh --export-file ./rustup-export.sh
source ./rustup-export.sh
(this needs to be done for each bash session where you're building the ESP32-S2 project)
- You probably want to do the following in
- Download the starter
git clone https://github.com/ryanc-me/esp32s2-starter
Some extra setup is required to allow flashing the chip from the WSL2 environment.
- From the Windows environment
- Install Rust: https://www.rust-lang.org/tools/install
cargo install espflash
cargo install espmonitor
Because of some Cargo limitations (or lack of understanding on my part) the native cargo build
does not work from the workspace root. From what I can gather, Rust is trying to use the same toolchain for both main and ULP crates.
As a workaround, you can build each crate separately, from within the crate directory:
cd ulp && cargo build
cd main && cargo build --features native
Alternatively, there is a build.sh
script which does the above for you.
Follow the standard flashing procedure, e.g.:
- Ensure tools are installed
cargo install espflash
cargo install espmonitor
espflash --monitor /dev/ttyUSB1 target/xtensa-esp32s2-espidf/debug/main
espmonitor /dev/ttyUSB1
- Make sure both
espflash
andespmonitor
are installed in the Windows environment.- Install Rust
cargo install espflash
cargo install espmonitor
- Then, from your WSL2 Linux environment:
powershell.exe -Command "espflash --monitor COM1 target/xtensa-esp32s2-espidf/debug/main
powershell.exe -Command "espmonitor COM1"
- Note that
powershell.exe
will only be available in your Linux env if you open your WSL2 target e.g. via Windows Terminal, or VSCode'sRemote WSL
extension (the latter is a very good option). Some info here if, for some reason, you need to do this via SSH.
- Or, alternatively, the
flash.sh
script runs the flash+monitor command for you.
- Ivan Markov for
rust-esp32-std-demo
(which themain
crate is based on) - Ivan Markov for
rust-esp32-ulp-blink
(which theulp
crate is based on)