Skip to main content

imxrt_dma/
error.rs

1//! DMA error status
2
3use core::fmt::{self, Debug, Display};
4
5/// A wrapper around a DMA error status value
6///
7/// The wrapper contains a copy of the DMA controller's
8/// error status register at the point of an error. The
9/// wrapper implements both `Debug` and `Display`. Format
10/// the error to see a summary of the error bits.
11#[derive(Clone, Copy)]
12pub struct Error {
13    /// The raw error status
14    es: u32,
15}
16
17impl Error {
18    #[inline(always)]
19    pub(crate) const fn new(es: u32) -> Self {
20        Error { es }
21    }
22    /// Returns the raw error status value
23    #[inline(always)]
24    pub const fn raw(self) -> u32 {
25        self.es
26    }
27    #[inline(always)]
28    const fn is_bit(self, bit: u32) -> bool {
29        (self.es >> bit) & 1 != 0
30    }
31    /// Logical OR of all DMA channel error status bits
32    ///
33    /// If you have an `Error` this should always be true.
34    #[inline(always)]
35    pub const fn is_valid(self) -> bool {
36        self.is_bit(31)
37    }
38    /// Indicates if the transfer was cancelled
39    #[inline(always)]
40    pub const fn is_cancelled(self) -> bool {
41        self.is_bit(16)
42    }
43    /// Indicates a group priority error
44    #[inline(always)]
45    pub const fn is_group_priority(self) -> bool {
46        self.is_bit(15)
47    }
48    /// Indicates a channel priority error
49    #[inline(always)]
50    pub const fn is_channel_priority(self) -> bool {
51        self.is_bit(14)
52    }
53    /// Indicates the channel number
54    #[inline(always)]
55    pub const fn channel_number(self) -> u32 {
56        (self.es >> 8) & 0x1F
57    }
58    /// Indicates a source address error
59    #[inline(always)]
60    pub const fn is_source_address(self) -> bool {
61        self.is_bit(7)
62    }
63    /// Indicates a source offset error
64    #[inline(always)]
65    pub const fn is_source_offset(self) -> bool {
66        self.is_bit(6)
67    }
68    /// Indicates a destination address error
69    #[inline(always)]
70    pub const fn is_destination_address(self) -> bool {
71        self.is_bit(5)
72    }
73    /// Indicates a destination offset error
74    #[inline(always)]
75    pub const fn is_destination_offset(self) -> bool {
76        self.is_bit(4)
77    }
78    /// Indicates a minor / major loop configuration error
79    #[inline(always)]
80    pub const fn is_loop_configuration(self) -> bool {
81        self.is_bit(3)
82    }
83    /// Indicates a scatter / gather configuration error
84    #[inline(always)]
85    pub const fn is_scatter_gather(self) -> bool {
86        self.is_bit(2)
87    }
88    /// Indicates a source bus error
89    #[inline(always)]
90    pub const fn is_source_bus(self) -> bool {
91        self.is_bit(1)
92    }
93    /// Indicates a destination bus error
94    #[inline(always)]
95    pub const fn is_destination_bus(self) -> bool {
96        self.is_bit(0)
97    }
98}
99
100impl Debug for Error {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        write!(f, "DMA_ES({:#010X})", self.es)
103    }
104}
105
106impl Display for Error {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        write!(
109            f,
110            "DMA_ES: VLD {vld} ECX {ecx} GPE {gpe} CPE {cpe} ERRCHN {errchn} SAE {sae} SOE {soe} DAE {dae} DOE {doe} NCE {nce} SGE {sge} SBE {sbe} DBE {dbe}",
111            vld = self.is_valid() as u32,
112            ecx = self.is_cancelled() as u32,
113            gpe = self.is_group_priority() as u32,
114            cpe = self.is_channel_priority() as u32,
115            errchn = self.channel_number(),
116            sae = self.is_source_address() as u32,
117            soe = self.is_source_offset() as u32,
118            dae = self.is_destination_address() as u32,
119            doe = self.is_destination_offset() as u32,
120            nce = self.is_loop_configuration() as u32,
121            sge = self.is_scatter_gather() as u32,
122            sbe = self.is_source_bus() as u32,
123            dbe = self.is_destination_bus() as u32,
124        )
125    }
126}