From 2e104170de36295243608fbbebebdc6f52e8f8d0 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 9 May 2022 02:07:48 +0200 Subject: usb: remove address arg from endpoint allocation. --- embassy-nrf/src/usb.rs | 59 ++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) (limited to 'embassy-nrf') diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index c032b2cc5..70393532f 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs @@ -143,38 +143,32 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> { fn alloc_endpoint_in( &mut self, - ep_addr: Option, ep_type: EndpointType, - max_packet_size: u16, + packet_size: u16, interval: u8, ) -> Result { - 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, ep_type: EndpointType, - max_packet_size: u16, + packet_size: u16, interval: u8, ) -> Result { - 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.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, 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 } }; -- cgit From 6af5f8eb2da6ba9e1eabb67871c3483ba1579dd2 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 10 May 2022 16:53:42 +0200 Subject: 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. --- embassy-nrf/src/usb.rs | 47 ++++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) (limited to 'embassy-nrf') diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index 70393532f..1162946a9 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs @@ -147,7 +147,7 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> { packet_size: u16, interval: u8, ) -> Result { - 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 { - 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.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 { - Bus { - phantom: PhantomData, - } + 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 { + fn allocate(&mut self, ep_type: EndpointType) -> Result { // 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) } -- cgit