Compare commits

...

2 Commits

Author SHA1 Message Date
doryan 29e5edde8a feat(check): add check for endpoint table length 2025-05-26 10:24:51 +04:00
doryan 137fcc78e5 refactor: reduce Iterator methods usage 2025-05-26 10:24:19 +04:00
2 changed files with 14 additions and 12 deletions

View File

@ -38,16 +38,14 @@ 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. // // If ep_aadr not provided, or current endpoint is allocated, try to find next free endpoint, otherwise return UsbError. //
_ => { _ => {
let index = self let index = self.ep_table[1..]
.ep_table
.iter() .iter()
.enumerate() .enumerate()
.skip(1)
.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)
} }

View File

@ -84,13 +84,17 @@ pub(crate) const ONE_MS_16_MGHZ: u32 = 16000;
impl<const L: usize> UsbDevice<L> { impl<const L: usize> UsbDevice<L> {
#[inline] #[inline]
pub fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator<Self> { pub fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator<Self> {
UsbBusAllocator::new(Self { if L > 1 {
pll: Mutex::new(pll), UsbBusAllocator::new(Self {
usb: Mutex::new(usb), pll: Mutex::new(pll),
ep_table: [USBEndpoint::default(); L], usb: Mutex::new(usb),
pending_ins: Mutex::new(Cell::new(0u8)), ep_table: [USBEndpoint::default(); L],
dpram_already_used: 0, pending_ins: Mutex::new(Cell::new(0u8)),
}) dpram_already_used: 0,
})
} else {
panic!("Endpoint table cannot be with length <= 1")
}
} }
#[inline(always)] #[inline(always)]