Embassy on ESP32: Hello World

Embassy on ESP32: Hello World

This is the firs tutorial in series : Embassy-rs Getting Started.

By following this article, you will understand :

  1. how to generate Rust project for esp32 from esp-rs.
  2. Basic cargo.toml configuration for embassy in esp32 Rust project.
  3. Compile and flash first Embassy Hello World to esp32c6.


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:

  • esp-template : a minimal esp-hal ( without support for the Rust standard library - no_std) application template for use with cargo-generate
  • esp-idf-template : A template of Rust binary crate for the esp-idf framework with esp-idf-hal ( with support for the Rust standard library - std)

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:

https://tweedegolf.nl/en/blog/65/async-rust-vs-rtos-showdown

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:

https://github.com/esp-rs/esp-hal/blob/main/examples/src/bin/embassy_hello_world.rs

your project now simply look like this:

3. Compile and flash first Embassy Hello World to esp32c6.

now you can build your project:

  • cargo build ( for Window)
  • CRATE_CC_NO_DEFAULTS=1 cargo build (for MAC M1)

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"        
on MAC the interface should be /de/cu... on Window, just select the comport that you connect esp kit

and this is what you get from terminal :


Full project can be found here : https://github.com/0xkelvin/hello-embassy-espc6


YikSeong Low

Exploring the new unlocked

8 个月

so helpful

回复
Tu Nguyen

Senior Embedded Software Engineer

8 个月

awesome ??

回复
Dario Lencina Talarico

Staff Engineer at May Mobility | YouTube Partner | Ex General Motors (Views are my own obviously)

8 个月

Nice!!

Nathan T.

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.

要查看或添加评论,请登录

社区洞察