Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f7c4b48b85 |
97
src/lib.rs
97
src/lib.rs
|
@ -24,60 +24,57 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
|||
_interval: u8,
|
||||
) -> UsbResult<EndpointAddress> {
|
||||
// Handle first endpoint. //
|
||||
free(|_cs| {
|
||||
if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
|
||||
Ok(ep_addr.unwrap())
|
||||
} else {
|
||||
let address = match ep_addr {
|
||||
// If current endpoint doesn't allocated, assign ep_addr to variable. //
|
||||
Some(addr) if !self.ep_table[addr.index()].is_allocated => addr,
|
||||
if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
|
||||
Ok(ep_addr.unwrap())
|
||||
} else {
|
||||
let address = match ep_addr {
|
||||
// If current endpoint doesn't allocated, assign ep_addr to variable. //
|
||||
Some(addr) if !self.ep_table[addr.index()].is_allocated => addr,
|
||||
|
||||
// If ep_aadr not provided, or current endpoint is allocated, try to find next free endpoint, otherwise return UsbError. //
|
||||
_ => {
|
||||
let index = self
|
||||
.ep_table
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip(1)
|
||||
.find(|(index, ep)| {
|
||||
!ep.is_allocated
|
||||
&& max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
|
||||
})
|
||||
.ok_or(UsbError::EndpointOverflow)?
|
||||
.0;
|
||||
// If ep_aadr not provided, or current endpoint is allocated, try to find next free endpoint, otherwise return UsbError. //
|
||||
_ => {
|
||||
let index = self
|
||||
.ep_table
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip(1)
|
||||
.find(|(index, ep)| {
|
||||
!ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
|
||||
})
|
||||
.ok_or(UsbError::EndpointOverflow)?
|
||||
.0;
|
||||
|
||||
EndpointAddress::from_parts(index, ep_dir)
|
||||
}
|
||||
};
|
||||
|
||||
// Select endpoint info by address index. //
|
||||
|
||||
let target_endpoint = &mut self.ep_table[address.index()];
|
||||
|
||||
// Get power of two number of endpoint size. //
|
||||
|
||||
let ep_size = max(8, max_packet_size.next_power_of_two());
|
||||
|
||||
// Endpoint allocation marker. //
|
||||
|
||||
if DPRAM_SIZE - self.dpram_already_used < ep_size {
|
||||
Err(UsbError::EndpointMemoryOverflow)
|
||||
} else {
|
||||
// Set endpoint parameters. //
|
||||
|
||||
target_endpoint.set_dir(ep_dir);
|
||||
target_endpoint.set_type(ep_type);
|
||||
target_endpoint.set_size(ep_size)?;
|
||||
|
||||
// Add used dpram memory. //
|
||||
|
||||
target_endpoint.is_allocated = true;
|
||||
self.dpram_already_used += ep_size;
|
||||
|
||||
Ok(address)
|
||||
EndpointAddress::from_parts(index, ep_dir)
|
||||
}
|
||||
};
|
||||
|
||||
// Select endpoint info by address index. //
|
||||
|
||||
let target_endpoint = &mut self.ep_table[address.index()];
|
||||
|
||||
// Get power of two number of endpoint size. //
|
||||
|
||||
let ep_size = max(8, max_packet_size.next_power_of_two());
|
||||
|
||||
// Endpoint allocation marker. //
|
||||
|
||||
if DPRAM_SIZE - self.dpram_already_used < ep_size {
|
||||
Err(UsbError::EndpointMemoryOverflow)
|
||||
} else {
|
||||
// Set endpoint parameters. //
|
||||
|
||||
target_endpoint.set_dir(ep_dir);
|
||||
target_endpoint.set_type(ep_type);
|
||||
target_endpoint.set_size(ep_size)?;
|
||||
|
||||
// Add used dpram memory. //
|
||||
|
||||
target_endpoint.is_allocated = true;
|
||||
self.dpram_already_used += ep_size;
|
||||
|
||||
Ok(address)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn enable(&mut self) {
|
||||
|
|
Loading…
Reference in New Issue