Skip to main content

usb_stack/
endpoint.rs

1//! USB endpoint vocabulary.
2//!
3//! Endpoint numbers, directions, transfer types and the direction markers both
4//! directions of the stack use. The device-side handles that own an allocated
5//! endpoint live in [`crate::device::endpoint`].
6
7// ─── Direction ───────────────────────────────────────────────────────────────
8
9/// Marker trait for endpoint direction.
10pub trait EndpointDir: Sized + 'static {
11    /// The concrete direction value for this marker.
12    const DIRECTION: Direction;
13}
14
15/// Direction marker for device-to-host (IN) endpoints.
16pub struct In;
17impl EndpointDir for In {
18    const DIRECTION: Direction = Direction::In;
19}
20
21/// Direction marker for host-to-device (OUT) endpoints.
22pub struct Out;
23impl EndpointDir for Out {
24    const DIRECTION: Direction = Direction::Out;
25}
26
27/// USB transfer direction (host-centric: IN = device→host, OUT = host→device).
28#[derive(Clone, Copy, PartialEq, Eq, Debug)]
29#[cfg_attr(feature = "defmt", derive(defmt::Format))]
30pub enum Direction {
31    /// Host-to-device.
32    Out = 0x00,
33    /// Device-to-host.
34    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// ─── EndpointType ─────────────────────────────────────────────────────────────
49
50/// USB endpoint transfer type.
51#[derive(Clone, Copy, PartialEq, Eq, Debug)]
52#[cfg_attr(feature = "defmt", derive(defmt::Format))]
53pub enum EndpointType {
54    /// Control endpoint — only EP0 uses this in practice.
55    Control,
56    /// Isochronous endpoint — time-critical, unreliable.
57    Isochronous {
58        /// Synchronization type.
59        synchronization: IsoSynchronization,
60        /// Usage type.
61        usage: IsoUsage,
62    },
63    /// Bulk endpoint — large, reliable, best-effort.
64    Bulk,
65    /// Interrupt endpoint — small, reliable, bounded latency.
66    Interrupt,
67}
68
69impl EndpointType {
70    /// Encode this type into the `bmAttributes` transfer-type field (USB 2.0 §9.6.6).
71    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/// Isochronous synchronization type (USB 2.0 §5.12.4.1).
98#[derive(Clone, Copy, PartialEq, Eq, Debug)]
99#[cfg_attr(feature = "defmt", derive(defmt::Format))]
100pub enum IsoSynchronization {
101    /// No synchronization.
102    None,
103    /// Asynchronous sink/source.
104    Asynchronous,
105    /// Adaptive sink/source.
106    Adaptive,
107    /// Synchronous sink/source.
108    Synchronous,
109}
110
111/// Isochronous usage type (USB 2.0 §9.6.6).
112#[derive(Clone, Copy, PartialEq, Eq, Debug)]
113#[cfg_attr(feature = "defmt", derive(defmt::Format))]
114pub enum IsoUsage {
115    /// Isochronous data endpoint.
116    Data,
117    /// Isochronous feedback endpoint.
118    Feedback,
119    /// Implicit feedback data endpoint.
120    ImplicitFeedbackData,
121}
122
123// ─── EndpointAddress ──────────────────────────────────────────────────────────
124
125/// Type-safe USB endpoint address (number + direction encoded in one byte).
126#[derive(Clone, Copy, PartialEq, Eq)]
127pub struct EndpointAddress(u8);
128
129impl EndpointAddress {
130    /// EP0 OUT address (control, host→device).
131    pub const EP0_OUT: EndpointAddress = EndpointAddress(0x00);
132    /// EP0 IN address (control, device→host).
133    pub const EP0_IN: EndpointAddress = EndpointAddress(0x80);
134
135    /// Construct from an endpoint number and direction.
136    #[inline]
137    pub const fn new(number: u8, direction: Direction) -> Self {
138        Self((number & 0x0f) | direction as u8)
139    }
140
141    /// Construct an OUT endpoint address.
142    #[inline]
143    pub const fn out(number: u8) -> Self {
144        Self(number & 0x0f)
145    }
146
147    /// Construct an IN endpoint address.
148    #[inline]
149    pub const fn in_(number: u8) -> Self {
150        Self((number & 0x0f) | 0x80)
151    }
152
153    /// Raw address byte.
154    #[inline]
155    pub const fn raw(self) -> u8 {
156        self.0
157    }
158
159    /// Endpoint number (0–15).
160    #[inline]
161    pub const fn number(self) -> u8 {
162        self.0 & 0x0f
163    }
164
165    /// Transfer direction.
166    #[inline]
167    pub fn direction(self) -> Direction {
168        Direction::from(self.0)
169    }
170
171    /// `true` if this is an IN (device→host) endpoint.
172    #[inline]
173    pub const fn is_in(self) -> bool {
174        self.0 & 0x80 != 0
175    }
176
177    /// `true` if this is an OUT (host→device) endpoint.
178    #[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}