Skip to main content

cortex_m/register/
apsr.rs

1//! Application Program Status Register
2
3#[cfg(cortex_m)]
4use core::arch::asm;
5use cortex_m_macros::asm_cfg;
6
7/// Application Program Status Register
8#[derive(Clone, Copy, Debug)]
9pub struct Apsr {
10    bits: u32,
11}
12
13impl Apsr {
14    /// Returns the contents of the register as raw bits
15    #[inline]
16    pub fn bits(self) -> u32 {
17        self.bits
18    }
19
20    /// DSP overflow and saturation flag
21    #[inline]
22    pub fn q(self) -> bool {
23        self.bits & (1 << 27) == (1 << 27)
24    }
25
26    /// Overflow flag
27    #[inline]
28    pub fn v(self) -> bool {
29        self.bits & (1 << 28) == (1 << 28)
30    }
31
32    /// Carry or borrow flag
33    #[inline]
34    pub fn c(self) -> bool {
35        self.bits & (1 << 29) == (1 << 29)
36    }
37
38    /// Zero flag
39    #[inline]
40    pub fn z(self) -> bool {
41        self.bits & (1 << 30) == (1 << 30)
42    }
43
44    /// Negative flag
45    #[inline]
46    pub fn n(self) -> bool {
47        self.bits & (1 << 31) == (1 << 31)
48    }
49}
50
51/// Reads the CPU register
52#[inline]
53#[asm_cfg(cortex_m)]
54pub fn read() -> Apsr {
55    let bits;
56    unsafe { asm!("mrs {}, APSR", out(reg) bits, options(nomem, nostack, preserves_flags)) };
57    Apsr { bits }
58}