Project 10: USB Host Mode (Keyboard/Mouse Reader)
Turn the RP2040/RP2350 into a USB host that reads HID keyboards and mice.
Quick Reference
| Attribute | Value |
|---|---|
| Difficulty | Level 4: Expert |
| Time Estimate | 1-2 weeks |
| Main Programming Language | C (TinyUSB host) |
| Alternative Programming Languages | Rust |
| Coolness Level | Level 4: “host stack builder” |
| Business Potential | 3. USB hubs, KVMs, test tools |
| Prerequisites | USB fundamentals, clock accuracy, power control |
| Key Topics | USB host enumeration, HID polling, VBUS power |
1. Learning Objectives
- Provide VBUS power safely to USB devices.
- Enumerate HID devices and parse reports.
- Implement a 1 ms polling loop without missed frames.
- Handle disconnect/reconnect gracefully.
2. All Theory Needed (Per-Concept Breakdown)
2.1 USB Host vs Device Roles
Fundamentals
A USB host initiates enumeration and schedules transfers. A device responds. Host mode requires power management and transaction scheduling.
Deep Dive into the concept
Hosts supply 5V on VBUS, detect device attach, reset the device, then enumerate. Interrupt transfers (HID) are polled at intervals; the host must keep a 1 ms schedule. Unlike device mode, the host is responsible for bus management and timing. This is why a stable 48 MHz clock and low latency scheduling are critical.
2.2 VBUS Power and OTG Safety
Fundamentals
USB host mode must supply 5V VBUS and obey current limits. Incorrect power handling can damage devices.
Deep Dive into the concept
Use a power switch or boost regulator to supply VBUS. Monitor overcurrent if possible. Enable VBUS only after you are ready to enumerate. Some boards require a USB OTG switch or external circuitry to safely supply power.
2.3 HID Polling and Report Parsing
Fundamentals
HID devices send reports on a schedule defined by the host. Keyboards and mice typically use 8‑byte reports.
Deep Dive into the concept
The host polls the HID interrupt endpoint at the specified interval (often 1–10 ms). Reports contain keycodes or mouse deltas. You must parse these and map them to human‑readable events.
3. Project Specification
3.1 What You Will Build
A USB host firmware that enumerates HID keyboards/mice and prints decoded events over UART or USB serial.
3.2 Functional Requirements
- Supply VBUS power safely.
- Enumerate HID devices.
- Poll at 1 ms or device‑specified interval.
- Handle disconnects and re‑enumeration.
3.7 Real World Outcome
Plugging a USB keyboard into the board prints key events in real time, with no missed keys during a 1‑minute typing test.
4. Solution Architecture
VBUS Power -> USB Host Stack -> HID Parser -> Logger
5. Implementation Guide
5.10 Implementation Phases
- Phase 1: VBUS power + host enumeration
- Phase 2: HID report parsing
- Phase 3: Robust disconnect handling
6. Testing Strategy
- Enumerate 3 different keyboards
- Stress test with rapid typing
7. Common Pitfalls & Debugging
- VBUS power instability
- Clock drift causing missed frames
8. Extensions & Challenges
- Add USB hub support
- Add mass‑storage enumeration
9. Submission / Completion Criteria
Minimum Viable Completion:
- One HID keyboard enumerates and prints events.
Full Completion:
- Keyboard + mouse with reconnect support.
Excellence:
- Hub support with multiple devices.