A memory barrier constrains the ordering or completion of memory operations. On Cortex-M, a buffered write can become visible on the system bus after a later read, even though the write appears first in the program. Barriers help make hand-offs to peripherals or DMA obey the required sequence. section 5.1.3 section 5.5.2
Cortex-M provides barriers for different requirements:
- DMB orders memory accesses: earlier accesses are observed before later accesses. It does not simply pause all subsequent computation. section 5.2.3 section 5.1
- DSB is stronger: it waits for preceding memory operations, including implicit operations, to complete before subsequent instructions execute. section 5.1.3 section 5.5.2
- ISB flushes the processor pipeline so subsequent instructions are fetched under preceding configuration changes, such as an MPU or CONTROL register change. section 5.2.3
Volatile access and barriers solve different problems. Rust’s read_volatile and write_volatile prevent the compiler from removing or combining register accesses, but do not alone guarantee hardware ordering or completion. Volatile also does not perform cache maintenance. section 5.1 section 5.2.3
use core::ptr;
use cortex_m::asm;
unsafe {
// Example addresses.
let store_addr: *mut u32 = 0x2000_1000 as *mut u32;
let load_addr: *const u32 = 0x4001_0000 as *const u32;
// Volatile store.
ptr::write_volatile(store_addr, 0xDEAD_BEEF);
// DMB: enforce ordering of memory operations.
// All explicit memory accesses before this point must be observed
// before any explicit memory access after it.
asm::dmb();
// Non-memory computation, memory ordering, not "pausing" the CPU.
let tmp = 3u32.wrapping_add(4);
// Will not be reordered before the store.
let value = ptr::read_volatile(load_addr);
}
This Rust example places cortex_m::asm::dmb() between a volatile store and a volatile load. The addresses are illustrative; the barrier orders memory accesses rather than pausing all computation. section 5.2.3
unsafe {
// MPU_CTRL register: 0xE000_ED94
// Alternatively accessible via cortex_m::peripheral::MPU
core::ptr::write_volatile(0xE000_ED94 as *mut u32, 0x0); // Disable MPU
asm::dsb();
// Configure MPU regions here...
core::ptr::write_volatile(0xE000_ED94 as *mut u32, 0x1); // Enable MPU
asm::dsb(); // Ensure enable write completes before ISB
asm::isb(); // Flush pipeline: subsequent instructions use new MPU config
}
This MPU configuration pattern uses DSB to complete the enable write, then ISB so subsequent instructions use the updated configuration. Region configuration is left as a placeholder. section 5.5.2
Use cortex_m::asm::dmb(), dsb(), or isb() at the point where ordering, completion, or an instruction-side change matters. Inline assembly through core::arch::asm! is an alternative. section 5.2.3



