DSB (Data Synchronization Barrier) is a Cortex-M instruction that waits for preceding memory operations, including implicit operations, to complete before any subsequent instruction executes. It is useful when firmware must ensure that an operation has completed at the system level before continuing. section 5.1.3 section 5.2.3
Program order does not always match bus visibility: a write can remain in a buffer while a later read proceeds. DMB orders memory accesses; DSB is stricter and also holds up subsequent non-memory instructions until preceding operations complete. DSB is more costly. section 5.1.3 section 5.5.2
STR r0, [r1] ; Store to a bufferable address
DSB ; Data Synchronization Barrier: ensures ALL pending memory
; operations (explicit and implicit, including cache line
; fills) complete before any subsequent instruction executes
ADD r2, r3, r4 ; Non-memory instruction: cannot execute until the DSB
; has fully completed, unlike with DMB
LDR r5, [r6] ; Load: guaranteed to occur after all prior memory
; operations have fully completed
The store must complete before the DSB allows the addition or subsequent load to execute. section 5.1.3
Volatile reads and writes control how the compiler treats an access; they do not by themselves ensure system-wide ordering or completion of buffered writes. Volatile access also does not perform cache maintenance. Use a barrier when hardware-visible ordering or completion is required. section 5.2.3
In Rust, prefer cortex_m::asm::dsb() to emit the barrier. Inline assembly via core::arch::asm! is an alternative when available intrinsics are insufficient. section 5.2.3 section 5.6
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
}
When reconfiguring the MPU, the second DSB completes the enable write before ISB flushes the pipeline so subsequent instructions use the new configuration. section 5.5.2
