2021-12-14 01:50:08 +01:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-12-15 18:11:00 +01:00
|
|
|
use crate::interrupt::Interrupt;
|
|
|
|
use crate::pac;
|
|
|
|
|
|
|
|
use core::marker::PhantomData;
|
|
|
|
use embassy::util::Unborrow;
|
2021-12-14 01:50:08 +01:00
|
|
|
use nrf_usbd::{UsbPeripheral, Usbd};
|
2021-12-15 00:48:48 +01:00
|
|
|
use usb_device::bus::UsbBusAllocator;
|
2021-12-14 01:50:08 +01:00
|
|
|
|
2021-12-15 18:11:00 +01:00
|
|
|
pub use embassy_hal_common::usb::*;
|
|
|
|
|
|
|
|
pub struct UsbBus<'d, T: Instance> {
|
|
|
|
phantom: PhantomData<&'d mut T>,
|
2021-12-14 21:51:50 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 18:11:00 +01:00
|
|
|
unsafe impl<'d, T: Instance> UsbPeripheral for UsbBus<'d, T> {
|
2021-12-15 19:00:55 +01:00
|
|
|
// todo how to use T::regs
|
|
|
|
const REGISTERS: *const () = pac::USBD::ptr() as *const ();
|
2021-12-15 18:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> UsbBus<'d, T> {
|
|
|
|
pub fn new(_usb: impl Unborrow<Target = T> + 'd) -> UsbBusAllocator<Usbd<UsbBus<'d, T>>> {
|
2021-12-15 18:29:19 +01:00
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
r.intenset.write(|w| {
|
|
|
|
w.sof().set_bit();
|
|
|
|
w.usbevent().set_bit();
|
|
|
|
w.ep0datadone().set_bit();
|
|
|
|
w.ep0setup().set_bit();
|
|
|
|
w.usbreset().set_bit()
|
|
|
|
});
|
2021-12-15 00:48:48 +01:00
|
|
|
|
2021-12-15 18:11:00 +01:00
|
|
|
Usbd::new(UsbBus {
|
|
|
|
phantom: PhantomData,
|
|
|
|
})
|
2021-12-14 21:51:50 +01:00
|
|
|
}
|
2021-12-14 01:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {}
|
2021-12-15 18:11:00 +01:00
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Instance {
|
|
|
|
fn regs() -> &'static pac::usbd::RegisterBlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static + Send {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|