1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![allow(clippy::identity_op)]
#![allow(unused_braces)]
use bitflags::bitflags;
use modular_bitfield::specifiers::{B1, B5};
use modular_bitfield::*;
bitflags! {
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,
}
#[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(())
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum Error {
BusError,
ArbitrationLost,
LengthError,
Nack,
Timeout,
}