usb: merge alloc_control_pipe and into_bus into start.

This prevents calling `alloc_control_pipe` twice at compile time, which was
always an error.
This commit is contained in:
Dario Nieuwenhuis 2022-05-10 16:53:42 +02:00
parent 02ae1138e1
commit 6af5f8eb2d
3 changed files with 27 additions and 49 deletions

View File

@ -147,7 +147,7 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> {
packet_size: u16,
interval: u8,
) -> Result<Self::EndpointIn, driver::EndpointAllocError> {
let index = self.alloc_in.allocate(ep_type, packet_size, interval)?;
let index = self.alloc_in.allocate(ep_type)?;
let ep_addr = EndpointAddress::from_parts(index, UsbDirection::In);
Ok(Endpoint::new(EndpointInfo {
addr: ep_addr,
@ -163,7 +163,7 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> {
packet_size: u16,
interval: u8,
) -> Result<Self::EndpointOut, driver::EndpointAllocError> {
let index = self.alloc_out.allocate(ep_type, packet_size, interval)?;
let index = self.alloc_out.allocate(ep_type)?;
let ep_addr = EndpointAddress::from_parts(index, UsbDirection::Out);
Ok(Endpoint::new(EndpointInfo {
addr: ep_addr,
@ -173,24 +173,16 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> {
}))
}
fn alloc_control_pipe(
&mut self,
max_packet_size: u16,
) -> Result<Self::ControlPipe, driver::EndpointAllocError> {
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,
})
}
fn into_bus(self) -> Self::Bus {
fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe) {
(
Bus {
phantom: PhantomData,
}
},
ControlPipe {
_phantom: PhantomData,
max_packet_size: control_max_packet_size,
},
)
}
}
@ -791,24 +783,14 @@ fn dma_end() {
struct Allocator {
used: u16,
// Buffers can be up to 64 Bytes since this is a Full-Speed implementation.
lens: [u8; 9],
}
impl Allocator {
fn new() -> Self {
Self {
used: 0,
lens: [0; 9],
}
Self { used: 0 }
}
fn allocate(
&mut self,
ep_type: EndpointType,
max_packet_size: u16,
_interval: u8,
) -> Result<usize, driver::EndpointAllocError> {
fn allocate(&mut self, ep_type: EndpointType) -> Result<usize, driver::EndpointAllocError> {
// Endpoint addresses are fixed in hardware:
// - 0x80 / 0x00 - Control EP0
// - 0x81 / 0x01 - Bulk/Interrupt EP1
@ -840,7 +822,6 @@ impl Allocator {
}
self.used |= 1 << alloc_index;
self.lens[alloc_index] = max_packet_size as u8;
Ok(alloc_index)
}

View File

@ -14,7 +14,7 @@ pub trait Driver<'a> {
/// Allocates an endpoint and specified endpoint parameters. This method is called by the device
/// and class implementations to allocate endpoints, and can only be called before
/// [`enable`](UsbBus::enable) is called.
/// [`start`](UsbBus::start) is called.
///
/// # Arguments
///
@ -37,14 +37,15 @@ pub trait Driver<'a> {
interval: u8,
) -> Result<Self::EndpointIn, EndpointAllocError>;
fn alloc_control_pipe(
&mut self,
max_packet_size: u16,
) -> Result<Self::ControlPipe, EndpointAllocError>;
/// Enables and initializes the USB peripheral. Soon after enabling the device will be reset, so
/// there is no need to perform a USB reset in this method.
fn into_bus(self) -> Self::Bus;
/// Start operation of the USB device.
///
/// This returns the `Bus` and `ControlPipe` instances that are used to operate
/// the USB device. Additionally, this makes all the previously allocated endpoints
/// start operating.
///
/// This consumes the `Driver` instance, so it's no longer possible to allocate more
/// endpoints.
fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe);
/// Indicates that `set_device_address` must be called before accepting the corresponding
/// control transfer, not after.

View File

@ -126,7 +126,7 @@ struct Inner<'d, D: Driver<'d>> {
impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
pub(crate) fn build(
mut driver: D,
driver: D,
config: Config<'d>,
handler: Option<&'d dyn DeviceStateHandler>,
device_descriptor: &'d [u8],
@ -135,13 +135,9 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
interfaces: Vec<Interface<'d>, MAX_INTERFACE_COUNT>,
control_buf: &'d mut [u8],
) -> UsbDevice<'d, D> {
let control = driver
.alloc_control_pipe(config.max_packet_size_0 as u16)
.expect("failed to alloc control endpoint");
// Enable the USB bus.
// Start the USB bus.
// This prevent further allocation by consuming the driver.
let bus = driver.into_bus();
let (bus, control) = driver.start(config.max_packet_size_0 as u16);
Self {
control_buf,