Skip to content

Commit

Permalink
Merge #12
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bors[bot] and Jonas Schievink authored Sep 27, 2022
2 parents a92cacf + ced03db commit 0585ae7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[workspace]
members = [
".",
"example",
]
members = [".", "example"]
default-members = [".", "example"]

[package]
name = "nrf-usbd"
Expand Down
3 changes: 2 additions & 1 deletion example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use defmt_rtt as _;
use nrf_usbd::{UsbPeripheral, Usbd};
// global logger
use panic_probe as _;
use usb_device::class_prelude::UsbBusAllocator;

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

info!("starting...");

let usb_bus = Usbd::new(Peripheral);
let usb_bus = UsbBusAllocator::new(Usbd::new(Peripheral));
let mut serial = SerialPort::new(&usb_bus);

let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
Expand Down
15 changes: 10 additions & 5 deletions src/usbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::mem::MaybeUninit;
use core::sync::atomic::{compiler_fence, Ordering};
use critical_section::{CriticalSection, Mutex};
use usb_device::{
bus::{PollResult, UsbBus, UsbBusAllocator},
bus::{PollResult, UsbBus},
endpoint::{EndpointAddress, EndpointType},
UsbDirection, UsbError,
};
Expand Down Expand Up @@ -57,6 +57,11 @@ struct EP0State {
}

/// USB device implementation.
///
/// This type implements the [`UsbBus`] trait and can be passed to a [`UsbBusAllocator`] to
/// configure and use the USB device.
///
/// [`UsbBusAllocator`]: usb_device::bus::UsbBusAllocator
pub struct Usbd<T: UsbPeripheral> {
_periph: Mutex<T>,
// argument passed to `UsbDeviceBuilder.max_packet_size_0`
Expand All @@ -71,14 +76,14 @@ pub struct Usbd<T: UsbPeripheral> {
}

impl<T: UsbPeripheral> Usbd<T> {
/// Creates a new USB bus, taking ownership of the raw peripheral.
/// Creates a new USB device wrapper, taking ownership of the raw peripheral.
///
/// # Parameters
///
/// * `periph`: The raw USBD peripheral.
#[inline]
pub fn new(periph: T) -> UsbBusAllocator<Self> {
UsbBusAllocator::new(Self {
pub fn new(periph: T) -> Self {
Self {
_periph: Mutex::new(periph),
max_packet_size_0: 0,
bufs: Buffers::new(),
Expand All @@ -93,7 +98,7 @@ impl<T: UsbPeripheral> Usbd<T> {
is_set_address: false,
})),
busy_in_endpoints: Mutex::new(Cell::new(0)),
})
}
}

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

0 comments on commit 0585ae7

Please sign in to comment.