liveBook · Manning

Hardware abstraction layer

As explained in Embedded Software with Rust

A hardware abstraction layer is a software interface that represents hardware peripherals while hiding device-specific implementation details.

A hardware abstraction layer (HAL) represents peripherals through software interfaces while hiding device-specific register details. In embedded Rust, a HAL provides types and methods for pins, serial ports, timers, and clocks, so firmware can work at a higher level than individual register fields. section 3.5.5 section 5.3.4

A peripheral access crate (PAC) exposes a chip’s register blocks and bitfields; a HAL builds on those definitions to offer application-oriented APIs. A board support package can add a particular board’s pin assignments and peripherals. PACs give fine-grained control but require more hardware knowledge; HALs reduce routine configuration work and its potential for errors. section 3.5.5 section 5.3.4

HALs can encode peripheral configuration in Rust types. For example, a pin configured for UART receive exposes operations appropriate to that mode. Calling an SPI-specific operation on it fails at compile time; changing its function requires explicit reconfiguration. section 1.4.3

For portable drivers, embedded-hal defines traits for common operations, while a chip-specific HAL supplies implementations. A driver written against a trait can work with another chip’s HAL when that HAL implements the required trait. The underlying register operations can still differ between chips. section 3.5.4 section 5.3.5

impl embedded_hal::digital::OutputPin for LedPin {
    type Error = core::convert::Infallible;

    fn set_high(&mut self) -> Result<(), Self::Error> {
        // Delegates to the HAL’s method for setting the pin high.
        self.pin.set_high();
        Ok(())
    }

    fn set_low(&mut self) -> Result<(), Self::Error> {
        // Delegates to the HAL’s method for clearing the pin.
        self.pin.set_low();
        Ok(())
    }
}

This OutputPin implementation forwards high and low operations to a HAL pin. It declares Infallible as its error type and returns success after each operation; other implementations can report errors. section 3.5.4

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);
    }
}

The blink function accepts any output pin and delay provider implementing the required traits. It ignores pin-operation results for brevity; an application that needs to handle errors must check them. section 3.5.4

Configure a GPIO pin

On an STM32F103, stm32f1xx-hal builds on the PAC to turn a GPIO peripheral into strongly typed pin objects. A pin can then be converted to a push-pull output rather than configuring each register field directly. The HAL prelude brings the extension traits needed for methods such as constrain, freeze, and split into scope. section 5.3.4

Use a generic I²C driver

An SSD1306 display driver can depend on the embedded-hal I²C trait rather than a particular controller. The selected MCU HAL supplies the concrete I²C implementation, typically using its PAC for register access. section 5.3.5

How Embedded Software with Rust covers it

How deep it goes
Works from PAC register access through typed HAL configuration and portable trait-based drivers.
Examples
Demonstrates STM32F103 GPIO blinking, a generic LED driver, and an SSD1306 I²C driver.
What it assumes
Assumes prior embedded programming experience while explaining Rust HAL configuration and abstractions.
What you can do after
Apply HALs and embedded-hal traits to configure peripherals and write portable drivers.
How it connects
Connects HALs to peripheral access crates, board support packages, type-state, and embedded-hal traits.

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.