1#[cfg(not(any(feature = "f", feature = "wb", feature = "wl", feature = "h5")))]
4use crate::crc::PolynomialError;
5#[cfg(not(any(feature = "l552", feature = "h5", feature = "f4")))]
6use crate::dma::DmaError;
7#[cfg(not(feature = "h735"))]
8use crate::flash::FlashError;
9#[cfg(not(any(
10 feature = "f",
11 feature = "l4x3", feature = "g0",
13 feature = "g431",
14 feature = "g441",
15 feature = "g471",
16 feature = "g491",
17 feature = "g4a1",
18 feature = "wl",
19 feature = "l5", feature = "h5",
21 feature = "c0",
22)))]
23use crate::qspi::QspiError;
24#[cfg(not(feature = "f301"))]
25use crate::spi::SpiError;
26use crate::{clocks::RccError, i2c::I2cError, rtc::RtcError, timer::TimerError, usart::UsartError};
27
28macro_rules! impl_from_error {
29 ($error:ident) => {
30 impl From<$error> for Error {
31 fn from(error: $error) -> Self {
32 Self::$error(error)
33 }
34 }
35 };
36}
37
38pub type Result<T> = core::result::Result<T, Error>;
40
41#[derive(Debug, Clone, Copy, Eq, PartialEq, defmt::Format)]
43pub enum Error {
44 RegisterUnchanged,
48 #[cfg(not(any(feature = "l552", feature = "h5", feature = "f4")))]
49 DmaError(DmaError),
51 I2cError(I2cError),
52 UsartError(UsartError),
53 TimerError(TimerError),
54 #[cfg(not(feature = "h735"))]
56 FlashError(FlashError),
57 #[cfg(not(any(feature = "f", feature = "wb", feature = "wl", feature = "h5")))]
58 PolynomialError(PolynomialError),
60 #[cfg(not(feature = "f301"))]
61 SpiError(SpiError),
63 RtcError(RtcError),
65 RccError(RccError),
66 #[cfg(not(any(
67 feature = "f",
68 feature = "l4x3", feature = "g0",
70 feature = "g431",
71 feature = "g441",
72 feature = "g471",
73 feature = "g491",
74 feature = "g4a1",
75 feature = "wl",
76 feature = "l5", feature = "h5",
78 feature = "c0",
79 )))]
80 QspiError(QspiError),
81}
82
83#[cfg(not(any(feature = "l552", feature = "h5", feature = "f4")))]
84impl_from_error!(DmaError);
85impl_from_error!(I2cError);
86impl_from_error!(UsartError);
87impl_from_error!(TimerError);
88#[cfg(not(feature = "h735"))]
89impl_from_error!(FlashError);
90#[cfg(not(any(feature = "f", feature = "wb", feature = "wl", feature = "h5")))]
91impl_from_error!(PolynomialError);
92#[cfg(not(feature = "f301"))]
93impl_from_error!(SpiError);
94impl_from_error!(RtcError);
95impl_from_error!(RccError);
96#[cfg(not(any(
97 feature = "f",
98 feature = "l4x3", feature = "g0",
100 feature = "g431",
101 feature = "g441",
102 feature = "g471",
103 feature = "g491",
104 feature = "g4a1",
105 feature = "wl",
106 feature = "l5", feature = "h5",
108 feature = "c0",
109)))]
110impl_from_error!(QspiError);
111
112#[cfg(feature = "embedded_hal")]
113mod embedded_io_impl {
114 use embedded_hal::i2c::{Error as I2cEhError, ErrorKind as I2cErrorKind, NoAcknowledgeSource};
115 use embedded_io::{Error as IoError, ErrorKind as IoErrorKind};
116
117 use super::{Error, I2cError, UsartError};
118
119 impl I2cEhError for Error {
139 fn kind(&self) -> I2cErrorKind {
140 match self {
141 Error::I2cError(i) => match i {
142 I2cError::Bus => I2cErrorKind::Bus,
143 I2cError::Arbitration => I2cErrorKind::ArbitrationLoss,
144 I2cError::Nack => I2cErrorKind::NoAcknowledge(NoAcknowledgeSource::Unknown),
145 I2cError::Overrun => I2cErrorKind::Overrun,
146 _ => I2cErrorKind::Other,
147 },
149 _ => I2cErrorKind::Other,
150 }
151 }
152 }
153
154 impl IoError for Error {
155 fn kind(&self) -> IoErrorKind {
156 match self {
157 Error::RegisterUnchanged => IoErrorKind::TimedOut,
158 Error::UsartError(u) => match u {
159 UsartError::Framing => IoErrorKind::Other,
160 UsartError::Noise => IoErrorKind::Other,
161 UsartError::Overrun => IoErrorKind::OutOfMemory,
162 UsartError::Parity => IoErrorKind::InvalidData,
163 },
164 _ => IoErrorKind::Other,
165 }
166 }
167 }
168}