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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use bitflags::bitflags;
use core::convert::TryFrom;
const DRE: u8 = 0x01;
const TXC: u8 = 0x02;
const RXC: u8 = 0x04;
const RXS: u8 = 0x08;
pub(super) const CTSIC: u8 = 0x10;
const RXBRK: u8 = 0x20;
const ERROR: u8 = 0x80;
pub const RX_FLAG_MASK: u8 = RXC | RXS | RXBRK | ERROR;
pub const TX_FLAG_MASK: u8 = DRE | TXC;
pub const DUPLEX_FLAG_MASK: u8 = RX_FLAG_MASK | TX_FLAG_MASK;
bitflags! {
pub struct Flags: u8 {
const DRE = DRE;
const TXC = TXC;
const RXC = RXC;
const RXS = RXS ;
const CTSIC = CTSIC;
const RXBRK = RXBRK;
const ERROR = ERROR;
}
}
const PERR: u16 = 0x01;
const FERR: u16 = 0x02;
const BUFOVF: u16 = 0x04;
const CTS: u16 = 0x08;
const ISF: u16 = 0x10;
const COLL: u16 = 0x20;
pub const RX_STATUS_MASK: u16 = PERR | FERR | BUFOVF | ISF | COLL;
pub const DUPLEX_STATUS_MASK: u16 = RX_STATUS_MASK;
bitflags! {
pub struct Status: u16 {
const PERR = PERR;
const FERR = FERR;
const BUFOVF = BUFOVF;
const CTS = CTS;
const ISF = ISF;
const COLL = COLL;
}
}
#[derive(Debug, Clone, Copy)]
pub enum Error {
ParityError,
FrameError,
Overflow,
InconsistentSyncField,
CollisionDetected,
}
impl TryFrom<Status> for () {
type Error = Error;
#[inline]
fn try_from(errors: Status) -> Result<(), Error> {
use Error::*;
if errors.contains(Status::PERR) {
Err(ParityError)
} else if errors.contains(Status::FERR) {
Err(FrameError)
} else if errors.contains(Status::BUFOVF) {
Err(Overflow)
} else if errors.contains(Status::ISF) {
Err(InconsistentSyncField)
} else if errors.contains(Status::COLL) {
Err(CollisionDetected)
} else {
Ok(())
}
}
}
impl From<Error> for Status {
#[inline]
fn from(err: Error) -> Self {
use Error::*;
match err {
ParityError => Status::PERR,
FrameError => Status::FERR,
Overflow => Status::BUFOVF,
InconsistentSyncField => Status::ISF,
CollisionDetected => Status::COLL,
}
}
}