From 16812fe11940e6b1f381e5ca38778f34d4572608 Mon Sep 17 00:00:00 2001 From: doryan Date: Thu, 14 Nov 2024 02:14:03 +0400 Subject: [PATCH] fix --- src/lib.rs | 19 ++++++++++++------- src/types/usb_device.rs | 14 +++++++++++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ddff50a..ad1026a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,9 @@ use usb_device::{ }; mod types; -use types::{UsbDevice, DPRAM_SIZE, ENDPOINTS_ALLOC_LAYOUT}; +pub use types::UsbDevice; + +use types::{DPRAM_SIZE, ENDPOINTS_ALLOC_LAYOUT}; impl UsbBus for UsbDevice { fn alloc_ep( @@ -104,6 +106,10 @@ impl UsbBus for UsbDevice { usb.usbcon .modify(|_, w| w.frzclk().clear_bit().otgpade().set_bit()); + self.allocated_endpoints().for_each(|(i, _)| { + self.configure_endpoint(cs, i).unwrap(); + }); + // Interrupts. // usb.udien @@ -165,12 +171,7 @@ impl UsbBus for UsbDevice { if usb.usbcon.read().frzclk().bit_is_clear() { let (mut ep_out, mut ep_setup, mut ep_in_complete) = (0u8, 0u8, 0u8); - for (index, _ep) in self - .ep_table - .iter() - .enumerate() - .filter(|(_i, e)| e.is_allocated) - { + for (index, _ep) in self.allocated_endpoints() { if self.select_endpoint(cs, index).is_err() { // Endpoint selection has stopped working... break; @@ -273,6 +274,10 @@ impl UsbBus for UsbDevice { } }); + self.allocated_endpoints().for_each(|(i, _)| { + self.configure_endpoint(cs, i).unwrap(); + }); + // Reset endpoints // usb.uerst.modify(|_, w| unsafe { w.bits(u8::MAX >> 1) }); diff --git a/src/types/usb_device.rs b/src/types/usb_device.rs index 5e0c5c0..d5c0168 100644 --- a/src/types/usb_device.rs +++ b/src/types/usb_device.rs @@ -55,7 +55,7 @@ impl USBEndpoint { } } -pub(crate) struct UsbDevice { +pub struct UsbDevice { pub(crate) pll: Mutex, pub(crate) usb: Mutex, 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 UsbDevice { #[inline] - pub(crate) fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator { + pub fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator { let (pll, usb) = (Mutex::new(pll), Mutex::new(usb)); let ep_table: [USBEndpoint; L] = [Default::default(); L]; UsbBusAllocator::new(Self { @@ -78,6 +78,14 @@ impl UsbDevice { }) } + #[inline] + pub(crate) fn allocated_endpoints(&self) -> impl Iterator { + self.ep_table + .iter() + .enumerate() + .filter(|&(_, ep)| ep.is_allocated) + } + pub(crate) fn select_endpoint( &self, cs: CriticalSection<'_>, @@ -100,7 +108,7 @@ impl UsbDevice { } pub(crate) fn configure_endpoint( - &mut self, + &self, cs: CriticalSection<'_>, endpoint_index: usize, ) -> Result<(), UsbError> {