diff options
| -rw-r--r-- | embassy-usb/src/control.rs | 7 | ||||
| -rw-r--r-- | embassy-usb/src/lib.rs | 11 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb/cdc_acm.rs | 14 |
3 files changed, 15 insertions, 17 deletions
diff --git a/embassy-usb/src/control.rs b/embassy-usb/src/control.rs index cdae2d977..195b218dc 100644 --- a/embassy-usb/src/control.rs +++ b/embassy-usb/src/control.rs | |||
| @@ -133,8 +133,8 @@ pub enum OutResponse { | |||
| 133 | 133 | ||
| 134 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 134 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 135 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 135 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 136 | pub enum InResponse { | 136 | pub enum InResponse<'a> { |
| 137 | Accepted(usize), | 137 | Accepted(&'a [u8]), |
| 138 | Rejected, | 138 | Rejected, |
| 139 | } | 139 | } |
| 140 | 140 | ||
| @@ -164,8 +164,7 @@ pub trait ControlHandler { | |||
| 164 | /// # Arguments | 164 | /// # Arguments |
| 165 | /// | 165 | /// |
| 166 | /// * `req` - The request from the SETUP packet. | 166 | /// * `req` - The request from the SETUP packet. |
| 167 | /// * `resp` - The buffer for you to write the response. | 167 | fn control_in(&mut self, req: Request) -> InResponse<'_> { |
| 168 | fn control_in(&mut self, req: Request, resp: &mut [u8]) -> InResponse { | ||
| 169 | InResponse::Rejected | 168 | InResponse::Rejected |
| 170 | } | 169 | } |
| 171 | } | 170 | } |
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs index 9ac55db78..5a82f5cae 100644 --- a/embassy-usb/src/lib.rs +++ b/embassy-usb/src/lib.rs | |||
| @@ -284,13 +284,10 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 284 | .find(|(i, _)| req.index == *i as _) | 284 | .find(|(i, _)| req.index == *i as _) |
| 285 | .map(|(_, h)| h); | 285 | .map(|(_, h)| h); |
| 286 | match handler { | 286 | match handler { |
| 287 | Some(handler) => { | 287 | Some(handler) => match handler.control_in(req) { |
| 288 | let mut buf = [0; 128]; | 288 | InResponse::Accepted(data) => self.control.accept_in(data).await, |
| 289 | match handler.control_in(req, &mut buf) { | 289 | InResponse::Rejected => self.control.reject(), |
| 290 | InResponse::Accepted(len) => self.control.accept_in(&buf[..len]).await, | 290 | }, |
| 291 | InResponse::Rejected => self.control.reject(), | ||
| 292 | } | ||
| 293 | } | ||
| 294 | None => self.control.reject(), | 291 | None => self.control.reject(), |
| 295 | } | 292 | } |
| 296 | } | 293 | } |
diff --git a/examples/nrf/src/bin/usb/cdc_acm.rs b/examples/nrf/src/bin/usb/cdc_acm.rs index 4b4925937..2a78324fe 100644 --- a/examples/nrf/src/bin/usb/cdc_acm.rs +++ b/examples/nrf/src/bin/usb/cdc_acm.rs | |||
| @@ -66,6 +66,7 @@ pub struct CdcAcmClass<'d, D: Driver<'d>> { | |||
| 66 | 66 | ||
| 67 | struct Control<'a> { | 67 | struct Control<'a> { |
| 68 | shared: &'a ControlShared, | 68 | shared: &'a ControlShared, |
| 69 | buf: [u8; 7], | ||
| 69 | } | 70 | } |
| 70 | 71 | ||
| 71 | /// Shared data between Control and CdcAcmClass | 72 | /// Shared data between Control and CdcAcmClass |
| @@ -138,17 +139,17 @@ impl<'a> ControlHandler for Control<'a> { | |||
| 138 | } | 139 | } |
| 139 | } | 140 | } |
| 140 | 141 | ||
| 141 | fn control_in(&mut self, req: Request, resp: &mut [u8]) -> InResponse { | 142 | fn control_in(&mut self, req: Request) -> InResponse<'_> { |
| 142 | match req.request { | 143 | match req.request { |
| 143 | // REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below. | 144 | // REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below. |
| 144 | REQ_GET_LINE_CODING if req.length == 7 => { | 145 | REQ_GET_LINE_CODING if req.length == 7 => { |
| 145 | info!("Sending line coding"); | 146 | info!("Sending line coding"); |
| 146 | let coding = self.shared().line_coding.lock(|x| x.get()); | 147 | let coding = self.shared().line_coding.lock(|x| x.get()); |
| 147 | resp[0..4].copy_from_slice(&coding.data_rate.to_le_bytes()); | 148 | self.buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes()); |
| 148 | resp[4] = coding.stop_bits as u8; | 149 | self.buf[4] = coding.stop_bits as u8; |
| 149 | resp[5] = coding.parity_type as u8; | 150 | self.buf[5] = coding.parity_type as u8; |
| 150 | resp[6] = coding.data_bits; | 151 | self.buf[6] = coding.data_bits; |
| 151 | InResponse::Accepted(7) | 152 | InResponse::Accepted(&self.buf) |
| 152 | } | 153 | } |
| 153 | _ => InResponse::Rejected, | 154 | _ => InResponse::Rejected, |
| 154 | } | 155 | } |
| @@ -165,6 +166,7 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { | |||
| 165 | ) -> Self { | 166 | ) -> Self { |
| 166 | let control = state.control.write(Control { | 167 | let control = state.control.write(Control { |
| 167 | shared: &state.shared, | 168 | shared: &state.shared, |
| 169 | buf: [0; 7], | ||
| 168 | }); | 170 | }); |
| 169 | 171 | ||
| 170 | let control_shared = &state.shared; | 172 | let control_shared = &state.shared; |
