diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-04-23 01:29:19 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-04-23 01:29:19 +0200 |
| commit | 7c6a88f3dd59b0780a9f1ac7b5c3993ddfe0a265 (patch) | |
| tree | de47ccf275b60a47baeb981d73f775f248c4f6cc | |
| parent | 092c2b7dfea146681cea01fd4e4105c21c62b61f (diff) | |
usb: set the interface handler in InterfaceBuilder.
| -rw-r--r-- | embassy-usb-hid/src/lib.rs | 5 | ||||
| -rw-r--r-- | embassy-usb-serial/src/lib.rs | 5 | ||||
| -rw-r--r-- | embassy-usb/src/builder.rs | 11 |
3 files changed, 12 insertions, 9 deletions
diff --git a/embassy-usb-hid/src/lib.rs b/embassy-usb-hid/src/lib.rs index b9ba4f1e7..a75cb8c38 100644 --- a/embassy-usb-hid/src/lib.rs +++ b/embassy-usb-hid/src/lib.rs | |||
| @@ -112,7 +112,8 @@ fn build<'d, D: Driver<'d>>( | |||
| 112 | let len = config.report_descriptor.len(); | 112 | let len = config.report_descriptor.len(); |
| 113 | 113 | ||
| 114 | let mut func = builder.function(USB_CLASS_HID, USB_SUBCLASS_NONE, USB_PROTOCOL_NONE); | 114 | let mut func = builder.function(USB_CLASS_HID, USB_SUBCLASS_NONE, USB_PROTOCOL_NONE); |
| 115 | let mut iface = func.interface(Some(control)); | 115 | let mut iface = func.interface(); |
| 116 | iface.handler(control); | ||
| 116 | let mut alt = iface.alt_setting(USB_CLASS_HID, USB_SUBCLASS_NONE, USB_PROTOCOL_NONE); | 117 | let mut alt = iface.alt_setting(USB_CLASS_HID, USB_SUBCLASS_NONE, USB_PROTOCOL_NONE); |
| 117 | 118 | ||
| 118 | // HID descriptor | 119 | // HID descriptor |
| @@ -438,7 +439,7 @@ impl<'d> ControlHandler for Control<'d> { | |||
| 438 | self.out_report_offset.store(0, Ordering::Release); | 439 | self.out_report_offset.store(0, Ordering::Release); |
| 439 | } | 440 | } |
| 440 | 441 | ||
| 441 | fn get_descriptor<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> { | 442 | fn get_descriptor<'a>(&'a mut self, req: Request, _buf: &'a mut [u8]) -> InResponse<'a> { |
| 442 | match (req.value >> 8) as u8 { | 443 | match (req.value >> 8) as u8 { |
| 443 | HID_DESC_DESCTYPE_HID_REPORT => InResponse::Accepted(self.report_descriptor), | 444 | HID_DESC_DESCTYPE_HID_REPORT => InResponse::Accepted(self.report_descriptor), |
| 444 | HID_DESC_DESCTYPE_HID => InResponse::Accepted(&self.hid_descriptor), | 445 | HID_DESC_DESCTYPE_HID => InResponse::Accepted(&self.hid_descriptor), |
diff --git a/embassy-usb-serial/src/lib.rs b/embassy-usb-serial/src/lib.rs index 4bddc31af..4587bf713 100644 --- a/embassy-usb-serial/src/lib.rs +++ b/embassy-usb-serial/src/lib.rs | |||
| @@ -178,7 +178,8 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { | |||
| 178 | let mut func = builder.function(USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE); | 178 | let mut func = builder.function(USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE); |
| 179 | 179 | ||
| 180 | // Control interface | 180 | // Control interface |
| 181 | let mut iface = func.interface(Some(control)); | 181 | let mut iface = func.interface(); |
| 182 | iface.handler(control); | ||
| 182 | let comm_if = iface.interface_number(); | 183 | let comm_if = iface.interface_number(); |
| 183 | let data_if = u8::from(comm_if) + 1; | 184 | let data_if = u8::from(comm_if) + 1; |
| 184 | let mut alt = iface.alt_setting(USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE); | 185 | let mut alt = iface.alt_setting(USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE); |
| @@ -218,7 +219,7 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { | |||
| 218 | let comm_ep = alt.endpoint_interrupt_in(8, 255); | 219 | let comm_ep = alt.endpoint_interrupt_in(8, 255); |
| 219 | 220 | ||
| 220 | // Data interface | 221 | // Data interface |
| 221 | let mut iface = func.interface(None); | 222 | let mut iface = func.interface(); |
| 222 | let data_if = iface.interface_number(); | 223 | let data_if = iface.interface_number(); |
| 223 | let mut alt = iface.alt_setting(USB_CLASS_CDC_DATA, 0x00, CDC_PROTOCOL_NONE); | 224 | let mut alt = iface.alt_setting(USB_CLASS_CDC_DATA, 0x00, CDC_PROTOCOL_NONE); |
| 224 | let read_ep = alt.endpoint_bulk_out(max_packet_size); | 225 | let read_ep = alt.endpoint_bulk_out(max_packet_size); |
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs index c0aea9838..07b6c79d1 100644 --- a/embassy-usb/src/builder.rs +++ b/embassy-usb/src/builder.rs | |||
| @@ -267,17 +267,14 @@ impl<'a, 'd, D: Driver<'d>> FunctionBuilder<'a, 'd, D> { | |||
| 267 | /// Add an interface to the function. | 267 | /// Add an interface to the function. |
| 268 | /// | 268 | /// |
| 269 | /// Interface numbers are guaranteed to be allocated consecutively, starting from 0. | 269 | /// Interface numbers are guaranteed to be allocated consecutively, starting from 0. |
| 270 | pub fn interface( | 270 | pub fn interface(&mut self) -> InterfaceBuilder<'_, 'd, D> { |
| 271 | &mut self, | ||
| 272 | handler: Option<&'d mut dyn ControlHandler>, | ||
| 273 | ) -> InterfaceBuilder<'_, 'd, D> { | ||
| 274 | if let Some(i) = self.iface_count_index { | 271 | if let Some(i) = self.iface_count_index { |
| 275 | self.builder.config_descriptor.buf[i] += 1; | 272 | self.builder.config_descriptor.buf[i] += 1; |
| 276 | } | 273 | } |
| 277 | 274 | ||
| 278 | let number = self.builder.interfaces.len() as _; | 275 | let number = self.builder.interfaces.len() as _; |
| 279 | let iface = Interface { | 276 | let iface = Interface { |
| 280 | handler, | 277 | handler: None, |
| 281 | current_alt_setting: 0, | 278 | current_alt_setting: 0, |
| 282 | num_alt_settings: 0, | 279 | num_alt_settings: 0, |
| 283 | }; | 280 | }; |
| @@ -307,6 +304,10 @@ impl<'a, 'd, D: Driver<'d>> InterfaceBuilder<'a, 'd, D> { | |||
| 307 | self.interface_number | 304 | self.interface_number |
| 308 | } | 305 | } |
| 309 | 306 | ||
| 307 | pub fn handler(&mut self, handler: &'d mut dyn ControlHandler) { | ||
| 308 | self.builder.interfaces[self.interface_number.0 as usize].handler = Some(handler); | ||
| 309 | } | ||
| 310 | |||
| 310 | /// Add an alternate setting to the interface and write its descriptor. | 311 | /// Add an alternate setting to the interface and write its descriptor. |
| 311 | /// | 312 | /// |
| 312 | /// Alternate setting numbers are guaranteed to be allocated consecutively, starting from 0. | 313 | /// Alternate setting numbers are guaranteed to be allocated consecutively, starting from 0. |
