dont expose embedded_hal_common::usb

This commit is contained in:
Jacob Rosenthal 2021-12-14 15:47:54 -07:00
parent f31140a70b
commit 07cbd41131
3 changed files with 32 additions and 56 deletions

View File

@ -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);

View File

@ -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<UsbBus>> {
Usbd::new(UsbBus)
impl UsbThing {
pub fn new() -> UsbBusAllocator<Usbd<UsbThing>> {
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<B>,
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<Target = T> + 'd) -> Self {
let r = T::regs();
impl<'bus, B, T, I> Usb<'bus, B, T, I>
where
B: UsbBus,
T: ClassSet<B>,
I: USBInterrupt,
{
pub unsafe fn new<S: IntoClassSet<B, T>>(
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<Target = Self> + 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;
}
};
}

View File

@ -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")