Skip to main content

cortex_m/register/
msp.rs

1//! Main Stack Pointer
2
3#[cfg(cortex_m)]
4use core::arch::asm;
5use cortex_m_macros::asm_cfg;
6
7/// Reads the CPU register
8#[inline]
9#[asm_cfg(cortex_m)]
10pub fn read() -> u32 {
11    let r;
12    unsafe { asm!("mrs {}, MSP", out(reg) r, options(nomem, nostack, preserves_flags)) };
13    r
14}
15
16/// Writes `bits` to the CPU register
17#[inline]
18#[deprecated = "calling this function invokes Undefined Behavior, consider asm::bootstrap as an alternative"]
19#[asm_cfg(cortex_m)]
20pub unsafe fn write(bits: u32) {
21    // Technically is writing to the stack pointer "not pushing any data to the stack"?
22    // In any event, if we don't set `nostack` here, this method is useless as the new
23    // stack value is immediately mutated by returning. Really this is just not a good
24    // method.
25    unsafe { asm!("msr MSP, {}", in(reg) bits, options(nomem, nostack, preserves_flags)) };
26}
27
28/// Reads the Non-Secure CPU register from Secure state.
29///
30/// Executing this function in Non-Secure state will return zeroes.
31#[inline]
32#[asm_cfg(armv8m)]
33pub fn read_ns() -> u32 {
34    let r;
35    unsafe { asm!("mrs {}, MSP_NS", out(reg) r, options(nomem, nostack, preserves_flags)) };
36    r
37}
38
39/// Writes `bits` to the Non-Secure CPU register from Secure state.
40///
41/// Executing this function in Non-Secure state will be ignored.
42#[inline]
43#[asm_cfg(armv8m)]
44pub unsafe fn write_ns(bits: u32) {
45    unsafe { asm!("msr MSP_NS, {}", in(reg) bits, options(nomem, nostack, preserves_flags)) };
46}