Embassy: A Next-Generation Framework for Embedded Applications Built with Rust for Efficient and Safe Asynchronous Applications

Embassy is a next-generation framework for embedded applications that leverages the Rust programming language and its powerful asynchronous features to help developers build safer, more efficient, and energy-saving embedded software.

The Advantages of Rust

The Rust language is renowned for its high performance, memory safety, and extremely low runtime overhead. It has no runtime, garbage collector, or operating system, and captures most errors at compile time. Its comprehensive memory and thread safety, along with a robust type system, provide developers with a solid foundation.

Embassy: A Next-Generation Framework for Embedded Applications Built with Rust for Efficient and Safe Asynchronous Applications

The Power of Asynchronous Programming

The async/await mechanism in Rust brings unprecedented convenience and efficient multitasking capabilities to embedded systems. Tasks are transformed into state machines at compile time and run cooperatively. This approach eliminates the need for dynamic memory allocation and operates on a single stack, avoiding the cumbersome task of adjusting stack sizes for each task. It completely eliminates kernel context switching found in traditional RTOS, resulting in faster speeds and smaller footprints.

Rich Libraries and Tools

Hardware Abstraction Layer (HAL) Embassy provides HALs for various chips, offering safe, Rust-style APIs that allow developers to avoid direct register manipulation.

  • • embassy-stm32: for all STM32 microcontroller series

  • • embassy-nrf: for Nordic Semiconductor’s nRF52, nRF53, and nRF91 series

  • • embassy-rp: for Raspberry Pi RP2040 microcontroller

  • • esp-rs: for Espressif Systems ESP32 series chips

  • • ch32-hal: for WCH 32-bit RISC-V (CH32V) series chips

Time Management embassy_time provides Instant, Duration, and Timer types, allowing developers to say goodbye to the troubles of hardware timers.

Real-Time Capabilities Embassy supports the creation of multiple executors with different priorities, enabling high-priority tasks to preempt low-priority tasks.

Low Power Design The asynchronous executor of Embassy automatically puts the core into sleep mode when there are no active tasks. Tasks are awakened by interrupts, eliminating the need for polling, effectively reducing power consumption.

Networking Features The embassy-net stack supports protocols such as Ethernet, IP, TCP, UDP, ICMP, and DHCP, simplifying asynchronous network programming.

Bluetooth Support nrf-softdevice provides Bluetooth Low Energy 4.x and 5.x support for nRF52 microcontrollers. embassy-stm32-wpan offers Bluetooth Low Energy 5.x support for STM32WB microcontrollers.

LoRa Support lora-rs provides an asynchronous LoRa and LoRaWAN protocol stack for Embassy.

USB Support embassy-usb offers a device-side USB protocol stack, supporting common classes like USB Serial (CDC ACM) and USB HID, along with a rich builder API for developers to customize their own USB classes.

Bootloader and DFU embassy-boot is a lightweight bootloader that supports power-fail safe firmware upgrades and provides trial boot and rollback features.

Example Code

use defmt::info;
use embassy_executor::Spawner;
use embassy_time::{Duration,Timer};
use embassy_nrf::gpio::{AnyPin,Input,Level,Output,OutputDrive,Pin,Pull};
use embassy_nrf::Peripherals;

#[embassy_executor::task]
async fn blink(pin: AnyPin) {
    let mut led = Output::new(pin, Level::Low, OutputDrive::Standard);

    loop {
        led.set_high();
        Timer::after_millis(150).await;
        led.set_low();
        Timer::after_millis(150).await;
    }
}

#[embassy_executor::main]
async fn main(spawner: Spawner) {
    let p = embassy_nrf::init(Default::default());
    spawner.spawn(blink(p.P0_13.degrade())).unwrap();

    let mut button = Input::new(p.P0_11, Pull::Up);
    loop {
        button.wait_for_low().await;
        info!("Button pressed!");
        button.wait_for_high().await;
        info!("Button released!");
    }
}

Conclusion

Embassy provides developers with a comprehensive embedded development framework that leverages the advantages of Rust, simplifying the development process and improving code quality and efficiency. It helps developers quickly build safe, efficient, and energy-saving embedded applications.

Project Address: https://github.com/mbassy-rs/embassy

Leave a Comment