|
| 1 | +//! Controller Area Network |
| 2 | +
|
| 3 | +pub mod blocking; |
| 4 | +pub mod nb; |
| 5 | + |
| 6 | +mod id; |
| 7 | + |
| 8 | +pub use id::*; |
| 9 | + |
| 10 | +/// A CAN2.0 Frame |
| 11 | +pub trait Frame: Sized { |
| 12 | + /// Creates a new frame. |
| 13 | + /// Returns an error when the data slice is too long. |
| 14 | + fn new(id: impl Into<Id>, data: &[u8]) -> Result<Self, ()>; |
| 15 | + |
| 16 | + /// Creates a new remote frame (RTR bit set). |
| 17 | + /// Returns an error when the data length code (DLC) is not valid. |
| 18 | + fn new_remote(id: impl Into<Id>, dlc: usize) -> Result<Self, ()>; |
| 19 | + |
| 20 | + /// Returns true if this frame is a extended frame. |
| 21 | + fn is_extended(&self) -> bool; |
| 22 | + |
| 23 | + /// Returns true if this frame is a standard frame. |
| 24 | + fn is_standard(&self) -> bool { |
| 25 | + !self.is_extended() |
| 26 | + } |
| 27 | + |
| 28 | + /// Returns true if this frame is a remote frame. |
| 29 | + fn is_remote_frame(&self) -> bool; |
| 30 | + |
| 31 | + /// Returns true if this frame is a data frame. |
| 32 | + fn is_data_frame(&self) -> bool { |
| 33 | + !self.is_remote_frame() |
| 34 | + } |
| 35 | + |
| 36 | + /// Returns the frame identifier. |
| 37 | + fn id(&self) -> Id; |
| 38 | + |
| 39 | + /// Returns the data length code (DLC) which is in the range 0..8. |
| 40 | + /// |
| 41 | + /// For data frames the DLC value always matches the length of the data. |
| 42 | + /// Remote frames do not carry any data, yet the DLC can be greater than 0. |
| 43 | + fn dlc(&self) -> usize; |
| 44 | + |
| 45 | + /// Returns the frame data (0..8 bytes in length). |
| 46 | + fn data(&self) -> &[u8]; |
| 47 | +} |
| 48 | + |
| 49 | +/// CAN error |
| 50 | +pub trait Error: core::fmt::Debug { |
| 51 | + /// Convert error to a generic CAN error kind |
| 52 | + /// |
| 53 | + /// By using this method, CAN errors freely defined by HAL implementations |
| 54 | + /// can be converted to a set of generic serial errors upon which generic |
| 55 | + /// code can act. |
| 56 | + fn kind(&self) -> ErrorKind; |
| 57 | +} |
| 58 | + |
| 59 | +/// CAN error kind |
| 60 | +/// |
| 61 | +/// This represents a common set of CAN operation errors. HAL implementations are |
| 62 | +/// free to define more specific or additional error types. However, by providing |
| 63 | +/// a mapping to these common CAN errors, generic code can still react to them. |
| 64 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 65 | +#[non_exhaustive] |
| 66 | +pub enum ErrorKind { |
| 67 | + /// The peripheral receive buffer was overrun. |
| 68 | + Overrun, |
| 69 | + |
| 70 | + // MAC sublayer errors |
| 71 | + /// A bit error is detected at that bit time when the bit value that is |
| 72 | + /// monitored differs from the bit value sent. |
| 73 | + Bit, |
| 74 | + |
| 75 | + /// A stuff error is detected at the bit time of the sixth consecutive |
| 76 | + /// equal bit level in a frame field that shall be coded by the method |
| 77 | + /// of bit stuffing. |
| 78 | + Stuff, |
| 79 | + |
| 80 | + /// Calculated CRC sequence does not equal the received one. |
| 81 | + Crc, |
| 82 | + |
| 83 | + /// A form error shall be detected when a fixed-form bit field contains |
| 84 | + /// one or more illegal bits. |
| 85 | + Form, |
| 86 | + |
| 87 | + /// An ACK error shall be detected by a transmitter whenever it does not |
| 88 | + /// monitor a dominant bit during the ACK slot. |
| 89 | + Acknowledge, |
| 90 | + |
| 91 | + /// A different error occurred. The original error may contain more information. |
| 92 | + Other, |
| 93 | +} |
| 94 | + |
| 95 | +impl Error for ErrorKind { |
| 96 | + fn kind(&self) -> ErrorKind { |
| 97 | + *self |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl core::fmt::Display for ErrorKind { |
| 102 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 103 | + match self { |
| 104 | + Self::Overrun => write!(f, "The peripheral receive buffer was overrun"), |
| 105 | + Self::Bit => write!( |
| 106 | + f, |
| 107 | + "Bit value that is monitored differs from the bit value sent" |
| 108 | + ), |
| 109 | + Self::Stuff => write!(f, "Sixth consecutive equal bits detected"), |
| 110 | + Self::Crc => write!(f, "Calculated CRC sequence does not equal the received one"), |
| 111 | + Self::Form => write!( |
| 112 | + f, |
| 113 | + "A fixed-form bit field contains one or more illegal bits" |
| 114 | + ), |
| 115 | + Self::Acknowledge => write!(f, "Transmitted frame was not acknowledged"), |
| 116 | + Self::Other => write!( |
| 117 | + f, |
| 118 | + "A different error occurred. The original error may contain more information" |
| 119 | + ), |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments