aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb-driver/src
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-usb-driver/src')
-rw-r--r--embassy-usb-driver/src/lib.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/embassy-usb-driver/src/lib.rs b/embassy-usb-driver/src/lib.rs
index d204e4d85..3ad96c61d 100644
--- a/embassy-usb-driver/src/lib.rs
+++ b/embassy-usb-driver/src/lib.rs
@@ -136,6 +136,7 @@ pub trait Driver<'a> {
136 fn alloc_endpoint_out( 136 fn alloc_endpoint_out(
137 &mut self, 137 &mut self,
138 ep_type: EndpointType, 138 ep_type: EndpointType,
139 ep_addr: Option<EndpointAddress>,
139 max_packet_size: u16, 140 max_packet_size: u16,
140 interval_ms: u8, 141 interval_ms: u8,
141 ) -> Result<Self::EndpointOut, EndpointAllocError>; 142 ) -> Result<Self::EndpointOut, EndpointAllocError>;
@@ -153,6 +154,7 @@ pub trait Driver<'a> {
153 fn alloc_endpoint_in( 154 fn alloc_endpoint_in(
154 &mut self, 155 &mut self,
155 ep_type: EndpointType, 156 ep_type: EndpointType,
157 ep_addr: Option<EndpointAddress>,
156 max_packet_size: u16, 158 max_packet_size: u16,
157 interval_ms: u8, 159 interval_ms: u8,
158 ) -> Result<Self::EndpointIn, EndpointAllocError>; 160 ) -> Result<Self::EndpointIn, EndpointAllocError>;
@@ -234,6 +236,22 @@ pub trait EndpointOut: Endpoint {
234 /// 236 ///
235 /// This should also clear any NAK flags and prepare the endpoint to receive the next packet. 237 /// This should also clear any NAK flags and prepare the endpoint to receive the next packet.
236 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, EndpointError>; 238 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, EndpointError>;
239
240 /// Read until the buffer is full or we receive a short packet from the USB host returning the
241 /// actual length of the entire data block.
242 ///
243 /// This should also clear any NAK flags and prepare the endpoint to receive the next packet or
244 /// data block.
245 async fn read_transfer(&mut self, buf: &mut [u8]) -> Result<usize, EndpointError> {
246 let mut n = 0;
247 loop {
248 let i = self.read(&mut buf[n..]).await?;
249 n += i;
250 if i < self.info().max_packet_size as usize {
251 return Ok(n);
252 }
253 }
254 }
237} 255}
238 256
239/// USB control pipe trait. 257/// USB control pipe trait.
@@ -347,6 +365,20 @@ pub trait ControlPipe {
347pub trait EndpointIn: Endpoint { 365pub trait EndpointIn: Endpoint {
348 /// Write a single packet of data to the endpoint. 366 /// Write a single packet of data to the endpoint.
349 async fn write(&mut self, buf: &[u8]) -> Result<(), EndpointError>; 367 async fn write(&mut self, buf: &[u8]) -> Result<(), EndpointError>;
368
369 /// Write all the data from buf to the endpoint one wMaxPacketSize chunk at a time.
370 ///
371 /// If the buffer size is evenly divisible by wMaxPacketSize, this will also ensure the
372 /// terminating zero-length-packet is transmitted.
373 async fn write_transfer(&mut self, buf: &[u8], needs_zlp: bool) -> Result<(), EndpointError> {
374 for chunk in buf.chunks(self.info().max_packet_size as usize) {
375 self.write(chunk).await?;
376 }
377 if needs_zlp && buf.len() % self.info().max_packet_size as usize == 0 {
378 self.write(&[]).await?;
379 }
380 Ok(())
381 }
350} 382}
351 383
352#[derive(Copy, Clone, Eq, PartialEq, Debug)] 384#[derive(Copy, Clone, Eq, PartialEq, Debug)]