Embassy on ESP32: Hello World
This is the firs tutorial in series : Embassy-rs Getting Started.
By following this article, you will understand :
Hardware:
You will need an ESP32C6 development kit : here
1. Generate esp32 Rust project:
Espressif Systems does super brilliant job and spending huge effort to create esp-rs for embedded rust ecosystem, especially for esp chip.
There are two application templates for your first start:
you might wondering what are the differences between esp-hal vs esp-idf-hal, check out below discussion:
For first start, i strongly recommend go with esp-template .
Now, let's install cargo sub-commands:
cargo install cargo-generate
cargo install ldproxy
cargo install espup
cargo install espflash
cargo install cargo-espflash # Optional
Quite simple, just follow the Readme, first run the cargo generate esp-rs/esp-template command, name your project and select the config for your project as picture below:
Open the project with VS code, you can see the template generate basic stuff for main.rs and fill-in all dependencies needed for first start.
You can try to compile here already, but our target is add Embassy-rs, so let go to next step to modify the cargo.toml and main.rs .
2. Basic configuration for embassy in esp32 Rust project:
First of all, Embassy ( EMBedded ASYnc) is a project to make async/await a first-class option for embedded development, write safe and efficient embedded code. The heart of Embassy is executor, an async/await executor designed for embedded usage along with support functionality for interrupts and timers.
You might want to compare Embassy with RTIC , and here is the differences:
Embassy provides both Hardware Abstraction Layers, and an executor/runtime, while RTIC aims to only provide an execution framework. For example, embassy provides embassy-stm32 (a HAL), and embassy-executor (an executor). On the other hand, RTIC provides the framework in the form of rtic, and the user is responsible for providing a PAC and HAL implementation (generally from the stm32-rs project).
Additionally, RTIC aims to provide exclusive access to resources on as low a level of possible, ideally guarded by some form of hardware protection. This allows for access to hardware while not necessarily requiring locking mechanisms on the software level.
But then you might also wondering about embedded async in Rust vs RTOS, check out this article:
We will dive in in next series of embedded Rust.
to add embassy, you need to add below stuff to your current cargo.toml
esp-hal = { version = "0.16.0", features = [ "esp32c6" , "embassy", "embassy-time-timg0", "embassy-executor-thread", "embassy-executor-interrupt"]}
embassy-executor = { version = "0.5.0", features = ["nightly", "integrated-timers"] }
embassy-sync = "0.5.0"
embassy-time = "0.3.0"
embassy-time-driver = { version = "0.1.0", optional = true }
embedded-graphics = "0.8.1"
embedded-hal = "1.0.0"
embedded-hal-02 = { version = "0.2.7", package = "embedded-hal" }
embedded-hal-async = "1.0.0"
embedded-hal-bus = "0.1.0"
embedded-io-async = "0.6.1"
(will explain those stuff in the next post about diving into embassy)
and then in the main.rs file, you can just copy the example from esp-hal:
your project now simply look like this:
3. Compile and flash first Embassy Hello World to esp32c6.
now you can build your project:
type cargo r for flashing binary to esp32c6 dev kit
(cargo r can do the job, because it is already configured and generated in our code)
[target.riscv32imac-unknown-none-elf]
runner = "espflash flash --monitor"
and this is what you get from terminal :
Full project can be found here : https://github.com/0xkelvin/hello-embassy-espc6
Exploring the new unlocked
8 个月so helpful
Senior Embedded Software Engineer
8 个月awesome ??
Staff Engineer at May Mobility | YouTube Partner | Ex General Motors (Views are my own obviously)
8 个月Nice!!
Software Engineer | Rust, Linux | H1B
8 个月Nice! I recently picked up a few ESP32s as well with the intention of learning how to use ESPNOW and embedded Rust.