|
1 | 1 | //! Serial interface
|
2 | 2 |
|
3 |
| -pub use embedded_hal::serial::{Error, ErrorKind, ErrorType}; |
| 3 | +/// Serial error |
| 4 | +pub trait Error: core::fmt::Debug { |
| 5 | + /// Convert error to a generic serial error kind |
| 6 | + /// |
| 7 | + /// By using this method, serial errors freely defined by HAL implementations |
| 8 | + /// can be converted to a set of generic serial errors upon which generic |
| 9 | + /// code can act. |
| 10 | + fn kind(&self) -> ErrorKind; |
| 11 | +} |
| 12 | + |
| 13 | +impl Error for core::convert::Infallible { |
| 14 | + fn kind(&self) -> ErrorKind { |
| 15 | + match *self {} |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +/// Serial error kind |
| 20 | +/// |
| 21 | +/// This represents a common set of serial operation errors. HAL implementations are |
| 22 | +/// free to define more specific or additional error types. However, by providing |
| 23 | +/// a mapping to these common serial errors, generic code can still react to them. |
| 24 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 25 | +#[non_exhaustive] |
| 26 | +pub enum ErrorKind { |
| 27 | + /// The peripheral receive buffer was overrun. |
| 28 | + Overrun, |
| 29 | + /// Received data does not conform to the peripheral configuration. |
| 30 | + /// Can be caused by a misconfigured device on either end of the serial line. |
| 31 | + FrameFormat, |
| 32 | + /// Parity check failed. |
| 33 | + Parity, |
| 34 | + /// Serial line is too noisy to read valid data. |
| 35 | + Noise, |
| 36 | + /// A different error occurred. The original error may contain more information. |
| 37 | + Other, |
| 38 | +} |
| 39 | + |
| 40 | +impl Error for ErrorKind { |
| 41 | + fn kind(&self) -> ErrorKind { |
| 42 | + *self |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl core::fmt::Display for ErrorKind { |
| 47 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 48 | + match self { |
| 49 | + Self::Overrun => write!(f, "The peripheral receive buffer was overrun"), |
| 50 | + Self::Parity => write!(f, "Parity check failed"), |
| 51 | + Self::Noise => write!(f, "Serial line is too noisy to read valid data"), |
| 52 | + Self::FrameFormat => write!( |
| 53 | + f, |
| 54 | + "Received data does not conform to the peripheral configuration" |
| 55 | + ), |
| 56 | + Self::Other => write!( |
| 57 | + f, |
| 58 | + "A different error occurred. The original error may contain more information" |
| 59 | + ), |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Serial error type trait |
| 65 | +/// |
| 66 | +/// This just defines the error type, to be used by the other traits. |
| 67 | +pub trait ErrorType { |
| 68 | + /// Error type |
| 69 | + type Error: Error; |
| 70 | +} |
| 71 | + |
| 72 | +impl<T: ErrorType> ErrorType for &mut T { |
| 73 | + type Error = T::Error; |
| 74 | +} |
4 | 75 |
|
5 | 76 | /// Read half of a serial interface
|
6 | 77 | ///
|
@@ -40,8 +111,7 @@ impl<T: Write<Word>, Word: Copy> Write<Word> for &mut T {
|
40 | 111 | ///
|
41 | 112 | /// TODO write example of usage
|
42 | 113 |
|
43 |
| -impl<Word, Error: embedded_hal::serial::Error> core::fmt::Write |
44 |
| - for dyn Write<Word, Error = Error> + '_ |
| 114 | +impl<Word, Error: self::Error> core::fmt::Write for dyn Write<Word, Error = Error> + '_ |
45 | 115 | where
|
46 | 116 | Word: Copy + From<u8>,
|
47 | 117 | {
|
|
0 commit comments