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
