Skip to content

Commit

Permalink
Implement Display and Error traits for I2C/UART error types
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Dec 19, 2024
1 parent aa4d548 commit 1a1bfe7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
26 changes: 26 additions & 0 deletions esp-hal/src/i2c/master/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,38 @@ pub enum Error {
InvalidZeroLength,
}

impl core::error::Error for Error {}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::ExceedingFifo => write!(f, "The transmission exceeded the FIFO size"),
Error::AckCheckFailed => write!(f, "The acknowledgment check failed"),
Error::TimeOut => write!(f, "A timeout occurred during transmission"),
Error::ArbitrationLost => write!(f, "The arbitration for the bus was lost"),
Error::ExecIncomplete => write!(f, "The execution of the I2C command was incomplete"),
Error::CommandNrExceeded => {
write!(f, "The number of commands issued exceeded the limit")
}
Error::InvalidZeroLength => write!(f, "Zero length read or write operation"),
}
}
}

/// I2C-specific configuration errors
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum ConfigError {}

impl core::error::Error for ConfigError {}

impl core::fmt::Display for ConfigError {
fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Ok(())
}
}

// This enum is used to keep track of the last/next operation that was/will be
// performed in an embedded-hal(-async) I2c::transaction. It is used to
// determine whether a START condition should be issued at the start of the
Expand Down
29 changes: 28 additions & 1 deletion esp-hal/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ pub enum Error {
RxParityError,
}

impl core::error::Error for Error {}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::InvalidArgument => write!(f, "An invalid configuration argument was provided"),
Error::RxFifoOvf => write!(f, "The RX FIFO overflowed"),
Error::RxGlitchDetected => write!(f, "A glitch was detected on the RX line"),
Error::RxFrameError => write!(f, "A framing error was detected on the RX line"),
Error::RxParityError => write!(f, "A parity error was detected on the RX line"),
}
}
}

impl embedded_hal_nb::serial::Error for Error {
fn kind(&self) -> embedded_hal_nb::serial::ErrorKind {
embedded_hal_nb::serial::ErrorKind::Other
Expand Down Expand Up @@ -621,10 +635,23 @@ pub struct UartRx<'d, Dm, T = AnyUart> {
pub enum ConfigError {
/// The requested timeout is not supported.
UnsupportedTimeout,
/// The requested fifo threshold is not supported.
/// The requested FIFO threshold is not supported.
UnsupportedFifoThreshold,
}

impl core::error::Error for ConfigError {}

impl core::fmt::Display for ConfigError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ConfigError::UnsupportedTimeout => write!(f, "The requested timeout is not supported"),
ConfigError::UnsupportedFifoThreshold => {
write!(f, "The requested FIFO threshold is not supported")
}
}
}
}

#[cfg(any(doc, feature = "unstable"))]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
impl<Dm, T> SetConfig for Uart<'_, Dm, T>
Expand Down

0 comments on commit 1a1bfe7

Please sign in to comment.