atsamd_hal/sercom/i2c/
flags.rs
1#![allow(clippy::identity_op)]
3#![allow(unused_braces)]
4
5use bitflags::bitflags;
6use modular_bitfield::specifiers::{B1, B5};
7use modular_bitfield::*;
8
9bitflags! {
10 #[derive(Clone, Copy)]
15 pub struct Flags: u8 {
16 const MB = 0x01;
18 const SB = 0x02;
20 const ERROR = 0x80;
22 }
23}
24
25#[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#[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 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#[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}