diff options
| author | Felipe Balbi <[email protected]> | 2025-08-29 14:32:12 -0700 |
|---|---|---|
| committer | Felipe Balbi <[email protected]> | 2025-08-29 14:45:03 -0700 |
| commit | 6dc06d47bf76e32096d5e42914cd0269476f5855 (patch) | |
| tree | cb3f3759d72b49b1de0b1213abe736fc8e5a5e24 /embassy-usb-driver/src/lib.rs | |
| parent | f86cf87f2f20f723e2ba2fe7d83908a2b3bac2d1 (diff) | |
feat: add helper to read/write full transfer blocks
instead of always transferring only USB packets, add a provided method
to transmit an entire data block by using a simple loop construct.
Signed-off-by: Felipe Balbi <[email protected]>
Diffstat (limited to 'embassy-usb-driver/src/lib.rs')
| -rw-r--r-- | embassy-usb-driver/src/lib.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/embassy-usb-driver/src/lib.rs b/embassy-usb-driver/src/lib.rs index 99616f1ec..789d6de93 100644 --- a/embassy-usb-driver/src/lib.rs +++ b/embassy-usb-driver/src/lib.rs | |||
| @@ -236,6 +236,22 @@ pub trait EndpointOut: Endpoint { | |||
| 236 | /// | 236 | /// |
| 237 | /// 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. |
| 238 | 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_data(&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 | } | ||
| 239 | } | 255 | } |
| 240 | 256 | ||
| 241 | /// USB control pipe trait. | 257 | /// USB control pipe trait. |
| @@ -349,6 +365,20 @@ pub trait ControlPipe { | |||
| 349 | pub trait EndpointIn: Endpoint { | 365 | pub trait EndpointIn: Endpoint { |
| 350 | /// Write a single packet of data to the endpoint. | 366 | /// Write a single packet of data to the endpoint. |
| 351 | 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_data(&mut self, buf: &[u8]) -> Result<(), EndpointError> { | ||
| 374 | for chunk in buf.chunks(self.info().max_packet_size as usize) { | ||
| 375 | self.write(chunk).await?; | ||
| 376 | } | ||
| 377 | if buf.len() % self.info().max_packet_size as usize == 0 { | ||
| 378 | self.write(&[]).await?; | ||
| 379 | } | ||
| 380 | Ok(()) | ||
| 381 | } | ||
| 352 | } | 382 | } |
| 353 | 383 | ||
| 354 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 384 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
