This document aims to guide the readers in understanding Rust and how it can be utilized on ESP32 (Espressif Systems). After reading this article, the reader should have a clear understanding of how Rust is used in an embedded system like ESP32.
Rust is a powerful system programming language with memory safety featuring zero-cost abstraction. It's increasingly popular for system level programming and has recently gained attention for embedded system development. One such application is in the programming of ESP32 System on Chip (SoC).
ESP32 is a Wi-Fi & Bluetooth microchip and has an excellent performance in power consumption, RF performance, robustness, versatility, and reliability. It also supports a wide variety of peripherals. Rust brings robustness and safety to ESP32 development, making it much easier to develop reliable code.
Before we proceed, ensure you have the following:
To install Rust, you can use rustup, the recommended way to install the Rust programming language. Open a terminal and enter the following commands:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
After installation, add Rust to your system PATH by running:
source $HOME/.cargo/env
You can verify your installation by running the following command:
rustc --version
For programming ESP32 using Rust, you need to set up a suitable Rust toolchain. Here are the steps:
rustup target add xtensa-esp32-none-elf
cargo install cargo-xbuild
After the toolchain setup, let's write a simple Rust "Hello, World!" application for ESP32:
cargo init hello_world
main.rs with:#![no_std]
#![no_main]
use core::panic::PanicInfo;
#[no_mangle]
pub extern "C" fn app_main() {
println!("Hello, world!");
}
#[panic_handler]
fn handle_panic(_info: &PanicInfo) -> ! {
loop {}
}
cargo xbuild --target xtensa-esp32-none-elf
Debugging will require a GDB server running on your computer. In your terminal, open the GDB server as follows:
openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg
In another terminal, connect to your GDB server:
xtensa-esp32-elf-gdb target/xtensa-esp32-none-elf/debug/hello_world
The last quarter of 2023 has brought significant developments in the integration of Rust with Espressif chips. This update covers the major milestones achieved in this period.
Rust's support for RISC-V targets, which includes popular Espressif chips like ESP32C3 and ESP32C2, has seen considerable improvements. The most notable change is the enablement of atomic load/store code generation for non-atomic RISC-V targets. This enhancement, initially a major bottleneck, is currently in the nightly release and is expected to be stabilized in Rust 1.76.
The riscv32imafc-unknown-none-elf target has been introduced, primarily for the ESP32P4, Espressif's first chip without a rajio component.
Existing bare metal RISC-V targets have been elevated to tier 2, indicating improved support and stability.
The riscv32imafc-esp-espidf target for ESP32P4 marks another significant addition, enhancing Rust's compatibility with Espressif's newer models.
While there have been advancements in the Xtensa LLVM backend (now based on LLVM 17), no significant upstream progress is reported for this quarter.
The v0.13, v0.14, and v0.15 releases of esp-hal introduced several new features:
The discontinuation of atomic emulation support, as Rust now handles atomic operations on non-atomic targets natively.
The Q4 2023 update underscores Rust's growing integration and compatibility with Espressif chips. These enhancements not only streamline development but also open new possibilities for leveraging Rust's efficiency and safety in embedded systems.
Issue: Unable to compile due to 'linking with cc failed'.
Issue: 'Error: no device found' during flashing.