Skip to main content

ch32_hal/can/
enums.rs

1#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2pub enum CanError {
3    /// The peripheral receive buffer was overrun.
4    Overrun,
5    // MAC sublayer errors
6    /// A bit error is detected at that bit time when the bit value that is
7    /// monitored differs from the bit value sent.
8    Bit,
9    /// Bit stuffing error - more than 5 equal bits
10    Stuff,
11    /// The CRC check sum of a received message was incorrect. The CRC of an
12    /// incoming message does not match with the CRC calculated from the received data.
13    Crc,
14    /// Form error - A fixed format part of a received message has wrong format
15    Form,
16    /// The message transmitted by the FDCAN was not acknowledged by another node.
17    Acknowledge,
18    ///  CAN is in Bus_Off state.
19    BusOff,
20    ///  CAN is in the Error_Passive state.
21    BusPassive,
22    ///  At least one of error counter has reached the Error_Warning limit of 96.
23    BusWarning,
24    /// Timeout error.
25    Timeout,
26}
27
28impl core::fmt::Display for CanError {
29    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30        match self {
31            Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
32            Self::Bit => write!(f, "Bit value that is monitored differs from the bit value sent"),
33            Self::Stuff => write!(f, "Sixth consecutive equal bits detected"),
34            Self::Crc => write!(f, "Calculated CRC sequence does not equal the received one"),
35            Self::Form => write!(f, "A fixed-form bit field contains one or more illegal bits"),
36            Self::Acknowledge => write!(f, "Transmitted frame was not acknowledged"),
37            Self::BusOff => write!(f, "The peripheral is in Bus Off mode"),
38            Self::BusPassive => write!(f, "The peripheral is in Bus Passive mode"),
39            Self::BusWarning => write!(f, "A peripheral error counter has reached the Warning threshold"),
40            Self::Timeout => write!(f, "Operation timed out"),
41        }
42    }
43}
44
45impl Into<embedded_can::ErrorKind> for CanError {
46    fn into(self) -> embedded_can::ErrorKind {
47        match self {
48            Self::Overrun => embedded_can::ErrorKind::Overrun,
49            Self::Bit => embedded_can::ErrorKind::Bit,
50            Self::Stuff => embedded_can::ErrorKind::Stuff,
51            Self::Crc => embedded_can::ErrorKind::Crc,
52            Self::Form => embedded_can::ErrorKind::Form,
53            Self::Acknowledge => embedded_can::ErrorKind::Acknowledge,
54            Self::BusOff => embedded_can::ErrorKind::Other,
55            Self::BusPassive => embedded_can::ErrorKind::Other,
56            Self::BusWarning => embedded_can::ErrorKind::Other,
57            Self::Timeout => embedded_can::ErrorKind::Other,
58        }
59    }
60}
61
62impl embedded_can::Error for CanError {
63    fn kind(&self) -> embedded_can::ErrorKind {
64        (*self).into()
65    }
66}
67
68#[derive(PartialEq)]
69pub enum CanMode {
70    Normal,
71    Silent,
72    Loopback,
73    SilentLoopback,
74}
75
76pub(crate) struct CanModeRegs {
77    /// Loopback mode setting
78    pub(crate) lbkm: bool,
79    /// Silent mode setting
80    pub(crate) silm: bool,
81}
82
83impl CanMode {
84    pub(crate) fn regs(&self) -> CanModeRegs {
85        match self {
86            CanMode::Normal => CanModeRegs {
87                lbkm: false,
88                silm: false,
89            },
90            CanMode::Silent => CanModeRegs {
91                lbkm: false,
92                silm: true,
93            },
94            CanMode::Loopback => CanModeRegs {
95                lbkm: true,
96                silm: false,
97            },
98            CanMode::SilentLoopback => CanModeRegs { lbkm: true, silm: true },
99        }
100    }
101}
102
103#[derive(Copy, Clone)]
104pub enum CanFifo {
105    Fifo0,
106    Fifo1,
107}
108
109impl CanFifo {
110    pub(crate) fn val(&self) -> usize {
111        match self {
112            CanFifo::Fifo0 => 0,
113            CanFifo::Fifo1 => 1,
114        }
115    }
116
117    pub(crate) fn val_bool(&self) -> bool {
118        match self {
119            CanFifo::Fifo0 => false,
120            CanFifo::Fifo1 => true,
121        }
122    }
123}
124
125#[derive(Debug)]
126pub enum TxStatus {
127    /// Message was sent correctly
128    Sent,
129    /// Message wasn't sent correctly due to send timeout
130    TimeoutError,
131    /// Message wasn't sent correctly due to arbitration
132    ArbitrationError,
133    /// Message wasn't sent correctly due to error
134    OtherError,
135}