This commit is contained in:
doryan 2024-11-14 02:14:03 +04:00
parent 7436a78885
commit 16812fe119
2 changed files with 23 additions and 10 deletions

View File

@ -10,7 +10,9 @@ use usb_device::{
}; };
mod types; mod types;
use types::{UsbDevice, DPRAM_SIZE, ENDPOINTS_ALLOC_LAYOUT}; pub use types::UsbDevice;
use types::{DPRAM_SIZE, ENDPOINTS_ALLOC_LAYOUT};
impl<const L: usize> UsbBus for UsbDevice<L> { impl<const L: usize> UsbBus for UsbDevice<L> {
fn alloc_ep( fn alloc_ep(
@ -104,6 +106,10 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
usb.usbcon usb.usbcon
.modify(|_, w| w.frzclk().clear_bit().otgpade().set_bit()); .modify(|_, w| w.frzclk().clear_bit().otgpade().set_bit());
self.allocated_endpoints().for_each(|(i, _)| {
self.configure_endpoint(cs, i).unwrap();
});
// Interrupts. // // Interrupts. //
usb.udien usb.udien
@ -165,12 +171,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
if usb.usbcon.read().frzclk().bit_is_clear() { if usb.usbcon.read().frzclk().bit_is_clear() {
let (mut ep_out, mut ep_setup, mut ep_in_complete) = (0u8, 0u8, 0u8); let (mut ep_out, mut ep_setup, mut ep_in_complete) = (0u8, 0u8, 0u8);
for (index, _ep) in self for (index, _ep) in self.allocated_endpoints() {
.ep_table
.iter()
.enumerate()
.filter(|(_i, e)| e.is_allocated)
{
if self.select_endpoint(cs, index).is_err() { if self.select_endpoint(cs, index).is_err() {
// Endpoint selection has stopped working... // Endpoint selection has stopped working...
break; break;
@ -273,6 +274,10 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
} }
}); });
self.allocated_endpoints().for_each(|(i, _)| {
self.configure_endpoint(cs, i).unwrap();
});
// Reset endpoints // // Reset endpoints //
usb.uerst.modify(|_, w| unsafe { w.bits(u8::MAX >> 1) }); usb.uerst.modify(|_, w| unsafe { w.bits(u8::MAX >> 1) });

View File

@ -55,7 +55,7 @@ impl USBEndpoint {
} }
} }
pub(crate) struct UsbDevice<const L: usize> { pub struct UsbDevice<const L: usize> {
pub(crate) pll: Mutex<PLL>, pub(crate) pll: Mutex<PLL>,
pub(crate) usb: Mutex<USB_DEVICE>, pub(crate) usb: Mutex<USB_DEVICE>,
pub(crate) ep_table: [USBEndpoint; L], pub(crate) ep_table: [USBEndpoint; L],
@ -67,7 +67,7 @@ pub(crate) const ENDPOINTS_ALLOC_LAYOUT: [u16; 7] = [64, 256, 64, 64, 64, 64, 64
impl<const L: usize> UsbDevice<L> { impl<const L: usize> UsbDevice<L> {
#[inline] #[inline]
pub(crate) fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator<Self> { pub fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator<Self> {
let (pll, usb) = (Mutex::new(pll), Mutex::new(usb)); let (pll, usb) = (Mutex::new(pll), Mutex::new(usb));
let ep_table: [USBEndpoint; L] = [Default::default(); L]; let ep_table: [USBEndpoint; L] = [Default::default(); L];
UsbBusAllocator::new(Self { UsbBusAllocator::new(Self {
@ -78,6 +78,14 @@ impl<const L: usize> UsbDevice<L> {
}) })
} }
#[inline]
pub(crate) fn allocated_endpoints(&self) -> impl Iterator<Item = (usize, &USBEndpoint)> {
self.ep_table
.iter()
.enumerate()
.filter(|&(_, ep)| ep.is_allocated)
}
pub(crate) fn select_endpoint( pub(crate) fn select_endpoint(
&self, &self,
cs: CriticalSection<'_>, cs: CriticalSection<'_>,
@ -100,7 +108,7 @@ impl<const L: usize> UsbDevice<L> {
} }
pub(crate) fn configure_endpoint( pub(crate) fn configure_endpoint(
&mut self, &self,
cs: CriticalSection<'_>, cs: CriticalSection<'_>,
endpoint_index: usize, endpoint_index: usize,
) -> Result<(), UsbError> { ) -> Result<(), UsbError> {