liveBook · Manning

Peripheral access crate

As explained in Embedded Software with Rust

A peripheral access crate is a typically auto-generated Rust library that exposes fine-grained access to the registers and bit fields of a specific microcontroller family.

A peripheral access crate (PAC) is a Rust library that exposes registers and bit fields for a specific microcontroller family. It replaces hand-written addresses and offsets with named, strongly typed interfaces, but using it still requires knowledge of the hardware. section 3.5.5 section 5.3.2

Peripherals use memory-mapped registers: reading or writing their addresses interacts with hardware. A PAC makes that access more manageable than maintaining raw pointers and register offsets for an entire device. A hardware abstraction layer (HAL) builds on the PAC to provide higher-level interfaces; a board support package can add board-specific pin assignments and features. section 5.3.1 section 3.5.5

Many PACs are generated with svd2rust from an Arm System View Description (SVD), an XML description of peripheral addresses, registers, fields and interrupts. Generating a polished crate may require additional steps, so projects often use an existing community-maintained PAC. Check its maintenance and hardware support: incorrect or incomplete SVD data can produce incorrect interfaces. section 5.3.2

PACs generated by svd2rust conventionally provide a Peripherals struct. Calling Peripherals::take() obtains the device peripherals for singleton use; its fields provide typed handles to register blocks such as GPIO and clock control. section 5.3.3

Register operations have different effects:

  • read obtains the register value; write overwrites the register using a write proxy to set the desired fields. section 5.3.3
  • modify reads the register, changes selected fields and writes the result back. It preserves other fields, but the read-modify-write sequence differs from a dedicated set/reset write. section 5.3.3
gpioc.bsrr().write(|w| w.br13().set_bit());

This STM32F103 PAC write resets GPIO pin 13 through the dedicated BSRR register. It avoids a read-modify-write cycle; the peripheral clock and pin must also be configured for the intended operation. section 5.3.3 section 5.3.1

gpioc.crh().modify(|_, w| {
    w.mode13().output2() // Set mode to output, 2MHz
     .cnf13().push_pull() // Set configuration to push-pull
});

This modify call configures pin 13 as a push-pull output while preserving other register fields. section 5.3.3

A PAC also supplies device-specific external interrupt names. With cortex-m-rt and the target PAC, handler attributes map functions to interrupts; consult the PAC documentation for valid names and signatures. section 4.5.7

For portable drivers, a HAL can implement embedded-hal traits using the PAC underneath. The driver then depends on an interface such as I2C rather than a particular device’s registers. section 5.3.5

How Embedded Software with Rust covers it

How deep it goes
Explains SVD-based generation, singleton peripheral handles and register operations, while noting that a polished generated crate needs additional structuring.
Examples
Uses STM32F103 GPIO configuration and BSRR writes, plus a USART1 interrupt example.
What it assumes
Assumes familiarity with embedded Rust project configuration and builds on memory-mapped I/O and volatile access.
What you can do after
Apply it via an STM32F103 PAC to obtain peripheral handles and configure GPIO registers.
How it connects
Connects peripheral access crates to memory-mapped I/O, hardware abstraction layers, embedded-hal traits and interrupt handlers.

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.