stm32f4xx_hal/i2c/
common.rs

1#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2pub enum Address {
3    Seven(u8),
4    Ten(u16),
5}
6
7impl From<u8> for Address {
8    fn from(value: u8) -> Self {
9        Self::Seven(value)
10    }
11}
12
13impl From<u16> for Address {
14    fn from(value: u16) -> Self {
15        Self::Ten(value)
16    }
17}
18
19pub use embedded_hal::i2c::NoAcknowledgeSource;
20
21#[derive(Debug, Eq, PartialEq, Copy, Clone)]
22#[non_exhaustive]
23pub enum Error {
24    Overrun,
25    NoAcknowledge(NoAcknowledgeSource),
26    Timeout,
27    // Note: The Bus error type is not currently returned, but is maintained for compatibility.
28    Bus,
29    Crc,
30    ArbitrationLoss,
31}
32
33impl Error {
34    pub(crate) fn nack_addr(self) -> Self {
35        match self {
36            Error::NoAcknowledge(NoAcknowledgeSource::Unknown) => {
37                Error::NoAcknowledge(NoAcknowledgeSource::Address)
38            }
39            e => e,
40        }
41    }
42    pub(crate) fn nack_data(self) -> Self {
43        match self {
44            Error::NoAcknowledge(NoAcknowledgeSource::Unknown) => {
45                Error::NoAcknowledge(NoAcknowledgeSource::Data)
46            }
47            e => e,
48        }
49    }
50}
51
52use embedded_hal::i2c::ErrorKind;
53impl embedded_hal::i2c::Error for Error {
54    fn kind(&self) -> ErrorKind {
55        match *self {
56            Self::Overrun => ErrorKind::Overrun,
57            Self::Bus => ErrorKind::Bus,
58            Self::ArbitrationLoss => ErrorKind::ArbitrationLoss,
59            Self::NoAcknowledge(nack) => ErrorKind::NoAcknowledge(nack),
60            Self::Crc | Self::Timeout => ErrorKind::Other,
61        }
62    }
63}
64
65pub(crate) type Hal1Operation<'a> = embedded_hal::i2c::Operation<'a>;
66pub(crate) type Hal02Operation<'a> = embedded_hal_02::blocking::i2c::Operation<'a>;