mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +00:00
Add a control_buf to UsbDevice
This commit is contained in:
parent
c53bb7394a
commit
13370c28db
@ -120,6 +120,7 @@ impl<'a> Config<'a> {
|
|||||||
pub struct UsbDeviceBuilder<'d, D: Driver<'d>> {
|
pub struct UsbDeviceBuilder<'d, D: Driver<'d>> {
|
||||||
config: Config<'d>,
|
config: Config<'d>,
|
||||||
interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>,
|
interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>,
|
||||||
|
control_buf: &'d mut [u8],
|
||||||
|
|
||||||
bus: D,
|
bus: D,
|
||||||
next_interface_number: u8,
|
next_interface_number: u8,
|
||||||
@ -133,12 +134,17 @@ pub struct UsbDeviceBuilder<'d, D: Driver<'d>> {
|
|||||||
|
|
||||||
impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> {
|
impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> {
|
||||||
/// Creates a builder for constructing a new [`UsbDevice`].
|
/// Creates a builder for constructing a new [`UsbDevice`].
|
||||||
|
///
|
||||||
|
/// `control_buf` is a buffer used for USB control request data. It should be sized
|
||||||
|
/// large enough for the length of the largest control request (in or out)
|
||||||
|
/// anticipated by any class added to the device.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
bus: D,
|
bus: D,
|
||||||
config: Config<'d>,
|
config: Config<'d>,
|
||||||
device_descriptor_buf: &'d mut [u8],
|
device_descriptor_buf: &'d mut [u8],
|
||||||
config_descriptor_buf: &'d mut [u8],
|
config_descriptor_buf: &'d mut [u8],
|
||||||
bos_descriptor_buf: &'d mut [u8],
|
bos_descriptor_buf: &'d mut [u8],
|
||||||
|
control_buf: &'d mut [u8],
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// Magic values specified in USB-IF ECN on IADs.
|
// Magic values specified in USB-IF ECN on IADs.
|
||||||
if config.composite_with_iads
|
if config.composite_with_iads
|
||||||
@ -170,6 +176,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> {
|
|||||||
bus,
|
bus,
|
||||||
config,
|
config,
|
||||||
interfaces: Vec::new(),
|
interfaces: Vec::new(),
|
||||||
|
control_buf,
|
||||||
next_interface_number: 0,
|
next_interface_number: 0,
|
||||||
next_string_index: 4,
|
next_string_index: 4,
|
||||||
|
|
||||||
@ -191,6 +198,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> {
|
|||||||
self.config_descriptor.into_buf(),
|
self.config_descriptor.into_buf(),
|
||||||
self.bos_descriptor.writer.into_buf(),
|
self.bos_descriptor.writer.into_buf(),
|
||||||
self.interfaces,
|
self.interfaces,
|
||||||
|
self.control_buf,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,6 +210,12 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> {
|
|||||||
InterfaceNumber::new(number)
|
InterfaceNumber::new(number)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the size of the control request data buffer. Can be used by
|
||||||
|
/// classes to validate the buffer is large enough for their needs.
|
||||||
|
pub fn control_buf_len(&self) -> usize {
|
||||||
|
self.control_buf.len()
|
||||||
|
}
|
||||||
|
|
||||||
/// Allocates a new interface number, with a handler that will be called
|
/// Allocates a new interface number, with a handler that will be called
|
||||||
/// for all the control requests directed to it.
|
/// for all the control requests directed to it.
|
||||||
pub fn alloc_interface_with_handler(
|
pub fn alloc_interface_with_handler(
|
||||||
|
@ -164,7 +164,7 @@ pub trait ControlHandler {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `req` - The request from the SETUP packet.
|
/// * `req` - The request from the SETUP packet.
|
||||||
fn control_in(&mut self, req: Request) -> InResponse<'_> {
|
fn control_in<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> {
|
||||||
InResponse::Rejected
|
InResponse::Rejected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,7 @@ pub struct UsbDevice<'d, D: Driver<'d>> {
|
|||||||
device_descriptor: &'d [u8],
|
device_descriptor: &'d [u8],
|
||||||
config_descriptor: &'d [u8],
|
config_descriptor: &'d [u8],
|
||||||
bos_descriptor: &'d [u8],
|
bos_descriptor: &'d [u8],
|
||||||
|
control_buf: &'d mut [u8],
|
||||||
|
|
||||||
device_state: UsbDeviceState,
|
device_state: UsbDeviceState,
|
||||||
remote_wakeup_enabled: bool,
|
remote_wakeup_enabled: bool,
|
||||||
@ -78,6 +79,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
|||||||
config_descriptor: &'d [u8],
|
config_descriptor: &'d [u8],
|
||||||
bos_descriptor: &'d [u8],
|
bos_descriptor: &'d [u8],
|
||||||
interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>,
|
interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>,
|
||||||
|
control_buf: &'d mut [u8],
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let control = driver
|
let control = driver
|
||||||
.alloc_control_pipe(config.max_packet_size_0 as u16)
|
.alloc_control_pipe(config.max_packet_size_0 as u16)
|
||||||
@ -94,6 +96,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
|||||||
device_descriptor,
|
device_descriptor,
|
||||||
config_descriptor,
|
config_descriptor,
|
||||||
bos_descriptor,
|
bos_descriptor,
|
||||||
|
control_buf,
|
||||||
device_state: UsbDeviceState::Default,
|
device_state: UsbDeviceState::Default,
|
||||||
remote_wakeup_enabled: false,
|
remote_wakeup_enabled: false,
|
||||||
self_powered: false,
|
self_powered: false,
|
||||||
@ -204,10 +207,9 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
|||||||
_ => self.control.reject(),
|
_ => self.control.reject(),
|
||||||
},
|
},
|
||||||
(RequestType::Class, Recipient::Interface) => {
|
(RequestType::Class, Recipient::Interface) => {
|
||||||
let mut buf = [0; 128];
|
|
||||||
let data = if req.length > 0 {
|
let data = if req.length > 0 {
|
||||||
let size = self.control.data_out(&mut buf).await.unwrap();
|
let size = self.control.data_out(self.control_buf).await.unwrap();
|
||||||
&buf[0..size]
|
&self.control_buf[0..size]
|
||||||
} else {
|
} else {
|
||||||
&[]
|
&[]
|
||||||
};
|
};
|
||||||
@ -284,7 +286,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
|||||||
.find(|(i, _)| req.index == *i as _)
|
.find(|(i, _)| req.index == *i as _)
|
||||||
.map(|(_, h)| h);
|
.map(|(_, h)| h);
|
||||||
match handler {
|
match handler {
|
||||||
Some(handler) => match handler.control_in(req) {
|
Some(handler) => match handler.control_in(req, self.control_buf) {
|
||||||
InResponse::Accepted(data) => self.control.accept_in(data).await,
|
InResponse::Accepted(data) => self.control.accept_in(data).await,
|
||||||
InResponse::Rejected => self.control.reject(),
|
InResponse::Rejected => self.control.reject(),
|
||||||
},
|
},
|
||||||
|
@ -66,7 +66,6 @@ pub struct CdcAcmClass<'d, D: Driver<'d>> {
|
|||||||
|
|
||||||
struct Control<'a> {
|
struct Control<'a> {
|
||||||
shared: &'a ControlShared,
|
shared: &'a ControlShared,
|
||||||
buf: [u8; 7],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shared data between Control and CdcAcmClass
|
/// 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) {
|
fn reset(&mut self) {
|
||||||
let shared = self.shared();
|
let shared = self.shared();
|
||||||
shared.line_coding.lock(|x| x.set(LineCoding::default()));
|
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 {
|
match req.request {
|
||||||
// REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below.
|
// REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below.
|
||||||
REQ_GET_LINE_CODING if req.length == 7 => {
|
REQ_GET_LINE_CODING if req.length == 7 => {
|
||||||
info!("Sending line coding");
|
info!("Sending line coding");
|
||||||
let coding = self.shared().line_coding.lock(|x| x.get());
|
let coding = self.shared().line_coding.lock(|x| x.get());
|
||||||
self.buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes());
|
assert!(buf.len() >= 7);
|
||||||
self.buf[4] = coding.stop_bits as u8;
|
buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes());
|
||||||
self.buf[5] = coding.parity_type as u8;
|
buf[4] = coding.stop_bits as u8;
|
||||||
self.buf[6] = coding.data_bits;
|
buf[5] = coding.parity_type as u8;
|
||||||
InResponse::Accepted(&self.buf)
|
buf[6] = coding.data_bits;
|
||||||
|
InResponse::Accepted(&buf[0..7])
|
||||||
}
|
}
|
||||||
_ => InResponse::Rejected,
|
_ => InResponse::Rejected,
|
||||||
}
|
}
|
||||||
@ -166,11 +166,12 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let control = state.control.write(Control {
|
let control = state.control.write(Control {
|
||||||
shared: &state.shared,
|
shared: &state.shared,
|
||||||
buf: [0; 7],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let control_shared = &state.shared;
|
let control_shared = &state.shared;
|
||||||
|
|
||||||
|
assert!(builder.control_buf_len() >= 7);
|
||||||
|
|
||||||
let comm_if = builder.alloc_interface_with_handler(control);
|
let comm_if = builder.alloc_interface_with_handler(control);
|
||||||
let comm_ep = builder.alloc_interrupt_endpoint_in(8, 255);
|
let comm_ep = builder.alloc_interrupt_endpoint_in(8, 255);
|
||||||
let data_if = builder.alloc_interface();
|
let data_if = builder.alloc_interface();
|
||||||
|
@ -47,6 +47,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
|
|||||||
let mut device_descriptor = [0; 256];
|
let mut device_descriptor = [0; 256];
|
||||||
let mut config_descriptor = [0; 256];
|
let mut config_descriptor = [0; 256];
|
||||||
let mut bos_descriptor = [0; 256];
|
let mut bos_descriptor = [0; 256];
|
||||||
|
let mut control_buf = [0; 7];
|
||||||
|
|
||||||
let mut state = State::new();
|
let mut state = State::new();
|
||||||
|
|
||||||
@ -56,6 +57,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
|
|||||||
&mut device_descriptor,
|
&mut device_descriptor,
|
||||||
&mut config_descriptor,
|
&mut config_descriptor,
|
||||||
&mut bos_descriptor,
|
&mut bos_descriptor,
|
||||||
|
&mut control_buf,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create classes on the builder.
|
// Create classes on the builder.
|
||||||
|
Loading…
Reference in New Issue
Block a user