usb: remove address arg from endpoint allocation.

This commit is contained in:
Dario Nieuwenhuis 2022-05-09 02:07:48 +02:00
parent e9ab960ebf
commit 2e104170de
7 changed files with 32 additions and 53 deletions

View File

@ -143,38 +143,32 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> {
fn alloc_endpoint_in(
&mut self,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
packet_size: u16,
interval: u8,
) -> Result<Self::EndpointIn, driver::EndpointAllocError> {
let index = self
.alloc_in
.allocate(ep_addr, ep_type, max_packet_size, interval)?;
let index = self.alloc_in.allocate(ep_type, packet_size, interval)?;
let ep_addr = EndpointAddress::from_parts(index, UsbDirection::In);
Ok(Endpoint::new(EndpointInfo {
addr: ep_addr,
ep_type,
max_packet_size,
max_packet_size: packet_size,
interval,
}))
}
fn alloc_endpoint_out(
&mut self,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
packet_size: u16,
interval: u8,
) -> Result<Self::EndpointOut, driver::EndpointAllocError> {
let index = self
.alloc_out
.allocate(ep_addr, ep_type, max_packet_size, interval)?;
let index = self.alloc_out.allocate(ep_type, packet_size, interval)?;
let ep_addr = EndpointAddress::from_parts(index, UsbDirection::Out);
Ok(Endpoint::new(EndpointInfo {
addr: ep_addr,
ep_type,
max_packet_size,
max_packet_size: packet_size,
interval,
}))
}
@ -183,8 +177,10 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> {
&mut self,
max_packet_size: u16,
) -> Result<Self::ControlPipe, driver::EndpointAllocError> {
self.alloc_endpoint_out(Some(0x00.into()), EndpointType::Control, max_packet_size, 0)?;
self.alloc_endpoint_in(Some(0x80.into()), EndpointType::Control, max_packet_size, 0)?;
self.alloc_in.used |= 0x01;
self.alloc_in.lens[0] = max_packet_size as u8;
self.alloc_out.used |= 0x01;
self.alloc_out.lens[0] = max_packet_size as u8;
Ok(ControlPipe {
_phantom: PhantomData,
max_packet_size,
@ -681,8 +677,7 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
.await;
// Reset shorts
regs.shorts
.modify(|_, w| w.ep0datadone_ep0status().clear_bit());
regs.shorts.write(|w| w);
regs.events_ep0setup.reset();
let mut buf = [0; 8];
@ -746,7 +741,7 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
}
regs.shorts
.modify(|_, w| w.ep0datadone_ep0status().bit(last_packet));
.write(|w| w.ep0datadone_ep0status().bit(last_packet));
regs.intenset.write(|w| {
w.usbreset().set();
@ -810,7 +805,6 @@ impl Allocator {
fn allocate(
&mut self,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
_interval: u8,
@ -828,27 +822,16 @@ impl Allocator {
// Endpoint directions are allocated individually.
let alloc_index = if let Some(ep_addr) = ep_addr {
match (ep_addr.index(), ep_type) {
(0, EndpointType::Control) => {}
(8, EndpointType::Isochronous) => {}
(n, EndpointType::Bulk) | (n, EndpointType::Interrupt) if n >= 1 && n <= 7 => {}
_ => return Err(driver::EndpointAllocError),
}
ep_addr.index()
} else {
match ep_type {
EndpointType::Isochronous => 8,
EndpointType::Control => 0,
EndpointType::Interrupt | EndpointType::Bulk => {
// Find rightmost zero bit in 1..=7
let ones = (self.used >> 1).trailing_ones() as usize;
if ones >= 7 {
return Err(driver::EndpointAllocError);
}
ones + 1
let alloc_index = match ep_type {
EndpointType::Isochronous => 8,
EndpointType::Control => return Err(driver::EndpointAllocError),
EndpointType::Interrupt | EndpointType::Bulk => {
// Find rightmost zero bit in 1..=7
let ones = (self.used >> 1).trailing_ones() as usize;
if ones >= 7 {
return Err(driver::EndpointAllocError);
}
ones + 1
}
};

View File

@ -372,7 +372,6 @@ impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
fn endpoint_in(
&mut self,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
@ -380,7 +379,7 @@ impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
let ep = self
.builder
.driver
.alloc_endpoint_in(ep_addr, ep_type, max_packet_size, interval)
.alloc_endpoint_in(ep_type, max_packet_size, interval)
.expect("alloc_endpoint_in failed");
self.builder.config_descriptor.endpoint(ep.info());
@ -390,7 +389,6 @@ impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
fn endpoint_out(
&mut self,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
@ -398,7 +396,7 @@ impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
let ep = self
.builder
.driver
.alloc_endpoint_out(ep_addr, ep_type, max_packet_size, interval)
.alloc_endpoint_out(ep_type, max_packet_size, interval)
.expect("alloc_endpoint_out failed");
self.builder.config_descriptor.endpoint(ep.info());
@ -411,7 +409,7 @@ impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
/// Descriptors are written in the order builder functions are called. Note that some
/// classes care about the order.
pub fn endpoint_bulk_in(&mut self, max_packet_size: u16) -> D::EndpointIn {
self.endpoint_in(None, EndpointType::Bulk, max_packet_size, 0)
self.endpoint_in(EndpointType::Bulk, max_packet_size, 0)
}
/// Allocate a BULK OUT endpoint and write its descriptor.
@ -419,7 +417,7 @@ impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
/// Descriptors are written in the order builder functions are called. Note that some
/// classes care about the order.
pub fn endpoint_bulk_out(&mut self, max_packet_size: u16) -> D::EndpointOut {
self.endpoint_out(None, EndpointType::Bulk, max_packet_size, 0)
self.endpoint_out(EndpointType::Bulk, max_packet_size, 0)
}
/// Allocate a INTERRUPT IN endpoint and write its descriptor.
@ -427,11 +425,11 @@ impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
/// Descriptors are written in the order builder functions are called. Note that some
/// classes care about the order.
pub fn endpoint_interrupt_in(&mut self, max_packet_size: u16, interval: u8) -> D::EndpointIn {
self.endpoint_in(None, EndpointType::Interrupt, max_packet_size, interval)
self.endpoint_in(EndpointType::Interrupt, max_packet_size, interval)
}
/// Allocate a INTERRUPT OUT endpoint and write its descriptor.
pub fn endpoint_interrupt_out(&mut self, max_packet_size: u16, interval: u8) -> D::EndpointOut {
self.endpoint_out(None, EndpointType::Interrupt, max_packet_size, interval)
self.endpoint_out(EndpointType::Interrupt, max_packet_size, interval)
}
}

View File

@ -25,7 +25,6 @@ pub trait Driver<'a> {
/// * `interval` - Polling interval parameter for interrupt endpoints.
fn alloc_endpoint_out(
&mut self,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
@ -33,7 +32,6 @@ pub trait Driver<'a> {
fn alloc_endpoint_in(
&mut self,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,

View File

@ -71,7 +71,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 16];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let device_state_handler = MyDeviceStateHandler::new();

View File

@ -50,7 +50,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 16];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut state = State::new();

View File

@ -48,7 +48,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 7];
let mut control_buf = [0; 64];
let mut state = State::new();

View File

@ -64,7 +64,7 @@ async fn main(spawner: Spawner, p: Peripherals) {
device_descriptor: [u8; 256],
config_descriptor: [u8; 256],
bos_descriptor: [u8; 256],
control_buf: [u8; 7],
control_buf: [u8; 64],
serial_state: State<'static>,
}
static RESOURCES: Forever<Resources> = Forever::new();
@ -72,7 +72,7 @@ async fn main(spawner: Spawner, p: Peripherals) {
device_descriptor: [0; 256],
config_descriptor: [0; 256],
bos_descriptor: [0; 256],
control_buf: [0; 7],
control_buf: [0; 64],
serial_state: State::new(),
});