(L3.2) Connected Embedded Devices
While the ecosystem is mostly external to the Rust project,
many functionalities are already available in the default installation.
By default, Rust provides 3 main libarires: core
, alloc
and std
.
The core
library is the minimal set of functionality that is available to all Rust programs.
The alloc
library provides functionality for allocating memory on the heap,
and thus requires an allocator to be present.
The std
library is the standard library that provides the full functionality of Rust,
which often requires a full-fledged operating system to be present.
Bootstrapping a bare-metal program
Altough you can use Rust to write bare-metal programs,
the std
library is not available in this context.
Bootstrapping a bare-metal program also requires a custom runtime,
which is platform specific.
For example, the cortex-m-rt
crate provides a runtime for ARM Cortex-M microcontrollers.
It takes care of initializing static memory, vector tables and the stack pointer.
You ecosystem also contains runtimes for platforms based on MIPS, RISC-V, or ESP-C architectures,
being mips-rt
, riscv-rt
and esp32c-rt
respectively.
Note that for the ESP32 series, a fork of the Rust compiler is also provided by Espressif Systems.
This fork provides std
support for the ESP32 series.
Peripheral Access Crates (PACs)
Microcontroller peripherals are accessed through registers,
which are memory-mapped to specific addresses.
The description of the registers available on a microcontroller is provided in a Systems View Description (SVD) file.
The svd2rust
tool generates Rust code from these files,
providing a safe API to access the peripherals.
These libraries are available as Peripheral Access Crates (PACs).
Here is a list of some of the most popular PACs:
stm32-rs
provides a PAC for all STM32 microcontrollers.nrf-pacs
provides a PAC for common Nordic Semiconductor microcontrollers.esp-pacs
provides a PAC for the ESP32 series.- Other PACs are available for a wide range of microcontrollers.
Hardware Abstraction Layers (HALs)
Using these PACs, Hardware Abstraction Layers (HALs) can be built.
HALs provide a higher-level API to interact with the hardware,
abstracting the low-level details of the microcontroller.
These HALs are usually implementing traits from the embedded-hal
libary.
It contains a collection of traits for low-level hardware interfaces,
such as digital I/O, serial communication, and timers.
The advantage of using traits for these interfaces is that it allows for generic programming.
For example, a driver for a specific sensor can be written in a generic way,
so that it can be used with any micrcontroller that implements the embedded-hal
traits.
A list of implementations of the embedded-hal
traits can be found here.
Note that some operating systems and frameworks also provide their own HALs,
for examples, Embassy provides HALs
that are asynchronous and non-blocking.
Networking and Security
Networking and security are important aspects of embedded systems.
The smoltcp
crate provides a TCP/IP stack that is designed for embedded systems.
It is written in pure Rust and is designed to be memory efficient.
For CoAP, the coap-lite
crate provides a CoAP implementation in Rust.
The Rust community has also developed a number of security-related crates,
provided by the RustCrypto project.
These crates provide cryptographic algorithms, secure random number generators, and other security-related functionalities.
The rustls
crate provides a TLS implementation in Rust,
which is useable in embedded systems.
Another no_std
capable TLS implementation is the embedded-tls
crate.
A more complete list of the Embedded Rust ecoystem can be found here: Awesome Embedded Rust.