2021-12-14 01:50:08 +01:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-12-15 18:11:00 +01:00
|
|
|
use core::marker::PhantomData;
|
2022-03-10 01:05:33 +01:00
|
|
|
use core::mem::MaybeUninit;
|
2022-06-16 08:08:58 +02:00
|
|
|
use core::sync::atomic::{compiler_fence, AtomicBool, AtomicU32, Ordering};
|
2022-03-09 01:34:35 +01:00
|
|
|
use core::task::Poll;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2022-03-30 01:18:37 +02:00
|
|
|
use cortex_m::peripheral::NVIC;
|
2022-03-09 01:34:35 +01:00
|
|
|
use embassy::waitqueue::AtomicWaker;
|
|
|
|
use embassy_hal_common::unborrow;
|
2022-06-12 22:15:44 +02:00
|
|
|
pub use embassy_usb;
|
2022-04-10 21:41:51 +02:00
|
|
|
use embassy_usb::driver::{self, EndpointError, Event, Unsupported};
|
2022-03-09 01:34:35 +01:00
|
|
|
use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection};
|
|
|
|
use futures::future::poll_fn;
|
|
|
|
use futures::Future;
|
2022-04-02 22:35:03 +02:00
|
|
|
use pac::usbd::RegisterBlock;
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::interrupt::{Interrupt, InterruptExt};
|
2022-03-28 16:46:26 +02:00
|
|
|
use crate::util::slice_in_ram;
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::{pac, Unborrow};
|
2021-12-14 01:50:08 +01:00
|
|
|
|
2022-03-09 23:06:27 +01:00
|
|
|
const NEW_AW: AtomicWaker = AtomicWaker::new();
|
|
|
|
static BUS_WAKER: AtomicWaker = NEW_AW;
|
2022-04-02 04:42:20 +02:00
|
|
|
static EP0_WAKER: AtomicWaker = NEW_AW;
|
|
|
|
static EP_IN_WAKERS: [AtomicWaker; 8] = [NEW_AW; 8];
|
|
|
|
static EP_OUT_WAKERS: [AtomicWaker; 8] = [NEW_AW; 8];
|
2022-03-10 01:05:33 +01:00
|
|
|
static READY_ENDPOINTS: AtomicU32 = AtomicU32::new(0);
|
2021-12-15 18:11:00 +01:00
|
|
|
|
2022-07-09 08:40:10 +02:00
|
|
|
/// There are multiple ways to detect USB power. The behavior
|
|
|
|
/// here provides a hook into determining whether it is.
|
|
|
|
pub trait UsbSupply {
|
|
|
|
fn is_usb_detected(&self) -> bool;
|
|
|
|
|
|
|
|
type UsbPowerReadyFuture<'a>: Future<Output = Result<(), ()>> + 'a
|
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
fn wait_power_ready(&mut self) -> Self::UsbPowerReadyFuture<'_>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Driver<'d, T: Instance, P: UsbSupply> {
|
2021-12-15 18:11:00 +01:00
|
|
|
phantom: PhantomData<&'d mut T>,
|
2022-03-09 01:34:35 +01:00
|
|
|
alloc_in: Allocator,
|
|
|
|
alloc_out: Allocator,
|
2022-07-09 08:40:10 +02:00
|
|
|
usb_supply: P,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Uses the POWER peripheral to detect when power is available
|
|
|
|
/// for USB. Unsuitable for usage with the nRF softdevice.
|
|
|
|
#[cfg(not(feature = "_nrf5340-app"))]
|
2022-07-21 19:47:09 +02:00
|
|
|
pub struct PowerUsb {
|
|
|
|
_private: (),
|
|
|
|
}
|
2022-07-09 08:40:10 +02:00
|
|
|
|
|
|
|
/// Can be used to signal that power is available. Particularly suited for
|
|
|
|
/// use with the nRF softdevice.
|
|
|
|
pub struct SignalledSupply {
|
|
|
|
usb_detected: AtomicBool,
|
|
|
|
power_ready: AtomicBool,
|
|
|
|
}
|
|
|
|
|
|
|
|
static POWER_WAKER: AtomicWaker = NEW_AW;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "_nrf5340-app"))]
|
|
|
|
impl PowerUsb {
|
|
|
|
pub fn new(power_irq: impl Interrupt) -> Self {
|
|
|
|
let regs = unsafe { &*pac::POWER::ptr() };
|
|
|
|
|
|
|
|
power_irq.set_handler(Self::on_interrupt);
|
|
|
|
power_irq.unpend();
|
|
|
|
power_irq.enable();
|
|
|
|
|
|
|
|
regs.intenset
|
|
|
|
.write(|w| w.usbdetected().set().usbremoved().set().usbpwrrdy().set());
|
|
|
|
|
2022-07-21 19:47:09 +02:00
|
|
|
Self { _private: () }
|
2022-07-09 08:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "_nrf5340-app"))]
|
|
|
|
fn on_interrupt(_: *mut ()) {
|
|
|
|
let regs = unsafe { &*pac::POWER::ptr() };
|
|
|
|
|
|
|
|
if regs.events_usbdetected.read().bits() != 0 {
|
|
|
|
regs.events_usbdetected.reset();
|
|
|
|
BUS_WAKER.wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
if regs.events_usbremoved.read().bits() != 0 {
|
|
|
|
regs.events_usbremoved.reset();
|
|
|
|
BUS_WAKER.wake();
|
|
|
|
POWER_WAKER.wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
if regs.events_usbpwrrdy.read().bits() != 0 {
|
|
|
|
regs.events_usbpwrrdy.reset();
|
|
|
|
POWER_WAKER.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "_nrf5340-app"))]
|
|
|
|
impl UsbSupply for PowerUsb {
|
|
|
|
fn is_usb_detected(&self) -> bool {
|
|
|
|
let regs = unsafe { &*pac::POWER::ptr() };
|
|
|
|
regs.usbregstatus.read().vbusdetect().is_vbus_present()
|
|
|
|
}
|
|
|
|
|
|
|
|
type UsbPowerReadyFuture<'a> = impl Future<Output = Result<(), ()>> + 'a where Self: 'a;
|
|
|
|
fn wait_power_ready(&mut self) -> Self::UsbPowerReadyFuture<'_> {
|
|
|
|
poll_fn(move |cx| {
|
|
|
|
POWER_WAKER.register(cx.waker());
|
|
|
|
let regs = unsafe { &*pac::POWER::ptr() };
|
|
|
|
|
|
|
|
if regs.usbregstatus.read().outputrdy().is_ready() {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else if !self.is_usb_detected() {
|
|
|
|
Poll::Ready(Err(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SignalledSupply {
|
|
|
|
pub fn new(usb_detected: bool, power_ready: bool) -> Self {
|
|
|
|
BUS_WAKER.wake();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
usb_detected: AtomicBool::new(usb_detected),
|
|
|
|
power_ready: AtomicBool::new(power_ready),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn detected(&self, detected: bool) {
|
|
|
|
self.usb_detected.store(detected, Ordering::Relaxed);
|
|
|
|
self.power_ready.store(false, Ordering::Relaxed);
|
|
|
|
BUS_WAKER.wake();
|
|
|
|
POWER_WAKER.wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ready(&self) {
|
|
|
|
self.power_ready.store(true, Ordering::Relaxed);
|
|
|
|
POWER_WAKER.wake();
|
|
|
|
}
|
2021-12-14 21:51:50 +01:00
|
|
|
}
|
|
|
|
|
2022-07-09 08:40:10 +02:00
|
|
|
impl UsbSupply for SignalledSupply {
|
|
|
|
fn is_usb_detected(&self) -> bool {
|
|
|
|
self.usb_detected.load(Ordering::Relaxed)
|
2022-07-08 08:22:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-09 08:40:10 +02:00
|
|
|
type UsbPowerReadyFuture<'a> = impl Future<Output = Result<(), ()>> + 'a where Self: 'a;
|
|
|
|
fn wait_power_ready(&mut self) -> Self::UsbPowerReadyFuture<'_> {
|
|
|
|
poll_fn(move |cx| {
|
|
|
|
POWER_WAKER.register(cx.waker());
|
|
|
|
|
|
|
|
if self.power_ready.load(Ordering::Relaxed) {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else if !self.usb_detected.load(Ordering::Relaxed) {
|
|
|
|
Poll::Ready(Err(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance, P: UsbSupply> Driver<'d, T, P> {
|
|
|
|
pub fn new(
|
2022-07-08 08:22:25 +02:00
|
|
|
_usb: impl Unborrow<Target = T> + 'd,
|
|
|
|
irq: impl Unborrow<Target = T::Interrupt> + 'd,
|
2022-07-09 08:40:10 +02:00
|
|
|
usb_supply: P,
|
2022-07-08 08:22:25 +02:00
|
|
|
) -> Self {
|
2022-03-09 01:34:35 +01:00
|
|
|
unborrow!(irq);
|
|
|
|
irq.set_handler(Self::on_interrupt);
|
|
|
|
irq.unpend();
|
|
|
|
irq.enable();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
phantom: PhantomData,
|
|
|
|
alloc_in: Allocator::new(),
|
|
|
|
alloc_out: Allocator::new(),
|
2022-07-09 08:40:10 +02:00
|
|
|
usb_supply,
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_interrupt(_: *mut ()) {
|
|
|
|
let regs = T::regs();
|
|
|
|
|
2022-03-09 23:06:27 +01:00
|
|
|
if regs.events_usbreset.read().bits() != 0 {
|
|
|
|
regs.intenclr.write(|w| w.usbreset().clear());
|
|
|
|
BUS_WAKER.wake();
|
2022-04-06 03:14:22 +02:00
|
|
|
EP0_WAKER.wake();
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
if regs.events_ep0setup.read().bits() != 0 {
|
|
|
|
regs.intenclr.write(|w| w.ep0setup().clear());
|
2022-04-02 04:42:20 +02:00
|
|
|
EP0_WAKER.wake();
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-09 23:06:27 +01:00
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
if regs.events_ep0datadone.read().bits() != 0 {
|
|
|
|
regs.intenclr.write(|w| w.ep0datadone().clear());
|
2022-04-02 04:42:20 +02:00
|
|
|
EP0_WAKER.wake();
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// USBEVENT and EPDATA events are weird. They're the "aggregate"
|
|
|
|
// of individual bits in EVENTCAUSE and EPDATASTATUS. We handle them
|
|
|
|
// differently than events normally.
|
|
|
|
//
|
|
|
|
// They seem to be edge-triggered, not level-triggered: when an
|
|
|
|
// individual bit goes 0->1, the event fires *just once*.
|
|
|
|
// Therefore, it's fine to clear just the event, and let main thread
|
|
|
|
// check the individual bits in EVENTCAUSE and EPDATASTATUS. It
|
|
|
|
// doesn't cause an infinite irq loop.
|
|
|
|
if regs.events_usbevent.read().bits() != 0 {
|
|
|
|
regs.events_usbevent.reset();
|
|
|
|
BUS_WAKER.wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
if regs.events_epdata.read().bits() != 0 {
|
|
|
|
regs.events_epdata.reset();
|
|
|
|
|
|
|
|
let r = regs.epdatastatus.read().bits();
|
2022-03-10 01:05:33 +01:00
|
|
|
regs.epdatastatus.write(|w| unsafe { w.bits(r) });
|
|
|
|
READY_ENDPOINTS.fetch_or(r, Ordering::AcqRel);
|
2022-03-09 23:06:27 +01:00
|
|
|
for i in 1..=7 {
|
2022-04-02 22:35:03 +02:00
|
|
|
if r & In::mask(i) != 0 {
|
|
|
|
In::waker(i).wake();
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
2022-04-02 22:35:03 +02:00
|
|
|
if r & Out::mask(i) != 0 {
|
|
|
|
Out::waker(i).wake();
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 18:11:00 +01:00
|
|
|
}
|
|
|
|
|
2022-07-09 08:40:10 +02:00
|
|
|
impl<'d, T: Instance, P: UsbSupply + 'd> driver::Driver<'d> for Driver<'d, T, P> {
|
2022-03-09 01:34:35 +01:00
|
|
|
type EndpointOut = Endpoint<'d, T, Out>;
|
|
|
|
type EndpointIn = Endpoint<'d, T, In>;
|
2022-03-25 21:46:14 +01:00
|
|
|
type ControlPipe = ControlPipe<'d, T>;
|
2022-07-09 08:40:10 +02:00
|
|
|
type Bus = Bus<'d, T, P>;
|
2021-12-15 18:29:19 +01:00
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
fn alloc_endpoint_in(
|
|
|
|
&mut self,
|
|
|
|
ep_type: EndpointType,
|
2022-05-09 02:07:48 +02:00
|
|
|
packet_size: u16,
|
2022-03-09 01:34:35 +01:00
|
|
|
interval: u8,
|
|
|
|
) -> Result<Self::EndpointIn, driver::EndpointAllocError> {
|
2022-05-10 16:53:42 +02:00
|
|
|
let index = self.alloc_in.allocate(ep_type)?;
|
2022-03-09 01:34:35 +01:00
|
|
|
let ep_addr = EndpointAddress::from_parts(index, UsbDirection::In);
|
2022-03-10 01:05:33 +01:00
|
|
|
Ok(Endpoint::new(EndpointInfo {
|
|
|
|
addr: ep_addr,
|
|
|
|
ep_type,
|
2022-05-09 02:07:48 +02:00
|
|
|
max_packet_size: packet_size,
|
2022-03-10 01:05:33 +01:00
|
|
|
interval,
|
|
|
|
}))
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2021-12-15 00:48:48 +01:00
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
fn alloc_endpoint_out(
|
|
|
|
&mut self,
|
|
|
|
ep_type: EndpointType,
|
2022-05-09 02:07:48 +02:00
|
|
|
packet_size: u16,
|
2022-03-09 01:34:35 +01:00
|
|
|
interval: u8,
|
|
|
|
) -> Result<Self::EndpointOut, driver::EndpointAllocError> {
|
2022-05-10 16:53:42 +02:00
|
|
|
let index = self.alloc_out.allocate(ep_type)?;
|
2022-03-09 01:34:35 +01:00
|
|
|
let ep_addr = EndpointAddress::from_parts(index, UsbDirection::Out);
|
2022-03-10 01:05:33 +01:00
|
|
|
Ok(Endpoint::new(EndpointInfo {
|
|
|
|
addr: ep_addr,
|
|
|
|
ep_type,
|
2022-05-09 02:07:48 +02:00
|
|
|
max_packet_size: packet_size,
|
2022-03-10 01:05:33 +01:00
|
|
|
interval,
|
|
|
|
}))
|
2021-12-14 21:51:50 +01:00
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-05-10 16:53:42 +02:00
|
|
|
fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe) {
|
|
|
|
(
|
2022-06-16 08:08:58 +02:00
|
|
|
Bus {
|
|
|
|
phantom: PhantomData,
|
|
|
|
power_available: false,
|
2022-07-09 08:40:10 +02:00
|
|
|
usb_supply: self.usb_supply,
|
2022-06-16 08:08:58 +02:00
|
|
|
},
|
2022-05-10 16:53:42 +02:00
|
|
|
ControlPipe {
|
|
|
|
_phantom: PhantomData,
|
|
|
|
max_packet_size: control_max_packet_size,
|
|
|
|
},
|
|
|
|
)
|
2022-04-10 21:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-09 08:40:10 +02:00
|
|
|
pub struct Bus<'d, T: Instance, P: UsbSupply> {
|
2022-04-10 21:41:51 +02:00
|
|
|
phantom: PhantomData<&'d mut T>,
|
2022-06-16 08:08:58 +02:00
|
|
|
power_available: bool,
|
2022-07-09 08:40:10 +02:00
|
|
|
usb_supply: P,
|
2022-04-10 21:41:51 +02:00
|
|
|
}
|
|
|
|
|
2022-07-09 08:40:10 +02:00
|
|
|
impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> {
|
2022-04-10 21:41:51 +02:00
|
|
|
type EnableFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a;
|
2022-04-12 23:51:50 +02:00
|
|
|
type DisableFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a;
|
2022-04-10 21:41:51 +02:00
|
|
|
type PollFuture<'a> = impl Future<Output = Event> + 'a where Self: 'a;
|
|
|
|
type RemoteWakeupFuture<'a> = impl Future<Output = Result<(), Unsupported>> + 'a where Self: 'a;
|
|
|
|
|
|
|
|
fn enable(&mut self) -> Self::EnableFuture<'_> {
|
2022-04-07 04:10:18 +02:00
|
|
|
async move {
|
|
|
|
let regs = T::regs();
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-07 04:10:18 +02:00
|
|
|
errata::pre_enable();
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-07 04:10:18 +02:00
|
|
|
regs.enable.write(|w| w.enable().enabled());
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-07 04:10:18 +02:00
|
|
|
// Wait until the peripheral is ready.
|
|
|
|
regs.intenset.write(|w| w.usbevent().set_bit());
|
|
|
|
poll_fn(|cx| {
|
|
|
|
BUS_WAKER.register(cx.waker());
|
|
|
|
if regs.eventcause.read().ready().is_ready() {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
regs.eventcause.write(|w| w.ready().set_bit()); // Write 1 to clear.
|
|
|
|
|
|
|
|
errata::post_enable();
|
|
|
|
|
|
|
|
unsafe { NVIC::unmask(pac::Interrupt::USBD) };
|
|
|
|
|
|
|
|
regs.intenset.write(|w| {
|
|
|
|
w.usbreset().set_bit();
|
|
|
|
w.usbevent().set_bit();
|
|
|
|
w.epdata().set_bit();
|
|
|
|
w
|
|
|
|
});
|
2022-07-09 08:40:10 +02:00
|
|
|
|
|
|
|
if self.usb_supply.wait_power_ready().await.is_ok() {
|
|
|
|
// Enable the USB pullup, allowing enumeration.
|
|
|
|
regs.usbpullup.write(|w| w.connect().enabled());
|
|
|
|
trace!("enabled");
|
|
|
|
} else {
|
|
|
|
trace!("usb power not ready due to usb removal");
|
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:51:50 +02:00
|
|
|
fn disable(&mut self) -> Self::DisableFuture<'_> {
|
|
|
|
async move {
|
|
|
|
let regs = T::regs();
|
|
|
|
regs.enable.write(|x| x.enable().disabled());
|
|
|
|
}
|
2022-04-10 21:41:51 +02:00
|
|
|
}
|
2022-03-09 23:06:27 +01:00
|
|
|
|
|
|
|
fn poll<'a>(&'a mut self) -> Self::PollFuture<'a> {
|
2022-04-10 21:41:51 +02:00
|
|
|
poll_fn(move |cx| {
|
2022-03-09 23:06:27 +01:00
|
|
|
BUS_WAKER.register(cx.waker());
|
|
|
|
let regs = T::regs();
|
|
|
|
|
|
|
|
if regs.events_usbreset.read().bits() != 0 {
|
|
|
|
regs.events_usbreset.reset();
|
|
|
|
regs.intenset.write(|w| w.usbreset().set());
|
2022-04-16 04:47:27 +02:00
|
|
|
|
|
|
|
// Disable all endpoints except EP0
|
|
|
|
regs.epinen.write(|w| unsafe { w.bits(0x01) });
|
|
|
|
regs.epouten.write(|w| unsafe { w.bits(0x01) });
|
|
|
|
READY_ENDPOINTS.store(In::mask(0), Ordering::Release);
|
|
|
|
for i in 1..=7 {
|
|
|
|
In::waker(i).wake();
|
|
|
|
Out::waker(i).wake();
|
|
|
|
}
|
|
|
|
|
2022-03-09 23:06:27 +01:00
|
|
|
return Poll::Ready(Event::Reset);
|
|
|
|
}
|
|
|
|
|
|
|
|
let r = regs.eventcause.read();
|
|
|
|
|
|
|
|
if r.isooutcrc().bit() {
|
|
|
|
regs.eventcause.write(|w| w.isooutcrc().set_bit());
|
2022-03-30 01:30:58 +02:00
|
|
|
trace!("USB event: isooutcrc");
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
if r.usbwuallowed().bit() {
|
|
|
|
regs.eventcause.write(|w| w.usbwuallowed().set_bit());
|
2022-03-30 01:30:58 +02:00
|
|
|
trace!("USB event: usbwuallowed");
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
if r.suspend().bit() {
|
|
|
|
regs.eventcause.write(|w| w.suspend().set_bit());
|
2022-04-10 21:41:51 +02:00
|
|
|
regs.lowpower.write(|w| w.lowpower().low_power());
|
|
|
|
return Poll::Ready(Event::Suspend);
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
if r.resume().bit() {
|
|
|
|
regs.eventcause.write(|w| w.resume().set_bit());
|
2022-04-10 21:41:51 +02:00
|
|
|
return Poll::Ready(Event::Resume);
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
if r.ready().bit() {
|
|
|
|
regs.eventcause.write(|w| w.ready().set_bit());
|
2022-03-30 01:30:58 +02:00
|
|
|
trace!("USB event: ready");
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
|
2022-07-09 08:40:10 +02:00
|
|
|
if self.usb_supply.is_usb_detected() != self.power_available {
|
2022-06-16 08:08:58 +02:00
|
|
|
self.power_available = !self.power_available;
|
|
|
|
if self.power_available {
|
|
|
|
trace!("Power event: available");
|
|
|
|
return Poll::Ready(Event::PowerDetected);
|
|
|
|
} else {
|
|
|
|
trace!("Power event: removed");
|
|
|
|
return Poll::Ready(Event::PowerRemoved);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 23:06:27 +01:00
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-02 22:35:03 +02:00
|
|
|
#[inline]
|
2022-04-16 04:47:27 +02:00
|
|
|
fn set_address(&mut self, _addr: u8) {
|
|
|
|
// Nothing to do, the peripheral handles this.
|
|
|
|
}
|
|
|
|
|
|
|
|
fn endpoint_set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool) {
|
2022-05-30 00:07:15 +02:00
|
|
|
let regs = T::regs();
|
|
|
|
unsafe {
|
|
|
|
if ep_addr.index() == 0 {
|
2022-06-12 22:15:44 +02:00
|
|
|
regs.tasks_ep0stall.write(|w| w.tasks_ep0stall().bit(stalled));
|
2022-05-30 00:07:15 +02:00
|
|
|
} else {
|
|
|
|
regs.epstall.write(|w| {
|
|
|
|
w.ep().bits(ep_addr.index() as u8 & 0b111);
|
|
|
|
w.io().bit(ep_addr.is_in());
|
|
|
|
w.stall().bit(stalled)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn endpoint_is_stalled(&mut self, ep_addr: EndpointAddress) -> bool {
|
2022-05-30 00:07:15 +02:00
|
|
|
let regs = T::regs();
|
|
|
|
let i = ep_addr.index();
|
|
|
|
match ep_addr.direction() {
|
|
|
|
UsbDirection::Out => regs.halted.epout[i].read().getstatus().is_halted(),
|
|
|
|
UsbDirection::In => regs.halted.epin[i].read().getstatus().is_halted(),
|
|
|
|
}
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn endpoint_set_enabled(&mut self, ep_addr: EndpointAddress, enabled: bool) {
|
2022-04-02 22:35:03 +02:00
|
|
|
let regs = T::regs();
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
let i = ep_addr.index();
|
|
|
|
let mask = 1 << i;
|
2022-04-02 22:35:03 +02:00
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
debug!("endpoint_set_enabled {:?} {}", ep_addr, enabled);
|
2022-04-02 22:35:03 +02:00
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
match ep_addr.direction() {
|
|
|
|
UsbDirection::In => {
|
|
|
|
let mut was_enabled = false;
|
|
|
|
regs.epinen.modify(|r, w| {
|
|
|
|
let mut bits = r.bits();
|
|
|
|
was_enabled = (bits & mask) != 0;
|
|
|
|
if enabled {
|
|
|
|
bits |= mask
|
|
|
|
} else {
|
|
|
|
bits &= !mask
|
|
|
|
}
|
|
|
|
unsafe { w.bits(bits) }
|
|
|
|
});
|
2022-04-02 22:35:03 +02:00
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
let ready_mask = In::mask(i);
|
|
|
|
if enabled {
|
|
|
|
if !was_enabled {
|
|
|
|
READY_ENDPOINTS.fetch_or(ready_mask, Ordering::AcqRel);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
READY_ENDPOINTS.fetch_and(!ready_mask, Ordering::AcqRel);
|
|
|
|
}
|
|
|
|
|
|
|
|
In::waker(i).wake();
|
|
|
|
}
|
|
|
|
UsbDirection::Out => {
|
|
|
|
regs.epouten.modify(|r, w| {
|
|
|
|
let mut bits = r.bits();
|
|
|
|
if enabled {
|
|
|
|
bits |= mask
|
|
|
|
} else {
|
|
|
|
bits &= !mask
|
|
|
|
}
|
|
|
|
unsafe { w.bits(bits) }
|
|
|
|
});
|
|
|
|
|
|
|
|
let ready_mask = Out::mask(i);
|
|
|
|
if enabled {
|
2022-04-02 22:35:03 +02:00
|
|
|
// when first enabled, bulk/interrupt OUT endpoints will *not* receive data (the
|
|
|
|
// peripheral will NAK all incoming packets) until we write a zero to the SIZE
|
|
|
|
// register (see figure 203 of the 52840 manual). To avoid that we write a 0 to the
|
|
|
|
// SIZE register
|
2022-04-16 04:47:27 +02:00
|
|
|
regs.size.epout[i].reset();
|
|
|
|
} else {
|
|
|
|
READY_ENDPOINTS.fetch_and(!ready_mask, Ordering::AcqRel);
|
2022-04-02 22:35:03 +02:00
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
Out::waker(i).wake();
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-04-10 21:41:51 +02:00
|
|
|
fn remote_wakeup(&mut self) -> Self::RemoteWakeupFuture<'_> {
|
|
|
|
async move {
|
|
|
|
let regs = T::regs();
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
if regs.lowpower.read().lowpower().is_low_power() {
|
|
|
|
errata::pre_wakeup();
|
|
|
|
|
|
|
|
regs.lowpower.write(|w| w.lowpower().force_normal());
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
poll_fn(|cx| {
|
|
|
|
BUS_WAKER.register(cx.waker());
|
|
|
|
let regs = T::regs();
|
|
|
|
let r = regs.eventcause.read();
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
if regs.events_usbreset.read().bits() != 0 {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else if r.resume().bit() {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else if r.usbwuallowed().bit() {
|
|
|
|
regs.eventcause.write(|w| w.usbwuallowed().set_bit());
|
|
|
|
|
|
|
|
regs.dpdmvalue.write(|w| w.state().resume());
|
2022-06-12 22:15:44 +02:00
|
|
|
regs.tasks_dpdmdrive.write(|w| w.tasks_dpdmdrive().set_bit());
|
2022-04-10 21:41:51 +02:00
|
|
|
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
errata::post_wakeup();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Out {}
|
|
|
|
pub enum In {}
|
|
|
|
|
2022-04-02 22:35:03 +02:00
|
|
|
trait EndpointDir {
|
|
|
|
fn waker(i: usize) -> &'static AtomicWaker;
|
|
|
|
fn mask(i: usize) -> u32;
|
|
|
|
fn is_enabled(regs: &RegisterBlock, i: usize) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EndpointDir for In {
|
|
|
|
#[inline]
|
|
|
|
fn waker(i: usize) -> &'static AtomicWaker {
|
|
|
|
&EP_IN_WAKERS[i - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn mask(i: usize) -> u32 {
|
|
|
|
1 << i
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn is_enabled(regs: &RegisterBlock, i: usize) -> bool {
|
|
|
|
(regs.epinen.read().bits() & (1 << i)) != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EndpointDir for Out {
|
|
|
|
#[inline]
|
|
|
|
fn waker(i: usize) -> &'static AtomicWaker {
|
|
|
|
&EP_OUT_WAKERS[i - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn mask(i: usize) -> u32 {
|
|
|
|
1 << (i + 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn is_enabled(regs: &RegisterBlock, i: usize) -> bool {
|
|
|
|
(regs.epouten.read().bits() & (1 << i)) != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
pub struct Endpoint<'d, T: Instance, Dir> {
|
|
|
|
_phantom: PhantomData<(&'d mut T, Dir)>,
|
|
|
|
info: EndpointInfo,
|
|
|
|
}
|
|
|
|
|
2022-03-10 01:05:33 +01:00
|
|
|
impl<'d, T: Instance, Dir> Endpoint<'d, T, Dir> {
|
|
|
|
fn new(info: EndpointInfo) -> Self {
|
|
|
|
Self {
|
|
|
|
info,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 22:35:03 +02:00
|
|
|
impl<'d, T: Instance, Dir: EndpointDir> driver::Endpoint for Endpoint<'d, T, Dir> {
|
2022-03-09 01:34:35 +01:00
|
|
|
fn info(&self) -> &EndpointInfo {
|
|
|
|
&self.info
|
|
|
|
}
|
|
|
|
|
2022-04-02 22:35:03 +02:00
|
|
|
type WaitEnabledFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a;
|
|
|
|
|
|
|
|
fn wait_enabled(&mut self) -> Self::WaitEnabledFuture<'_> {
|
|
|
|
let i = self.info.addr.index();
|
|
|
|
assert!(i != 0);
|
|
|
|
|
|
|
|
poll_fn(move |cx| {
|
|
|
|
Dir::waker(i).register(cx.waker());
|
|
|
|
if Dir::is_enabled(T::regs(), i) {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance, Dir> Endpoint<'d, T, Dir> {
|
|
|
|
async fn wait_data_ready(&mut self) -> Result<(), ()>
|
|
|
|
where
|
|
|
|
Dir: EndpointDir,
|
|
|
|
{
|
|
|
|
let i = self.info.addr.index();
|
|
|
|
assert!(i != 0);
|
|
|
|
poll_fn(|cx| {
|
|
|
|
Dir::waker(i).register(cx.waker());
|
|
|
|
let r = READY_ENDPOINTS.load(Ordering::Acquire);
|
|
|
|
if !Dir::is_enabled(T::regs(), i) {
|
|
|
|
Poll::Ready(Err(()))
|
|
|
|
} else if r & Dir::mask(i) != 0 {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Mark as not ready
|
|
|
|
READY_ENDPOINTS.fetch_and(!Dir::mask(i), Ordering::AcqRel);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-12-14 01:50:08 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 04:04:11 +02:00
|
|
|
unsafe fn read_dma<T: Instance>(i: usize, buf: &mut [u8]) -> Result<usize, EndpointError> {
|
2022-03-25 21:46:14 +01:00
|
|
|
let regs = T::regs();
|
|
|
|
|
|
|
|
// Check that the packet fits into the buffer
|
2022-03-28 02:18:13 +02:00
|
|
|
let size = regs.size.epout[i].read().bits() as usize;
|
2022-03-25 21:46:14 +01:00
|
|
|
if size > buf.len() {
|
2022-04-06 04:04:11 +02:00
|
|
|
return Err(EndpointError::BufferOverflow);
|
2022-03-25 21:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let epout = [
|
|
|
|
®s.epout0,
|
|
|
|
®s.epout1,
|
|
|
|
®s.epout2,
|
|
|
|
®s.epout3,
|
|
|
|
®s.epout4,
|
|
|
|
®s.epout5,
|
|
|
|
®s.epout6,
|
|
|
|
®s.epout7,
|
|
|
|
];
|
|
|
|
epout[i].ptr.write(|w| w.bits(buf.as_ptr() as u32));
|
|
|
|
// MAXCNT must match SIZE
|
|
|
|
epout[i].maxcnt.write(|w| w.bits(size as u32));
|
|
|
|
|
|
|
|
dma_start();
|
|
|
|
regs.events_endepout[i].reset();
|
|
|
|
regs.tasks_startepout[i].write(|w| w.tasks_startepout().set_bit());
|
2022-06-12 22:15:44 +02:00
|
|
|
while regs.events_endepout[i].read().events_endepout().bit_is_clear() {}
|
2022-03-25 21:46:14 +01:00
|
|
|
regs.events_endepout[i].reset();
|
|
|
|
dma_end();
|
|
|
|
|
|
|
|
regs.size.epout[i].reset();
|
|
|
|
|
|
|
|
Ok(size)
|
|
|
|
}
|
|
|
|
|
2022-03-30 02:01:09 +02:00
|
|
|
unsafe fn write_dma<T: Instance>(i: usize, buf: &[u8]) {
|
2022-03-25 21:46:14 +01:00
|
|
|
let regs = T::regs();
|
2022-03-30 02:01:09 +02:00
|
|
|
assert!(buf.len() <= 64);
|
2022-03-25 21:46:14 +01:00
|
|
|
|
|
|
|
let mut ram_buf: MaybeUninit<[u8; 64]> = MaybeUninit::uninit();
|
2022-03-28 16:46:26 +02:00
|
|
|
let ptr = if !slice_in_ram(buf) {
|
|
|
|
// EasyDMA can't read FLASH, so we copy through RAM
|
|
|
|
let ptr = ram_buf.as_mut_ptr() as *mut u8;
|
|
|
|
core::ptr::copy_nonoverlapping(buf.as_ptr(), ptr, buf.len());
|
|
|
|
ptr
|
|
|
|
} else {
|
|
|
|
buf.as_ptr()
|
|
|
|
};
|
2022-03-25 21:46:14 +01:00
|
|
|
|
|
|
|
let epin = [
|
|
|
|
®s.epin0,
|
|
|
|
®s.epin1,
|
|
|
|
®s.epin2,
|
|
|
|
®s.epin3,
|
|
|
|
®s.epin4,
|
|
|
|
®s.epin5,
|
|
|
|
®s.epin6,
|
|
|
|
®s.epin7,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Set the buffer length so the right number of bytes are transmitted.
|
|
|
|
// Safety: `buf.len()` has been checked to be <= the max buffer length.
|
|
|
|
epin[i].ptr.write(|w| w.bits(ptr as u32));
|
|
|
|
epin[i].maxcnt.write(|w| w.maxcnt().bits(buf.len() as u8));
|
|
|
|
|
|
|
|
regs.events_endepin[i].reset();
|
|
|
|
|
|
|
|
dma_start();
|
|
|
|
regs.tasks_startepin[i].write(|w| w.bits(1));
|
|
|
|
while regs.events_endepin[i].read().bits() == 0 {}
|
|
|
|
dma_end();
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
impl<'d, T: Instance> driver::EndpointOut for Endpoint<'d, T, Out> {
|
2022-04-06 04:04:11 +02:00
|
|
|
type ReadFuture<'a> = impl Future<Output = Result<usize, EndpointError>> + 'a where Self: 'a;
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
|
|
|
async move {
|
2022-03-09 23:06:27 +01:00
|
|
|
let i = self.info.addr.index();
|
2022-03-25 21:46:14 +01:00
|
|
|
assert!(i != 0);
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
self.wait_data_ready().await.map_err(|_| EndpointError::Disabled)?;
|
2022-04-02 17:58:01 +02:00
|
|
|
|
|
|
|
unsafe { read_dma::<T>(i, buf) }
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> driver::EndpointIn for Endpoint<'d, T, In> {
|
2022-04-06 04:04:11 +02:00
|
|
|
type WriteFuture<'a> = impl Future<Output = Result<(), EndpointError>> + 'a where Self: 'a;
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
async move {
|
2022-03-10 01:05:33 +01:00
|
|
|
let i = self.info.addr.index();
|
2022-03-25 21:46:14 +01:00
|
|
|
assert!(i != 0);
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
self.wait_data_ready().await.map_err(|_| EndpointError::Disabled)?;
|
2022-03-25 21:46:14 +01:00
|
|
|
|
|
|
|
unsafe { write_dma::<T>(i, buf) }
|
2022-03-30 02:01:09 +02:00
|
|
|
|
|
|
|
Ok(())
|
2022-03-25 21:46:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-03-25 21:46:14 +01:00
|
|
|
pub struct ControlPipe<'d, T: Instance> {
|
|
|
|
_phantom: PhantomData<&'d mut T>,
|
|
|
|
max_packet_size: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
|
2022-05-12 18:45:10 +02:00
|
|
|
type SetupFuture<'a> = impl Future<Output = [u8;8]> + 'a where Self: 'a;
|
2022-04-06 04:04:11 +02:00
|
|
|
type DataOutFuture<'a> = impl Future<Output = Result<usize, EndpointError>> + 'a where Self: 'a;
|
|
|
|
type DataInFuture<'a> = impl Future<Output = Result<(), EndpointError>> + 'a where Self: 'a;
|
2022-05-30 00:35:27 +02:00
|
|
|
type AcceptFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a;
|
|
|
|
type RejectFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a;
|
2022-03-30 02:26:30 +02:00
|
|
|
|
|
|
|
fn max_packet_size(&self) -> usize {
|
|
|
|
usize::from(self.max_packet_size)
|
|
|
|
}
|
2022-03-25 21:46:14 +01:00
|
|
|
|
|
|
|
fn setup<'a>(&'a mut self) -> Self::SetupFuture<'a> {
|
|
|
|
async move {
|
|
|
|
let regs = T::regs();
|
|
|
|
|
2022-05-12 18:45:10 +02:00
|
|
|
// Reset shorts
|
|
|
|
regs.shorts.write(|w| w);
|
|
|
|
|
2022-03-25 21:46:14 +01:00
|
|
|
// Wait for SETUP packet
|
2022-05-12 18:45:10 +02:00
|
|
|
regs.intenset.write(|w| w.ep0setup().set());
|
2022-03-25 21:46:14 +01:00
|
|
|
poll_fn(|cx| {
|
2022-04-02 04:42:20 +02:00
|
|
|
EP0_WAKER.register(cx.waker());
|
2022-03-25 21:46:14 +01:00
|
|
|
let regs = T::regs();
|
|
|
|
if regs.events_ep0setup.read().bits() != 0 {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
regs.events_ep0setup.reset();
|
|
|
|
|
|
|
|
let mut buf = [0; 8];
|
|
|
|
buf[0] = regs.bmrequesttype.read().bits() as u8;
|
|
|
|
buf[1] = regs.brequest.read().brequest().bits();
|
|
|
|
buf[2] = regs.wvaluel.read().wvaluel().bits();
|
|
|
|
buf[3] = regs.wvalueh.read().wvalueh().bits();
|
|
|
|
buf[4] = regs.windexl.read().windexl().bits();
|
|
|
|
buf[5] = regs.windexh.read().windexh().bits();
|
|
|
|
buf[6] = regs.wlengthl.read().wlengthl().bits();
|
|
|
|
buf[7] = regs.wlengthh.read().wlengthh().bits();
|
|
|
|
|
2022-05-12 18:14:48 +02:00
|
|
|
buf
|
2022-03-25 21:46:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
fn data_out<'a>(&'a mut self, buf: &'a mut [u8], _first: bool, _last: bool) -> Self::DataOutFuture<'a> {
|
2022-03-25 21:46:14 +01:00
|
|
|
async move {
|
2022-03-30 02:26:30 +02:00
|
|
|
let regs = T::regs();
|
|
|
|
|
2022-05-12 18:45:10 +02:00
|
|
|
regs.events_ep0datadone.reset();
|
|
|
|
|
|
|
|
// This starts a RX on EP0. events_ep0datadone notifies when done.
|
2022-06-12 22:15:44 +02:00
|
|
|
regs.tasks_ep0rcvout.write(|w| w.tasks_ep0rcvout().set_bit());
|
2022-05-12 18:14:48 +02:00
|
|
|
|
2022-03-30 02:26:30 +02:00
|
|
|
// Wait until ready
|
2022-04-06 03:14:22 +02:00
|
|
|
regs.intenset.write(|w| {
|
|
|
|
w.usbreset().set();
|
|
|
|
w.ep0setup().set();
|
|
|
|
w.ep0datadone().set()
|
|
|
|
});
|
2022-03-30 02:26:30 +02:00
|
|
|
poll_fn(|cx| {
|
2022-04-02 04:42:20 +02:00
|
|
|
EP0_WAKER.register(cx.waker());
|
2022-03-30 02:26:30 +02:00
|
|
|
let regs = T::regs();
|
2022-04-07 16:49:50 +02:00
|
|
|
if regs.events_ep0datadone.read().bits() != 0 {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else if regs.events_usbreset.read().bits() != 0 {
|
2022-04-06 03:14:22 +02:00
|
|
|
trace!("aborted control data_out: usb reset");
|
2022-04-06 04:04:11 +02:00
|
|
|
Poll::Ready(Err(EndpointError::Disabled))
|
2022-04-06 03:14:22 +02:00
|
|
|
} else if regs.events_ep0setup.read().bits() != 0 {
|
|
|
|
trace!("aborted control data_out: received another SETUP");
|
2022-04-06 04:04:11 +02:00
|
|
|
Poll::Ready(Err(EndpointError::Disabled))
|
2022-03-30 02:26:30 +02:00
|
|
|
} else {
|
|
|
|
Poll::Pending
|
2022-03-10 01:05:33 +01:00
|
|
|
}
|
2022-03-30 02:26:30 +02:00
|
|
|
})
|
2022-04-06 03:14:22 +02:00
|
|
|
.await?;
|
2022-03-30 02:26:30 +02:00
|
|
|
|
|
|
|
unsafe { read_dma::<T>(0, buf) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
fn data_in<'a>(&'a mut self, buf: &'a [u8], _first: bool, last: bool) -> Self::DataInFuture<'a> {
|
2022-03-30 02:26:30 +02:00
|
|
|
async move {
|
|
|
|
let regs = T::regs();
|
|
|
|
regs.events_ep0datadone.reset();
|
2022-03-25 21:46:14 +01:00
|
|
|
|
2022-05-30 00:08:28 +02:00
|
|
|
regs.shorts.write(|w| w.ep0datadone_ep0status().bit(last));
|
2022-03-30 02:26:30 +02:00
|
|
|
|
2022-05-12 18:45:10 +02:00
|
|
|
// This starts a TX on EP0. events_ep0datadone notifies when done.
|
|
|
|
unsafe { write_dma::<T>(0, buf) }
|
|
|
|
|
2022-04-06 03:14:22 +02:00
|
|
|
regs.intenset.write(|w| {
|
|
|
|
w.usbreset().set();
|
|
|
|
w.ep0setup().set();
|
|
|
|
w.ep0datadone().set()
|
|
|
|
});
|
2022-03-30 02:26:30 +02:00
|
|
|
|
2022-04-06 03:14:22 +02:00
|
|
|
poll_fn(|cx| {
|
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
EP0_WAKER.register(cx.waker());
|
|
|
|
let regs = T::regs();
|
2022-04-07 16:49:50 +02:00
|
|
|
if regs.events_ep0datadone.read().bits() != 0 {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else if regs.events_usbreset.read().bits() != 0 {
|
2022-04-06 03:14:22 +02:00
|
|
|
trace!("aborted control data_in: usb reset");
|
2022-04-06 04:04:11 +02:00
|
|
|
Poll::Ready(Err(EndpointError::Disabled))
|
2022-04-06 03:14:22 +02:00
|
|
|
} else if regs.events_ep0setup.read().bits() != 0 {
|
|
|
|
trace!("aborted control data_in: received another SETUP");
|
2022-04-06 04:04:11 +02:00
|
|
|
Poll::Ready(Err(EndpointError::Disabled))
|
2022-04-06 03:14:22 +02:00
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
2022-03-25 21:46:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 00:35:27 +02:00
|
|
|
fn accept<'a>(&'a mut self) -> Self::AcceptFuture<'a> {
|
|
|
|
async move {
|
|
|
|
let regs = T::regs();
|
2022-06-12 22:15:44 +02:00
|
|
|
regs.tasks_ep0status.write(|w| w.tasks_ep0status().bit(true));
|
2022-05-30 00:35:27 +02:00
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-25 21:46:14 +01:00
|
|
|
|
2022-05-30 00:35:27 +02:00
|
|
|
fn reject<'a>(&'a mut self) -> Self::RejectFuture<'a> {
|
|
|
|
async move {
|
|
|
|
let regs = T::regs();
|
|
|
|
regs.tasks_ep0stall.write(|w| w.tasks_ep0stall().bit(true));
|
|
|
|
}
|
2022-03-25 21:46:14 +01:00
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dma_start() {
|
|
|
|
compiler_fence(Ordering::Release);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dma_end() {
|
|
|
|
compiler_fence(Ordering::Acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Allocator {
|
|
|
|
used: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Allocator {
|
|
|
|
fn new() -> Self {
|
2022-05-10 16:53:42 +02:00
|
|
|
Self { used: 0 }
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 16:53:42 +02:00
|
|
|
fn allocate(&mut self, ep_type: EndpointType) -> Result<usize, driver::EndpointAllocError> {
|
2022-03-09 01:34:35 +01:00
|
|
|
// Endpoint addresses are fixed in hardware:
|
|
|
|
// - 0x80 / 0x00 - Control EP0
|
|
|
|
// - 0x81 / 0x01 - Bulk/Interrupt EP1
|
|
|
|
// - 0x82 / 0x02 - Bulk/Interrupt EP2
|
|
|
|
// - 0x83 / 0x03 - Bulk/Interrupt EP3
|
|
|
|
// - 0x84 / 0x04 - Bulk/Interrupt EP4
|
|
|
|
// - 0x85 / 0x05 - Bulk/Interrupt EP5
|
|
|
|
// - 0x86 / 0x06 - Bulk/Interrupt EP6
|
|
|
|
// - 0x87 / 0x07 - Bulk/Interrupt EP7
|
|
|
|
// - 0x88 / 0x08 - Isochronous
|
|
|
|
|
|
|
|
// Endpoint directions are allocated individually.
|
|
|
|
|
2022-05-09 02:07:48 +02:00
|
|
|
let alloc_index = match ep_type {
|
|
|
|
EndpointType::Isochronous => 8,
|
|
|
|
EndpointType::Control => return Err(driver::EndpointAllocError),
|
|
|
|
EndpointType::Interrupt | EndpointType::Bulk => {
|
|
|
|
// Find rightmost zero bit in 1..=7
|
|
|
|
let ones = (self.used >> 1).trailing_ones() as usize;
|
|
|
|
if ones >= 7 {
|
|
|
|
return Err(driver::EndpointAllocError);
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-05-09 02:07:48 +02:00
|
|
|
ones + 1
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.used & (1 << alloc_index) != 0 {
|
|
|
|
return Err(driver::EndpointAllocError);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.used |= 1 << alloc_index;
|
|
|
|
|
|
|
|
Ok(alloc_index)
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
mod errata {
|
|
|
|
|
|
|
|
/// Writes `val` to `addr`. Used to apply Errata workarounds.
|
2022-04-10 22:25:40 +02:00
|
|
|
#[cfg(any(feature = "nrf52840", feature = "nrf52833", feature = "nrf52820"))]
|
2022-03-09 01:34:35 +01:00
|
|
|
unsafe fn poke(addr: u32, val: u32) {
|
|
|
|
(addr as *mut u32).write_volatile(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads 32 bits from `addr`.
|
2022-04-10 22:25:40 +02:00
|
|
|
#[cfg(feature = "nrf52840")]
|
2022-03-09 01:34:35 +01:00
|
|
|
unsafe fn peek(addr: u32) -> u32 {
|
|
|
|
(addr as *mut u32).read_volatile()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pre_enable() {
|
|
|
|
// Works around Erratum 187 on chip revisions 1 and 2.
|
2022-04-10 21:41:51 +02:00
|
|
|
#[cfg(any(feature = "nrf52840", feature = "nrf52833", feature = "nrf52820"))]
|
2022-03-09 01:34:35 +01:00
|
|
|
unsafe {
|
|
|
|
poke(0x4006EC00, 0x00009375);
|
|
|
|
poke(0x4006ED14, 0x00000003);
|
|
|
|
poke(0x4006EC00, 0x00009375);
|
|
|
|
}
|
|
|
|
|
|
|
|
pre_wakeup();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn post_enable() {
|
|
|
|
post_wakeup();
|
|
|
|
|
|
|
|
// Works around Erratum 187 on chip revisions 1 and 2.
|
2022-04-10 21:41:51 +02:00
|
|
|
#[cfg(any(feature = "nrf52840", feature = "nrf52833", feature = "nrf52820"))]
|
2022-03-09 01:34:35 +01:00
|
|
|
unsafe {
|
|
|
|
poke(0x4006EC00, 0x00009375);
|
|
|
|
poke(0x4006ED14, 0x00000000);
|
|
|
|
poke(0x4006EC00, 0x00009375);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pre_wakeup() {
|
|
|
|
// Works around Erratum 171 on chip revisions 1 and 2.
|
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
#[cfg(feature = "nrf52840")]
|
2022-03-09 01:34:35 +01:00
|
|
|
unsafe {
|
|
|
|
if peek(0x4006EC00) == 0x00000000 {
|
|
|
|
poke(0x4006EC00, 0x00009375);
|
|
|
|
}
|
|
|
|
|
|
|
|
poke(0x4006EC14, 0x000000C0);
|
|
|
|
poke(0x4006EC00, 0x00009375);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn post_wakeup() {
|
|
|
|
// Works around Erratum 171 on chip revisions 1 and 2.
|
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
#[cfg(feature = "nrf52840")]
|
2022-03-09 01:34:35 +01:00
|
|
|
unsafe {
|
|
|
|
if peek(0x4006EC00) == 0x00000000 {
|
|
|
|
poke(0x4006EC00, 0x00009375);
|
|
|
|
}
|
|
|
|
|
|
|
|
poke(0x4006EC14, 0x00000000);
|
|
|
|
poke(0x4006EC00, 0x00009375);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|