aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-05-10 16:53:42 +0200
committerDario Nieuwenhuis <[email protected]>2022-05-10 17:30:07 +0200
commit6af5f8eb2da6ba9e1eabb67871c3483ba1579dd2 (patch)
tree5f694b339d0c34f569069243dac861bcb96a32a0
parent02ae1138e1b67a18a0fea4f1871751ee1ad858c0 (diff)
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.
-rw-r--r--embassy-nrf/src/usb.rs47
-rw-r--r--embassy-usb/src/driver.rs19
-rw-r--r--embassy-usb/src/lib.rs10
3 files changed, 27 insertions, 49 deletions
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> {
147 packet_size: u16, 147 packet_size: u16,
148 interval: u8, 148 interval: u8,
149 ) -> Result<Self::EndpointIn, driver::EndpointAllocError> { 149 ) -> Result<Self::EndpointIn, driver::EndpointAllocError> {
150 let index = self.alloc_in.allocate(ep_type, packet_size, interval)?; 150 let index = self.alloc_in.allocate(ep_type)?;
151 let ep_addr = EndpointAddress::from_parts(index, UsbDirection::In); 151 let ep_addr = EndpointAddress::from_parts(index, UsbDirection::In);
152 Ok(Endpoint::new(EndpointInfo { 152 Ok(Endpoint::new(EndpointInfo {
153 addr: ep_addr, 153 addr: ep_addr,
@@ -163,7 +163,7 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> {
163 packet_size: u16, 163 packet_size: u16,
164 interval: u8, 164 interval: u8,
165 ) -> Result<Self::EndpointOut, driver::EndpointAllocError> { 165 ) -> Result<Self::EndpointOut, driver::EndpointAllocError> {
166 let index = self.alloc_out.allocate(ep_type, packet_size, interval)?; 166 let index = self.alloc_out.allocate(ep_type)?;
167 let ep_addr = EndpointAddress::from_parts(index, UsbDirection::Out); 167 let ep_addr = EndpointAddress::from_parts(index, UsbDirection::Out);
168 Ok(Endpoint::new(EndpointInfo { 168 Ok(Endpoint::new(EndpointInfo {
169 addr: ep_addr, 169 addr: ep_addr,
@@ -173,24 +173,16 @@ impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> {
173 })) 173 }))
174 } 174 }
175 175
176 fn alloc_control_pipe( 176 fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe) {
177 &mut self, 177 (
178 max_packet_size: u16, 178 Bus {
179 ) -> Result<Self::ControlPipe, driver::EndpointAllocError> { 179 phantom: PhantomData,
180 self.alloc_in.used |= 0x01; 180 },
181 self.alloc_in.lens[0] = max_packet_size as u8; 181 ControlPipe {
182 self.alloc_out.used |= 0x01; 182 _phantom: PhantomData,
183 self.alloc_out.lens[0] = max_packet_size as u8; 183 max_packet_size: control_max_packet_size,
184 Ok(ControlPipe { 184 },
185 _phantom: PhantomData, 185 )
186 max_packet_size,
187 })
188 }
189
190 fn into_bus(self) -> Self::Bus {
191 Bus {
192 phantom: PhantomData,
193 }
194 } 186 }
195} 187}
196 188
@@ -791,24 +783,14 @@ fn dma_end() {
791 783
792struct Allocator { 784struct Allocator {
793 used: u16, 785 used: u16,
794 // Buffers can be up to 64 Bytes since this is a Full-Speed implementation.
795 lens: [u8; 9],
796} 786}
797 787
798impl Allocator { 788impl Allocator {
799 fn new() -> Self { 789 fn new() -> Self {
800 Self { 790 Self { used: 0 }
801 used: 0,
802 lens: [0; 9],
803 }
804 } 791 }
805 792
806 fn allocate( 793 fn allocate(&mut self, ep_type: EndpointType) -> Result<usize, driver::EndpointAllocError> {
807 &mut self,
808 ep_type: EndpointType,
809 max_packet_size: u16,
810 _interval: u8,
811 ) -> Result<usize, driver::EndpointAllocError> {
812 // Endpoint addresses are fixed in hardware: 794 // Endpoint addresses are fixed in hardware:
813 // - 0x80 / 0x00 - Control EP0 795 // - 0x80 / 0x00 - Control EP0
814 // - 0x81 / 0x01 - Bulk/Interrupt EP1 796 // - 0x81 / 0x01 - Bulk/Interrupt EP1
@@ -840,7 +822,6 @@ impl Allocator {
840 } 822 }
841 823
842 self.used |= 1 << alloc_index; 824 self.used |= 1 << alloc_index;
843 self.lens[alloc_index] = max_packet_size as u8;
844 825
845 Ok(alloc_index) 826 Ok(alloc_index)
846 } 827 }
diff --git a/embassy-usb/src/driver.rs b/embassy-usb/src/driver.rs
index a782b377c..57f2b0656 100644
--- a/embassy-usb/src/driver.rs
+++ b/embassy-usb/src/driver.rs
@@ -14,7 +14,7 @@ pub trait Driver<'a> {
14 14
15 /// Allocates an endpoint and specified endpoint parameters. This method is called by the device 15 /// Allocates an endpoint and specified endpoint parameters. This method is called by the device
16 /// and class implementations to allocate endpoints, and can only be called before 16 /// and class implementations to allocate endpoints, and can only be called before
17 /// [`enable`](UsbBus::enable) is called. 17 /// [`start`](UsbBus::start) is called.
18 /// 18 ///
19 /// # Arguments 19 /// # Arguments
20 /// 20 ///
@@ -37,14 +37,15 @@ pub trait Driver<'a> {
37 interval: u8, 37 interval: u8,
38 ) -> Result<Self::EndpointIn, EndpointAllocError>; 38 ) -> Result<Self::EndpointIn, EndpointAllocError>;
39 39
40 fn alloc_control_pipe( 40 /// Start operation of the USB device.
41 &mut self, 41 ///
42 max_packet_size: u16, 42 /// This returns the `Bus` and `ControlPipe` instances that are used to operate
43 ) -> Result<Self::ControlPipe, EndpointAllocError>; 43 /// the USB device. Additionally, this makes all the previously allocated endpoints
44 44 /// start operating.
45 /// Enables and initializes the USB peripheral. Soon after enabling the device will be reset, so 45 ///
46 /// there is no need to perform a USB reset in this method. 46 /// This consumes the `Driver` instance, so it's no longer possible to allocate more
47 fn into_bus(self) -> Self::Bus; 47 /// endpoints.
48 fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe);
48 49
49 /// Indicates that `set_device_address` must be called before accepting the corresponding 50 /// Indicates that `set_device_address` must be called before accepting the corresponding
50 /// control transfer, not after. 51 /// control transfer, not after.
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index 99c0f8237..b135f5eb3 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -126,7 +126,7 @@ struct Inner<'d, D: Driver<'d>> {
126 126
127impl<'d, D: Driver<'d>> UsbDevice<'d, D> { 127impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
128 pub(crate) fn build( 128 pub(crate) fn build(
129 mut driver: D, 129 driver: D,
130 config: Config<'d>, 130 config: Config<'d>,
131 handler: Option<&'d dyn DeviceStateHandler>, 131 handler: Option<&'d dyn DeviceStateHandler>,
132 device_descriptor: &'d [u8], 132 device_descriptor: &'d [u8],
@@ -135,13 +135,9 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
135 interfaces: Vec<Interface<'d>, MAX_INTERFACE_COUNT>, 135 interfaces: Vec<Interface<'d>, MAX_INTERFACE_COUNT>,
136 control_buf: &'d mut [u8], 136 control_buf: &'d mut [u8],
137 ) -> UsbDevice<'d, D> { 137 ) -> UsbDevice<'d, D> {
138 let control = driver 138 // Start the USB bus.
139 .alloc_control_pipe(config.max_packet_size_0 as u16)
140 .expect("failed to alloc control endpoint");
141
142 // Enable the USB bus.
143 // This prevent further allocation by consuming the driver. 139 // This prevent further allocation by consuming the driver.
144 let bus = driver.into_bus(); 140 let (bus, control) = driver.start(config.max_packet_size_0 as u16);
145 141
146 Self { 142 Self {
147 control_buf, 143 control_buf,