Compare commits

..

2 Commits

2 changed files with 17 additions and 24 deletions

View File

@@ -38,14 +38,16 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
// If ep_aadr not provided, or current endpoint is allocated, try to find next free endpoint, otherwise return UsbError. //
_ => {
let index = self.ep_table[1..]
let index = self
.ep_table
.iter()
.enumerate()
.skip(1)
.find(|(index, ep)| {
!ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
})
.map(|(index, _)| index)
.ok_or(UsbError::EndpointOverflow)?;
.ok_or(UsbError::EndpointOverflow)?
.0;
EndpointAddress::from_parts(index, ep_dir)
}
@@ -188,7 +190,6 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
if usb.usbcon.read().frzclk().bit_is_clear() {
let (mut ep_out, mut ep_in_complete, mut ep_setup) = (0u16, 0u16, 0u16);
let pending_ins = self.pending_ins.borrow(cs);
for (ep_index, _ep) in self.allocated_endpoints() {
if self.select_endpoint(cs, ep_index).is_err() {
@@ -203,9 +204,8 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
ep_setup |= 1 << ep_index;
}
if pending_ins.get() & (1 << ep_index) != 0 && ueintx.txini().bit_is_set() {
if ueintx.txini().bit_is_set() {
ep_in_complete |= 1 << ep_index;
pending_ins.set(pending_ins.get() & !(1 << ep_index));
}
}
}
@@ -450,9 +450,6 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
}
};
let pending_ins = self.pending_ins.borrow(cs);
pending_ins.set(pending_ins.get() | 1 << ep_addr.index());
Ok(buf.len())
})
}

View File

@@ -1,5 +1,3 @@
use core::cell::Cell;
use avr_device::{
atmega32u4::{PLL, USB_DEVICE},
interrupt::{CriticalSection, Mutex},
@@ -8,7 +6,7 @@ use usb_device::{bus::UsbBusAllocator, endpoint::EndpointType, UsbDirection, Usb
#[allow(unused)]
#[derive(Default, Copy, Clone)]
pub(crate) struct USBEndpoint {
pub struct USBEndpoint {
pub(crate) is_allocated: bool,
pub(crate) size: u8,
pub(crate) ep_type: u8,
@@ -72,7 +70,6 @@ pub struct UsbDevice<const L: usize> {
pub(crate) pll: Mutex<PLL>,
pub(crate) usb: Mutex<USB_DEVICE>,
pub(crate) ep_table: [USBEndpoint; L],
pub(crate) pending_ins: Mutex<Cell<u8>>,
pub(crate) dpram_already_used: u16,
}
@@ -84,17 +81,12 @@ pub(crate) const ONE_MS_16_MGHZ: u32 = 16000;
impl<const L: usize> UsbDevice<L> {
#[inline]
pub fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator<Self> {
if L > 1 {
UsbBusAllocator::new(Self {
pll: Mutex::new(pll),
usb: Mutex::new(usb),
ep_table: [USBEndpoint::default(); L],
pending_ins: Mutex::new(Cell::new(0u8)),
dpram_already_used: 0,
})
} else {
panic!("Endpoint table cannot be with length <= 1")
}
UsbBusAllocator::new(Self {
pll: Mutex::new(pll),
usb: Mutex::new(usb),
ep_table: [USBEndpoint::default(); L],
dpram_already_used: 0,
})
}
#[inline(always)]
@@ -140,6 +132,10 @@ impl<const L: usize> UsbDevice<L> {
Ok(())
}
pub fn get_ep_table(&self) -> &[USBEndpoint] {
&self.ep_table
}
pub(crate) fn configure_endpoint(
&self,
cs: CriticalSection<'_>,