Add a control_buf to UsbDevice

This commit is contained in:
alexmoon
2022-03-29 15:09:24 -04:00
committed by Dario Nieuwenhuis
parent c53bb7394a
commit 13370c28db
5 changed files with 33 additions and 14 deletions

View File

@ -66,7 +66,6 @@ pub struct CdcAcmClass<'d, D: Driver<'d>> {
struct Control<'a> {
shared: &'a ControlShared,
buf: [u8; 7],
}
/// Shared data between Control and CdcAcmClass
@ -97,7 +96,7 @@ impl<'a> Control<'a> {
}
}
impl<'a> ControlHandler for Control<'a> {
impl<'d> ControlHandler for Control<'d> {
fn reset(&mut self) {
let shared = self.shared();
shared.line_coding.lock(|x| x.set(LineCoding::default()));
@ -139,17 +138,18 @@ impl<'a> ControlHandler for Control<'a> {
}
}
fn control_in(&mut self, req: Request) -> InResponse<'_> {
fn control_in<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> {
match req.request {
// REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below.
REQ_GET_LINE_CODING if req.length == 7 => {
info!("Sending line coding");
let coding = self.shared().line_coding.lock(|x| x.get());
self.buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes());
self.buf[4] = coding.stop_bits as u8;
self.buf[5] = coding.parity_type as u8;
self.buf[6] = coding.data_bits;
InResponse::Accepted(&self.buf)
assert!(buf.len() >= 7);
buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes());
buf[4] = coding.stop_bits as u8;
buf[5] = coding.parity_type as u8;
buf[6] = coding.data_bits;
InResponse::Accepted(&buf[0..7])
}
_ => InResponse::Rejected,
}
@ -166,11 +166,12 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
) -> Self {
let control = state.control.write(Control {
shared: &state.shared,
buf: [0; 7],
});
let control_shared = &state.shared;
assert!(builder.control_buf_len() >= 7);
let comm_if = builder.alloc_interface_with_handler(control);
let comm_ep = builder.alloc_interrupt_endpoint_in(8, 255);
let data_if = builder.alloc_interface();

View File

@ -47,6 +47,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 7];
let mut state = State::new();
@ -56,6 +57,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut control_buf,
);
// Create classes on the builder.