liveBook · Manning

Volatile access

As explained in Embedded Software with Rust

A volatile memory access is a read or write that the compiler must preserve as an observable memory operation.

A volatile access is a read or write the compiler must preserve as an observable memory operation. It is useful for memory-mapped I/O (MMIO), where a register access can have hardware side effects. A plain raw-pointer dereference is not guaranteed to be volatile: optimization may remove or combine accesses that the hardware needs to see. section 5.1 section 5.2.2

In Rust, core::ptr::read_volatile and core::ptr::write_volatile explicitly perform volatile loads and stores. The caller must still ensure that the address is valid, correctly aligned, accessible in the current privilege and security state, and appropriate for the register type. section 5.2.2 section 5.2.3

// SysTick Reload Value Register
const SYST_RVR: *mut u32 = 0xE000_E014 as *mut u32;

// SysTick Current Value Register
const SYST_CVR: *mut u32 = 0xE000_E018 as *mut u32;

unsafe {
    core::ptr::write_volatile(SYST_RVR, 0x00FF_FFFF); // Set reload value
    let current = core::ptr::read_volatile(SYST_CVR);  // Read current countdown value
}

This example writes the SysTick reload register and reads its current value using volatile operations on fixed addresses. section 5.2.3

Volatile does not provide hardware-level ordering or guarantee that buffered writes have completed. When ordering matters, such as before enabling an interrupt that depends on peripheral configuration, a DMB can enforce memory-access ordering. Use a DSB when outstanding memory operations must complete before execution continues. Volatile also does not perform cache maintenance. section 5.2.3

For a static mut, obtain a raw address with addr_of! or addr_of_mut! rather than forming a reference; this matters in the Rust 2024 edition. For hardware register blocks, #[repr(C)] controls field layout but does not make accesses volatile. A vcell::VolatileCell provides volatile .get() and .set() operations for individual fields. section 5.2.3 section 5.4.2

Writing a full register value can overwrite unrelated bits. Depending on the hardware, an atomic set/reset register or a read-modify-write pattern may be preferable. PAC and HAL APIs can also perform volatile accesses internally while providing a higher-level interface. section 5.2.2

How Embedded Software with Rust covers it

How deep it goes
Explains raw-pointer volatile reads and writes, safety constraints, and hardware-ordering limitations.
Examples
Uses SysTick register reads and writes, a static mutable register, and a USART register structure.
What you can do after
Apply it via core::ptr volatile operations or vcell::VolatileCell while accounting for address safety and ordering.
How it connects
Connects memory-mapped I/O with memory barriers, register layout, peripheral access crates, and hardware abstraction layers.

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.