feat(alloc_ep): refactor + add comments
This commit is contained in:
parent
8e3517eafe
commit
ecb5e513d1
|
@ -146,41 +146,56 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
|||
ep_type: EndpointType,
|
||||
max_packet_size: u16,
|
||||
_interval: u8,
|
||||
) -> Result<EndpointAddress> {
|
||||
) -> UsbResult<EndpointAddress> {
|
||||
// Handle first endpoint. //
|
||||
if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
|
||||
return Ok(ep_addr.unwrap());
|
||||
}
|
||||
|
||||
let address = match ep_addr {
|
||||
// If current endpoint doesn't allocated, assign ep_addr to variable. //
|
||||
Some(ep_addr) if !self.ep_table[ep_addr.index()].is_allocated => ep_addr,
|
||||
Some(_) => unreachable!(),
|
||||
None => {
|
||||
|
||||
// If ep_aadr not provided, or current endpoint is allocated, try to find next free endpoint, otherwise return UsbError. //
|
||||
None | Some(_) => {
|
||||
let endpoint = self
|
||||
.ep_table
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip(1)
|
||||
.find(|(i, &ep)| !ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*i])
|
||||
.find(|(i, &ep)| {
|
||||
!ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*i]
|
||||
})
|
||||
.ok_or(UsbError::EndpointMemoryOverflow)?;
|
||||
|
||||
EndpointAddress::from_parts(endpoint.0, ep_dir)
|
||||
}
|
||||
};
|
||||
|
||||
// Select endpoint info by address index. //
|
||||
|
||||
let target_endpoint = &mut self.ep_table[address.index()];
|
||||
|
||||
// Endpoint allocation marker. //
|
||||
|
||||
if DPRAM_SIZE - self.dpram_already_used <= max_packet_size || max_packet_size >= 512 {
|
||||
Err(UsbError::EndpointMemoryOverflow)
|
||||
} else {
|
||||
|
||||
// Get power of two number of endpoint size. //
|
||||
|
||||
let max_packet_size = max(8, max_packet_size.next_power_of_two());
|
||||
|
||||
// Set endpoint parameters. //
|
||||
|
||||
target_endpoint.set_size(max_packet_size);
|
||||
target_endpoint.set_dir(ep_dir);
|
||||
target_endpoint.set_type(ep_type);
|
||||
|
||||
target_endpoint.is_allocated = true;
|
||||
|
||||
self.dpram_already_used = max_packet_size;
|
||||
// Add used dpram memory. //
|
||||
|
||||
self.dpram_already_used += max_packet_size;
|
||||
|
||||
Ok(address)
|
||||
}
|
||||
|
@ -231,8 +246,10 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
|||
fn force_reset(&self) -> usb_device::Result<()> {
|
||||
free(|cs| {
|
||||
let usb = self.usb.borrow(cs);
|
||||
|
||||
usb.usbcon.modify(|_, w| w.usbe().clear_bit());
|
||||
usb.usbcon.modify(|_, w| w.usbe().set_bit());
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue