From 07cbd41131a89ec0982f0ac6a3237d0544375f97 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Tue, 14 Dec 2021 15:47:54 -0700 Subject: [PATCH] dont expose embedded_hal_common::usb --- embassy-nrf/src/chips/nrf52840.rs | 5 --- embassy-nrf/src/usb.rs | 75 +++++++++++++------------------ examples/nrf/src/bin/usb_uart.rs | 8 +--- 3 files changed, 32 insertions(+), 56 deletions(-) diff --git a/embassy-nrf/src/chips/nrf52840.rs b/embassy-nrf/src/chips/nrf52840.rs index 5aea0652..6e5e8ed9 100644 --- a/embassy-nrf/src/chips/nrf52840.rs +++ b/embassy-nrf/src/chips/nrf52840.rs @@ -8,9 +8,6 @@ pub const FLASH_SIZE: usize = 1024 * 1024; embassy_hal_common::peripherals! { - // USB - USBD, - // RTC RTC0, RTC1, @@ -161,8 +158,6 @@ embassy_hal_common::peripherals! { TEMP, } -impl_usb!(USBD, USBD, USBD); - impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); impl_uarte!(UARTE1, UARTE1, UARTE1); diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index ca1d656a..6776d569 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs @@ -5,64 +5,49 @@ use embassy::util::Unborrow; use crate::interrupt::Interrupt; use crate::pac; +use embassy_hal_common::usb::{ClassSet, IntoClassSet, USBInterrupt}; +pub use embassy_hal_common::usb::{ReadInterface, State, UsbSerial, WriteInterface}; use nrf_usbd::{UsbPeripheral, Usbd}; -use usb_device::bus::UsbBusAllocator; +use usb_device::{bus::UsbBusAllocator, class_prelude::UsbBus, device::UsbDevice}; -// todo using different type than Usb because T isnt Send -pub struct UsbBus; -unsafe impl UsbPeripheral for UsbBus { +pub struct UsbThing; +unsafe impl UsbPeripheral for UsbThing { // todo hardcoding const REGISTERS: *const () = crate::pac::USBD::ptr() as *const (); } -impl UsbBus { - pub fn new() -> UsbBusAllocator> { - Usbd::new(UsbBus) +impl UsbThing { + pub fn new() -> UsbBusAllocator> { + Usbd::new(UsbThing) } } unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} -pub struct Usb<'d, T: Instance> { - phantom: PhantomData<&'d mut T>, +pub struct Usb<'bus, B, T, I> +where + B: UsbBus, + T: ClassSet, + I: USBInterrupt, +{ + // Don't you dare moving out `PeripheralMutex` + usb: embassy_hal_common::usb::Usb<'bus, B, T, I>, } -impl<'d, T: Instance> Usb<'d, T> { - #[allow(unused_unsafe)] - pub fn new(_usb: impl Unborrow + 'd) -> Self { - let r = T::regs(); +impl<'bus, B, T, I> Usb<'bus, B, T, I> +where + B: UsbBus, + T: ClassSet, + I: USBInterrupt, +{ + pub unsafe fn new>( + state: &'bus mut State<'bus, B, T, I>, + device: UsbDevice<'bus, B>, + class_set: S, + irq: I, + ) -> Self { + let usb = embassy_hal_common::usb::Usb::new(state, device, class_set, irq); - Self { - phantom: PhantomData, - } - } - - fn on_interrupt(_: *mut ()) { - let r = T::regs(); + Self { usb } } } - -pub(crate) mod sealed { - use super::*; - - pub trait Instance { - fn regs() -> &'static pac::usbd::RegisterBlock; - } -} - -pub trait Instance: Unborrow + sealed::Instance + 'static { - type Interrupt: Interrupt; -} - -macro_rules! impl_usb { - ($type:ident, $pac_type:ident, $irq:ident) => { - impl crate::usb::sealed::Instance for peripherals::$type { - fn regs() -> &'static pac::usbd::RegisterBlock { - unsafe { &*pac::$pac_type::ptr() } - } - } - impl crate::usb::Instance for peripherals::$type { - type Interrupt = crate::interrupt::$irq; - } - }; -} diff --git a/examples/nrf/src/bin/usb_uart.rs b/examples/nrf/src/bin/usb_uart.rs index 902075df..9313fdb1 100644 --- a/examples/nrf/src/bin/usb_uart.rs +++ b/examples/nrf/src/bin/usb_uart.rs @@ -16,8 +16,7 @@ use panic_probe as _; // print out panic messages 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::{ReadInterface, State, Usb, UsbSerial, UsbThing, WriteInterface}; use embassy_nrf::{interrupt, Peripherals}; use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; @@ -26,12 +25,9 @@ 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 = UsbThing::new(); let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); - let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) .manufacturer("Fake company") .product("Serial port")