@@ -53,3 +53,63 @@ pub const MODE_3: Mode = Mode {
5353 polarity : Polarity :: IdleHigh ,
5454 phase : Phase :: CaptureOnSecondTransition ,
5555} ;
56+
57+ /// SPI error
58+ pub trait Error : core:: fmt:: Debug {
59+ /// Convert error to a generic SPI error kind
60+ ///
61+ /// By using this method, SPI errors freely defined by HAL implementations
62+ /// can be converted to a set of generic SPI errors upon which generic
63+ /// code can act.
64+ fn kind ( & self ) -> ErrorKind ;
65+ }
66+
67+ /// SPI error kind
68+ ///
69+ /// This represents a common set of SPI operation errors. HAL implementations are
70+ /// free to define more specific or additional error types. However, by providing
71+ /// a mapping to these common SPI errors, generic code can still react to them.
72+ #[ derive( Debug , Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
73+ #[ non_exhaustive]
74+ pub enum ErrorKind {
75+ /// An unspecific bus error occurred
76+ Bus ,
77+ /// The peripheral receive buffer was overrun
78+ Overrun ,
79+ /// Multiple devices on the SPI bus are trying across each other, e.g. in a multi-master setup
80+ ModeFault ,
81+ /// CRC does not match the received data
82+ Crc ,
83+ /// Received data does not conform to the peripheral configuration
84+ FrameFormat ,
85+ /// A different error occurred. The original error may contain more information.
86+ Other ,
87+ }
88+
89+ impl Error for ErrorKind {
90+ fn kind ( & self ) -> ErrorKind {
91+ * self
92+ }
93+ }
94+
95+ impl core:: fmt:: Display for ErrorKind {
96+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
97+ match self {
98+ Self :: Bus => write ! ( f, "An unspecific bus error occurred" ) ,
99+ Self :: Overrun => write ! ( f, "The peripheral receive buffer was overrun" ) ,
100+ Self :: ModeFault => write ! (
101+ f,
102+ "Multiple devices on the SPI bus are trying across each other"
103+ ) ,
104+ Self :: Crc => write ! ( f, "CRC does not match the received data" ) ,
105+ Self :: FrameFormat => write ! (
106+ f,
107+ "Received data does not conform to the peripheral configuration"
108+ ) ,
109+ Self :: Other => write ! (
110+ f,
111+ "A different error occurred. The original error may contain more information"
112+ ) ,
113+ }
114+ }
115+ }
0 commit comments