diff options
| -rw-r--r-- | embassy-usb-driver/CHANGELOG.md | 2 | ||||
| -rw-r--r-- | embassy-usb-driver/src/lib.rs | 30 |
2 files changed, 32 insertions, 0 deletions
diff --git a/embassy-usb-driver/CHANGELOG.md b/embassy-usb-driver/CHANGELOG.md index 15875e087..71768d7e5 100644 --- a/embassy-usb-driver/CHANGELOG.md +++ b/embassy-usb-driver/CHANGELOG.md | |||
| @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
| 8 | <!-- next-header --> | 8 | <!-- next-header --> |
| 9 | ## Unreleased - ReleaseDate | 9 | ## Unreleased - ReleaseDate |
| 10 | 10 | ||
| 11 | - Add `EndpointOut::read_data()` and `EndpointIn::write_data()` provided methods. | ||
| 12 | |||
| 11 | ## 0.2.0 - 2025-07-16 | 13 | ## 0.2.0 - 2025-07-16 |
| 12 | 14 | ||
| 13 | - Make USB endpoint allocator methods accept an optional `EndpointAddress`. | 15 | - Make USB endpoint allocator methods accept an optional `EndpointAddress`. |
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)] |
