#![allow(clippy::identity_op)]
#![allow(unused_braces)]
use bitflags::bitflags;
use modular_bitfield::specifiers::{B1, B5};
use modular_bitfield::*;
bitflags! {
#[derive(Clone, Copy)]
pub struct Flags: u8 {
const MB = 0x01;
const SB = 0x02;
const ERROR = 0x80;
}
}
#[derive(BitfieldSpecifier, PartialEq)]
pub enum BusState {
Unknown = 0x00,
Idle = 0x01,
Owner = 0x02,
Busy = 0x03,
}
impl Default for Status {
fn default() -> Self {
Self::new()
}
}
#[bitfield]
#[repr(u16)]
pub struct Status {
pub buserr: bool,
pub arblost: bool,
#[skip(setters)]
pub rxnack: bool,
#[skip]
_reserved: B1,
pub busstate: BusState,
pub lowtout: bool,
#[skip(setters)]
pub clkhold: bool,
pub mexttout: bool,
pub sexttout: bool,
pub lenerr: bool,
#[skip]
_reserved: B5,
}
impl Status {
pub fn check_bus_error(self) -> Result<(), Error> {
if self.buserr() {
Err(Error::BusError)
} else if self.arblost() {
Err(Error::ArbitrationLost)
} else if self.lenerr() {
Err(Error::LengthError)
} else if self.rxnack() {
Err(Error::Nack)
} else {
Ok(())
}
}
pub fn is_idle(self) -> bool {
self.busstate() == BusState::Idle
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
BusError,
ArbitrationLost,
LengthError,
Nack,
Timeout,
#[cfg(feature = "dma")]
Dma(crate::dmac::Error),
}