aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src/lib.rs
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 /embassy-usb/src/lib.rs
parent732614579b86c2a63856b6e8e2622e09322600a7 (diff)
Add logging and interface for debugging buffer usage
Diffstat (limited to 'embassy-usb/src/lib.rs')
-rw-r--r--embassy-usb/src/lib.rs37
1 files changed, 37 insertions, 0 deletions
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.