Skip to content

Commit 5f7cd3b

Browse files
committed
can: Add error traits like rust-embedded#296
1 parent 5f8ed23 commit 5f7cd3b

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/can/blocking.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub trait Can {
66
type Frame: crate::can::Frame;
77

88
/// Associated error type.
9-
type Error;
9+
type Error: crate::can::Error;
1010

1111
/// Puts a frame in the transmit buffer. Blocks until space is available in
1212
/// the transmit buffer.

src/can/mod.rs

+68
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,71 @@ pub trait Frame: Sized {
4545
/// Returns the frame data (0..8 bytes in length).
4646
fn data(&self) -> &[u8];
4747
}
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+
Ack,
90+
}
91+
92+
impl Error for ErrorKind {
93+
fn kind(&self) -> ErrorKind {
94+
*self
95+
}
96+
}
97+
98+
impl core::fmt::Display for ErrorKind {
99+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
100+
match self {
101+
Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
102+
Self::Bit => write!(
103+
f,
104+
"Bit value that is monitored differs from the bit value sent"
105+
),
106+
Self::Stuff => write!(f, "Sixth consecutive equal bits detected"),
107+
Self::Crc => write!(f, "Calculated CRC sequence does not equal the received one"),
108+
Self::Form => write!(
109+
f,
110+
"A fixed-form bit field contains one or more illegal bits"
111+
),
112+
Self::Ack => write!(f, "Transmitted frame was not acknowledged"),
113+
}
114+
}
115+
}

src/can/nb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub trait Can {
66
type Frame: crate::can::Frame;
77

88
/// Associated error type.
9-
type Error;
9+
type Error: crate::can::Error;
1010

1111
/// Puts a frame in the transmit buffer to be sent on the bus.
1212
///

0 commit comments

Comments
 (0)