Skip to content
systemcoding.dev
Embedded

PACs vs HALs vs BSPs in the Embedded Rust Ecosystem

Learn the layers of embedded Rust crates, from cortex-m and the PAC through the HAL to a board support crate, with one pin toggle written at each level.

6 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the Rust, Arm and Linux logos.

Search crates.io for a chip and you will find three or four crates with nearly the same name, and picking the wrong one costs an afternoon. stm32f4, stm32f4xx-hal, stm32f4-discovery and cortex-m are not alternatives. They are layers, stacked so that each one adds safety and convenience at the price of generality. This post names the layers, shows the same operation (turning an LED on) written at each of them with Rust 1.85, and gives a rule for choosing where to stop. It is the map behind the complete guide to embedded Rust on microcontrollers, and the vocabulary comes from the Embedded Rust book's portability chapter.

The stack, bottom to top

LayerExample crateWhat it knowsGenerated or written
Micro-architecture cratecortex-m, riscvthe CPU core: SysTick, NVIC, intrinsics such as wfiwritten
Runtime cratecortex-m-rt, riscv-rtreset, vector table, RAM init, #[entry]written
Peripheral access crate (PAC)stm32f4, rp2040-pac, nrf52840-pacevery register and bit field of one chip familygenerated from SVD
Hardware abstraction layer (HAL)stm32f4xx-hal, rp2040-hal, nrf52840-halclocks, pins, buses, built from the PACwritten
Board support crate (BSP)rp-pico, microbit-v2, stm32f3-discoverywhich pin is the LED, which crystal is fittedwritten

Two more crates sit beside the stack. embedded-hal defines traits such as OutputPin and I2c, and HALs implement them. Drivers for sensors and displays depend only on those traits, so they work on any HAL, which is the subject of writing a portable sensor driver.

The PAC: registers with names

A PAC is generated by svd2rust from the vendor's SVD file, an XML description of every peripheral, register and field (svd2rust docs). It adds no behavior. It gives each register a typed accessor, so a typo in a field name is a compile error instead of a wrong bit.

Using stm32f4 0.16 with the stm32f411 feature, here is a complete LED-on program for PA5 with nothing but the PAC and the runtime:

src/main.rsrust
#![no_std]
#![no_main]

use cortex_m_rt::entry;
use panic_halt as _;
use stm32f4::stm32f411 as pac;

#[entry]
fn main() -> ! {
    let dp = pac::Peripherals::take().unwrap();

    // Enable the GPIOA clock on the AHB1 bus.
    dp.RCC.ahb1enr().modify(|_, w| w.gpioaen().set_bit());

    // Pin 5 as a general purpose output.
    dp.GPIOA.moder().modify(|_, w| w.moder5().output());

    // Drive it high through the atomic set/reset register.
    dp.GPIOA.bsrr().write(|w| w.bs5().set_bit());

    loop {
        cortex_m::asm::wfi();
    }
}

Three register writes do the job, and you had to know the order: the clock must be enabled before the pin registers respond, the mode must be set before the level matters, and the set/reset register exists so the update is a single write rather than a read-modify-write. Nothing stops you from setting the pin high in a program that never switched the clock on. The PAC guarantees the bits exist and are named correctly; it does not guarantee the sequence.

A PAC is the right choice when no HAL exists for your chip, when you need a register the HAL does not expose, or when you write a HAL yourself.

The HAL: the sequence made safe

A HAL wraps the PAC so that the sequence is encoded in types. The same program with stm32f4xx-hal 0.21:

src/main.rsrust
#![no_std]
#![no_main]

use cortex_m_rt::entry;
use panic_halt as _;
use stm32f4xx_hal::{pac, prelude::*};

#[entry]
fn main() -> ! {
    let dp = pac::Peripherals::take().unwrap();

    let gpioa = dp.GPIOA.split();
    let mut led = gpioa.pa5.into_push_pull_output();
    led.set_high();

    loop {
        cortex_m::asm::wfi();
    }
}

split() enables the GPIOA clock for you and hands back pins as separate values. into_push_pull_output() consumes the pin and returns a different type, so setting the level of an input pin does not compile. The clock enable, the mode write and the level write still happen; the HAL did them in a fixed order you cannot get wrong. The PAC is still reachable as stm32f4xx_hal::pac, so dropping a level for one odd register is allowed.

HALs also implement the embedded-hal traits. The led above implements OutputPin, so a driver that takes impl OutputPin accepts it. That is the property that makes the ecosystem composable.

The BSP: the board's wiring

The HAL knows the chip, not the PCB. Which GPIO drives the LED, which pins run to the USB connector, what crystal frequency the clock tree should assume: that is board knowledge, and a BSP records it. With rp-pico 0.9 on the Raspberry Pi Pico:

src/main.rsrust
#![no_std]
#![no_main]

use panic_halt as _;
use rp_pico::entry;
use rp_pico::hal::{self, pac, prelude::*, Sio};

#[entry]
fn main() -> ! {
    let mut pac = pac::Peripherals::take().unwrap();
    let sio = Sio::new(pac.SIO);

    let pins = rp_pico::Pins::new(
        pac.IO_BANK0,
        pac.PADS_BANK0,
        sio.gpio_bank0,
        &mut pac.RESETS,
    );

    let mut led = pins.led.into_push_pull_output();
    led.set_high().unwrap();

    loop {
        cortex_m::asm::wfi();
    }
}

pins.led is a named field. It is GPIO25 on the Pico, but the program never says so. The BSP also re-exports the HAL (rp_pico::hal), the PAC and the runtime attribute, and it ships the second-stage bootloader the RP2040 needs, so the board crate is the only dependency a beginner adds. The cost is lock-in to one board: the same program on a different RP2040 board with the LED on a different pin needs a different BSP or none.

Choosing the layer

Pick the highest layer that covers your hardware, and drop one level only for a specific reason.

SituationLayer
Common dev board, learning, a prototypeBSP
Custom board with a supported chipHAL, own pin assignments
Supported chip, but a feature the HAL lacksHAL plus direct PAC access
New or unsupported chipwrite or generate a PAC, then a HAL
Portable library or drivernone of these: depend on embedded-hal traits only

The last row is the one people miss. A sensor driver that imports stm32f4xx-hal works on exactly one family. A driver that is generic over I2c works everywhere.

What can go wrong when layers mix

Two HAL versions, two PAC versions. HAL crates re-export their PAC. If your project depends on stm32f4 directly at one version and the HAL pulls in another, Peripherals::take() returns different types and the "singleton" is no longer unique across them. Use the HAL's re-export, stm32f4xx_hal::pac, instead of naming the PAC yourself.

Feature flags select a chip. PACs and HALs for families use Cargo features for each member (stm32f411, stm32f407). Pick exactly one. Two chip features enabled together fail to compile with a conflict error from the PAC.

A PAC bump changes the register API. svd2rust itself changed accessor style across versions, from dp.RCC.ahb1enr as a field to dp.RCC.ahb1enr() as a method. A HAL pins a compatible PAC; direct PAC use pins you to the generator version, so read the changelog when you upgrade.

When none of this applies

If you build on a vendor's C SDK (the STM32 HAL in C, the Pico SDK, ESP-IDF) and call into it from Rust, the layers above are replaced by FFI bindings. ESP32 is a partial exception: esp-hal is a pure Rust HAL generated against its own PAC-like layer, while esp-idf-hal wraps the C SDK. The two have different trade-offs and are not interchangeable.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.