Skip to content

Commit d628911

Browse files
committed
Adding USB trace support
1 parent 232decb commit d628911

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmt = { version = "0.3", optional = true }
1414
portable-atomic = { version = "1.2.0", default-features = false }
1515
num_enum = { version = "0.7.1", default-features = false }
1616
heapless = "0.8"
17+
log = { version = "0.4", default-features = false, optional = true}
1718

1819
[dev-dependencies]
1920
rusb = "0.9.1"
@@ -23,6 +24,9 @@ rand = "0.8.5"
2324
# Use a 256 byte buffer for control transfers instead of 128.
2425
control-buffer-256 = []
2526

27+
# Enable logging and tracing via the `log` crate
28+
log = ["dep:log"]
29+
2630
# Use larger endpoint buffers for highspeed operation (default fullspeed)
2731
#
2832
# Note: usb-device doesn't truly support high speed enumeration yet, so setting this will make

src/control_pipe.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
6363
}
6464

6565
pub fn reset(&mut self) {
66+
usb_trace!("Control pipe reset");
6667
self.state = ControlState::Idle;
6768
}
6869

@@ -87,6 +88,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
8788

8889
// Now that we have properly parsed the setup packet, ensure the end-point is no longer in
8990
// a stalled state.
91+
usb_trace!("EP0 request received: {req:?}");
9092
self.ep_out.unstall();
9193

9294
/*sprintln!("SETUP {:?} {:?} {:?} req:{} val:{} idx:{} len:{} {:?}",
@@ -142,8 +144,10 @@ impl<B: UsbBus> ControlPipe<'_, B> {
142144
};
143145

144146
self.i += count;
147+
usb_trace!("Read {count} bytes on EP0-OUT");
145148

146149
if self.i >= self.len {
150+
usb_debug!("Request OUT complete: {req}");
147151
self.state = ControlState::CompleteOut;
148152
return Some(req);
149153
}
@@ -154,11 +158,19 @@ impl<B: UsbBus> ControlPipe<'_, B> {
154158
| ControlState::DataInLast
155159
| ControlState::DataInZlp
156160
| ControlState::StatusOut => {
161+
usb_debug!(
162+
"Terminating DATA stage early. Current state: {:?}",
163+
self.state
164+
);
157165
let _ = self.ep_out.read(&mut []);
158166
self.state = ControlState::Idle;
159167
}
160168
_ => {
161169
// Discard the packet
170+
usb_debug!(
171+
"Discarding EP0 data due to unexpected state. Current state: {:?}",
172+
self.state
173+
);
162174
let _ = self.ep_out.read(&mut []);
163175

164176
// Unexpected OUT packet
@@ -181,6 +193,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
181193
return false;
182194
}
183195

196+
usb_trace!("wrote EP0-IN: ZLP");
184197
self.state = ControlState::DataInLast;
185198
}
186199
ControlState::DataInLast => {
@@ -192,12 +205,14 @@ impl<B: UsbBus> ControlPipe<'_, B> {
192205
return true;
193206
}
194207
ControlState::Idle => {
208+
usb_debug!("Ignoring EP0-IN while in IDLE");
195209
// If we received a message on EP0 while sending the last portion of an IN
196210
// transfer, we may have already transitioned to IDLE without getting the last
197211
// IN-complete status. Just ignore this indication.
198212
}
199213
_ => {
200214
// Unexpected IN packet
215+
usb_debug!("Unexpected EP0-IN. Current state: {:?}", self.state);
201216
self.set_error();
202217
}
203218
};
@@ -213,9 +228,14 @@ impl<B: UsbBus> ControlPipe<'_, B> {
213228
Ok(c) => c,
214229
// There isn't much we can do if the write fails, except to wait for another poll or for
215230
// the host to resend the request.
216-
Err(_) => return,
231+
Err(_err) => {
232+
usb_debug!("Failed to write EP0-IN: {_err:?}");
233+
return;
234+
}
217235
};
218236

237+
usb_trace!("wrote EP0-IN: {:?}", &buffer[self.i..(self.i + count)]);
238+
219239
self.i += count;
220240

221241
if self.i >= self.len {
@@ -286,6 +306,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
286306
}
287307

288308
fn set_error(&mut self) {
309+
usb_debug!("EP0 stalled - error");
289310
self.state = ControlState::Error;
290311
self.ep_out.stall();
291312
self.ep_in.stall();

src/device.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ impl<B: UsbBus> UsbDevice<'_, B> {
204204

205205
// Pending events for endpoint 0?
206206
if (eps & 1) != 0 {
207+
usb_debug!(
208+
"EP0: setup={}, in={}, out={}",
209+
ep_setup & 1,
210+
ep_in_complete & 1,
211+
ep_out & 1
212+
);
207213
// Handle EP0-IN conditions first. When both EP0-IN and EP0-OUT have completed,
208214
// it is possible that EP0-OUT is a zero-sized out packet to complete the STATUS
209215
// phase of the control transfer. We have to process EP0-IN first to update our
@@ -230,6 +236,10 @@ impl<B: UsbBus> UsbDevice<'_, B> {
230236
None
231237
};
232238

239+
if let Some(_req) = req {
240+
usb_trace!("Handling EP0 request: {_req}");
241+
}
242+
233243
match req {
234244
Some(req) if req.direction == UsbDirection::In => {
235245
self.control_in(classes, req)
@@ -250,18 +260,21 @@ impl<B: UsbBus> UsbDevice<'_, B> {
250260
for i in 1..MAX_ENDPOINTS {
251261
if (ep_setup & bit) != 0 {
252262
for cls in classes.iter_mut() {
263+
usb_trace!("Handling EP{i}-SETUP");
253264
cls.endpoint_setup(EndpointAddress::from_parts(
254265
i,
255266
UsbDirection::Out,
256267
));
257268
}
258269
} else if (ep_out & bit) != 0 {
270+
usb_trace!("Handling EP{i}-OUT");
259271
for cls in classes.iter_mut() {
260272
cls.endpoint_out(EndpointAddress::from_parts(i, UsbDirection::Out));
261273
}
262274
}
263275

264276
if (ep_in_complete & bit) != 0 {
277+
usb_trace!("Handling EP{i}-IN");
265278
for cls in classes.iter_mut() {
266279
cls.endpoint_in_complete(EndpointAddress::from_parts(
267280
i,
@@ -289,6 +302,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
289302
}
290303
PollResult::Resume => {}
291304
PollResult::Suspend => {
305+
usb_debug!("Suspending bus");
292306
self.bus.suspend();
293307
self.suspended_device_state = Some(self.device_state);
294308
self.device_state = UsbDeviceState::Suspend;
@@ -314,6 +328,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
314328

315329
match (req.recipient, req.request) {
316330
(Recipient::Device, Request::GET_STATUS) => {
331+
usb_trace!("Processing Device::GetStatus");
317332
let status: u16 = if self.self_powered { 0x0001 } else { 0x0000 }
318333
| if self.remote_wakeup_enabled {
319334
0x0002
@@ -325,12 +340,14 @@ impl<B: UsbBus> UsbDevice<'_, B> {
325340
}
326341

327342
(Recipient::Interface, Request::GET_STATUS) => {
343+
usb_trace!("Processing Interface::GetStatus");
328344
let status: u16 = 0x0000;
329345

330346
let _ = xfer.accept_with(&status.to_le_bytes());
331347
}
332348

333349
(Recipient::Endpoint, Request::GET_STATUS) => {
350+
usb_trace!("Processing EP::GetStatus");
334351
let ep_addr = ((req.index as u8) & 0x8f).into();
335352

336353
let status: u16 = if self.bus.is_stalled(ep_addr) {
@@ -343,10 +360,12 @@ impl<B: UsbBus> UsbDevice<'_, B> {
343360
}
344361

345362
(Recipient::Device, Request::GET_DESCRIPTOR) => {
363+
usb_trace!("Processing Device::GetDescriptor");
346364
UsbDevice::get_descriptor(&self.config, classes, xfer)
347365
}
348366

349367
(Recipient::Device, Request::GET_CONFIGURATION) => {
368+
usb_trace!("Processing Device::GetConfiguration");
350369
let config = match self.device_state {
351370
UsbDeviceState::Configured => CONFIGURATION_VALUE,
352371
_ => CONFIGURATION_NONE,
@@ -356,6 +375,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
356375
}
357376

358377
(Recipient::Interface, Request::GET_INTERFACE) => {
378+
usb_trace!("Processing Interface::GetInterface");
359379
// Reject interface numbers bigger than 255
360380
if req.index > core::u8::MAX.into() {
361381
let _ = xfer.reject();
@@ -381,6 +401,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
381401
}
382402

383403
if self.control.waiting_for_response() {
404+
usb_debug!("Rejecting control transfer because we were waiting for a response");
384405
let _ = self.control.reject();
385406
}
386407
}
@@ -409,11 +430,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
409430
Request::CLEAR_FEATURE,
410431
Request::FEATURE_DEVICE_REMOTE_WAKEUP,
411432
) => {
433+
usb_debug!("Remote wakeup disabled");
412434
self.remote_wakeup_enabled = false;
413435
let _ = xfer.accept();
414436
}
415437

416438
(Recipient::Endpoint, Request::CLEAR_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
439+
usb_debug!("EP{} halt removed", req.index & 0x8f);
417440
self.bus
418441
.set_stalled(((req.index as u8) & 0x8f).into(), false);
419442
let _ = xfer.accept();
@@ -424,17 +447,20 @@ impl<B: UsbBus> UsbDevice<'_, B> {
424447
Request::SET_FEATURE,
425448
Request::FEATURE_DEVICE_REMOTE_WAKEUP,
426449
) => {
450+
usb_debug!("Remote wakeup enabled");
427451
self.remote_wakeup_enabled = true;
428452
let _ = xfer.accept();
429453
}
430454

431455
(Recipient::Endpoint, Request::SET_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
456+
usb_debug!("EP{} halted", req.index & 0x8f);
432457
self.bus
433458
.set_stalled(((req.index as u8) & 0x8f).into(), true);
434459
let _ = xfer.accept();
435460
}
436461

437462
(Recipient::Device, Request::SET_ADDRESS, 1..=127) => {
463+
usb_debug!("Setting device address to {}", req.value);
438464
if B::QUIRK_SET_ADDRESS_BEFORE_STATUS {
439465
self.bus.set_device_address(req.value as u8);
440466
self.device_state = UsbDeviceState::Addressed;
@@ -445,11 +471,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
445471
}
446472

447473
(Recipient::Device, Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => {
474+
usb_debug!("Device configured");
448475
self.device_state = UsbDeviceState::Configured;
449476
let _ = xfer.accept();
450477
}
451478

452479
(Recipient::Device, Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => {
480+
usb_debug!("Device deconfigured");
453481
match self.device_state {
454482
UsbDeviceState::Default => {
455483
let _ = xfer.accept();
@@ -479,8 +507,10 @@ impl<B: UsbBus> UsbDevice<'_, B> {
479507

480508
// Default behaviour, if no class implementation accepted the alternate setting.
481509
if alt_setting == DEFAULT_ALTERNATE_SETTING_U16 {
510+
usb_debug!("Accepting unused alternate settings");
482511
let _ = xfer.accept();
483512
} else {
513+
usb_debug!("Rejecting unused alternate settings");
484514
let _ = xfer.reject();
485515
}
486516
}
@@ -493,6 +523,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
493523
}
494524

495525
if self.control.waiting_for_response() {
526+
usb_debug!("Rejecting control transfer due to waiting response");
496527
let _ = self.control.reject();
497528
}
498529
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#![no_std]
3737
#![warn(missing_docs)]
3838

39+
#[macro_use]
40+
mod macros;
41+
3942
/// A USB stack error.
4043
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
4144
#[cfg_attr(feature = "defmt", derive(defmt::Format))]

0 commit comments

Comments
 (0)