Skip to content

Commit 27709df

Browse files
committed
Implement core::error::Error for STM32 Serial Devices
1 parent 00ef474 commit 27709df

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

embassy-stm32/src/i2c/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ pub enum Error {
4444
ZeroLengthTransfer,
4545
}
4646

47+
impl core::fmt::Display for Error {
48+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49+
let message = match self {
50+
Self::Bus => "Bus Error",
51+
Self::Arbitration => "Arbitration Lost",
52+
Self::Nack => "ACK Not Received",
53+
Self::Timeout => "Request Timed Out",
54+
Self::Crc => "CRC Mismatch",
55+
Self::Overrun => "Buffer Overrun",
56+
Self::ZeroLengthTransfer => "Zero-Length Transfers are not allowed",
57+
};
58+
59+
write!(f, "{}", message)
60+
}
61+
}
62+
63+
impl core::error::Error for Error {}
64+
4765
/// I2C config
4866
#[non_exhaustive]
4967
#[derive(Copy, Clone)]

embassy-stm32/src/spi/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ pub enum Error {
3131
Overrun,
3232
}
3333

34+
impl core::fmt::Display for Error {
35+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36+
let message = match self {
37+
Self::Framing => "Invalid Framing",
38+
Self::Crc => "Hardware CRC Check Failed",
39+
Self::ModeFault => "Mode Fault",
40+
Self::Overrun => "Buffer Overrun",
41+
};
42+
43+
write!(f, "{}", message)
44+
}
45+
}
46+
47+
impl core::error::Error for Error {}
48+
3449
/// SPI bit order
3550
#[derive(Copy, Clone)]
3651
pub enum BitOrder {

embassy-stm32/src/usart/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,22 @@ pub enum Error {
293293
BufferTooLong,
294294
}
295295

296+
impl core::fmt::Display for Error {
297+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
298+
let message = match self {
299+
Self::Framing => "Framing Error",
300+
Self::Noise => "Noise Error",
301+
Self::Overrun => "RX Buffer Overrun",
302+
Self::Parity => "Parity Check Error",
303+
Self::BufferTooLong => "Buffer too large for DMA",
304+
};
305+
306+
write!(f, "{}", message)
307+
}
308+
}
309+
310+
impl core::error::Error for Error {}
311+
296312
enum ReadCompletionEvent {
297313
// DMA Read transfer completed first
298314
DmaCompleted,

0 commit comments

Comments
 (0)