atsamd_hal/sercom/i2c/
flags.rs

1//! Flag definitions
2#![allow(clippy::identity_op)]
3#![allow(unused_braces)]
4
5use bitflags::bitflags;
6use modular_bitfield::specifiers::{B1, B5};
7use modular_bitfield::*;
8
9bitflags! {
10    /// Interrupt bitflags for I2C transactions
11    ///
12    /// The available interrupt flags are `MB`, `SB`, and `ERROR`. The binary format of the underlying bits exactly matches the
13    /// INTFLAG bits.
14    #[derive(Clone, Copy)]
15    pub struct Flags: u8 {
16        /// Master on bus interrupt
17        const MB = 0x01;
18        /// Slave on bus interrupt
19        const SB = 0x02;
20        /// Error interrupt
21        const ERROR = 0x80;
22    }
23}
24
25/// Type representing the current bus state
26#[derive(BitfieldSpecifier, PartialEq)]
27pub enum BusState {
28    Unknown = 0x00,
29    Idle = 0x01,
30    Owner = 0x02,
31    Busy = 0x03,
32}
33
34impl Default for Status {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40/// Status flags for I2C transactions
41///
42/// The available status flags are `BUSERR`, `ARBLOST`, `RXNACK`,
43/// `BUSSTATE`, `LOWTOUT` and `CLKHOLD`, `MEXTTOUT`, `SEXTTOUT` and
44/// `LENERR`. The binary format of the underlying bits exactly matches
45/// the STATUS bits.
46#[bitfield]
47#[repr(u16)]
48pub struct Status {
49    pub buserr: bool,
50    pub arblost: bool,
51    #[skip(setters)]
52    pub rxnack: bool,
53    #[skip]
54    _reserved: B1,
55    pub busstate: BusState,
56    pub lowtout: bool,
57    #[skip(setters)]
58    pub clkhold: bool,
59    pub mexttout: bool,
60    pub sexttout: bool,
61    pub lenerr: bool,
62    #[skip]
63    _reserved: B5,
64}
65
66impl Status {
67    /// Check whether [`Self`] originates from an error.
68    ///
69    /// # Errors
70    ///
71    /// Returns an error if `STATUS` contains:
72    ///
73    /// * `BUSERR` - Bus Error
74    /// * `ARBLOST` - Arbitration lost
75    /// * `LENERR` - Length error
76    /// * `RXNACK` - Receive not acknowledged
77    pub fn check_bus_error(self) -> Result<(), Error> {
78        if self.buserr() {
79            Err(Error::BusError)
80        } else if self.arblost() {
81            Err(Error::ArbitrationLost)
82        } else if self.lenerr() {
83            Err(Error::LengthError)
84        } else if self.rxnack() {
85            Err(Error::Nack)
86        } else {
87            Ok(())
88        }
89    }
90
91    pub fn is_idle(self) -> bool {
92        self.busstate() == BusState::Idle
93    }
94}
95
96/// Errors available for I2C transactions
97#[derive(Debug, Clone, Copy, Eq, PartialEq)]
98#[cfg_attr(feature = "defmt", derive(defmt::Format))]
99pub enum Error {
100    BusError,
101    ArbitrationLost,
102    LengthError,
103    Nack,
104    Timeout,
105    #[cfg(feature = "dma")]
106    Dma(crate::dmac::Error),
107}