Skip to main content
THe1
Associate
March 24, 2022
Question

How to debug an USB-Connection?

  • March 24, 2022
  • 4 replies
  • 3541 views

Hello everyone,

I am trying to expand the STM32Cube USB library which comes with the generated Code from STM32CubeMX. There are still some bugs in the Code but it is really hard to find them without appropriate debugging tools. The normal debugger can not be used while the host disconnects the device if there is no answer. I also figured out that printf-Debugging with UART is also complicated because it is too slow.

Could you recommend a suitable method to debug an USB connection?

Thank you for helping!

This topic has been closed for replies.

4 replies

KnarfB
Super User
March 24, 2022

UART speeds up to 921600 baud worked reliably for me in similar settings.

Depending on the chip series you might redirect printf to SWO which can be even faster than that.

Use a high resolution timestamp (extra timer or DWT cycle counter avail. on some cores) for timestamping you logs.

You may also set aside a larger text (char) buffer in RAM and log to RAM (sprintf) with very little overhead and inspect/dump the RAM buffer later.

Might combine this with setting breakpoints at some critical points in the code and inspect the RAM buffer then.

If you have spare pins, you might add a logic analyzer (LA) and set some pins aside for signalling certain states of the connection.

There are SW tools that act like a software LA using a RAM buffer technique. Search for TraceX or https://gitlab.com/KnarfB/timedoctor.

A host side tool like wireshark may capture USB data on the wire.

Add assertions to your code to catch design flaws early.

hth

KnarfB

Piranha
Principal III
March 24, 2022

> You may also set aside a larger text (char) buffer in RAM and log to RAM (sprintf) with very little overhead and inspect/dump the RAM buffer later.

Or implement it as a large FIFO buffer and UART/SWO running asynchronously at it's own pace.

THe1
THe1Author
Associate
March 25, 2022

Thank you for the suggestions! I will try these.

Lubos KOUDELKA
ST Employee
March 25, 2022

Hello,

if you deal often with USB, external USB protocol analyzer could be good option for you. On our side we're using Ellisys USB Explorer 260 or LeCroy Mercury T2. It can show you all communication translated also into nicely readable form also with timestamps. Possible to use in both host and device configuration. (if STM32 is acting as host, PC USB analyzer cannot be used)

Best regards,

Lubos