RobotGuide

Hardware & electrical · topic E17 · 35 hours

USB: from electrical layer to enumeration

USB is the most common «human-to-machine conversation» interface on a robot, and the one most easily treated as a black box. Understand it, and you know why it cannot do hard real-time, why cameras must compress, and why «just try another port» actually works.

Specialization stage · Hardcore
System software · Aware Embedded & controls · Proficient

Tools
Tools

What to learn

  • Electricals: D+ / D− differential; whether the 1.5 kΩ pull-up sits on D+ or D− decides full-speed vs low-speed; VBUS and power capability (500 mA / 900 mA)
  • Encoding: NRZI + bit stuffing + SYNC field + EOP
  • Packet structure: PID / ADDR / ENDP / CRC5 / CRC16; token packets, data packets, handshake packets, SOF
  • Four transfer types: control (enumeration and configuration), bulk (correct, no timing guarantee), interrupt (guaranteed interval), isochronous (guaranteed bandwidth, no correctness guarantee)
  • Endpoints and pipes; host-exclusive scheduling (1 ms frames / 125 µs microframes)
  • Why it cannot do 1 kHz hard real-time: host polling plus millisecond-scale scheduling jitter
  • The full enumeration sequence: plug-in → reset → read descriptors → assign address → select configuration → bind driver
  • Engineering judgment: how to compute USB camera bandwidth (1080p30 YUY2 ≈ 1 Gbps, hence most use MJPEG); USB 3.x is two buses coexisting, not «a faster 2.0»

The lesson · plain words

USB is the most universal interface on a robot: sensors, debug serial, cameras, and flashing all go through it. But between «it plugs in» and «it communicates» sits an entire enumeration flow — understand it, and USB problems stop being mysticism.

Enumeration: that second or two after plugging in

The six steps in the animation: plug detect → reset → speed negotiation → SET_ADDRESS → read descriptors → bind driver. Each failing step maps to one class of field symptom: no response on plug-in (detect failed), device connects and disconnects repeatedly (unstable negotiation), device recognized but driver will not load (descriptor / driver matching).

Descriptors are the device's «ID card + résumé»: the device descriptor (who it is, VID/PID), the configuration descriptor (which interfaces it has), the endpoint descriptors (how data is transferred). The host matches drivers against this list — change the VID/PID and the host will recognize it as a different device; that is exactly how USB-serial chips (CP210x/CH340) work.

Interactive figure · drag it and see

What this figure shows: a six-step animated timeline of USB enumeration — plug detect (the host sees the D+/D- pull-up), reset (host holds SE0 ≥10 ms), speed negotiation (chirp handshake for full- or high-speed), SET_ADDRESS (the host commands: from now on you are address 7), read descriptors (who you are, what you can do), bind driver (the device appears in the system, e.g. /dev/ttyUSB0) — with a step counter showing enumerating: step n. Takeaway: the 1–2 s after plugging in is host and device playing out this whole drama, and each step has a distinct failure signature you can diagnose from the field symptom.

Speed and electricals: do the math

USB 2.0 high-speed is 480 Mbps (~40 MB/s effective); USB 3.0 is 5 Gbps (~400 MB/s). For cameras and point-cloud devices, compute bandwidth first (the multiplication in E19), then pick the USB version — 480 Mbps is already full with one 1080p30 stream.

Electrical details: D+/D− differential at 90 Ω; VBUS starts at 500 mA (BC1.2 / Type-C PD can negotiate more); cable length stable within 3 m. Under-powering has a classic symptom: the device fails enumeration repeatedly or drops mid-session — swap to a powered HUB and you will know immediately.

USB's three jobs on a robot

① Debug channel: serial adapters / USB-to-serial for logs and flashing. ② High-speed sensing: USB cameras (UVC protocol, plug-and-play) and depth cameras. ③ Data return: recording drives, video export.

Engineering notes: robot vibration loosens USB connectors — use latching connectors or threaded panel-mount types; industrial cameras on long USB3 runs need active optical cables. When «the camera drops occasionally» in the field, suspect contact first, not the driver.

How deep to go

Name the six enumeration steps and their matching symptoms; read VID/PID with lsusb / Device Manager; compute the bandwidth one UVC camera consumes. With these, you can troubleshoot the USB layer on your own.

Three questions — do you really get it?

1. A device «connects and disconnects repeatedly» — which enumeration step is most likely unstable?

  • ADriver matching
  • BDescriptor reading
  • CSpeed negotiation / unstable power failing enumeration mid-way
Show answer

C. A failed negotiation triggers a new reset and retry, showing up as repeated disconnect/reconnect. Swapping the power port or cable is the fastest test.

2. What do VID/PID do?

  • AEncrypt the device
  • BDecide the speed
  • CIdentify the device so the host can match a driver
Show answer

C. The OS matches drivers by VID/PID plus interface class. Changing them can masquerade the device type.

3. USB 2.0 high-speed's theoretical and effective bandwidth are roughly?

  • A12 Mbps / 1 MB/s
  • B5 Gbps / 400 MB/s
  • C480 Mbps / ~40 MB/s
Show answer

C. Protocol overhead (8b/10b + frame structure) eats most of it. Bandwidth budgets must use effective values.

Keep going deeper

Links without a marker stay on this site; the rest point to the Chinese edition until its translation batch lands.

How it fails in the field

  • Thinking USB is an «upgraded serial port»: it is a host-exclusive, complex protocol stack, not a point-to-point data stream
  • Using USB for real-time control: millisecond jitter means a 1 kHz loop will inevitably collapse
  • Hooking up multiple cameras without compressing: once bandwidth overflows, frames drop — with no error reported
  • Ignoring power capability: exceed the 500 mA limit and the device re-enumerates repeatedly, looking exactly like «unstable driver»

You have learned enough when

You can sketch the packet sequence of one enumeration and say which packet class each step corresponds to in a capture; given «4 × 1080p30 cameras», you can compute the bandwidth, choose an interface with justification, and state the latency cost of compression.

The Chinese edition carries the full topic page for USB: from electrical layer to enumeration, including the complete outline, depth requirements per career track, and further derivations: robotguide.dev/k/E17/.