Skip to content

Return Self from Usbd::new #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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