Compare commits

..

5 Commits
test ... main

2 changed files with 13 additions and 8 deletions

View File

@ -46,8 +46,8 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
.find(|(index, ep)| { .find(|(index, ep)| {
!ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index] !ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
}) })
.ok_or(UsbError::EndpointOverflow)? .map(|(index, _)| index)
.0; .ok_or(UsbError::EndpointOverflow)?;
EndpointAddress::from_parts(index, ep_dir) EndpointAddress::from_parts(index, ep_dir)
} }
@ -190,6 +190,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_in_complete, mut ep_setup) = (0u16, 0u16, 0u16); 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() { for (ep_index, _ep) in self.allocated_endpoints() {
if self.select_endpoint(cs, ep_index).is_err() { if self.select_endpoint(cs, ep_index).is_err() {
@ -204,8 +205,9 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
ep_setup |= 1 << ep_index; ep_setup |= 1 << ep_index;
} }
if ueintx.txini().bit_is_set() { if pending_ins.get() & (1 << ep_index) != 0 && ueintx.txini().bit_is_set() {
ep_in_complete |= 1 << ep_index; ep_in_complete |= 1 << ep_index;
pending_ins.set(pending_ins.get() & !(1 << ep_index));
} }
} }
} }
@ -450,6 +452,9 @@ 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()) Ok(buf.len())
}) })
} }

View File

@ -1,3 +1,5 @@
use core::cell::Cell;
use avr_device::{ use avr_device::{
atmega32u4::{PLL, USB_DEVICE}, atmega32u4::{PLL, USB_DEVICE},
interrupt::{CriticalSection, Mutex}, interrupt::{CriticalSection, Mutex},
@ -6,7 +8,7 @@ use usb_device::{bus::UsbBusAllocator, endpoint::EndpointType, UsbDirection, Usb
#[allow(unused)] #[allow(unused)]
#[derive(Default, Copy, Clone)] #[derive(Default, Copy, Clone)]
pub struct USBEndpoint { pub(crate) struct USBEndpoint {
pub(crate) is_allocated: bool, pub(crate) is_allocated: bool,
pub(crate) size: u8, pub(crate) size: u8,
pub(crate) ep_type: u8, pub(crate) ep_type: u8,
@ -70,6 +72,7 @@ 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],
pub(crate) pending_ins: Mutex<Cell<u8>>,
pub(crate) dpram_already_used: u16, pub(crate) dpram_already_used: u16,
} }
@ -85,6 +88,7 @@ impl<const L: usize> UsbDevice<L> {
pll: Mutex::new(pll), pll: Mutex::new(pll),
usb: Mutex::new(usb), usb: Mutex::new(usb),
ep_table: [USBEndpoint::default(); L], ep_table: [USBEndpoint::default(); L],
pending_ins: Mutex::new(Cell::new(0u8)),
dpram_already_used: 0, dpram_already_used: 0,
}) })
} }
@ -132,10 +136,6 @@ impl<const L: usize> UsbDevice<L> {
Ok(()) Ok(())
} }
pub fn get_ep_table(&self) -> &[USBEndpoint] {
&self.ep_table
}
pub(crate) fn configure_endpoint( pub(crate) fn configure_endpoint(
&self, &self,
cs: CriticalSection<'_>, cs: CriticalSection<'_>,