A control and status register holds processor execution state or configuration. Unlike a memory-mapped peripheral register, a Cortex-M system register resides in the processor’s system-register file and is accessed with instructions such as MRS and MSR. section 5.5.3
Register access depends on the target architecture:
- On Cortex-M, registers such as MSP, PSP, CONTROL, and PRIMASK can be read or written with inline assembly. Some operations require privilege; changes to execution context may also require DSB followed by ISB. section 5.5.3
- On RISC-V, the riscv crate provides interaction with control and status registers (CSRs) and interrupt management. section 3.5.1
use core::arch::asm;
// asm!(): Writes 1 to PRIMASK via MSR, which masks all exceptions except NMI
// and HardFault.
#[inline(always)]
fn disable_interrupts() {
unsafe {
core::arch::asm!(
"msr PRIMASK, {reg}",
reg = in(reg) 1u32,
options(nomem, nostack),
);
}
}
This Rust asm! example writes 1 to PRIMASK with MSR, masking exceptions other than NMI and HardFault. section 5.5.3
