liveBook · Manning

Embedded-hal

As explained in Embedded Software with Rust

embedded-hal is a Rust crate defining standardized traits for accessing common embedded peripherals and services independently of specific hardware.

embedded-hal is a Rust crate that defines common interfaces for embedded hardware. Its traits let a driver describe what it needs—such as an I2C bus or an output pin—without naming a particular chip. A hardware abstraction layer (HAL) supplies the concrete implementation for the device. section 3.5.3 section 5.3.5

The abstraction does not configure the hardware for you. A HAL typically uses a peripheral access crate (PAC) to perform register-level operations; application code must still obtain and configure the appropriate peripherals, pins, and clocks. section 5.3.5 section 3.5.3

Common traits cover several firmware tasks:

  • OutputPin and InputPin drive and read digital pins; DelayNs supplies timing delays. section 5.3.5
  • I2c performs bus transactions; SpiBus provides SPI bus access, while SpiDevice manages transactions with chip select. section 5.3.5
  • SetDutyCycle controls PWM outputs, such as those used for LEDs or motors. section 5.3.5
use embedded_hal::digital::OutputPin;
use embedded_hal::delay::DelayNs;

// This function can blink any LED that implements the OutputPin trait
fn blink_led<P, D>(led: &mut P, delay: &mut D) -> !
where
    P: OutputPin,
    D: DelayNs,
{
    loop {
        let _ = led.set_high();
        delay.delay_ms(500);

        let _ = led.set_low();
        delay.delay_ms(500);
    }
}

This generic blink function accepts any output pin and delay provider implementing the required traits. It discards pin errors for brevity; real firmware should handle errors appropriate to its implementation. section 3.5.4

OutputPin operations take a mutable reference and return a Result with an implementation-specific error type. A HAL may implement them by forwarding to its own pin methods; a simple GPIO implementation can use Infallible when it has no error to report. section 3.5.4

use embedded_hal::i2c::I2c;

// A generic driver function: works with any I²C bus that implements the
// embedded_hal::i2c::I2c trait, regardless of the underlying hardware.
// Probes a device by sending a valid SSD1306 display-off command (0x00, 0xAE).
fn probe_device<I: I2c>(i2c: &mut I, addr: u8) -> bool {
    i2c.write(addr, &[0x00, 0xAE]).is_ok()
}

The I2c bound lets this function send an SSD1306 display-off command through any compatible bus implementation. Its boolean reports whether the write succeeded. section 5.3.5

One sensor driver, two environments

An lsm303agr sensor driver can use an I2C device opened as /dev/i2c-1 on embedded Linux or an I2C peripheral on a bare-metal Blue Pill board. The driver calls remain similar, but bare-metal setup must configure clocks and the I2C pins; its delay provider also differs. PB6 and PB7 are configured as alternate-function open-drain pins so the I2C hardware can drive the bus, with pull-up resistors bringing lines high. section 3.5.3

For generic byte-stream I/O, the sibling embedded-io crate defines separate traits usable in no_std environments. Choose the traits that match the operation a driver needs rather than tying it to one device family. section 5.3.5 section 5.6

How Embedded Software with Rust covers it

How deep it goes
Explains trait bounds, error types, HAL implementations, and platform setup through GPIO and I2C examples.
Examples
Uses a generic LED blinker, an SSD1306 I2C command, and an lsm303agr sensor across Linux and bare metal.
What you can do after
Apply it via embedded-hal traits to write reusable peripheral code and select compatible HAL implementations.
How it connects
Connects hardware abstraction layers, peripheral access crates, memory-mapped I/O, and embedded-io.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.