cortex_ar/register/
dfsr.rs1use arbitrary_int::{prelude::*, u4, u5};
4
5use crate::register::{SysReg, SysRegRead, SysRegWrite};
6
7use super::ifsr::FsrStatus;
8
9#[derive(Debug, Copy, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[repr(u8)]
13pub enum DfsrStatus {
14 AlignmentFault = 0b00001,
15 FaultOnInstructionCacheMaintenance = 0b00100,
16 AsyncExternalAbort = 0b10110,
17 AsyncParityErrorOnMemAccess = 0b11000,
18 CommonFsr(FsrStatus),
19}
20
21impl TryFrom<u8> for DfsrStatus {
22 type Error = u8;
23 fn try_from(value: u8) -> Result<Self, Self::Error> {
24 match value {
25 0b00001 => Ok(DfsrStatus::AlignmentFault),
26 0b00100 => Ok(DfsrStatus::FaultOnInstructionCacheMaintenance),
27 0b10110 => Ok(DfsrStatus::AsyncExternalAbort),
28 0b11000 => Ok(DfsrStatus::AsyncParityErrorOnMemAccess),
29 _ => FsrStatus::try_from(value)
30 .map(DfsrStatus::CommonFsr)
31 .map_err(|_| value),
32 }
33 }
34}
35
36#[bitbybit::bitfield(u32, defmt_bitfields(feature = "defmt"))]
38pub struct Dfsr {
39 #[bit(12, rw)]
41 ext: bool,
42 #[bit(11, rw)]
44 wnr: bool,
45 #[bits(4..=7, rw)]
46 domain: u4,
47 #[bits([0..=3, 10], rw)]
49 status_raw: u5,
50}
51
52impl SysReg for Dfsr {
53 const CP: u32 = 15;
54 const CRN: u32 = 5;
55 const OP1: u32 = 0;
56 const CRM: u32 = 0;
57 const OP2: u32 = 0;
58}
59impl crate::register::SysRegRead for Dfsr {}
60impl Dfsr {
61 pub fn status(&self) -> Result<DfsrStatus, u8> {
62 let status = self.status_raw().as_u8();
63 DfsrStatus::try_from(status).map_err(|_| status)
64 }
65
66 #[inline]
67 pub fn read() -> Dfsr {
69 unsafe { Self::new_with_raw_value(<Self as SysRegRead>::read_raw()) }
70 }
71}
72impl crate::register::SysRegWrite for Dfsr {}
73impl Dfsr {
74 #[inline]
75 pub unsafe fn write(value: Self) {
81 unsafe {
82 <Self as SysRegWrite>::write_raw(value.raw_value());
83 }
84 }
85}
86
87impl core::fmt::Debug for Dfsr {
88 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
89 write!(
90 f,
91 "DFSR {{ ext={} wnr={} Domain={:#06b} Status={:#07b} }}",
92 self.ext(),
93 self.wnr(),
94 self.domain(),
95 self.status_raw()
96 )
97 }
98}