liveBook · Manning

Chapter 5: Memory-Mapped I/O, Register Abstractions, and Low-Level Control on Cortex-M

What you will learn in chapter 5 of Embedded Software with Rust

The chapter shifts from firmware startup to correctness-sensitive, day-to-day hardware interaction on Cortex-M.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter shifts from firmware startup to correctness-sensitive, day-to-day hardware interaction on Cortex-M.

Key terms: memory-mapped I/O, barrier, peripheral

5.1 What are memory-mapped I/Os in a microcontroller?

The section explains how Cortex-M memory-mapped I/O places hardware registers in the address space and why accessing them differs from accessing ordinary memory.

Key terms: memory-mapped I/O (MMIO), hardware registers, load/store instructions, side effects, Peripheral region, register block, base addresses, fixed offset, core peripherals, System region

5.1.1 The memory map

This section locates code, data, and peripheral registers within the Cortex-M address space.

Key terms: memory map, address space, Peripheral region, register blocks

5.1.2 What is memory-mapped?

This section explains how memory-mapped I/O makes hardware registers reachable through the address map and why Cortex-M’s defined core addresses aid portability.

Key terms: MMIO, address map, registers, GPIO, Cortex-M, System region, architecturally defined addresses, SysTick, NVIC, MPU

5.1.3 From the CPU’s perspective

This section explains why CPU loads and stores to memory-mapped peripherals need attention to memory types, access ordering, and barriers.

Key terms: load/store instructions, Device memory, Normal memory, Strongly ordered, bufferable address, write buffer, system bus, memory accesses, DMB, Data Memory Barrier

5.2 Rust’s Embedded ecosystem: Abstractions and unsafe code

The section introduces why direct access to memory-mapped registers in Rust must be marked unsafe.

Key terms: memory-mapped registers, unsafe block, safe and unsafe operations

5.2.1 Unsafe code: A necessary evil

This subsection explains what an unsafe block permits, why it still requires review, and why unsafe operations should be isolated behind safe abstractions.

Key terms: unsafe blocks, safety guarantees, raw pointers, unsafe functions or methods, mutable static variables, safe abstractions, PACs, HALs

5.2.2 Accessing registers via raw pointers

The section shows direct register access through raw pointers in Rust and explains why plain dereferences are unsafe and insufficient for reliable MMIO.

Key terms: raw pointer, unsafe block, GPIOA Output Data Register (ODR), *mut u32, alignment, plain dereference, volatile MMIO access, volatile operations, PAC/HAL API, ODR

5.2.3 Using read_volatile and write_volatile

Explains how volatile register accesses constrain the compiler, why they do not provide system-wide guarantees, and when Cortex-M barriers are needed.

Key terms: core::ptr, read_volatile, write_volatile, volatile, static mut, const raw pointer, core::ptr::addr_of!, addr_of_mut!, bus masters, buffered or posted writes

5.2.4 Safer MMIO with the volatile-register crate

This section explains how a register-block struct and volatile register wrappers make MMIO clearer and restrict invalid reads and writes, while leaving important hardware-safety checks to the programmer.

Key terms: struct overlay, volatile-register, RO, WO, RW, unsafe, software contract, struct layout, reserved bits, vcell

5.2.5 Real-World examples

The section compares raw, volatile, and typed ways to access hardware registers through SysTick, NVIC, and GPIO examples.

Key terms: MMIO, raw pointer dereferencing, write_volatile, read_volatile, NVIC, Interrupt Set-Enable Register, ISER, write-one-to-set, read-modify-write, struct overlay

5.3 Working with Peripheral Access Crates (PACs) & Hardware Abstraction Layers (HALs)

The section motivates moving from direct unsafe memory-mapped I/O access toward PACs and HALs by identifying the limitations of hand-written low-level code.

Key terms: memory-mapped I/O, unsafe code, Peripheral Access Crates (PACs), Hardware Abstraction Layers (HALs)

5.3.1 From Unsafe Code to Safe Wrappers

This section shows how a small wrapper can contain unsafe register access, why STM32 GPIO wrappers should use BSRR for bit updates, and where manual wrappers fall short.

Key terms: raw register access, unsafe, wrapper, narrow, well-defined API, ODR, Output Data Register, read-modify-write, BSRR, atomic set/reset register, manual wrapper

5.3.2 Introducing svd2rust and auto-generated PACs

This subsection explains how SVD files and svd2rust produce register-level PACs, and why the quality of a PAC depends on its source data and maintenance.

Key terms: Peripheral Access Crates (PACs), System View Description (SVD), svd2rust, register-level API, PAC, SVD data, community-maintained PAC, crates.io

5.3.3 Using a PAC: stm32f1xx GPIO example

This section shows how an STM32 PAC provides exclusive, typed peripheral access, field-level register operations, and typed device interrupt identifiers.

Key terms: Peripherals, Peripherals::take(), singleton, peripheral register block, read, write, modify, read proxy, write proxy, read-modify-write

5.3.4 Moving up to the HALs

This section shows how a HAL builds on a PAC to offer higher-level peripheral APIs, type-checked pin configuration, and clock-aware timing.

Key terms: PAC, HAL, embedded-hal, register blocks, bitfields, .split(), GPIOC, pc13, push-pull output, typestate

5.3.5 The Role of embedded-hal

This section explains how embedded-hal traits let drivers work across microcontrollers and distinguishes the sibling embedded-io crate.

Key terms: embedded-hal, traits, HAL, PAC, embedded_hal::i2c::I2c, DelayNs, embedded-io, generic I/O traits, no_std

5.4 Memory & data placement considerations

This section explains why embedded Rust needs explicit control over memory layout and data placement despite the protections of safe Rust.

Key terms: memory layout, alignment, data placement, ownership model

5.4.1 Understanding data alignment and layout

This section explains why values have alignment requirements and uses a struct example to show how padding affects size and placement.

Key terms: size, alignment, misaligned memory accesses, SampleLayout, padding, most-aligned field, char

5.4.2 Struct layout

Explains why hardware register structures need a defined field layout and why that layout alone does not make register access volatile-safe.

Key terms: #[repr©], structure layout, padding, C Application Binary Interface (ABI), hardware register blocks, tail padding, vcell, VolatileCell, volatile memory access, .get()

5.4.3 Packed layout and alignment

This section contrasts packed structs, which remove padding but can cause unaligned accesses, with explicit alignment for cache-sensitive buffers.

Key terms: #[repr(packed)], #[repr(C, packed)], padding, alignment, MMIO register overlays, #[repr(align(N))], cache lines, split-line, DMA coherency, cache clean/invalidate barriers

5.4.4 Section placement with #[unsafe(link_section)]

Explains how Rust section attributes and linker scripts place code or data in device memory, using DTCM as an example and addressing placement safety and initialization.

Key terms: #[unsafe(link_section)], MEMORY, SECTIONS, linker script, TCM, ITCM, DTCM, NOLOAD, cortex-m-rt, .bss

5.4.5 Revisiting Vector Table Placement

This section explains how a Cortex-M vector table is laid out, why an STM32F1 can find it at reset, and how Rust section attributes and a linker script place it in Flash.

Key terms: vector table, Main Stack Pointer (MSP), reset handler, STM32F1, boot/memory remap logic, vector-table base address, BOOT0, BOOT1, #[unsafe(link_section)], linker script

5.5 Writing Low-Level Code with Assembly in Rust

The section explains why embedded Rust sometimes needs assembly-level access despite Rust’s high-level abstractions and memory safety guarantees.

Key terms: assembly-level access, CPU registers, critical hardware state

5.5.1 Why Write Assembly in Embedded Rust?

This subsection explains when embedded Rust needs assembly and why using it requires explicit responsibility for safety.

Key terms: assembly, processor-specific instructions, privileged CPU state, timing-critical routines, #[no_std], Rust’s safety model, side effects, register constraints, synchronization

5.5.2 Safe Intrinsics via cortex-m::asm

This section explains how cortex-m intrinsics issue precise Cortex-M operations for debugging, memory ordering, system-control changes, and waiting for work.

Key terms: cortex-m crate, intrinsic functions, inline assembly, bkpt(), breakpoint instruction, memory barriers, dmb(), dsb(), system-bus visibility, DMA descriptors

5.5.3 Writing Inline Assembly with asm!()

This section explains how to use Rust’s inline assembly for Cortex-M while accurately describing its effects to the compiler.

Key terms: core::arch::asm!(), cortex-m::asm, unsafe, nomem, nostack, preserves_flags, in(reg), out(reg), inout(reg), ABI

5.5.4 Using global_asm! for Module-Level Assembly

Explains when to use global_asm! for module-level assembly, how its symbols connect to Rust, and what ABI obligations its routines must meet.

Key terms: global_asm!, asm!, .global, .section, named symbols, ABI, callee-saved registers, stack invariants, r4–r11, unsafe extern "C"

5.5.5 Linking External Assembly Files

This section explains that Rust projects can include separate assembly files through a Cargo build script.

Key terms: .S file, build.rs, Cargo build script

5.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Startup and Initialization in Bare-Metal Rust Chapter 6: Debugging, observability, and profiling →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.