From 33e56ae6ec36da71847ebd75905205d7cdf2d5d0 Mon Sep 17 00:00:00 2001 From: Thales Fragoso Date: Fri, 12 Mar 2021 18:31:47 -0300 Subject: [PATCH 1/2] Add read_ep_address and make write_ep_address pub --- src/cdc_acm.rs | 85 +++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/src/cdc_acm.rs b/src/cdc_acm.rs index 91113a4..3d5277c 100644 --- a/src/cdc_acm.rs +++ b/src/cdc_acm.rs @@ -102,9 +102,14 @@ impl CdcAcmClass<'_, B> { } /// Gets the address of the IN endpoint. - pub(crate) fn write_ep_address(&self) -> EndpointAddress { + pub fn write_ep_address(&self) -> EndpointAddress { self.write_ep.address() } + + /// Gets the address of the OUT endpoint. + pub fn read_ep_address(&self) -> EndpointAddress { + self.read_ep.address() + } } impl UsbClass for CdcAcmClass<'_, B> { @@ -114,51 +119,54 @@ impl UsbClass for CdcAcmClass<'_, B> { 2, USB_CLASS_CDC, CDC_SUBCLASS_ACM, - CDC_PROTOCOL_NONE)?; + CDC_PROTOCOL_NONE, + )?; writer.interface( self.comm_if, USB_CLASS_CDC, CDC_SUBCLASS_ACM, - CDC_PROTOCOL_NONE)?; + CDC_PROTOCOL_NONE, + )?; writer.write( CS_INTERFACE, &[ CDC_TYPE_HEADER, // bDescriptorSubtype - 0x10, 0x01 // bcdCDC (1.10) - ])?; + 0x10, + 0x01, // bcdCDC (1.10) + ], + )?; writer.write( CS_INTERFACE, &[ CDC_TYPE_ACM, // bDescriptorSubtype - 0x00 // bmCapabilities - ])?; + 0x00, // bmCapabilities + ], + )?; writer.write( CS_INTERFACE, &[ - CDC_TYPE_UNION, // bDescriptorSubtype + CDC_TYPE_UNION, // bDescriptorSubtype self.comm_if.into(), // bControlInterface - self.data_if.into() // bSubordinateInterface - ])?; + self.data_if.into(), // bSubordinateInterface + ], + )?; writer.write( CS_INTERFACE, &[ CDC_TYPE_CALL_MANAGEMENT, // bDescriptorSubtype - 0x00, // bmCapabilities - self.data_if.into() // bDataInterface - ])?; + 0x00, // bmCapabilities + self.data_if.into(), // bDataInterface + ], + )?; writer.endpoint(&self.comm_ep)?; - writer.interface( - self.data_if, - USB_CLASS_CDC_DATA, - 0x00, - 0x00)?; + writer.interface(self.data_if, USB_CLASS_CDC_DATA, 0x00, 0x00)?; writer.endpoint(&self.write_ep)?; writer.endpoint(&self.read_ep)?; @@ -192,9 +200,12 @@ impl UsbClass for CdcAcmClass<'_, B> { data[6] = self.line_coding.data_bits; Ok(7) - }).ok(); - }, - _ => { xfer.reject().ok(); }, + }) + .ok(); + } + _ => { + xfer.reject().ok(); + } } } @@ -213,7 +224,7 @@ impl UsbClass for CdcAcmClass<'_, B> { // We don't actually support encapsulated commands but pretend we do for standards // compatibility. xfer.accept().ok(); - }, + } REQ_SET_LINE_CODING if xfer.data().len() >= 7 => { self.line_coding.data_rate = u32::from_le_bytes(xfer.data()[0..4].try_into().unwrap()); @@ -222,15 +233,17 @@ impl UsbClass for CdcAcmClass<'_, B> { self.line_coding.data_bits = xfer.data()[6]; xfer.accept().ok(); - }, + } REQ_SET_CONTROL_LINE_STATE => { self.dtr = (req.value & 0x0001) != 0; self.rts = (req.value & 0x0002) != 0; xfer.accept().ok(); - }, - _ => { xfer.reject().ok(); } - }; + } + _ => { + xfer.reject().ok(); + } + }; } } @@ -250,7 +263,7 @@ pub enum StopBits { impl From for StopBits { fn from(value: u8) -> Self { if value <= 2 { - unsafe { mem::transmute(value )} + unsafe { mem::transmute(value) } } else { StopBits::One } @@ -270,7 +283,7 @@ pub enum ParityType { impl From for ParityType { fn from(value: u8) -> Self { if value <= 4 { - unsafe { mem::transmute(value )} + unsafe { mem::transmute(value) } } else { ParityType::None } @@ -290,16 +303,24 @@ pub struct LineCoding { impl LineCoding { /// Gets the number of stop bits for UART communication. - pub fn stop_bits(&self) -> StopBits { self.stop_bits } + pub fn stop_bits(&self) -> StopBits { + self.stop_bits + } /// Gets the number of data bits for UART communication. - pub fn data_bits(&self) -> u8 { self.data_bits } + pub fn data_bits(&self) -> u8 { + self.data_bits + } /// Gets the parity type for UART communication. - pub fn parity_type(&self) -> ParityType { self.parity_type } + pub fn parity_type(&self) -> ParityType { + self.parity_type + } /// Gets the data rate in bits per second for UART communication. - pub fn data_rate(&self) -> u32 { self.data_rate } + pub fn data_rate(&self) -> u32 { + self.data_rate + } } impl Default for LineCoding { From aafbc335e1001b8aaad9ac1f8daee12ba1f5267c Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 4 Mar 2024 10:16:51 +0100 Subject: [PATCH 2/2] Adding accessor functions --- CHANGELOG.md | 2 ++ src/cdc_acm.rs | 22 ++++++++++++++++------ src/serial_port.rs | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99bf23e..74ee6bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added * Added the following `embedded-io` traits to the `SerialPort` object: `Write`, `WriteReady`, `Read`, and `ReadReady`, and `ErrorType` +* The `CdcAcmClass` now exposes the IN and OUT endpoints for direct access via the `read_ep{_mut}` + and `write_ep{_mut}` functions. ## [0.2.0] - 2023-11-13 diff --git a/src/cdc_acm.rs b/src/cdc_acm.rs index 115d485..3939763 100644 --- a/src/cdc_acm.rs +++ b/src/cdc_acm.rs @@ -124,14 +124,24 @@ impl<'a, B: UsbBus> CdcAcmClass<'a, B> { self.read_ep.read(data) } - /// Gets the address of the IN endpoint. - pub fn write_ep_address(&self) -> EndpointAddress { - self.write_ep.address() + /// Gets the IN endpoint. + pub fn write_ep(&self) -> &EndpointIn<'a, B> { + &self.write_ep } - /// Gets the address of the OUT endpoint. - pub fn read_ep_address(&self) -> EndpointAddress { - self.read_ep.address() + /// Mutably gets the IN endpoint. + pub fn write_ep_mut(&mut self) -> &mut EndpointIn<'a, B> { + &mut self.write_ep + } + + /// Gets the OUT endpoint. + pub fn read_ep(&self) -> &EndpointOut<'a, B> { + &self.read_ep + } + + /// Mutably gets the OUT endpoint. + pub fn read_ep_mut(&mut self) -> &mut EndpointOut<'a, B> { + &mut self.read_ep } } diff --git a/src/serial_port.rs b/src/serial_port.rs index cf31bb1..1e05e89 100644 --- a/src/serial_port.rs +++ b/src/serial_port.rs @@ -256,7 +256,7 @@ where } fn endpoint_in_complete(&mut self, addr: EndpointAddress) { - if addr == self.inner.write_ep_address() { + if addr == self.inner.write_ep().address() { self.flush().ok(); } }