From 535d30335a7c4ae47e923c5e85851df80d2021a0 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 13 Dec 2021 18:13:38 -0700 Subject: [PATCH] make send, consolidate usb types --- embassy-nrf/src/usb.rs | 21 ++++++--------------- examples/nrf/src/bin/usb_uart.rs | 6 ++---- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index ca1d656a..4e3fcaaa 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs @@ -8,17 +8,8 @@ use crate::pac; use nrf_usbd::{UsbPeripheral, Usbd}; use usb_device::bus::UsbBusAllocator; -// todo using different type than Usb because T isnt Send -pub struct UsbBus; -unsafe impl UsbPeripheral for UsbBus { - // todo hardcoding - const REGISTERS: *const () = crate::pac::USBD::ptr() as *const (); -} - -impl UsbBus { - pub fn new() -> UsbBusAllocator> { - Usbd::new(UsbBus) - } +unsafe impl<'d, T: Instance> UsbPeripheral for Usb<'d, T> { + const REGISTERS: *const () = T::regs as *const (); } unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} @@ -29,12 +20,12 @@ pub struct Usb<'d, T: Instance> { impl<'d, T: Instance> Usb<'d, T> { #[allow(unused_unsafe)] - pub fn new(_usb: impl Unborrow + 'd) -> Self { + pub fn new(_usb: impl Unborrow + 'd) -> UsbBusAllocator> { let r = T::regs(); - Self { + Usbd::new(Self { phantom: PhantomData, - } + }) } fn on_interrupt(_: *mut ()) { @@ -50,7 +41,7 @@ pub(crate) mod sealed { } } -pub trait Instance: Unborrow + sealed::Instance + 'static { +pub trait Instance: Unborrow + sealed::Instance + 'static + Send { type Interrupt: Interrupt; } diff --git a/examples/nrf/src/bin/usb_uart.rs b/examples/nrf/src/bin/usb_uart.rs index 902075df..86ead84b 100644 --- a/examples/nrf/src/bin/usb_uart.rs +++ b/examples/nrf/src/bin/usb_uart.rs @@ -17,7 +17,7 @@ use embassy::executor::Spawner; use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; use embassy::time::{Duration, Timer}; use embassy_hal_common::usb::{State, Usb, UsbSerial}; -use embassy_nrf::usb::{Usb as UsbDevice, UsbBus}; +use embassy_nrf::usb::Usb as UsbDevice; use embassy_nrf::{interrupt, Peripherals}; use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; @@ -26,9 +26,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut tx_buffer = [0u8; 1024]; let mut rx_buffer = [0u8; 640]; - let _usb_dev = UsbDevice::new(p.USBD); - - let usb_bus = UsbBus::new(); + let usb_bus = UsbDevice::new(p.USBD); let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer);