Skip to content

Commit

Permalink
Updating usbd-serial for usb-device 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Nov 13, 2023
1 parent bb2c088 commit d38f4e1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ repository = "https://github.com/mvirkkunen/usbd-serial"
[dependencies]
embedded-hal = "0.2.4"
nb = "1"
usb-device = "0.2.7"
usb-device = {path = "../usb-device"}
23 changes: 17 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<S: BorrowMut<[u8]>> Buffer<S> {
return 0;
}

&self.store.borrow_mut()[self.wpos..self.wpos + count].copy_from_slice(&data[..count]);
self.store.borrow_mut()[self.wpos..self.wpos + count].copy_from_slice(&data[..count]);

self.wpos += count;
count
Expand Down Expand Up @@ -125,6 +125,12 @@ impl<S: BorrowMut<[u8]>> Buffer<S> {
/// Default backing store for the mediocre buffer
pub struct DefaultBufferStore([u8; 128]);

impl Default for DefaultBufferStore {
fn default() -> Self {
Self([0u8; 128])
}
}

impl Borrow<[u8]> for DefaultBufferStore {
fn borrow(&self) -> &[u8] {
&self.0
Expand Down Expand Up @@ -169,15 +175,18 @@ mod tests {
b.read(3, |data| {
assert_eq!(data, &DATA[0..3]);
Ok::<usize, Infallible>(3)
});
})
.unwrap();
b.read(1, |data| {
assert_eq!(data, &DATA[3..4]);
Ok::<usize, Infallible>(1)
});
})
.unwrap();
b.read(1, |data| {
assert_eq!(data, &[]);
Ok::<usize, Infallible>(1)
});
})
.unwrap();
}

#[test]
Expand All @@ -199,13 +208,15 @@ mod tests {
b.read(2, |data| {
assert_eq!(data, &DATA[0..2]);
Ok::<usize, Infallible>(2)
});
})
.unwrap();

assert_eq!(b.write(&DATA[4..7]), 3);
b.read(5, |data| {
assert_eq!(data, &DATA[2..7]);
Ok::<usize, Infallible>(5)
});
})
.unwrap();

assert_eq!(b.available_read(), 0);
}
Expand Down
4 changes: 3 additions & 1 deletion src/cdc_acm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::convert::TryInto;
use core::mem;
use usb_device::class_prelude::*;
use usb_device::descriptor::lang_id::LangID;
use usb_device::device::DEFAULT_ALTERNATE_SETTING;
use usb_device::Result;

Expand Down Expand Up @@ -137,6 +138,7 @@ impl<B: UsbBus> UsbClass<B> for CdcAcmClass<'_, B> {
USB_CLASS_CDC,
CDC_SUBCLASS_ACM,
CDC_PROTOCOL_NONE,
None,
)?;

writer.interface_alt(
Expand Down Expand Up @@ -200,7 +202,7 @@ impl<B: UsbBus> UsbClass<B> for CdcAcmClass<'_, B> {
Ok(())
}

fn get_string(&self, index: StringIndex, _lang_id: u16) -> Option<&str> {
fn get_string(&self, index: StringIndex, _lang_id: LangID) -> Option<&str> {
match (self.comm_if_name, self.data_if_name) {
(Some((i, s)), _) if i == index => Some(s),
(_, Some((i, s))) if i == index => Some(s),
Expand Down
16 changes: 7 additions & 9 deletions src/serial_port.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::buffer::{Buffer, DefaultBufferStore};
use crate::cdc_acm::*;
use core::borrow::BorrowMut;
use core::mem;
use core::slice;
use usb_device::class_prelude::*;
use usb_device::descriptor::lang_id::LangID;
use usb_device::Result;

/// USB (CDC-ACM) serial port with built-in buffering to implement stream-like behavior.
Expand Down Expand Up @@ -59,8 +59,8 @@ where
) -> SerialPort<'a, B, DefaultBufferStore, DefaultBufferStore> {
SerialPort::new_with_store_and_interface_names(
alloc,
unsafe { mem::uninitialized() },
unsafe { mem::uninitialized() },
DefaultBufferStore::default(),
DefaultBufferStore::default(),
comm_if_name,
data_if_name,
)
Expand Down Expand Up @@ -165,13 +165,11 @@ where
return Err(UsbError::WouldBlock);
}

let r = buf.read(data.len(), |buf_data| {
&data[..buf_data.len()].copy_from_slice(buf_data);
buf.read(data.len(), |buf_data| {
data[..buf_data.len()].copy_from_slice(buf_data);

Ok(buf_data.len())
});

r
})
}

/// Sends as much as possible of the current write buffer. Returns `Ok` if all data that has
Expand Down Expand Up @@ -239,7 +237,7 @@ where
self.inner.get_configuration_descriptors(writer)
}

fn get_string(&self, index: StringIndex, lang_id: u16) -> Option<&str> {
fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str> {
self.inner.get_string(index, lang_id)
}

Expand Down

0 comments on commit d38f4e1

Please sign in to comment.