diff options
| author | Jacob Rosenthal <[email protected]> | 2021-12-14 15:47:54 -0700 |
|---|---|---|
| committer | Jacob Rosenthal <[email protected]> | 2021-12-14 15:47:54 -0700 |
| commit | 07cbd41131a89ec0982f0ac6a3237d0544375f97 (patch) | |
| tree | 95aa5efac0283baef94a24ccb43862d5f968153c | |
| parent | f31140a70bbc7c14b07acf6305eb8a0a73560e5a (diff) | |
dont expose embedded_hal_common::usb
| -rw-r--r-- | embassy-nrf/src/chips/nrf52840.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/usb.rs | 77 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb_uart.rs | 8 |
3 files changed, 33 insertions, 57 deletions
diff --git a/embassy-nrf/src/chips/nrf52840.rs b/embassy-nrf/src/chips/nrf52840.rs index 5aea06524..6e5e8ed9f 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; | |||
| 8 | 8 | ||
| 9 | embassy_hal_common::peripherals! { | 9 | embassy_hal_common::peripherals! { |
| 10 | 10 | ||
| 11 | // USB | ||
| 12 | USBD, | ||
| 13 | |||
| 14 | // RTC | 11 | // RTC |
| 15 | RTC0, | 12 | RTC0, |
| 16 | RTC1, | 13 | RTC1, |
| @@ -161,8 +158,6 @@ embassy_hal_common::peripherals! { | |||
| 161 | TEMP, | 158 | TEMP, |
| 162 | } | 159 | } |
| 163 | 160 | ||
| 164 | impl_usb!(USBD, USBD, USBD); | ||
| 165 | |||
| 166 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); | 161 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); |
| 167 | impl_uarte!(UARTE1, UARTE1, UARTE1); | 162 | impl_uarte!(UARTE1, UARTE1, UARTE1); |
| 168 | 163 | ||
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index ca1d656a1..6776d5696 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs | |||
| @@ -5,64 +5,49 @@ use embassy::util::Unborrow; | |||
| 5 | 5 | ||
| 6 | use crate::interrupt::Interrupt; | 6 | use crate::interrupt::Interrupt; |
| 7 | use crate::pac; | 7 | use crate::pac; |
| 8 | use embassy_hal_common::usb::{ClassSet, IntoClassSet, USBInterrupt}; | ||
| 9 | pub use embassy_hal_common::usb::{ReadInterface, State, UsbSerial, WriteInterface}; | ||
| 8 | use nrf_usbd::{UsbPeripheral, Usbd}; | 10 | use nrf_usbd::{UsbPeripheral, Usbd}; |
| 9 | use usb_device::bus::UsbBusAllocator; | 11 | use usb_device::{bus::UsbBusAllocator, class_prelude::UsbBus, device::UsbDevice}; |
| 10 | 12 | ||
| 11 | // todo using different type than Usb because T isnt Send | 13 | pub struct UsbThing; |
| 12 | pub struct UsbBus; | 14 | unsafe impl UsbPeripheral for UsbThing { |
| 13 | unsafe impl UsbPeripheral for UsbBus { | ||
| 14 | // todo hardcoding | 15 | // todo hardcoding |
| 15 | const REGISTERS: *const () = crate::pac::USBD::ptr() as *const (); | 16 | const REGISTERS: *const () = crate::pac::USBD::ptr() as *const (); |
| 16 | } | 17 | } |
| 17 | 18 | ||
| 18 | impl UsbBus { | 19 | impl UsbThing { |
| 19 | pub fn new() -> UsbBusAllocator<Usbd<UsbBus>> { | 20 | pub fn new() -> UsbBusAllocator<Usbd<UsbThing>> { |
| 20 | Usbd::new(UsbBus) | 21 | Usbd::new(UsbThing) |
| 21 | } | 22 | } |
| 22 | } | 23 | } |
| 23 | 24 | ||
| 24 | unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} | 25 | unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} |
| 25 | 26 | ||
| 26 | pub struct Usb<'d, T: Instance> { | 27 | pub struct Usb<'bus, B, T, I> |
| 27 | phantom: PhantomData<&'d mut T>, | 28 | where |
| 29 | B: UsbBus, | ||
| 30 | T: ClassSet<B>, | ||
| 31 | I: USBInterrupt, | ||
| 32 | { | ||
| 33 | // Don't you dare moving out `PeripheralMutex` | ||
| 34 | usb: embassy_hal_common::usb::Usb<'bus, B, T, I>, | ||
| 28 | } | 35 | } |
| 29 | 36 | ||
| 30 | impl<'d, T: Instance> Usb<'d, T> { | 37 | impl<'bus, B, T, I> Usb<'bus, B, T, I> |
| 31 | #[allow(unused_unsafe)] | 38 | where |
| 32 | pub fn new(_usb: impl Unborrow<Target = T> + 'd) -> Self { | 39 | B: UsbBus, |
| 33 | let r = T::regs(); | 40 | T: ClassSet<B>, |
| 34 | 41 | I: USBInterrupt, | |
| 35 | Self { | 42 | { |
| 36 | phantom: PhantomData, | 43 | pub unsafe fn new<S: IntoClassSet<B, T>>( |
| 37 | } | 44 | state: &'bus mut State<'bus, B, T, I>, |
| 38 | } | 45 | device: UsbDevice<'bus, B>, |
| 39 | 46 | class_set: S, | |
| 40 | fn on_interrupt(_: *mut ()) { | 47 | irq: I, |
| 41 | let r = T::regs(); | 48 | ) -> Self { |
| 49 | let usb = embassy_hal_common::usb::Usb::new(state, device, class_set, irq); | ||
| 50 | |||
| 51 | Self { usb } | ||
| 42 | } | 52 | } |
| 43 | } | 53 | } |
| 44 | |||
| 45 | pub(crate) mod sealed { | ||
| 46 | use super::*; | ||
| 47 | |||
| 48 | pub trait Instance { | ||
| 49 | fn regs() -> &'static pac::usbd::RegisterBlock; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static { | ||
| 54 | type Interrupt: Interrupt; | ||
| 55 | } | ||
| 56 | |||
| 57 | macro_rules! impl_usb { | ||
| 58 | ($type:ident, $pac_type:ident, $irq:ident) => { | ||
| 59 | impl crate::usb::sealed::Instance for peripherals::$type { | ||
| 60 | fn regs() -> &'static pac::usbd::RegisterBlock { | ||
| 61 | unsafe { &*pac::$pac_type::ptr() } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | impl crate::usb::Instance for peripherals::$type { | ||
| 65 | type Interrupt = crate::interrupt::$irq; | ||
| 66 | } | ||
| 67 | }; | ||
| 68 | } | ||
diff --git a/examples/nrf/src/bin/usb_uart.rs b/examples/nrf/src/bin/usb_uart.rs index 902075dfc..9313fdb1e 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 | |||
| 16 | use embassy::executor::Spawner; | 16 | use embassy::executor::Spawner; |
| 17 | use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; | 17 | use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; |
| 18 | use embassy::time::{Duration, Timer}; | 18 | use embassy::time::{Duration, Timer}; |
| 19 | use embassy_hal_common::usb::{State, Usb, UsbSerial}; | 19 | use embassy_nrf::usb::{ReadInterface, State, Usb, UsbSerial, UsbThing, WriteInterface}; |
| 20 | use embassy_nrf::usb::{Usb as UsbDevice, UsbBus}; | ||
| 21 | use embassy_nrf::{interrupt, Peripherals}; | 20 | use embassy_nrf::{interrupt, Peripherals}; |
| 22 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; | 21 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; |
| 23 | 22 | ||
| @@ -26,12 +25,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 26 | let mut tx_buffer = [0u8; 1024]; | 25 | let mut tx_buffer = [0u8; 1024]; |
| 27 | let mut rx_buffer = [0u8; 640]; | 26 | let mut rx_buffer = [0u8; 640]; |
| 28 | 27 | ||
| 29 | let _usb_dev = UsbDevice::new(p.USBD); | 28 | let usb_bus = UsbThing::new(); |
| 30 | |||
| 31 | let usb_bus = UsbBus::new(); | ||
| 32 | 29 | ||
| 33 | let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); | 30 | let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); |
| 34 | |||
| 35 | let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) | 31 | let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) |
| 36 | .manufacturer("Fake company") | 32 | .manufacturer("Fake company") |
| 37 | .product("Serial port") | 33 | .product("Serial port") |
