Skip to main content

usb_stack/device/
endpoint.rs

1//! Endpoint handles owned by the device stack.
2//!
3//! [`Endpoint`] (and its type aliases [`EndpointIn`] / [`EndpointOut`]) provide
4//! type-safe access to an individual USB endpoint.  A class constructor receives
5//! a reference to the [`UsbBusAllocator`](crate::device::bus::UsbBusAllocator), which allocates endpoints and returns
6//! handles that borrow from that allocator.  The owned bus inside the allocator
7//! is therefore kept alive for as long as any endpoint handle exists.
8
9use core::marker::PhantomData;
10
11use crate::device::bus::UsbBus;
12use crate::device::error::UsbResult;
13use crate::endpoint::{EndpointAddress, EndpointDir, EndpointType, In, Out};
14
15// ─── Endpoint handle ──────────────────────────────────────────────────────────
16
17/// A handle to a specific USB endpoint.
18///
19/// The lifetime `'a` is tied to the [`crate::device::bus::UsbBusAllocator`] that produced
20/// this handle, ensuring the underlying bus cannot be dropped or moved while the
21/// handle is alive.
22///
23/// Use the type aliases [`EndpointIn`] and [`EndpointOut`] for convenience.
24pub struct Endpoint<'a, B: UsbBus, D: EndpointDir> {
25    /// Shared reference to the bus — valid for `'a`.
26    bus: &'a B,
27    address: EndpointAddress,
28    ep_type: EndpointType,
29    max_packet_size: u16,
30    interval: u8,
31    _dir: PhantomData<fn() -> D>,
32}
33
34impl<'a, B: UsbBus, D: EndpointDir> Endpoint<'a, B, D> {
35    /// Construct a new endpoint handle (called only by `UsbBusAllocator`).
36    pub(crate) fn new(
37        bus: &'a B,
38        address: EndpointAddress,
39        ep_type: EndpointType,
40        max_packet_size: u16,
41        interval: u8,
42    ) -> Self {
43        Self {
44            bus,
45            address,
46            ep_type,
47            max_packet_size,
48            interval,
49            _dir: PhantomData,
50        }
51    }
52
53    /// Endpoint address (number + direction).
54    #[inline]
55    pub fn address(&self) -> EndpointAddress {
56        self.address
57    }
58
59    /// Transfer type of this endpoint.
60    #[inline]
61    pub fn ep_type(&self) -> EndpointType {
62        self.ep_type
63    }
64
65    /// Maximum packet size in bytes.
66    #[inline]
67    pub fn max_packet_size(&self) -> u16 {
68        self.max_packet_size
69    }
70
71    /// Polling interval for interrupt / isochronous endpoints.
72    #[inline]
73    pub fn interval(&self) -> u8 {
74        self.interval
75    }
76
77    /// Stall this endpoint.
78    pub fn stall(&self) {
79        self.bus.set_stalled(self.address, true);
80    }
81
82    /// Clear the stall condition on this endpoint.
83    pub fn unstall(&self) {
84        self.bus.set_stalled(self.address, false);
85    }
86}
87
88/// A device-to-host (IN) endpoint handle.
89pub type EndpointIn<'a, B> = Endpoint<'a, B, In>;
90/// A host-to-device (OUT) endpoint handle.
91pub type EndpointOut<'a, B> = Endpoint<'a, B, Out>;
92
93impl<B: UsbBus> EndpointIn<'_, B> {
94    /// Write a single packet to this endpoint.
95    ///
96    /// Returns the number of bytes actually written.  If the USB peripheral's
97    /// transmit buffer is still occupied, returns [`UsbError::WouldBlock`](crate::device::error::UsbError::WouldBlock).
98    pub fn write(&self, data: &[u8]) -> UsbResult<usize> {
99        self.bus.write(self.address, data)
100    }
101}
102
103impl<B: UsbBus> EndpointOut<'_, B> {
104    /// Read a single packet from this endpoint into `buf`.
105    ///
106    /// Returns the number of bytes read.  Returns [`UsbError::WouldBlock`](crate::device::error::UsbError::WouldBlock) when
107    /// no packet is available.
108    pub fn read(&self, buf: &mut [u8]) -> UsbResult<usize> {
109        self.bus.read(self.address, buf)
110    }
111}
112
113// Safety: Endpoint<'a, B, D> is Send/Sync if B is Send/Sync; the shared &'a B
114// is sound because the bus uses interior mutability for hardware state.
115unsafe impl<B: UsbBus + Sync, D: EndpointDir> Sync for Endpoint<'_, B, D> {}
116unsafe impl<B: UsbBus + Send, D: EndpointDir> Send for Endpoint<'_, B, D> {}