1use core::marker::PhantomData;
10
11use crate::bus::UsbBus;
12use crate::error::UsbResult;
13
14pub trait EndpointDir: Sized + 'static {
18 const DIRECTION: Direction;
20}
21
22pub struct In;
24impl EndpointDir for In {
25 const DIRECTION: Direction = Direction::In;
26}
27
28pub struct Out;
30impl EndpointDir for Out {
31 const DIRECTION: Direction = Direction::Out;
32}
33
34#[derive(Clone, Copy, PartialEq, Eq, Debug)]
36#[cfg_attr(feature = "defmt", derive(defmt::Format))]
37pub enum Direction {
38 Out = 0x00,
40 In = 0x80,
42}
43
44impl From<u8> for Direction {
45 #[inline]
46 fn from(v: u8) -> Self {
47 if v & 0x80 != 0 {
48 Direction::In
49 } else {
50 Direction::Out
51 }
52 }
53}
54
55#[derive(Clone, Copy, PartialEq, Eq, Debug)]
59#[cfg_attr(feature = "defmt", derive(defmt::Format))]
60pub enum EndpointType {
61 Control,
63 Isochronous {
65 synchronization: IsoSynchronization,
67 usage: IsoUsage,
69 },
70 Bulk,
72 Interrupt,
74}
75
76impl EndpointType {
77 pub fn bm_attributes(self) -> u8 {
79 match self {
80 EndpointType::Control => 0b00,
81 EndpointType::Bulk => 0b10,
82 EndpointType::Interrupt => 0b11,
83 EndpointType::Isochronous {
84 synchronization,
85 usage,
86 } => {
87 let sync = match synchronization {
88 IsoSynchronization::None => 0b00,
89 IsoSynchronization::Asynchronous => 0b01,
90 IsoSynchronization::Adaptive => 0b10,
91 IsoSynchronization::Synchronous => 0b11,
92 };
93 let usg = match usage {
94 IsoUsage::Data => 0b00,
95 IsoUsage::Feedback => 0b01,
96 IsoUsage::ImplicitFeedbackData => 0b10,
97 };
98 0b01 | (sync << 2) | (usg << 4)
99 }
100 }
101 }
102}
103
104#[derive(Clone, Copy, PartialEq, Eq, Debug)]
106#[cfg_attr(feature = "defmt", derive(defmt::Format))]
107pub enum IsoSynchronization {
108 None,
110 Asynchronous,
112 Adaptive,
114 Synchronous,
116}
117
118#[derive(Clone, Copy, PartialEq, Eq, Debug)]
120#[cfg_attr(feature = "defmt", derive(defmt::Format))]
121pub enum IsoUsage {
122 Data,
124 Feedback,
126 ImplicitFeedbackData,
128}
129
130#[derive(Clone, Copy, PartialEq, Eq)]
134pub struct EndpointAddress(u8);
135
136impl EndpointAddress {
137 pub const EP0_OUT: EndpointAddress = EndpointAddress(0x00);
139 pub const EP0_IN: EndpointAddress = EndpointAddress(0x80);
141
142 #[inline]
144 pub const fn new(number: u8, direction: Direction) -> Self {
145 Self((number & 0x0f) | direction as u8)
146 }
147
148 #[inline]
150 pub const fn out(number: u8) -> Self {
151 Self(number & 0x0f)
152 }
153
154 #[inline]
156 pub const fn in_(number: u8) -> Self {
157 Self((number & 0x0f) | 0x80)
158 }
159
160 #[inline]
162 pub const fn raw(self) -> u8 {
163 self.0
164 }
165
166 #[inline]
168 pub const fn number(self) -> u8 {
169 self.0 & 0x0f
170 }
171
172 #[inline]
174 pub fn direction(self) -> Direction {
175 Direction::from(self.0)
176 }
177
178 #[inline]
180 pub const fn is_in(self) -> bool {
181 self.0 & 0x80 != 0
182 }
183
184 #[inline]
186 pub const fn is_out(self) -> bool {
187 !self.is_in()
188 }
189}
190
191impl From<u8> for EndpointAddress {
192 #[inline]
193 fn from(v: u8) -> Self {
194 Self(v)
195 }
196}
197
198impl From<EndpointAddress> for u8 {
199 #[inline]
200 fn from(a: EndpointAddress) -> Self {
201 a.0
202 }
203}
204
205impl core::fmt::Debug for EndpointAddress {
206 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
207 write!(
208 f,
209 "EP{} {}",
210 self.number(),
211 if self.is_in() { "IN" } else { "OUT" }
212 )
213 }
214}
215
216#[cfg(feature = "defmt")]
217impl defmt::Format for EndpointAddress {
218 fn format(&self, f: defmt::Formatter) {
219 defmt::write!(
220 f,
221 "EP{} {}",
222 self.number(),
223 if self.is_in() { "IN" } else { "OUT" }
224 );
225 }
226}
227
228pub struct Endpoint<'a, B: UsbBus, D: EndpointDir> {
238 bus: &'a B,
240 address: EndpointAddress,
241 ep_type: EndpointType,
242 max_packet_size: u16,
243 interval: u8,
244 _dir: PhantomData<fn() -> D>,
245}
246
247impl<'a, B: UsbBus, D: EndpointDir> Endpoint<'a, B, D> {
248 pub(crate) fn new(
250 bus: &'a B,
251 address: EndpointAddress,
252 ep_type: EndpointType,
253 max_packet_size: u16,
254 interval: u8,
255 ) -> Self {
256 Self {
257 bus,
258 address,
259 ep_type,
260 max_packet_size,
261 interval,
262 _dir: PhantomData,
263 }
264 }
265
266 #[inline]
268 pub fn address(&self) -> EndpointAddress {
269 self.address
270 }
271
272 #[inline]
274 pub fn ep_type(&self) -> EndpointType {
275 self.ep_type
276 }
277
278 #[inline]
280 pub fn max_packet_size(&self) -> u16 {
281 self.max_packet_size
282 }
283
284 #[inline]
286 pub fn interval(&self) -> u8 {
287 self.interval
288 }
289
290 pub fn stall(&self) {
292 self.bus.set_stalled(self.address, true);
293 }
294
295 pub fn unstall(&self) {
297 self.bus.set_stalled(self.address, false);
298 }
299}
300
301pub type EndpointIn<'a, B> = Endpoint<'a, B, In>;
303pub type EndpointOut<'a, B> = Endpoint<'a, B, Out>;
305
306impl<B: UsbBus> EndpointIn<'_, B> {
307 pub fn write(&self, data: &[u8]) -> UsbResult<usize> {
312 self.bus.write(self.address, data)
313 }
314}
315
316impl<B: UsbBus> EndpointOut<'_, B> {
317 pub fn read(&self, buf: &mut [u8]) -> UsbResult<usize> {
322 self.bus.read(self.address, buf)
323 }
324}
325
326unsafe impl<B: UsbBus + Sync, D: EndpointDir> Sync for Endpoint<'_, B, D> {}
329unsafe impl<B: UsbBus + Send, D: EndpointDir> Send for Endpoint<'_, B, D> {}