Rust on ESP32, part 1: Simulate ESP32C3 in VSCode
Let’s get the disclaimers out of the way first. I’m not exactly a seasoned embedded developer so I’m probably making mistakes along the way. If you see any, please let me know! Also, this entire process and much more is written in The Rust on ESP Book.
I found running Rust on any MCUs a little tricky, to say the least, but recently I stumbled into esp-template by Sergio Gasquez. That, together with Wokwi VSCode extension made it incredibly easy to get up and running. Let’s get started!
I chose ESP32C3 for this project because I like the philosophy of RISC-V’s open-source ISA but this should work on any ESPs listed on the esp-template.
First, you need to install some prerequisites:
cargo-generate (for actually using the esp-template):cargo install cargo-generate
Wokwi for VS Code (follow that link for instructions)
Next, Let’s create a starter project with the esp-template:cargo generate esp-rs/esp-template
This will prompt you the project details, this was my setup:
➜ ~ cargo generate esp-rs/esp-template
⚠️ Favorite `esp-rs/esp-template` not found in config, using it as a git repository: https://github.com/esp-rs/esp-template.git
🤷 Project Name: aquatic-sock-puppet
🔧 Destination: /Users/tuomaslouekari/aquatic-sock-puppet ...
🔧 project-name: aquatic-sock-puppet ...
🔧 Generating template ...
✔ 🤷 Which MCU to target? · esp32c3
✔ 🤷 Configure advanced template options? · true
✔ 🤷 Enable allocations via the esp-alloc crate? · true
✔ 🤷 Configure project to use Dev Containers (VS Code and GitHub Codespaces)? · false
✔ 🤷 Configure project to support Wokwi simulation with Wokwi VS Code extension? · true
✔ 🤷 Add CI files for GitHub Action? · false
✔ 🤷 Setup logging using the log crate? · true
✔ 🤷 Enable WiFi/Bluetooth/ESP-NOW via the esp-wifi crate? · true
For more information and examples of esp-wifi showcasing Wifi,BLE and ESP-NOW, see https://github.com/esp-rs/esp-wifi/blob/main/examples.md
After this, build your project from its folder as usual:cargo build
You should now be able to run the simulator in your VSCode. Open the command palette (shift+cmd+p for mac) and choose Wokwi: Start Simulator.
For me, the executable name differs from the one in wokwi.toml
so if you get an error for firmware binary target, just edit wokwi.toml
to correct the filename. My binary had dashes but wokwi used underscores.
If you’re looking at something like this, great success:
Then just go wild with it! I highly recommend Wokwi for examples, community, and more. They even have the option to flash devices directly but that’s another story entirely. In part 2, we’ll flash the board from the command line.