1303: Add logging and interface for debugging USB buffer usage r=Dirbaio a=jamesmunns



Co-authored-by: James Munns <james@onevariable.com>
This commit is contained in:
bors[bot] 2023-03-27 16:31:13 +00:00 committed by GitHub
commit cf179f3076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 0 deletions

View File

@ -201,6 +201,14 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
self.config_descriptor.end_configuration(); self.config_descriptor.end_configuration();
self.bos_descriptor.end_bos(); self.bos_descriptor.end_bos();
// Log the number of allocator bytes actually used in descriptor buffers
info!("USB: device_descriptor used: {}", self.device_descriptor.position());
info!("USB: config_descriptor used: {}", self.config_descriptor.position());
info!("USB: bos_descriptor used: {}", self.bos_descriptor.writer.position());
#[cfg(feature = "msos-descriptor")]
info!("USB: msos_descriptor used: {}", msos_descriptor.len());
info!("USB: control_buf size: {}", self.control_buf.len());
UsbDevice::build( UsbDevice::build(
self.driver, self.driver,
self.config, self.config,

View File

@ -458,6 +458,9 @@ impl<'d> Handler for Control<'d> {
return None; return None;
} }
// This uses a defmt-specific formatter that causes use of the `log`
// feature to fail to build, so leave it defmt-specific for now.
#[cfg(feature = "defmt")]
trace!("HID control_out {:?} {=[u8]:x}", req, data); trace!("HID control_out {:?} {=[u8]:x}", req, data);
match req.request { match req.request {
HID_REQ_SET_IDLE => { HID_REQ_SET_IDLE => {

View File

@ -165,6 +165,25 @@ struct Interface {
num_alt_settings: u8, num_alt_settings: u8,
} }
/// A report of the used size of the runtime allocated buffers
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct UsbBufferReport {
/// Number of device descriptor bytes used
pub device_descriptor_used: usize,
/// Number of config descriptor bytes used
pub config_descriptor_used: usize,
/// Number of bos descriptor bytes used
pub bos_descriptor_used: usize,
/// Number of msos descriptor bytes used
///
/// Will be `None` if the "msos-descriptor" feature is not active.
/// Otherwise will return Some(bytes).
pub msos_descriptor_used: Option<usize>,
/// Size of the control buffer
pub control_buffer_size: usize,
}
/// Main struct for the USB device stack. /// Main struct for the USB device stack.
pub struct UsbDevice<'d, D: Driver<'d>> { pub struct UsbDevice<'d, D: Driver<'d>> {
control_buf: &'d mut [u8], control_buf: &'d mut [u8],
@ -239,6 +258,24 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
} }
} }
/// Returns a report of the consumed buffers
///
/// Useful for tuning buffer sizes for actual usage
pub fn buffer_usage(&self) -> UsbBufferReport {
#[cfg(not(feature = "msos-descriptor"))]
let mdu = None;
#[cfg(feature = "msos-descriptor")]
let mdu = Some(self.inner.msos_descriptor.len());
UsbBufferReport {
device_descriptor_used: self.inner.device_descriptor.len(),
config_descriptor_used: self.inner.config_descriptor.len(),
bos_descriptor_used: self.inner.bos_descriptor.len(),
msos_descriptor_used: mdu,
control_buffer_size: self.control_buf.len(),
}
}
/// Runs the `UsbDevice` forever. /// Runs the `UsbDevice` forever.
/// ///
/// This future may leave the bus in an invalid state if it is dropped. /// This future may leave the bus in an invalid state if it is dropped.

View File

@ -32,6 +32,11 @@ impl<'d> MsOsDescriptorSet<'d> {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.descriptor.is_empty() self.descriptor.is_empty()
} }
/// Returns the length of the descriptor field
pub fn len(&self) -> usize {
self.descriptor.len()
}
} }
/// Writes a Microsoft OS 2.0 Descriptor set into a buffer. /// Writes a Microsoft OS 2.0 Descriptor set into a buffer.