Skip to content

Commit 0585ae7

Browse files
bors[bot]Jonas Schievink
and
Jonas Schievink
authored
Merge #12
12: Return `Self` from `Usbd::new` r=jonas-schievink a=jonas-schievink This makes the `device_address` method usable, and allows adding more methods in the future (although they'll only work before the bus is initialized). Also build/check the example by default. cc #8 Co-authored-by: Jonas Schievink <[email protected]>
2 parents a92cacf + ced03db commit 0585ae7

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

Cargo.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
[workspace]
2-
members = [
3-
".",
4-
"example",
5-
]
2+
members = [".", "example"]
3+
default-members = [".", "example"]
64

75
[package]
86
name = "nrf-usbd"

example/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use defmt_rtt as _;
55
use nrf_usbd::{UsbPeripheral, Usbd};
66
// global logger
77
use panic_probe as _;
8+
use usb_device::class_prelude::UsbBusAllocator;
89

910
use core::str;
1011
use core::sync::atomic::{AtomicUsize, Ordering};
@@ -40,7 +41,7 @@ fn main() -> ! {
4041

4142
info!("starting...");
4243

43-
let usb_bus = Usbd::new(Peripheral);
44+
let usb_bus = UsbBusAllocator::new(Usbd::new(Peripheral));
4445
let mut serial = SerialPort::new(&usb_bus);
4546

4647
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))

src/usbd.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::mem::MaybeUninit;
1111
use core::sync::atomic::{compiler_fence, Ordering};
1212
use critical_section::{CriticalSection, Mutex};
1313
use usb_device::{
14-
bus::{PollResult, UsbBus, UsbBusAllocator},
14+
bus::{PollResult, UsbBus},
1515
endpoint::{EndpointAddress, EndpointType},
1616
UsbDirection, UsbError,
1717
};
@@ -57,6 +57,11 @@ struct EP0State {
5757
}
5858

5959
/// USB device implementation.
60+
///
61+
/// This type implements the [`UsbBus`] trait and can be passed to a [`UsbBusAllocator`] to
62+
/// configure and use the USB device.
63+
///
64+
/// [`UsbBusAllocator`]: usb_device::bus::UsbBusAllocator
6065
pub struct Usbd<T: UsbPeripheral> {
6166
_periph: Mutex<T>,
6267
// argument passed to `UsbDeviceBuilder.max_packet_size_0`
@@ -71,14 +76,14 @@ pub struct Usbd<T: UsbPeripheral> {
7176
}
7277

7378
impl<T: UsbPeripheral> Usbd<T> {
74-
/// Creates a new USB bus, taking ownership of the raw peripheral.
79+
/// Creates a new USB device wrapper, taking ownership of the raw peripheral.
7580
///
7681
/// # Parameters
7782
///
7883
/// * `periph`: The raw USBD peripheral.
7984
#[inline]
80-
pub fn new(periph: T) -> UsbBusAllocator<Self> {
81-
UsbBusAllocator::new(Self {
85+
pub fn new(periph: T) -> Self {
86+
Self {
8287
_periph: Mutex::new(periph),
8388
max_packet_size_0: 0,
8489
bufs: Buffers::new(),
@@ -93,7 +98,7 @@ impl<T: UsbPeripheral> Usbd<T> {
9398
is_set_address: false,
9499
})),
95100
busy_in_endpoints: Mutex::new(Cell::new(0)),
96-
})
101+
}
97102
}
98103

99104
fn regs<'a>(&self, _cs: &'a CriticalSection) -> &'a RegisterBlock {

0 commit comments

Comments
 (0)