aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf
diff options
context:
space:
mode:
authorHenrik Alsér <[email protected]>2022-05-12 15:24:46 +0200
committerGitHub <[email protected]>2022-05-12 15:24:46 +0200
commit0be9184efc8f814a19081c2176b8317bd5217f0f (patch)
tree5b9469de15c0a1cc6e106a9a7455602e3bfaad17 /embassy-nrf
parent1ca5475010a1cae6ebc55a27948ca4320decd5cd (diff)
parent30d4d0e9d78681e16a68ff953c61b96c9863bfc6 (diff)
Merge branch 'embassy-rs:master' into qdec
Diffstat (limited to 'embassy-nrf')
-rw-r--r--embassy-nrf/src/usb.rs94
1 files changed, 29 insertions, 65 deletions
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs
index c032b2cc5..1162946a9 100644
--- a/embassy-nrf/src/usb.rs
+++ b/embassy-nrf/src/usb.rs
@@ -143,58 +143,46 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> {
143 143
144 fn alloc_endpoint_in( 144 fn alloc_endpoint_in(
145 &mut self, 145 &mut self,
146 ep_addr: Option<EndpointAddress>,
147 ep_type: EndpointType, 146 ep_type: EndpointType,
148 max_packet_size: u16, 147 packet_size: u16,
149 interval: u8, 148 interval: u8,
150 ) -> Result<Self::EndpointIn, driver::EndpointAllocError> { 149 ) -> Result<Self::EndpointIn, driver::EndpointAllocError> {
151 let index = self 150 let index = self.alloc_in.allocate(ep_type)?;
152 .alloc_in
153 .allocate(ep_addr, ep_type, max_packet_size, interval)?;
154 let ep_addr = EndpointAddress::from_parts(index, UsbDirection::In); 151 let ep_addr = EndpointAddress::from_parts(index, UsbDirection::In);
155 Ok(Endpoint::new(EndpointInfo { 152 Ok(Endpoint::new(EndpointInfo {
156 addr: ep_addr, 153 addr: ep_addr,
157 ep_type, 154 ep_type,
158 max_packet_size, 155 max_packet_size: packet_size,
159 interval, 156 interval,
160 })) 157 }))
161 } 158 }
162 159
163 fn alloc_endpoint_out( 160 fn alloc_endpoint_out(
164 &mut self, 161 &mut self,
165 ep_addr: Option<EndpointAddress>,
166 ep_type: EndpointType, 162 ep_type: EndpointType,
167 max_packet_size: u16, 163 packet_size: u16,
168 interval: u8, 164 interval: u8,
169 ) -> Result<Self::EndpointOut, driver::EndpointAllocError> { 165 ) -> Result<Self::EndpointOut, driver::EndpointAllocError> {
170 let index = self 166 let index = self.alloc_out.allocate(ep_type)?;
171 .alloc_out
172 .allocate(ep_addr, ep_type, max_packet_size, interval)?;
173 let ep_addr = EndpointAddress::from_parts(index, UsbDirection::Out); 167 let ep_addr = EndpointAddress::from_parts(index, UsbDirection::Out);
174 Ok(Endpoint::new(EndpointInfo { 168 Ok(Endpoint::new(EndpointInfo {
175 addr: ep_addr, 169 addr: ep_addr,
176 ep_type, 170 ep_type,
177 max_packet_size, 171 max_packet_size: packet_size,
178 interval, 172 interval,
179 })) 173 }))
180 } 174 }
181 175
182 fn alloc_control_pipe( 176 fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe) {
183 &mut self, 177 (
184 max_packet_size: u16, 178 Bus {
185 ) -> Result<Self::ControlPipe, driver::EndpointAllocError> { 179 phantom: PhantomData,
186 self.alloc_endpoint_out(Some(0x00.into()), EndpointType::Control, max_packet_size, 0)?; 180 },
187 self.alloc_endpoint_in(Some(0x80.into()), EndpointType::Control, max_packet_size, 0)?; 181 ControlPipe {
188 Ok(ControlPipe { 182 _phantom: PhantomData,
189 _phantom: PhantomData, 183 max_packet_size: control_max_packet_size,
190 max_packet_size, 184 },
191 }) 185 )
192 }
193
194 fn into_bus(self) -> Self::Bus {
195 Bus {
196 phantom: PhantomData,
197 }
198 } 186 }
199} 187}
200 188
@@ -681,8 +669,7 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
681 .await; 669 .await;
682 670
683 // Reset shorts 671 // Reset shorts
684 regs.shorts 672 regs.shorts.write(|w| w);
685 .modify(|_, w| w.ep0datadone_ep0status().clear_bit());
686 regs.events_ep0setup.reset(); 673 regs.events_ep0setup.reset();
687 674
688 let mut buf = [0; 8]; 675 let mut buf = [0; 8];
@@ -746,7 +733,7 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
746 } 733 }
747 734
748 regs.shorts 735 regs.shorts
749 .modify(|_, w| w.ep0datadone_ep0status().bit(last_packet)); 736 .write(|w| w.ep0datadone_ep0status().bit(last_packet));
750 737
751 regs.intenset.write(|w| { 738 regs.intenset.write(|w| {
752 w.usbreset().set(); 739 w.usbreset().set();
@@ -796,25 +783,14 @@ fn dma_end() {
796 783
797struct Allocator { 784struct Allocator {
798 used: u16, 785 used: u16,
799 // Buffers can be up to 64 Bytes since this is a Full-Speed implementation.
800 lens: [u8; 9],
801} 786}
802 787
803impl Allocator { 788impl Allocator {
804 fn new() -> Self { 789 fn new() -> Self {
805 Self { 790 Self { used: 0 }
806 used: 0,
807 lens: [0; 9],
808 }
809 } 791 }
810 792
811 fn allocate( 793 fn allocate(&mut self, ep_type: EndpointType) -> Result<usize, driver::EndpointAllocError> {
812 &mut self,
813 ep_addr: Option<EndpointAddress>,
814 ep_type: EndpointType,
815 max_packet_size: u16,
816 _interval: u8,
817 ) -> Result<usize, driver::EndpointAllocError> {
818 // Endpoint addresses are fixed in hardware: 794 // Endpoint addresses are fixed in hardware:
819 // - 0x80 / 0x00 - Control EP0 795 // - 0x80 / 0x00 - Control EP0
820 // - 0x81 / 0x01 - Bulk/Interrupt EP1 796 // - 0x81 / 0x01 - Bulk/Interrupt EP1
@@ -828,27 +804,16 @@ impl Allocator {
828 804
829 // Endpoint directions are allocated individually. 805 // Endpoint directions are allocated individually.
830 806
831 let alloc_index = if let Some(ep_addr) = ep_addr { 807 let alloc_index = match ep_type {
832 match (ep_addr.index(), ep_type) { 808 EndpointType::Isochronous => 8,
833 (0, EndpointType::Control) => {} 809 EndpointType::Control => return Err(driver::EndpointAllocError),
834 (8, EndpointType::Isochronous) => {} 810 EndpointType::Interrupt | EndpointType::Bulk => {
835 (n, EndpointType::Bulk) | (n, EndpointType::Interrupt) if n >= 1 && n <= 7 => {} 811 // Find rightmost zero bit in 1..=7
836 _ => return Err(driver::EndpointAllocError), 812 let ones = (self.used >> 1).trailing_ones() as usize;
837 } 813 if ones >= 7 {
838 814 return Err(driver::EndpointAllocError);
839 ep_addr.index()
840 } else {
841 match ep_type {
842 EndpointType::Isochronous => 8,
843 EndpointType::Control => 0,
844 EndpointType::Interrupt | EndpointType::Bulk => {
845 // Find rightmost zero bit in 1..=7
846 let ones = (self.used >> 1).trailing_ones() as usize;
847 if ones >= 7 {
848 return Err(driver::EndpointAllocError);
849 }
850 ones + 1
851 } 815 }
816 ones + 1
852 } 817 }
853 }; 818 };
854 819
@@ -857,7 +822,6 @@ impl Allocator {
857 } 822 }
858 823
859 self.used |= 1 << alloc_index; 824 self.used |= 1 << alloc_index;
860 self.lens[alloc_index] = max_packet_size as u8;
861 825
862 Ok(alloc_index) 826 Ok(alloc_index)
863 } 827 }