liveBook · Manning

Memory-mapped register block

As explained in Embedded Software with Rust

A memory-mapped register block is a structured set of hardware control and status registers exposed at memory addresses for programmatic access.

A memory-mapped register block is a set of hardware registers at fixed offsets from a peripheral’s base address. The processor accesses them with ordinary loads and stores, but unlike RAM, a register read or write can have hardware effects: a write might start a timer, while a read might return a status flag. section 5.1

On Cortex-M, most device peripherals occupy the Peripheral region. For example, STM32F103 GPIOA starts at 0x4001_0800 and USART1 at 0x4001_3800. Core peripherals such as SysTick and NVIC also have memory-mapped registers, in the System region. Check the device’s memory map for the correct base address and offsets. section 5.1 section 5.1.1

Figure 5.1 Registers' address offsets
CH05 F01 Cabanis

The register-offset diagram shows how registers are arranged relative to a peripheral’s base address. section 5.1

A Rust struct can model a block’s layout. Use #[repr(C)] to preserve declaration order under C layout rules, and verify that padding and field offsets match the hardware definition. The attribute can still permit tail padding and does not make accesses volatile. In this example, vcell::VolatileCell provides volatile register reads and writes through get and set. section 5.4.2

#[repr(C)]
pub struct UsartRegisters {
    // Status Register @ 0x00
    sr: vcell::VolatileCell<u32>,
    // Data Register @ 0x04
    dr: vcell::VolatileCell<u32>,
    // Baud Rate Register @ 0x08
    brr: vcell::VolatileCell<u32>,
    // Control Register 1 @ 0x0C
    cr1: vcell::VolatileCell<u32>,
    // Control Register 2 @ 0x10
    cr2: vcell::VolatileCell<u32>,
    // Control Register 3 @ 0x14
    cr3: vcell::VolatileCell<u32>,
    // Guard Time and Prescaler Register @ 0x18
    gtpr: vcell::VolatileCell<u32>,
}

This USART register struct pairs each 32-bit field with its documented offset. section 5.4.2

Alternatively, a peripheral access crate (PAC) provides typed handles to register blocks. With the STM32F103 PAC, Peripherals::take() obtains exclusive access to the device peripherals. Registers expose read, write, and modify methods: write overwrites a register, while modify reads it, changes selected fields, and writes it back. section 5.3.3

Reset a GPIO pin with a PAC

The call gpioc.bsrr().write(|w| w.br13().set_bit()) writes the BR13 bit to the dedicated set/reset register. It avoids a read-modify-write cycle, which can race with interrupts or other contexts. Use modify when other fields in a suitable register need to be preserved. section 5.3.3

Directly accessing an arbitrary register address in Rust requires unsafe code. The programmer must ensure the access is correct; a layout attribute alone does not provide volatile access or establish safety. section 5.2 section 5.4.2

How Embedded Software with Rust covers it

How deep it goes
Explains address layout and side effects, then demonstrates Rust struct layout, volatile access, and PAC register operations.
Examples
Uses STM32F103 GPIO and USART addresses, a USART register struct, and a GPIO set/reset write.
What you can do after
Applies the pattern via the stm32f1 peripheral access crate to configure GPIO registers.
How it connects
Connects memory-mapped I/O with volatile access, Rust struct layout, and peripheral access crates.

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.