stm32_hal2/
error.rs

1//! Common error definitions.
2
3#[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", // todo: PAC bug?
12    feature = "g0",
13    feature = "g431",
14    feature = "g441",
15    feature = "g471",
16    feature = "g491",
17    feature = "g4a1",
18    feature = "wl",
19    feature = "l5", // todo: PAC errors on some regs.
20    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
38/// Alias for Result<T, Error>.
39pub type Result<T> = core::result::Result<T, Error>;
40
41/// Collection of all errors that can occur.
42#[derive(Debug, Clone, Copy, Eq, PartialEq, defmt::Format)]
43pub enum Error {
44    /// Occurs when an expected change of a register does happen in time.
45    ///
46    /// This is returned when a bounded loop exceeds its alotted iteration count.
47    RegisterUnchanged,
48    #[cfg(not(any(feature = "l552", feature = "h5", feature = "f4")))]
49    /// Direct Memory Access (DMA) error
50    DmaError(DmaError),
51    I2cError(I2cError),
52    UsartError(UsartError),
53    TimerError(TimerError),
54    ///
55    #[cfg(not(feature = "h735"))]
56    FlashError(FlashError),
57    #[cfg(not(any(feature = "f", feature = "wb", feature = "wl", feature = "h5")))]
58    /// CRC
59    PolynomialError(PolynomialError),
60    #[cfg(not(feature = "f301"))]
61    /// SPI errors.
62    SpiError(SpiError),
63    /// Clock errors.
64    RtcError(RtcError),
65    RccError(RccError),
66    #[cfg(not(any(
67        feature = "f",
68        feature = "l4x3", // todo: PAC bug?
69        feature = "g0",
70        feature = "g431",
71        feature = "g441",
72        feature = "g471",
73        feature = "g491",
74        feature = "g4a1",
75        feature = "wl",
76        feature = "l5", // todo: PAC errors on some regs.
77        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", // todo: PAC bug?
99    feature = "g0",
100    feature = "g431",
101    feature = "g441",
102    feature = "g471",
103    feature = "g491",
104    feature = "g4a1",
105    feature = "wl",
106    feature = "l5", // todo: PAC errors on some regs.
107    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    // Other,
120    // NotFound,
121    // PermissionDenied,
122    // ConnectionRefused,
123    // ConnectionReset,
124    // ConnectionAborted,
125    // NotConnected,
126    // AddrInUse,
127    // AddrNotAvailable,
128    // BrokenPipe,
129    // AlreadyExists,
130    // InvalidInput,
131    // InvalidData,
132    // TimedOut,
133    // Interrupted,
134    // Unsupported,
135    // OutOfMemory,
136    // WriteZero,
137
138    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                    // I2cError::Other => I2cErrorKind::Other,
148                },
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}