Skip to content

Commit

Permalink
can: Add error traits like rust-embedded#296
Browse files Browse the repository at this point in the history
  • Loading branch information
timokroeger committed Oct 4, 2021
1 parent fcd75d9 commit 695e9f5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/can/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub trait Can {
type Frame: crate::can::Frame;

/// Associated error type.
type Error;
type Error: crate::can::Error;

/// Puts a frame in the transmit buffer. Blocks until space is available in
/// the transmit buffer.
Expand Down
68 changes: 68 additions & 0 deletions src/can/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,71 @@ pub trait Frame: Sized {
/// Returns the frame data (0..8 bytes in length).
fn data(&self) -> &[u8];
}

/// CAN error
pub trait Error: core::fmt::Debug {
/// Convert error to a generic CAN error kind
///
/// By using this method, CAN errors freely defined by HAL implementations
/// can be converted to a set of generic serial errors upon which generic
/// code can act.
fn kind(&self) -> ErrorKind;
}

/// CAN error kind
///
/// This represents a common set of CAN operation errors. HAL implementations are
/// free to define more specific or additional error types. However, by providing
/// a mapping to these common CAN errors, generic code can still react to them.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
/// The peripheral receive buffer was overrun.
Overrun,

// MAC sublayer errors
/// A bit error is detected at that bit time when the bit value that is
/// monitored differs from the bit value sent.
Bit,

/// A stuff error is detected at the bit time of the sixth consecutive
/// equal bit level in a frame field that shall be coded by the method
/// of bit stuffing.
Stuff,

/// Calculated CRC sequence does not equal the received one.
Crc,

/// A form error shall be detected when a fixed-form bit field contains
/// one or more illegal bits.
Form,

/// An ACK error shall be detected by a transmitter whenever it does not
/// monitor a dominant bit during the ACK slot.
Ack,
}

impl Error for ErrorKind {
fn kind(&self) -> ErrorKind {
*self
}
}

impl core::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
Self::Bit => write!(
f,
"Bit value that is monitored differs from the bit value sent"
),
Self::Stuff => write!(f, "Sixth consecutive equal bits detected"),
Self::Crc => write!(f, "Calculated CRC sequence does not equal the received one"),
Self::Form => write!(
f,
"A fixed-form bit field contains one or more illegal bits"
),
Self::Ack => write!(f, "Transmitted frame was not acknowledged"),
}
}
}
2 changes: 1 addition & 1 deletion src/can/nb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub trait Can {
type Frame: crate::can::Frame;

/// Associated error type.
type Error;
type Error: crate::can::Error;

/// Puts a frame in the transmit buffer to be sent on the bus.
///
Expand Down

0 comments on commit 695e9f5

Please sign in to comment.