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#[cfg_attr(feature = "defmt", derive(defmt::Format))]
43#[derive(Debug, Clone, Copy, Eq, PartialEq)]
44pub enum Error {
45    /// Occurs when an expected change of a register does happen in time.
46    ///
47    /// This is returned when a bounded loop exceeds its alotted iteration count.
48    RegisterUnchanged,
49    #[cfg(not(any(feature = "l552", feature = "h5", feature = "f4")))]
50    /// Direct Memory Access (DMA) error
51    DmaError(DmaError),
52    I2cError(I2cError),
53    UsartError(UsartError),
54    TimerError(TimerError),
55    ///
56    #[cfg(not(feature = "h735"))]
57    FlashError(FlashError),
58    #[cfg(not(any(feature = "f", feature = "wb", feature = "wl", feature = "h5")))]
59    /// CRC
60    PolynomialError(PolynomialError),
61    #[cfg(not(feature = "f301"))]
62    /// SPI errors.
63    SpiError(SpiError),
64    /// Clock errors.
65    RtcError(RtcError),
66    RccError(RccError),
67    #[cfg(not(any(
68        feature = "f",
69        feature = "l4x3", // todo: PAC bug?
70        feature = "g0",
71        feature = "g431",
72        feature = "g441",
73        feature = "g471",
74        feature = "g491",
75        feature = "g4a1",
76        feature = "wl",
77        feature = "l5", // todo: PAC errors on some regs.
78        feature = "h5",
79        feature = "c0",
80    )))]
81    QspiError(QspiError),
82}
83
84#[cfg(not(any(feature = "l552", feature = "h5", feature = "f4")))]
85impl_from_error!(DmaError);
86impl_from_error!(I2cError);
87impl_from_error!(UsartError);
88impl_from_error!(TimerError);
89#[cfg(not(feature = "h735"))]
90impl_from_error!(FlashError);
91#[cfg(not(any(feature = "f", feature = "wb", feature = "wl", feature = "h5")))]
92impl_from_error!(PolynomialError);
93#[cfg(not(feature = "f301"))]
94impl_from_error!(SpiError);
95impl_from_error!(RtcError);
96impl_from_error!(RccError);
97#[cfg(not(any(
98    feature = "f",
99    feature = "l4x3", // todo: PAC bug?
100    feature = "g0",
101    feature = "g431",
102    feature = "g441",
103    feature = "g471",
104    feature = "g491",
105    feature = "g4a1",
106    feature = "wl",
107    feature = "l5", // todo: PAC errors on some regs.
108    feature = "h5",
109    feature = "c0",
110)))]
111impl_from_error!(QspiError);
112
113#[cfg(feature = "embedded_hal")]
114mod embedded_io_impl {
115    use embedded_hal::i2c::{Error as I2cEhError, ErrorKind as I2cErrorKind, NoAcknowledgeSource};
116    use embedded_io::{Error as IoError, ErrorKind as IoErrorKind};
117
118    use super::{Error, I2cError, UsartError};
119
120    // Other,
121    // NotFound,
122    // PermissionDenied,
123    // ConnectionRefused,
124    // ConnectionReset,
125    // ConnectionAborted,
126    // NotConnected,
127    // AddrInUse,
128    // AddrNotAvailable,
129    // BrokenPipe,
130    // AlreadyExists,
131    // InvalidInput,
132    // InvalidData,
133    // TimedOut,
134    // Interrupted,
135    // Unsupported,
136    // OutOfMemory,
137    // WriteZero,
138
139    impl I2cEhError for Error {
140        fn kind(&self) -> I2cErrorKind {
141            match self {
142                Error::I2cError(i) => match i {
143                    I2cError::Bus => I2cErrorKind::Bus,
144                    I2cError::Arbitration => I2cErrorKind::ArbitrationLoss,
145                    I2cError::Nack => I2cErrorKind::NoAcknowledge(NoAcknowledgeSource::Unknown),
146                    I2cError::Overrun => I2cErrorKind::Overrun,
147                    _ => I2cErrorKind::Other,
148                    // I2cError::Other => I2cErrorKind::Other,
149                },
150                _ => I2cErrorKind::Other,
151            }
152        }
153    }
154
155    impl IoError for Error {
156        fn kind(&self) -> IoErrorKind {
157            match self {
158                Error::RegisterUnchanged => IoErrorKind::TimedOut,
159                Error::UsartError(u) => match u {
160                    UsartError::Framing => IoErrorKind::Other,
161                    UsartError::Noise => IoErrorKind::Other,
162                    UsartError::Overrun => IoErrorKind::OutOfMemory,
163                    UsartError::Parity => IoErrorKind::InvalidData,
164                },
165                _ => IoErrorKind::Other,
166            }
167        }
168    }
169}