diff options
| author | Rafael Bachmann <[email protected]> | 2023-10-15 23:45:44 +0200 |
|---|---|---|
| committer | Rafael Bachmann <[email protected]> | 2023-10-15 23:52:44 +0200 |
| commit | 31d4516516940720101300a40d0d6d2bb8d1728e (patch) | |
| tree | ce44bfebf56fea7726bccae7d2617efd360af319 /embassy-usb/src/class | |
| parent | 66e62e999409fd6967ab959a061f7eae660102d0 (diff) | |
Apply Pedantic Clippy Lints
Diffstat (limited to 'embassy-usb/src/class')
| -rw-r--r-- | embassy-usb/src/class/cdc_acm.rs | 45 | ||||
| -rw-r--r-- | embassy-usb/src/class/cdc_ncm/mod.rs | 48 | ||||
| -rw-r--r-- | embassy-usb/src/class/hid.rs | 20 | ||||
| -rw-r--r-- | embassy-usb/src/class/midi.rs | 16 |
4 files changed, 59 insertions, 70 deletions
diff --git a/embassy-usb/src/class/cdc_acm.rs b/embassy-usb/src/class/cdc_acm.rs index 790f6faab..f1066d2f2 100644 --- a/embassy-usb/src/class/cdc_acm.rs +++ b/embassy-usb/src/class/cdc_acm.rs | |||
| @@ -11,7 +11,7 @@ use embassy_sync::waitqueue::WakerRegistration; | |||
| 11 | 11 | ||
| 12 | use crate::control::{self, InResponse, OutResponse, Recipient, Request, RequestType}; | 12 | use crate::control::{self, InResponse, OutResponse, Recipient, Request, RequestType}; |
| 13 | use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut}; | 13 | use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut}; |
| 14 | use crate::types::*; | 14 | use crate::types::InterfaceNumber; |
| 15 | use crate::{Builder, Handler}; | 15 | use crate::{Builder, Handler}; |
| 16 | 16 | ||
| 17 | /// This should be used as `device_class` when building the `UsbDevice`. | 17 | /// This should be used as `device_class` when building the `UsbDevice`. |
| @@ -50,7 +50,7 @@ impl<'a> State<'a> { | |||
| 50 | pub fn new() -> Self { | 50 | pub fn new() -> Self { |
| 51 | Self { | 51 | Self { |
| 52 | control: MaybeUninit::uninit(), | 52 | control: MaybeUninit::uninit(), |
| 53 | shared: Default::default(), | 53 | shared: ControlShared::default(), |
| 54 | } | 54 | } |
| 55 | } | 55 | } |
| 56 | } | 56 | } |
| @@ -61,9 +61,9 @@ impl<'a> State<'a> { | |||
| 61 | /// writing USB packets with no intermediate buffers, but it will not act like a stream-like serial | 61 | /// writing USB packets with no intermediate buffers, but it will not act like a stream-like serial |
| 62 | /// port. The following constraints must be followed if you use this class directly: | 62 | /// port. The following constraints must be followed if you use this class directly: |
| 63 | /// | 63 | /// |
| 64 | /// - `read_packet` must be called with a buffer large enough to hold max_packet_size bytes. | 64 | /// - `read_packet` must be called with a buffer large enough to hold `max_packet_size` bytes. |
| 65 | /// - `write_packet` must not be called with a buffer larger than max_packet_size bytes. | 65 | /// - `write_packet` must not be called with a buffer larger than `max_packet_size` bytes. |
| 66 | /// - If you write a packet that is exactly max_packet_size bytes long, it won't be processed by the | 66 | /// - If you write a packet that is exactly `max_packet_size` bytes long, it won't be processed by the |
| 67 | /// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP) | 67 | /// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP) |
| 68 | /// can be sent if there is no other data to send. This is because USB bulk transactions must be | 68 | /// can be sent if there is no other data to send. This is because USB bulk transactions must be |
| 69 | /// terminated with a short packet, even if the bulk endpoint is used for stream-like data. | 69 | /// terminated with a short packet, even if the bulk endpoint is used for stream-like data. |
| @@ -109,17 +109,16 @@ impl Default for ControlShared { | |||
| 109 | 109 | ||
| 110 | impl ControlShared { | 110 | impl ControlShared { |
| 111 | async fn changed(&self) { | 111 | async fn changed(&self) { |
| 112 | poll_fn(|cx| match self.changed.load(Ordering::Relaxed) { | 112 | poll_fn(|cx| { |
| 113 | true => { | 113 | if self.changed.load(Ordering::Relaxed) { |
| 114 | self.changed.store(false, Ordering::Relaxed); | 114 | self.changed.store(false, Ordering::Relaxed); |
| 115 | Poll::Ready(()) | 115 | Poll::Ready(()) |
| 116 | } | 116 | } else { |
| 117 | false => { | ||
| 118 | self.waker.borrow_mut().register(cx.waker()); | 117 | self.waker.borrow_mut().register(cx.waker()); |
| 119 | Poll::Pending | 118 | Poll::Pending |
| 120 | } | 119 | } |
| 121 | }) | 120 | }) |
| 122 | .await | 121 | .await; |
| 123 | } | 122 | } |
| 124 | } | 123 | } |
| 125 | 124 | ||
| @@ -198,7 +197,7 @@ impl<'d> Handler for Control<'d> { | |||
| 198 | // REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below. | 197 | // REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below. |
| 199 | REQ_GET_LINE_CODING if req.length == 7 => { | 198 | REQ_GET_LINE_CODING if req.length == 7 => { |
| 200 | debug!("Sending line coding"); | 199 | debug!("Sending line coding"); |
| 201 | let coding = self.shared().line_coding.lock(|x| x.get()); | 200 | let coding = self.shared().line_coding.lock(Cell::get); |
| 202 | assert!(buf.len() >= 7); | 201 | assert!(buf.len() >= 7); |
| 203 | buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes()); | 202 | buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes()); |
| 204 | buf[4] = coding.stop_bits as u8; | 203 | buf[4] = coding.stop_bits as u8; |
| @@ -212,8 +211,8 @@ impl<'d> Handler for Control<'d> { | |||
| 212 | } | 211 | } |
| 213 | 212 | ||
| 214 | impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { | 213 | impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { |
| 215 | /// Creates a new CdcAcmClass with the provided UsbBus and max_packet_size in bytes. For | 214 | /// Creates a new CdcAcmClass with the provided UsbBus and `max_packet_size` in bytes. For |
| 216 | /// full-speed devices, max_packet_size has to be one of 8, 16, 32 or 64. | 215 | /// full-speed devices, `max_packet_size` has to be one of 8, 16, 32 or 64. |
| 217 | pub fn new(builder: &mut Builder<'d, D>, state: &'d mut State<'d>, max_packet_size: u16) -> Self { | 216 | pub fn new(builder: &mut Builder<'d, D>, state: &'d mut State<'d>, max_packet_size: u16) -> Self { |
| 218 | assert!(builder.control_buf_len() >= 7); | 217 | assert!(builder.control_buf_len() >= 7); |
| 219 | 218 | ||
| @@ -289,7 +288,7 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { | |||
| 289 | /// Gets the current line coding. The line coding contains information that's mainly relevant | 288 | /// Gets the current line coding. The line coding contains information that's mainly relevant |
| 290 | /// for USB to UART serial port emulators, and can be ignored if not relevant. | 289 | /// for USB to UART serial port emulators, and can be ignored if not relevant. |
| 291 | pub fn line_coding(&self) -> LineCoding { | 290 | pub fn line_coding(&self) -> LineCoding { |
| 292 | self.control.line_coding.lock(|x| x.get()) | 291 | self.control.line_coding.lock(Cell::get) |
| 293 | } | 292 | } |
| 294 | 293 | ||
| 295 | /// Gets the DTR (data terminal ready) state | 294 | /// Gets the DTR (data terminal ready) state |
| @@ -314,7 +313,7 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { | |||
| 314 | 313 | ||
| 315 | /// Waits for the USB host to enable this interface | 314 | /// Waits for the USB host to enable this interface |
| 316 | pub async fn wait_connection(&mut self) { | 315 | pub async fn wait_connection(&mut self) { |
| 317 | self.read_ep.wait_enabled().await | 316 | self.read_ep.wait_enabled().await; |
| 318 | } | 317 | } |
| 319 | 318 | ||
| 320 | /// Split the class into a sender and receiver. | 319 | /// Split the class into a sender and receiver. |
| @@ -362,7 +361,7 @@ pub struct ControlChanged<'d> { | |||
| 362 | impl<'d> ControlChanged<'d> { | 361 | impl<'d> ControlChanged<'d> { |
| 363 | /// Return a future for when the control settings change | 362 | /// Return a future for when the control settings change |
| 364 | pub async fn control_changed(&self) { | 363 | pub async fn control_changed(&self) { |
| 365 | self.control.changed().await | 364 | self.control.changed().await; |
| 366 | } | 365 | } |
| 367 | } | 366 | } |
| 368 | 367 | ||
| @@ -384,7 +383,7 @@ impl<'d, D: Driver<'d>> Sender<'d, D> { | |||
| 384 | /// Gets the current line coding. The line coding contains information that's mainly relevant | 383 | /// Gets the current line coding. The line coding contains information that's mainly relevant |
| 385 | /// for USB to UART serial port emulators, and can be ignored if not relevant. | 384 | /// for USB to UART serial port emulators, and can be ignored if not relevant. |
| 386 | pub fn line_coding(&self) -> LineCoding { | 385 | pub fn line_coding(&self) -> LineCoding { |
| 387 | self.control.line_coding.lock(|x| x.get()) | 386 | self.control.line_coding.lock(Cell::get) |
| 388 | } | 387 | } |
| 389 | 388 | ||
| 390 | /// Gets the DTR (data terminal ready) state | 389 | /// Gets the DTR (data terminal ready) state |
| @@ -404,7 +403,7 @@ impl<'d, D: Driver<'d>> Sender<'d, D> { | |||
| 404 | 403 | ||
| 405 | /// Waits for the USB host to enable this interface | 404 | /// Waits for the USB host to enable this interface |
| 406 | pub async fn wait_connection(&mut self) { | 405 | pub async fn wait_connection(&mut self) { |
| 407 | self.write_ep.wait_enabled().await | 406 | self.write_ep.wait_enabled().await; |
| 408 | } | 407 | } |
| 409 | } | 408 | } |
| 410 | 409 | ||
| @@ -426,7 +425,7 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> { | |||
| 426 | /// Gets the current line coding. The line coding contains information that's mainly relevant | 425 | /// Gets the current line coding. The line coding contains information that's mainly relevant |
| 427 | /// for USB to UART serial port emulators, and can be ignored if not relevant. | 426 | /// for USB to UART serial port emulators, and can be ignored if not relevant. |
| 428 | pub fn line_coding(&self) -> LineCoding { | 427 | pub fn line_coding(&self) -> LineCoding { |
| 429 | self.control.line_coding.lock(|x| x.get()) | 428 | self.control.line_coding.lock(Cell::get) |
| 430 | } | 429 | } |
| 431 | 430 | ||
| 432 | /// Gets the DTR (data terminal ready) state | 431 | /// Gets the DTR (data terminal ready) state |
| @@ -446,7 +445,7 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> { | |||
| 446 | 445 | ||
| 447 | /// Waits for the USB host to enable this interface | 446 | /// Waits for the USB host to enable this interface |
| 448 | pub async fn wait_connection(&mut self) { | 447 | pub async fn wait_connection(&mut self) { |
| 449 | self.read_ep.wait_enabled().await | 448 | self.read_ep.wait_enabled().await; |
| 450 | } | 449 | } |
| 451 | } | 450 | } |
| 452 | 451 | ||
| @@ -520,17 +519,17 @@ impl LineCoding { | |||
| 520 | } | 519 | } |
| 521 | 520 | ||
| 522 | /// Gets the number of data bits for UART communication. | 521 | /// Gets the number of data bits for UART communication. |
| 523 | pub fn data_bits(&self) -> u8 { | 522 | pub const fn data_bits(&self) -> u8 { |
| 524 | self.data_bits | 523 | self.data_bits |
| 525 | } | 524 | } |
| 526 | 525 | ||
| 527 | /// Gets the parity type for UART communication. | 526 | /// Gets the parity type for UART communication. |
| 528 | pub fn parity_type(&self) -> ParityType { | 527 | pub const fn parity_type(&self) -> ParityType { |
| 529 | self.parity_type | 528 | self.parity_type |
| 530 | } | 529 | } |
| 531 | 530 | ||
| 532 | /// Gets the data rate in bits per second for UART communication. | 531 | /// Gets the data rate in bits per second for UART communication. |
| 533 | pub fn data_rate(&self) -> u32 { | 532 | pub const fn data_rate(&self) -> u32 { |
| 534 | self.data_rate | 533 | self.data_rate |
| 535 | } | 534 | } |
| 536 | } | 535 | } |
diff --git a/embassy-usb/src/class/cdc_ncm/mod.rs b/embassy-usb/src/class/cdc_ncm/mod.rs index 27716b37d..bea9dac27 100644 --- a/embassy-usb/src/class/cdc_ncm/mod.rs +++ b/embassy-usb/src/class/cdc_ncm/mod.rs | |||
| @@ -16,10 +16,11 @@ | |||
| 16 | 16 | ||
| 17 | use core::intrinsics::copy_nonoverlapping; | 17 | use core::intrinsics::copy_nonoverlapping; |
| 18 | use core::mem::{size_of, MaybeUninit}; | 18 | use core::mem::{size_of, MaybeUninit}; |
| 19 | use core::ptr::addr_of; | ||
| 19 | 20 | ||
| 20 | use crate::control::{self, InResponse, OutResponse, Recipient, Request, RequestType}; | 21 | use crate::control::{self, InResponse, OutResponse, Recipient, Request, RequestType}; |
| 21 | use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut}; | 22 | use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut}; |
| 22 | use crate::types::*; | 23 | use crate::types::{InterfaceNumber, StringIndex}; |
| 23 | use crate::{Builder, Handler}; | 24 | use crate::{Builder, Handler}; |
| 24 | 25 | ||
| 25 | pub mod embassy_net; | 26 | pub mod embassy_net; |
| @@ -62,9 +63,9 @@ const REQ_SET_NTB_INPUT_SIZE: u8 = 0x86; | |||
| 62 | //const NOTIF_POLL_INTERVAL: u8 = 20; | 63 | //const NOTIF_POLL_INTERVAL: u8 = 20; |
| 63 | 64 | ||
| 64 | const NTB_MAX_SIZE: usize = 2048; | 65 | const NTB_MAX_SIZE: usize = 2048; |
| 65 | const SIG_NTH: u32 = 0x484d434e; | 66 | const SIG_NTH: u32 = 0x484d_434e; |
| 66 | const SIG_NDP_NO_FCS: u32 = 0x304d434e; | 67 | const SIG_NDP_NO_FCS: u32 = 0x304d_434e; |
| 67 | const SIG_NDP_WITH_FCS: u32 = 0x314d434e; | 68 | const SIG_NDP_WITH_FCS: u32 = 0x314d_434e; |
| 68 | 69 | ||
| 69 | const ALTERNATE_SETTING_DISABLED: u8 = 0x00; | 70 | const ALTERNATE_SETTING_DISABLED: u8 = 0x00; |
| 70 | const ALTERNATE_SETTING_ENABLED: u8 = 0x01; | 71 | const ALTERNATE_SETTING_ENABLED: u8 = 0x01; |
| @@ -111,7 +112,7 @@ struct NtbParametersDir { | |||
| 111 | 112 | ||
| 112 | fn byteify<T>(buf: &mut [u8], data: T) -> &[u8] { | 113 | fn byteify<T>(buf: &mut [u8], data: T) -> &[u8] { |
| 113 | let len = size_of::<T>(); | 114 | let len = size_of::<T>(); |
| 114 | unsafe { copy_nonoverlapping(&data as *const _ as *const u8, buf.as_mut_ptr(), len) } | 115 | unsafe { copy_nonoverlapping(addr_of!(data).cast(), buf.as_mut_ptr(), len) } |
| 115 | &buf[..len] | 116 | &buf[..len] |
| 116 | } | 117 | } |
| 117 | 118 | ||
| @@ -132,12 +133,12 @@ impl<'a> State<'a> { | |||
| 132 | pub fn new() -> Self { | 133 | pub fn new() -> Self { |
| 133 | Self { | 134 | Self { |
| 134 | control: MaybeUninit::uninit(), | 135 | control: MaybeUninit::uninit(), |
| 135 | shared: Default::default(), | 136 | shared: ControlShared::default(), |
| 136 | } | 137 | } |
| 137 | } | 138 | } |
| 138 | } | 139 | } |
| 139 | 140 | ||
| 140 | /// Shared data between Control and CdcAcmClass | 141 | /// Shared data between Control and `CdcAcmClass` |
| 141 | #[derive(Default)] | 142 | #[derive(Default)] |
| 142 | struct ControlShared { | 143 | struct ControlShared { |
| 143 | mac_addr: [u8; 6], | 144 | mac_addr: [u8; 6], |
| @@ -378,12 +379,12 @@ impl<'d, D: Driver<'d>> Sender<'d, D> { | |||
| 378 | /// | 379 | /// |
| 379 | /// This waits until the packet is successfully stored in the CDC-NCM endpoint buffers. | 380 | /// This waits until the packet is successfully stored in the CDC-NCM endpoint buffers. |
| 380 | pub async fn write_packet(&mut self, data: &[u8]) -> Result<(), EndpointError> { | 381 | pub async fn write_packet(&mut self, data: &[u8]) -> Result<(), EndpointError> { |
| 381 | let seq = self.seq; | ||
| 382 | self.seq = self.seq.wrapping_add(1); | ||
| 383 | |||
| 384 | const OUT_HEADER_LEN: usize = 28; | 382 | const OUT_HEADER_LEN: usize = 28; |
| 385 | const ABS_MAX_PACKET_SIZE: usize = 512; | 383 | const ABS_MAX_PACKET_SIZE: usize = 512; |
| 386 | 384 | ||
| 385 | let seq = self.seq; | ||
| 386 | self.seq = self.seq.wrapping_add(1); | ||
| 387 | |||
| 387 | let header = NtbOutHeader { | 388 | let header = NtbOutHeader { |
| 388 | nth_sig: SIG_NTH, | 389 | nth_sig: SIG_NTH, |
| 389 | nth_len: 0x0c, | 390 | nth_len: 0x0c, |
| @@ -460,12 +461,9 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> { | |||
| 460 | let ntb = &ntb[..pos]; | 461 | let ntb = &ntb[..pos]; |
| 461 | 462 | ||
| 462 | // Process NTB header (NTH) | 463 | // Process NTB header (NTH) |
| 463 | let nth = match ntb.get(..12) { | 464 | let Some(nth) = ntb.get(..12) else { |
| 464 | Some(x) => x, | 465 | warn!("Received too short NTB"); |
| 465 | None => { | 466 | continue; |
| 466 | warn!("Received too short NTB"); | ||
| 467 | continue; | ||
| 468 | } | ||
| 469 | }; | 467 | }; |
| 470 | let sig = u32::from_le_bytes(nth[0..4].try_into().unwrap()); | 468 | let sig = u32::from_le_bytes(nth[0..4].try_into().unwrap()); |
| 471 | if sig != SIG_NTH { | 469 | if sig != SIG_NTH { |
| @@ -475,12 +473,9 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> { | |||
| 475 | let ndp_idx = u16::from_le_bytes(nth[10..12].try_into().unwrap()) as usize; | 473 | let ndp_idx = u16::from_le_bytes(nth[10..12].try_into().unwrap()) as usize; |
| 476 | 474 | ||
| 477 | // Process NTB Datagram Pointer (NDP) | 475 | // Process NTB Datagram Pointer (NDP) |
| 478 | let ndp = match ntb.get(ndp_idx..ndp_idx + 12) { | 476 | let Some(ndp) = ntb.get(ndp_idx..ndp_idx + 12) else { |
| 479 | Some(x) => x, | 477 | warn!("NTH has an NDP pointer out of range."); |
| 480 | None => { | 478 | continue; |
| 481 | warn!("NTH has an NDP pointer out of range."); | ||
| 482 | continue; | ||
| 483 | } | ||
| 484 | }; | 479 | }; |
| 485 | let sig = u32::from_le_bytes(ndp[0..4].try_into().unwrap()); | 480 | let sig = u32::from_le_bytes(ndp[0..4].try_into().unwrap()); |
| 486 | if sig != SIG_NDP_NO_FCS && sig != SIG_NDP_WITH_FCS { | 481 | if sig != SIG_NDP_NO_FCS && sig != SIG_NDP_WITH_FCS { |
| @@ -496,12 +491,9 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> { | |||
| 496 | } | 491 | } |
| 497 | 492 | ||
| 498 | // Process actual datagram, finally. | 493 | // Process actual datagram, finally. |
| 499 | let datagram = match ntb.get(datagram_index..datagram_index + datagram_len) { | 494 | let Some(datagram) = ntb.get(datagram_index..datagram_index + datagram_len) else { |
| 500 | Some(x) => x, | 495 | warn!("NDP has a datagram pointer out of range."); |
| 501 | None => { | 496 | continue; |
| 502 | warn!("NDP has a datagram pointer out of range."); | ||
| 503 | continue; | ||
| 504 | } | ||
| 505 | }; | 497 | }; |
| 506 | buf[..datagram_len].copy_from_slice(datagram); | 498 | buf[..datagram_len].copy_from_slice(datagram); |
| 507 | 499 | ||
diff --git a/embassy-usb/src/class/hid.rs b/embassy-usb/src/class/hid.rs index 0da29b1a6..0000b5b2b 100644 --- a/embassy-usb/src/class/hid.rs +++ b/embassy-usb/src/class/hid.rs | |||
| @@ -63,7 +63,7 @@ pub enum ReportId { | |||
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | impl ReportId { | 65 | impl ReportId { |
| 66 | fn try_from(value: u16) -> Result<Self, ()> { | 66 | const fn try_from(value: u16) -> Result<Self, ()> { |
| 67 | match value >> 8 { | 67 | match value >> 8 { |
| 68 | 1 => Ok(ReportId::In(value as u8)), | 68 | 1 => Ok(ReportId::In(value as u8)), |
| 69 | 2 => Ok(ReportId::Out(value as u8)), | 69 | 2 => Ok(ReportId::Out(value as u8)), |
| @@ -87,7 +87,7 @@ impl<'d> Default for State<'d> { | |||
| 87 | 87 | ||
| 88 | impl<'d> State<'d> { | 88 | impl<'d> State<'d> { |
| 89 | /// Create a new `State`. | 89 | /// Create a new `State`. |
| 90 | pub fn new() -> Self { | 90 | pub const fn new() -> Self { |
| 91 | State { | 91 | State { |
| 92 | control: MaybeUninit::uninit(), | 92 | control: MaybeUninit::uninit(), |
| 93 | out_report_offset: AtomicUsize::new(0), | 93 | out_report_offset: AtomicUsize::new(0), |
| @@ -154,7 +154,7 @@ fn build<'d, D: Driver<'d>>( | |||
| 154 | } | 154 | } |
| 155 | 155 | ||
| 156 | impl<'d, D: Driver<'d>, const READ_N: usize, const WRITE_N: usize> HidReaderWriter<'d, D, READ_N, WRITE_N> { | 156 | impl<'d, D: Driver<'d>, const READ_N: usize, const WRITE_N: usize> HidReaderWriter<'d, D, READ_N, WRITE_N> { |
| 157 | /// Creates a new HidReaderWriter. | 157 | /// Creates a new `HidReaderWriter`. |
| 158 | /// | 158 | /// |
| 159 | /// This will allocate one IN and one OUT endpoints. If you only need writing (sending) | 159 | /// This will allocate one IN and one OUT endpoints. If you only need writing (sending) |
| 160 | /// HID reports, consider using [`HidWriter::new`] instead, which allocates an IN endpoint only. | 160 | /// HID reports, consider using [`HidWriter::new`] instead, which allocates an IN endpoint only. |
| @@ -230,7 +230,7 @@ pub enum ReadError { | |||
| 230 | 230 | ||
| 231 | impl From<EndpointError> for ReadError { | 231 | impl From<EndpointError> for ReadError { |
| 232 | fn from(val: EndpointError) -> Self { | 232 | fn from(val: EndpointError) -> Self { |
| 233 | use EndpointError::*; | 233 | use EndpointError::{BufferOverflow, Disabled}; |
| 234 | match val { | 234 | match val { |
| 235 | BufferOverflow => ReadError::BufferOverflow, | 235 | BufferOverflow => ReadError::BufferOverflow, |
| 236 | Disabled => ReadError::Disabled, | 236 | Disabled => ReadError::Disabled, |
| @@ -258,16 +258,15 @@ impl<'d, D: Driver<'d>, const N: usize> HidWriter<'d, D, N> { | |||
| 258 | 258 | ||
| 259 | /// Waits for the interrupt in endpoint to be enabled. | 259 | /// Waits for the interrupt in endpoint to be enabled. |
| 260 | pub async fn ready(&mut self) { | 260 | pub async fn ready(&mut self) { |
| 261 | self.ep_in.wait_enabled().await | 261 | self.ep_in.wait_enabled().await; |
| 262 | } | 262 | } |
| 263 | 263 | ||
| 264 | /// Writes an input report by serializing the given report structure. | 264 | /// Writes an input report by serializing the given report structure. |
| 265 | #[cfg(feature = "usbd-hid")] | 265 | #[cfg(feature = "usbd-hid")] |
| 266 | pub async fn write_serialize<IR: AsInputReport>(&mut self, r: &IR) -> Result<(), EndpointError> { | 266 | pub async fn write_serialize<IR: AsInputReport>(&mut self, r: &IR) -> Result<(), EndpointError> { |
| 267 | let mut buf: [u8; N] = [0; N]; | 267 | let mut buf: [u8; N] = [0; N]; |
| 268 | let size = match serialize(&mut buf, r) { | 268 | let Ok(size) = serialize(&mut buf, r) else { |
| 269 | Ok(size) => size, | 269 | return Err(EndpointError::BufferOverflow); |
| 270 | Err(_) => return Err(EndpointError::BufferOverflow), | ||
| 271 | }; | 270 | }; |
| 272 | self.write(&buf[0..size]).await | 271 | self.write(&buf[0..size]).await |
| 273 | } | 272 | } |
| @@ -293,7 +292,7 @@ impl<'d, D: Driver<'d>, const N: usize> HidWriter<'d, D, N> { | |||
| 293 | impl<'d, D: Driver<'d>, const N: usize> HidReader<'d, D, N> { | 292 | impl<'d, D: Driver<'d>, const N: usize> HidReader<'d, D, N> { |
| 294 | /// Waits for the interrupt out endpoint to be enabled. | 293 | /// Waits for the interrupt out endpoint to be enabled. |
| 295 | pub async fn ready(&mut self) { | 294 | pub async fn ready(&mut self) { |
| 296 | self.ep_out.wait_enabled().await | 295 | self.ep_out.wait_enabled().await; |
| 297 | } | 296 | } |
| 298 | 297 | ||
| 299 | /// Delivers output reports from the Interrupt Out pipe to `handler`. | 298 | /// Delivers output reports from the Interrupt Out pipe to `handler`. |
| @@ -350,9 +349,8 @@ impl<'d, D: Driver<'d>, const N: usize> HidReader<'d, D, N> { | |||
| 350 | if size < max_packet_size || total == N { | 349 | if size < max_packet_size || total == N { |
| 351 | self.offset.store(0, Ordering::Release); | 350 | self.offset.store(0, Ordering::Release); |
| 352 | break; | 351 | break; |
| 353 | } else { | ||
| 354 | self.offset.store(total, Ordering::Release); | ||
| 355 | } | 352 | } |
| 353 | self.offset.store(total, Ordering::Release); | ||
| 356 | } | 354 | } |
| 357 | Err(err) => { | 355 | Err(err) => { |
| 358 | self.offset.store(0, Ordering::Release); | 356 | self.offset.store(0, Ordering::Release); |
diff --git a/embassy-usb/src/class/midi.rs b/embassy-usb/src/class/midi.rs index c5cf8d876..52a96f278 100644 --- a/embassy-usb/src/class/midi.rs +++ b/embassy-usb/src/class/midi.rs | |||
| @@ -27,9 +27,9 @@ const MIDI_OUT_SIZE: u8 = 0x09; | |||
| 27 | /// writing USB packets with no intermediate buffers, but it will not act like a stream-like port. | 27 | /// writing USB packets with no intermediate buffers, but it will not act like a stream-like port. |
| 28 | /// The following constraints must be followed if you use this class directly: | 28 | /// The following constraints must be followed if you use this class directly: |
| 29 | /// | 29 | /// |
| 30 | /// - `read_packet` must be called with a buffer large enough to hold max_packet_size bytes. | 30 | /// - `read_packet` must be called with a buffer large enough to hold `max_packet_size` bytes. |
| 31 | /// - `write_packet` must not be called with a buffer larger than max_packet_size bytes. | 31 | /// - `write_packet` must not be called with a buffer larger than `max_packet_size` bytes. |
| 32 | /// - If you write a packet that is exactly max_packet_size bytes long, it won't be processed by the | 32 | /// - If you write a packet that is exactly `max_packet_size` bytes long, it won't be processed by the |
| 33 | /// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP) | 33 | /// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP) |
| 34 | /// can be sent if there is no other data to send. This is because USB bulk transactions must be | 34 | /// can be sent if there is no other data to send. This is because USB bulk transactions must be |
| 35 | /// terminated with a short packet, even if the bulk endpoint is used for stream-like data. | 35 | /// terminated with a short packet, even if the bulk endpoint is used for stream-like data. |
| @@ -39,8 +39,8 @@ pub struct MidiClass<'d, D: Driver<'d>> { | |||
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | impl<'d, D: Driver<'d>> MidiClass<'d, D> { | 41 | impl<'d, D: Driver<'d>> MidiClass<'d, D> { |
| 42 | /// Creates a new MidiClass with the provided UsbBus, number of input and output jacks and max_packet_size in bytes. | 42 | /// Creates a new `MidiClass` with the provided UsbBus, number of input and output jacks and `max_packet_size` in bytes. |
| 43 | /// For full-speed devices, max_packet_size has to be one of 8, 16, 32 or 64. | 43 | /// For full-speed devices, `max_packet_size` has to be one of 8, 16, 32 or 64. |
| 44 | pub fn new(builder: &mut Builder<'d, D>, n_in_jacks: u8, n_out_jacks: u8, max_packet_size: u16) -> Self { | 44 | pub fn new(builder: &mut Builder<'d, D>, n_in_jacks: u8, n_out_jacks: u8, max_packet_size: u16) -> Self { |
| 45 | let mut func = builder.function(USB_AUDIO_CLASS, USB_AUDIOCONTROL_SUBCLASS, PROTOCOL_NONE); | 45 | let mut func = builder.function(USB_AUDIO_CLASS, USB_AUDIOCONTROL_SUBCLASS, PROTOCOL_NONE); |
| 46 | 46 | ||
| @@ -160,7 +160,7 @@ impl<'d, D: Driver<'d>> MidiClass<'d, D> { | |||
| 160 | 160 | ||
| 161 | /// Waits for the USB host to enable this interface | 161 | /// Waits for the USB host to enable this interface |
| 162 | pub async fn wait_connection(&mut self) { | 162 | pub async fn wait_connection(&mut self) { |
| 163 | self.read_ep.wait_enabled().await | 163 | self.read_ep.wait_enabled().await; |
| 164 | } | 164 | } |
| 165 | 165 | ||
| 166 | /// Split the class into a sender and receiver. | 166 | /// Split the class into a sender and receiver. |
| @@ -197,7 +197,7 @@ impl<'d, D: Driver<'d>> Sender<'d, D> { | |||
| 197 | 197 | ||
| 198 | /// Waits for the USB host to enable this interface | 198 | /// Waits for the USB host to enable this interface |
| 199 | pub async fn wait_connection(&mut self) { | 199 | pub async fn wait_connection(&mut self) { |
| 200 | self.write_ep.wait_enabled().await | 200 | self.write_ep.wait_enabled().await; |
| 201 | } | 201 | } |
| 202 | } | 202 | } |
| 203 | 203 | ||
| @@ -222,6 +222,6 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> { | |||
| 222 | 222 | ||
| 223 | /// Waits for the USB host to enable this interface | 223 | /// Waits for the USB host to enable this interface |
| 224 | pub async fn wait_connection(&mut self) { | 224 | pub async fn wait_connection(&mut self) { |
| 225 | self.read_ep.wait_enabled().await | 225 | self.read_ep.wait_enabled().await; |
| 226 | } | 226 | } |
| 227 | } | 227 | } |
