diff options
| author | alexmoon <[email protected]> | 2022-04-01 10:15:08 -0400 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-04-06 05:38:11 +0200 |
| commit | daf2379fa4d4ab8772982847613a09a750ac7ba8 (patch) | |
| tree | 2bc1a68dd7787ef114b4beb7343aff88c94d0baa | |
| parent | a51de5a39a4bc246d6b3696ac94a200e93d918ea (diff) | |
Make the interupt IN endpoint non-optional
| -rw-r--r-- | embassy-usb-hid/src/lib.rs | 48 |
1 files changed, 10 insertions, 38 deletions
diff --git a/embassy-usb-hid/src/lib.rs b/embassy-usb-hid/src/lib.rs index 0c449fa26..4a7e60320 100644 --- a/embassy-usb-hid/src/lib.rs +++ b/embassy-usb-hid/src/lib.rs | |||
| @@ -98,7 +98,7 @@ impl<'d, D: Driver<'d>, const IN_N: usize, const OUT_N: usize> HidClass<'d, D, I | |||
| 98 | poll_ms: u8, | 98 | poll_ms: u8, |
| 99 | ) -> Self { | 99 | ) -> Self { |
| 100 | let ep_out = Some(builder.alloc_interrupt_endpoint_out(64, poll_ms)); | 100 | let ep_out = Some(builder.alloc_interrupt_endpoint_out(64, poll_ms)); |
| 101 | let ep_in = Some(builder.alloc_interrupt_endpoint_in(64, poll_ms)); | 101 | let ep_in = builder.alloc_interrupt_endpoint_in(64, poll_ms); |
| 102 | Self::new_inner( | 102 | Self::new_inner( |
| 103 | builder, | 103 | builder, |
| 104 | state, | 104 | state, |
| @@ -119,28 +119,7 @@ impl<'d, D: Driver<'d>, const IN_N: usize, const OUT_N: usize> HidClass<'d, D, I | |||
| 119 | poll_ms: u8, | 119 | poll_ms: u8, |
| 120 | ) -> Self { | 120 | ) -> Self { |
| 121 | let ep_out = None; | 121 | let ep_out = None; |
| 122 | let ep_in = Some(builder.alloc_interrupt_endpoint_in(64, poll_ms)); | 122 | let ep_in = builder.alloc_interrupt_endpoint_in(64, poll_ms); |
| 123 | Self::new_inner( | ||
| 124 | builder, | ||
| 125 | state, | ||
| 126 | report_descriptor, | ||
| 127 | request_handler, | ||
| 128 | ep_out, | ||
| 129 | ep_in, | ||
| 130 | ) | ||
| 131 | } | ||
| 132 | |||
| 133 | /// Creates a new HidClass with the provided UsbBus & HID report descriptor. | ||
| 134 | /// See new() for more details. | ||
| 135 | pub fn new_ep_out( | ||
| 136 | builder: &mut UsbDeviceBuilder<'d, D>, | ||
| 137 | state: &'d mut State<'d, IN_N, OUT_N>, | ||
| 138 | report_descriptor: &'static [u8], | ||
| 139 | request_handler: Option<&'d dyn RequestHandler>, | ||
| 140 | poll_ms: u8, | ||
| 141 | ) -> Self { | ||
| 142 | let ep_out = Some(builder.alloc_interrupt_endpoint_out(64, poll_ms)); | ||
| 143 | let ep_in = None; | ||
| 144 | Self::new_inner( | 123 | Self::new_inner( |
| 145 | builder, | 124 | builder, |
| 146 | state, | 125 | state, |
| @@ -157,7 +136,7 @@ impl<'d, D: Driver<'d>, const IN_N: usize, const OUT_N: usize> HidClass<'d, D, I | |||
| 157 | report_descriptor: &'static [u8], | 136 | report_descriptor: &'static [u8], |
| 158 | request_handler: Option<&'d dyn RequestHandler>, | 137 | request_handler: Option<&'d dyn RequestHandler>, |
| 159 | ep_out: Option<D::EndpointOut>, | 138 | ep_out: Option<D::EndpointOut>, |
| 160 | ep_in: Option<D::EndpointIn>, | 139 | ep_in: D::EndpointIn, |
| 161 | ) -> Self { | 140 | ) -> Self { |
| 162 | let control = state.control.write(Control::new( | 141 | let control = state.control.write(Control::new( |
| 163 | report_descriptor, | 142 | report_descriptor, |
| @@ -165,7 +144,7 @@ impl<'d, D: Driver<'d>, const IN_N: usize, const OUT_N: usize> HidClass<'d, D, I | |||
| 165 | request_handler, | 144 | request_handler, |
| 166 | )); | 145 | )); |
| 167 | 146 | ||
| 168 | control.build(builder, ep_out.as_ref(), ep_in.as_ref()); | 147 | control.build(builder, ep_out.as_ref(), &ep_in); |
| 169 | 148 | ||
| 170 | Self { | 149 | Self { |
| 171 | input: ReportWriter { ep_in }, | 150 | input: ReportWriter { ep_in }, |
| @@ -196,7 +175,7 @@ impl<'d, D: Driver<'d>, const IN_N: usize, const OUT_N: usize> HidClass<'d, D, I | |||
| 196 | } | 175 | } |
| 197 | 176 | ||
| 198 | pub struct ReportWriter<'d, D: Driver<'d>, const N: usize> { | 177 | pub struct ReportWriter<'d, D: Driver<'d>, const N: usize> { |
| 199 | ep_in: Option<D::EndpointIn>, | 178 | ep_in: D::EndpointIn, |
| 200 | } | 179 | } |
| 201 | 180 | ||
| 202 | pub struct ReportReader<'d, D: Driver<'d>, const N: usize> { | 181 | pub struct ReportReader<'d, D: Driver<'d>, const N: usize> { |
| @@ -224,19 +203,14 @@ impl<'d, D: Driver<'d>, const N: usize> ReportWriter<'d, D, N> { | |||
| 224 | pub async fn write(&mut self, report: &[u8]) -> Result<(), WriteError> { | 203 | pub async fn write(&mut self, report: &[u8]) -> Result<(), WriteError> { |
| 225 | assert!(report.len() <= N); | 204 | assert!(report.len() <= N); |
| 226 | 205 | ||
| 227 | let ep = self | 206 | let max_packet_size = usize::from(self.ep_in.info().max_packet_size); |
| 228 | .ep_in | ||
| 229 | .as_mut() | ||
| 230 | .expect("An IN endpoint must be allocated to write input reports."); | ||
| 231 | |||
| 232 | let max_packet_size = usize::from(ep.info().max_packet_size); | ||
| 233 | let zlp_needed = report.len() < N && (report.len() % max_packet_size == 0); | 207 | let zlp_needed = report.len() < N && (report.len() % max_packet_size == 0); |
| 234 | for chunk in report.chunks(max_packet_size) { | 208 | for chunk in report.chunks(max_packet_size) { |
| 235 | ep.write(chunk).await?; | 209 | self.ep_in.write(chunk).await?; |
| 236 | } | 210 | } |
| 237 | 211 | ||
| 238 | if zlp_needed { | 212 | if zlp_needed { |
| 239 | ep.write(&[]).await?; | 213 | self.ep_in.write(&[]).await?; |
| 240 | } | 214 | } |
| 241 | 215 | ||
| 242 | Ok(()) | 216 | Ok(()) |
| @@ -363,7 +337,7 @@ impl<'a, const OUT_N: usize> Control<'a, OUT_N> { | |||
| 363 | &'d mut self, | 337 | &'d mut self, |
| 364 | builder: &mut UsbDeviceBuilder<'d, D>, | 338 | builder: &mut UsbDeviceBuilder<'d, D>, |
| 365 | ep_out: Option<&D::EndpointOut>, | 339 | ep_out: Option<&D::EndpointOut>, |
| 366 | ep_in: Option<&D::EndpointIn>, | 340 | ep_in: &D::EndpointIn, |
| 367 | ) { | 341 | ) { |
| 368 | let len = self.report_descriptor.len(); | 342 | let len = self.report_descriptor.len(); |
| 369 | let if_num = builder.alloc_interface_with_handler(self); | 343 | let if_num = builder.alloc_interface_with_handler(self); |
| @@ -394,12 +368,10 @@ impl<'a, const OUT_N: usize> Control<'a, OUT_N> { | |||
| 394 | ], | 368 | ], |
| 395 | ); | 369 | ); |
| 396 | 370 | ||
| 371 | builder.config_descriptor.endpoint(ep_in.info()); | ||
| 397 | if let Some(ep) = ep_out { | 372 | if let Some(ep) = ep_out { |
| 398 | builder.config_descriptor.endpoint(ep.info()); | 373 | builder.config_descriptor.endpoint(ep.info()); |
| 399 | } | 374 | } |
| 400 | if let Some(ep) = ep_in { | ||
| 401 | builder.config_descriptor.endpoint(ep.info()); | ||
| 402 | } | ||
| 403 | } | 375 | } |
| 404 | } | 376 | } |
| 405 | 377 | ||
