1pub trait EndpointDir: Sized + 'static {
11 const DIRECTION: Direction;
13}
14
15pub struct In;
17impl EndpointDir for In {
18 const DIRECTION: Direction = Direction::In;
19}
20
21pub struct Out;
23impl EndpointDir for Out {
24 const DIRECTION: Direction = Direction::Out;
25}
26
27#[derive(Clone, Copy, PartialEq, Eq, Debug)]
29#[cfg_attr(feature = "defmt", derive(defmt::Format))]
30pub enum Direction {
31 Out = 0x00,
33 In = 0x80,
35}
36
37impl From<u8> for Direction {
38 #[inline]
39 fn from(v: u8) -> Self {
40 if v & 0x80 != 0 {
41 Direction::In
42 } else {
43 Direction::Out
44 }
45 }
46}
47
48#[derive(Clone, Copy, PartialEq, Eq, Debug)]
52#[cfg_attr(feature = "defmt", derive(defmt::Format))]
53pub enum EndpointType {
54 Control,
56 Isochronous {
58 synchronization: IsoSynchronization,
60 usage: IsoUsage,
62 },
63 Bulk,
65 Interrupt,
67}
68
69impl EndpointType {
70 pub fn bm_attributes(self) -> u8 {
72 match self {
73 EndpointType::Control => 0b00,
74 EndpointType::Bulk => 0b10,
75 EndpointType::Interrupt => 0b11,
76 EndpointType::Isochronous {
77 synchronization,
78 usage,
79 } => {
80 let sync = match synchronization {
81 IsoSynchronization::None => 0b00,
82 IsoSynchronization::Asynchronous => 0b01,
83 IsoSynchronization::Adaptive => 0b10,
84 IsoSynchronization::Synchronous => 0b11,
85 };
86 let usg = match usage {
87 IsoUsage::Data => 0b00,
88 IsoUsage::Feedback => 0b01,
89 IsoUsage::ImplicitFeedbackData => 0b10,
90 };
91 0b01 | (sync << 2) | (usg << 4)
92 }
93 }
94 }
95}
96
97#[derive(Clone, Copy, PartialEq, Eq, Debug)]
99#[cfg_attr(feature = "defmt", derive(defmt::Format))]
100pub enum IsoSynchronization {
101 None,
103 Asynchronous,
105 Adaptive,
107 Synchronous,
109}
110
111#[derive(Clone, Copy, PartialEq, Eq, Debug)]
113#[cfg_attr(feature = "defmt", derive(defmt::Format))]
114pub enum IsoUsage {
115 Data,
117 Feedback,
119 ImplicitFeedbackData,
121}
122
123#[derive(Clone, Copy, PartialEq, Eq)]
127pub struct EndpointAddress(u8);
128
129impl EndpointAddress {
130 pub const EP0_OUT: EndpointAddress = EndpointAddress(0x00);
132 pub const EP0_IN: EndpointAddress = EndpointAddress(0x80);
134
135 #[inline]
137 pub const fn new(number: u8, direction: Direction) -> Self {
138 Self((number & 0x0f) | direction as u8)
139 }
140
141 #[inline]
143 pub const fn out(number: u8) -> Self {
144 Self(number & 0x0f)
145 }
146
147 #[inline]
149 pub const fn in_(number: u8) -> Self {
150 Self((number & 0x0f) | 0x80)
151 }
152
153 #[inline]
155 pub const fn raw(self) -> u8 {
156 self.0
157 }
158
159 #[inline]
161 pub const fn number(self) -> u8 {
162 self.0 & 0x0f
163 }
164
165 #[inline]
167 pub fn direction(self) -> Direction {
168 Direction::from(self.0)
169 }
170
171 #[inline]
173 pub const fn is_in(self) -> bool {
174 self.0 & 0x80 != 0
175 }
176
177 #[inline]
179 pub const fn is_out(self) -> bool {
180 !self.is_in()
181 }
182}
183
184impl From<u8> for EndpointAddress {
185 #[inline]
186 fn from(v: u8) -> Self {
187 Self(v)
188 }
189}
190
191impl From<EndpointAddress> for u8 {
192 #[inline]
193 fn from(a: EndpointAddress) -> Self {
194 a.0
195 }
196}
197
198impl core::fmt::Debug for EndpointAddress {
199 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
200 write!(
201 f,
202 "EP{} {}",
203 self.number(),
204 if self.is_in() { "IN" } else { "OUT" }
205 )
206 }
207}
208
209#[cfg(feature = "defmt")]
210impl defmt::Format for EndpointAddress {
211 fn format(&self, f: defmt::Formatter) {
212 defmt::write!(
213 f,
214 "EP{} {}",
215 self.number(),
216 if self.is_in() { "IN" } else { "OUT" }
217 );
218 }
219}