usb-serial: make inner guts private.

This commit is contained in:
Dario Nieuwenhuis 2022-03-30 02:16:34 +02:00
parent cdb7bae51a
commit 1672fdc666
2 changed files with 15 additions and 28 deletions

View File

@ -53,20 +53,17 @@ impl<'a> State<'a> {
/// writing USB packets with no intermediate buffers, but it will not act like a stream-like serial /// writing USB packets with no intermediate buffers, but it will not act like a stream-like serial
/// port. The following constraints must be followed if you use this class directly: /// port. The following constraints must be followed if you use this class directly:
/// ///
/// - `read_packet` must be called with a buffer large enough to hold max_packet_size bytes, and the /// - `read_packet` must be called with a buffer large enough to hold max_packet_size bytes.
/// method will return a `WouldBlock` error if there is no packet to be read. /// - `write_packet` must not be called with a buffer larger than max_packet_size bytes.
/// - `write_packet` must not be called with a buffer larger than max_packet_size bytes, and the
/// method will return a `WouldBlock` error if the previous packet has not been sent yet.
/// - If you write a packet that is exactly max_packet_size bytes long, it won't be processed by the /// - If you write a packet that is exactly max_packet_size bytes long, it won't be processed by the
/// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP) /// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP)
/// can be sent if there is no other data to send. This is because USB bulk transactions must be /// can be sent if there is no other data to send. This is because USB bulk transactions must be
/// terminated with a short packet, even if the bulk endpoint is used for stream-like data. /// terminated with a short packet, even if the bulk endpoint is used for stream-like data.
pub struct CdcAcmClass<'d, D: Driver<'d>> { pub struct CdcAcmClass<'d, D: Driver<'d>> {
// TODO not pub _comm_ep: D::EndpointIn,
pub comm_ep: D::EndpointIn, _data_if: InterfaceNumber,
pub data_if: InterfaceNumber, read_ep: D::EndpointOut,
pub read_ep: D::EndpointOut, write_ep: D::EndpointIn,
pub write_ep: D::EndpointIn,
control: &'d ControlShared, control: &'d ControlShared,
} }
@ -237,8 +234,8 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
builder.config_descriptor.endpoint(read_ep.info()); builder.config_descriptor.endpoint(read_ep.info());
CdcAcmClass { CdcAcmClass {
comm_ep, _comm_ep: comm_ep,
data_if, _data_if: data_if,
read_ep, read_ep,
write_ep, write_ep,
control: control_shared, control: control_shared,

View File

@ -6,15 +6,13 @@
use core::mem; use core::mem;
use defmt::*; use defmt::*;
use embassy::executor::Spawner; use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_nrf::interrupt; use embassy_nrf::interrupt;
use embassy_nrf::pac; use embassy_nrf::pac;
use embassy_nrf::usb::Driver; use embassy_nrf::usb::Driver;
use embassy_nrf::Peripherals; use embassy_nrf::Peripherals;
use embassy_usb::driver::{EndpointIn, EndpointOut};
use embassy_usb::{Config, UsbDeviceBuilder}; use embassy_usb::{Config, UsbDeviceBuilder};
use embassy_usb_serial::{CdcAcmClass, State}; use embassy_usb_serial::{CdcAcmClass, State};
use futures::future::join3; use futures::future::join;
use defmt_rtt as _; // global logger use defmt_rtt as _; // global logger
use panic_probe as _; use panic_probe as _;
@ -64,28 +62,20 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut usb = builder.build(); let mut usb = builder.build();
// Run the USB device. // Run the USB device.
let fut1 = usb.run(); let usb_fut = usb.run();
// Do stuff with the classes // Do stuff with the class!
let fut2 = async { let echo_fut = async {
let mut buf = [0; 64]; let mut buf = [0; 64];
loop { loop {
let n = class.read_ep.read(&mut buf).await.unwrap(); let n = class.read_packet(&mut buf).await.unwrap();
let data = &buf[..n]; let data = &buf[..n];
info!("data: {:x}", data); info!("data: {:x}", data);
} class.write_packet(data).await.unwrap();
};
let fut3 = async {
loop {
info!("writing...");
class.write_ep.write(b"Hello World!\r\n").await.unwrap();
info!("written");
Timer::after(Duration::from_secs(1)).await;
} }
}; };
// Run everything concurrently. // Run everything concurrently.
// If we had made everything `'static` above instead, we could do this using separate tasks instead. // If we had made everything `'static` above instead, we could do this using separate tasks instead.
join3(fut1, fut2, fut3).await; join(usb_fut, echo_fut).await;
} }