aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Munns <[email protected]>2023-03-27 14:19:00 +0200
committerJames Munns <[email protected]>2023-03-27 14:19:00 +0200
commita6cef4baf220409bb20d81af538f2c507ec4c4c9 (patch)
treec61abad87ccd55058e4e0e7d156c86fe20267e60
parent732614579b86c2a63856b6e8e2622e09322600a7 (diff)
Add logging and interface for debugging buffer usage
-rw-r--r--embassy-usb/src/builder.rs14
-rw-r--r--embassy-usb/src/class/hid.rs3
-rw-r--r--embassy-usb/src/lib.rs37
-rw-r--r--embassy-usb/src/msos.rs5
4 files changed, 59 insertions, 0 deletions
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs
index 305dfa02e..6649cd5b6 100644
--- a/embassy-usb/src/builder.rs
+++ b/embassy-usb/src/builder.rs
@@ -201,6 +201,20 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
201 self.config_descriptor.end_configuration(); 201 self.config_descriptor.end_configuration();
202 self.bos_descriptor.end_bos(); 202 self.bos_descriptor.end_bos();
203 203
204 info!("USB: device_descriptor used: {}", self.device_descriptor.position());
205 info!("USB: config_descriptor used: {}", self.config_descriptor.position());
206 info!("USB: bos_descriptor_buf used: {}", self.bos_descriptor.writer.position());
207 #[cfg(feature = "msos-descriptor")]
208 info!("USB: device_descriptor used: {}", msos_descriptor.len());
209 if self.control_buf.len() != self.config.max_packet_size_0.into() {
210 warn!(
211 "Mismatch in control buf and max packet size! buf len: {}, max ep0 size: {}",
212 self.control_buf.len(),
213 self.config.max_packet_size_0,
214 );
215 }
216 info!("USB: device_descriptor used: {}", self.config_descriptor.position());
217
204 UsbDevice::build( 218 UsbDevice::build(
205 self.driver, 219 self.driver,
206 self.config, 220 self.config,
diff --git a/embassy-usb/src/class/hid.rs b/embassy-usb/src/class/hid.rs
index 974268c62..597403427 100644
--- a/embassy-usb/src/class/hid.rs
+++ b/embassy-usb/src/class/hid.rs
@@ -458,6 +458,9 @@ impl<'d> Handler for Control<'d> {
458 return None; 458 return None;
459 } 459 }
460 460
461 // TODO(AJM): This uses a defmt-specific formatter that causes use of the `log`
462 // feature to fail to build
463 #[cfg(feature = "defmt")]
461 trace!("HID control_out {:?} {=[u8]:x}", req, data); 464 trace!("HID control_out {:?} {=[u8]:x}", req, data);
462 match req.request { 465 match req.request {
463 HID_REQ_SET_IDLE => { 466 HID_REQ_SET_IDLE => {
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index bfeccd5fe..3016b81cb 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -165,6 +165,25 @@ struct Interface {
165 num_alt_settings: u8, 165 num_alt_settings: u8,
166} 166}
167 167
168/// A report of the used size of the runtime allocated buffers
169#[derive(PartialEq, Eq, Copy, Clone, Debug)]
170#[cfg_attr(feature = "defmt", derive(defmt::Format))]
171pub struct UsbBufferReport {
172 /// Number of device descriptor bytes used
173 pub device_descriptor_used: usize,
174 /// Number of config descriptor bytes used
175 pub config_descriptor_used: usize,
176 /// Number of bos descriptor bytes used
177 pub bos_descriptor_used: usize,
178 /// Number of msos descriptor bytes used
179 ///
180 /// Will be `None` if the "msos-descriptor" feature is not active.
181 /// Otherwise will return Some(bytes).
182 pub msos_descriptor_used: Option<usize>,
183 /// Size of the control buffer
184 pub control_buffer_size: usize,
185}
186
168/// Main struct for the USB device stack. 187/// Main struct for the USB device stack.
169pub struct UsbDevice<'d, D: Driver<'d>> { 188pub struct UsbDevice<'d, D: Driver<'d>> {
170 control_buf: &'d mut [u8], 189 control_buf: &'d mut [u8],
@@ -239,6 +258,24 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
239 } 258 }
240 } 259 }
241 260
261 /// Returns a report of the consumed buffers
262 ///
263 /// Useful for tuning buffer sizes for actual usage
264 pub fn buffer_usage(&self) -> UsbBufferReport {
265 #[cfg(not(feature = "msos-descriptor"))]
266 let mdu = None;
267 #[cfg(feature = "msos-descriptor")]
268 let mdu = Some(self.inner.msos_descriptor.len());
269
270 UsbBufferReport {
271 device_descriptor_used: self.inner.device_descriptor.len(),
272 config_descriptor_used: self.inner.config_descriptor.len(),
273 bos_descriptor_used: self.inner.bos_descriptor.len(),
274 msos_descriptor_used: mdu,
275 control_buffer_size: self.control_buf.len(),
276 }
277 }
278
242 /// Runs the `UsbDevice` forever. 279 /// Runs the `UsbDevice` forever.
243 /// 280 ///
244 /// This future may leave the bus in an invalid state if it is dropped. 281 /// This future may leave the bus in an invalid state if it is dropped.
diff --git a/embassy-usb/src/msos.rs b/embassy-usb/src/msos.rs
index b1e0335ee..218d9931a 100644
--- a/embassy-usb/src/msos.rs
+++ b/embassy-usb/src/msos.rs
@@ -32,6 +32,11 @@ impl<'d> MsOsDescriptorSet<'d> {
32 pub fn is_empty(&self) -> bool { 32 pub fn is_empty(&self) -> bool {
33 self.descriptor.is_empty() 33 self.descriptor.is_empty()
34 } 34 }
35
36 /// Returns the length of the descriptor field
37 pub fn len(&self) -> usize {
38 self.descriptor.len()
39 }
35} 40}
36 41
37/// Writes a Microsoft OS 2.0 Descriptor set into a buffer. 42/// Writes a Microsoft OS 2.0 Descriptor set into a buffer.