docs: add build-doc tooling and a README for every example

Documentation tooling:
- Add the `build-doc` skill and `tools/build_doc.py` wrapper for local
  Sphinx builds (clean / -W / open).
- Enable Markdown (MyST) in conf.py and auto-collect
  examples/{device,host,dual}/*/README.md into a 3-level Examples nav
  (Examples > Device/Host/Dual > example), noting each page's source
  location and normalizing headings to a single H1.
- Remove the stale `.claude/commands/build-doc.md`; point the AGENTS.md
  Documentation section at the skill.

Example docs:
- Add a README.md for every device/host/dual example: what it does, USB
  interface table, notable tusb_config.h settings, generic CMake + Make
  build steps, and how to try it.
- Fold each *_freertos variant into its base README, noting the FreeRTOS
  source path and any RTOS-specific behavior.

Generated docs/examples/ output is git-ignored. Builds clean with
`sphinx-build -W`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
hathach
2026-06-29 10:16:25 +07:00
parent 0a25cc27d7
commit 4b1c8d16f7
48 changed files with 2001 additions and 202 deletions

View File

@ -0,0 +1,47 @@
# Host: Bare API
A USB host example that drives the TinyUSB host stack through its low-level API directly, without the higher-level class-driver application layer.
## What it does
- Enumerates any attached device and, on mount, fetches and prints the full device descriptor (VID/PID, USB version, class, max packet size, etc.) over the debug UART.
- Reads and prints the manufacturer, product, and serial-number string descriptors (UTF-16 to UTF-8).
- Fetches and walks the configuration descriptor with a small hand-written parser.
- For any HID interface found, opens its interrupt IN endpoint with the raw endpoint API (`tuh_edpt_open` / `tuh_edpt_xfer`) and continuously prints the incoming HID reports as raw hex bytes.
- Blinks the board LED once per second.
## Requirements
The board must support USB host mode (provide VBUS to the connected device); some boards need an external USB-A port / host adapter.
## Configuration
Notable `tusb_config.h` settings:
```c
#define CFG_TUH_ENABLED 1
#define CFG_TUH_HUB 1
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
#define CFG_TUH_ENUMERATION_BUFSIZE 256
#define CFG_TUH_API_EDPT_XFER 1
```
## Building
CMake:
```bash
mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .
```
Make:
```bash
make BOARD=raspberry_pi_pico all
```
## How to use
Plug any USB device (a HID keyboard/mouse works well) into the board's USB host port. On attach you should see `Device attached` followed by the full device/string/configuration descriptor dump on the debug UART. If the device exposes a HID interface, its interrupt-IN reports are printed as hex as they arrive (e.g. when you press a key or move the mouse).

View File

@ -0,0 +1,59 @@
# Host: CDC/MSC/HID
A USB host example that enumerates CDC serial, Mass Storage, and HID devices and reports their activity over the debug UART.
## What it does
- CDC (`CFG_TUH_CDC`): bidirectionally bridges the debug console and the attached CDC serial device — bytes typed on the debug UART are written to the device, and bytes received from the device are echoed back to the UART. Also supports common USB-serial adapters (FTDI, CP210x, CH34x, PL2303). On mount it prints the interface info and line coding (set to 115200 8N1 on enumeration).
- MSC (`CFG_TUH_MSC`): on mount, issues a SCSI Inquiry and prints the drive's vendor/product/revision strings and its capacity (block count, block size, total MB).
- HID (`CFG_TUH_HID`): receives reports and prints keyboard keystrokes as ASCII, mouse button state and cursor movement, and any other (generic) report as raw hex.
- Blinks the board LED once per second.
## Requirements
The board must support USB host mode (provide VBUS to the connected device); some boards need an external USB-A port / host adapter.
## Configuration
Notable `tusb_config.h` settings:
```c
#define CFG_TUH_ENABLED 1
#define CFG_TUH_HUB 1
#define CFG_TUH_CDC 1
#define CFG_TUH_CDC_FTDI 1
#define CFG_TUH_CDC_CP210X 1
#define CFG_TUH_CDC_CH34X 1
#define CFG_TUH_CDC_PL2303 1
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX)
#define CFG_TUH_MSC 1
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
#define CFG_TUH_HID_EPIN_BUFSIZE 64
#define CFG_TUH_HID_EPOUT_BUFSIZE 64
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM (CDC_CONTROL_LINE_STATE_DTR | CDC_CONTROL_LINE_STATE_RTS)
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
```
## Building
CMake:
```bash
mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .
```
Make:
```bash
make BOARD=raspberry_pi_pico all
```
## FreeRTOS variant
A FreeRTOS build is in `examples/host/cdc_msc_hid_freertos` — identical CDC/MSC/HID host behavior, running the host stack in a FreeRTOS task with a software-timer LED.
## How to use
Plug a USB keyboard, mouse, flash drive, or serial device into the board's USB host port (a USB hub also works, so several can be connected at once). Watch the debug UART: keystrokes and mouse movement appear as you use the input devices, an attached flash drive prints its inquiry/size info, and for a serial device you can type into the debug console to forward characters to it and see its output echoed back.

View File

@ -0,0 +1,45 @@
# Host: Device Info
A USB host example that enumerates any attached device and prints its descriptor information over the debug UART. No class drivers are enabled — only the hub driver, so it works with any kind of device.
## What it does
- On mount of any device, fetches and prints the full device descriptor: VID/PID, USB version, device class/subclass/protocol, max packet size, bcdDevice, and number of configurations.
- Reads and prints the manufacturer, product, and serial-number string descriptors (UTF-16 to UTF-8), falling back to a placeholder serial when none is present.
- Blinks the board LED, with a faster pattern while no device is mounted.
- Builds on either the bare main loop or a FreeRTOS task, depending on `CFG_TUSB_OS`.
## Requirements
The board must support USB host mode (provide VBUS to the connected device); some boards need an external USB-A port / host adapter.
## Configuration
Notable `tusb_config.h` settings:
```c
#define CFG_TUH_ENABLED 1
#define CFG_TUH_HUB 1
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
#define CFG_TUH_ENUMERATION_BUFSIZE 256
```
## Building
CMake:
```bash
mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .
```
Make:
```bash
make BOARD=raspberry_pi_pico all
```
## How to use
Plug any USB device into the board's USB host port. The debug UART prints the device's `ID vvvv:pppp`, serial number, and the decoded device descriptor (a hub lets you attach several devices, each printed in turn). Unplugging the device prints a removal message.

View File

@ -0,0 +1,48 @@
# Host: HID Controller
A USB host example that reads a USB game controller / gamepad (a HID device) and prints its inputs over the debug UART.
## What it does
- Enumerates HID devices (`CFG_TUH_HID`) and, on mount, prints the device's VID/PID.
- Decodes reports from explicitly supported controllers — Sony DualShock 4 and a few compatible PS4 pads (Hori FC4, Hori PS4 Mini, ASW GG xrd) — printing the joystick axes (x, y, z, rz), D-pad direction, and pressed buttons (Square/Cross/Circle/Triangle, L1/R1/L2/R2, Share/Option/L3/R3, PS, touchpad click). Output is only printed when the report changes meaningfully.
- Sends a periodic rumble output report back to the DualShock 4, with the motor intensities driven by the L2/R2 analog triggers.
- Blinks the board LED once per second.
Note: events are only shown for the explicitly supported controllers above; other HID devices enumerate but their reports are not decoded.
## Requirements
The board must support USB host mode (provide VBUS to the connected device); some boards need an external USB-A port / host adapter.
## Configuration
Notable `tusb_config.h` settings:
```c
#define CFG_TUH_ENABLED 1
#define CFG_TUH_HUB 0
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX)
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
#define CFG_TUH_HID_EP_BUFSIZE 64
```
## Building
CMake:
```bash
mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .
```
Make:
```bash
make BOARD=raspberry_pi_pico all
```
## How to use
Plug a supported USB game controller (e.g. a Sony DualShock 4) into the board's USB host port. Move the sticks and press buttons — the changes are printed on the debug UART, and squeezing the L2/R2 triggers makes the controller rumble.

View File

@ -0,0 +1,45 @@
# Host: MIDI 2.0
A minimal USB host example that receives MIDI from a connected USB-MIDI device and prints it over the debug UART, using the USB-MIDI 2.0 host driver (UMP).
## What it does
- Enumerates USB-MIDI devices (`CFG_TUH_MIDI2`) and, on mount, prints the negotiated protocol (MIDI 1.0 / MIDI 2.0) and the number of RX/TX cables.
- Receives Universal MIDI Packets (UMP) and prints each one, decoding common Channel Voice messages — Note On/Off, Control Change, Program Change, Channel Pressure, Pitch Bend — for both MIDI 2.0 and MIDI 1.0 message types, and falling back to a raw hex dump for anything else.
## Requirements
The board must support USB host mode (provide VBUS to the connected device); some boards need an external USB-A port / host adapter.
## Configuration
Notable `tusb_config.h` settings:
```c
#define CFG_TUH_ENABLED 1
#define CFG_TUH_HUB 1
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
#define CFG_TUH_MIDI2 CFG_TUH_DEVICE_MAX
#define CFG_TUH_MIDI2_RX_BUFSIZE 512
#define CFG_TUH_MIDI2_TX_BUFSIZE 512
```
## Building
CMake:
```bash
mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .
```
Make:
```bash
make BOARD=raspberry_pi_pico all
```
## How to use
Plug a USB-MIDI device (keyboard, controller, or interface) into the board's USB host port. On attach the debug UART prints the mount/descriptor info; then playing notes or moving controls on the device prints the decoded MIDI messages.

View File

@ -0,0 +1,44 @@
# Host: MIDI Receive
A USB host example that receives MIDI from a connected USB-MIDI device and prints the incoming bytes over the debug UART.
## What it does
- Enumerates USB-MIDI devices (`CFG_TUH_MIDI`) and, on mount, prints the interface index, device address, and the number of RX/TX cables.
- On each received MIDI packet, reads the stream and prints the cable number followed by the raw MIDI bytes as hex.
- Blinks the board LED once per second.
## Requirements
The board must support USB host mode (provide VBUS to the connected device); some boards need an external USB-A port / host adapter.
## Configuration
Notable `tusb_config.h` settings:
```c
#define CFG_TUH_ENABLED 1
#define CFG_TUH_HUB 1
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
#define CFG_TUH_MIDI CFG_TUH_DEVICE_MAX
```
## Building
CMake:
```bash
mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .
```
Make:
```bash
make BOARD=raspberry_pi_pico all
```
## How to use
Plug a USB-MIDI device (keyboard, controller, or interface) into the board's USB host port. On attach the debug UART prints the mount info; then playing notes or moving controls prints lines like `Cable 0 rx: 90 3C 7F` with the raw MIDI bytes.

View File

@ -27,16 +27,38 @@ CLI is presented over the board's serial console.
| mv | `mv <src> <dest>` | Rename/move a file or directory |
| rm | `rm <file>` | Remove a file |
## Configuration
Notable `tusb_config.h` settings:
```c
#define CFG_TUH_ENABLED 1
#define CFG_TUH_HUB 1
#define CFG_TUH_MSC 1
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
#define CFG_TUH_MSC_MAXLUN 4
```
## Build
Build for a specific board using CMake (see [Getting Started](https://docs.tinyusb.org/en/latest/getting_started.html)):
CMake:
```bash
# Example: build for Raspberry Pi Pico
cmake -B build -DBOARD=raspberry_pi_pico -DFAMILY=rp2040 examples/host/msc_file_explorer
cmake --build build
mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .
```
Make:
```bash
make BOARD=raspberry_pi_pico all
```
## FreeRTOS variant
A FreeRTOS build is in `examples/host/msc_file_explorer_freertos` — identical MSC FatFS file-explorer CLI, running the host stack and CLI as FreeRTOS tasks.
## Usage
1. Flash the firmware to your board.

View File

@ -1,105 +0,0 @@
# MSC File Explorer (FreeRTOS)
This host example implements an interactive command-line file browser for USB Mass Storage devices.
When a USB flash drive is connected, the device is automatically mounted using FatFS and a shell-like
CLI is presented over the board's serial console.
## Features
- Automatic mount/unmount of USB storage devices
- FAT12/16/32 filesystem support via FatFS
- Interactive CLI with command history
- Read speed benchmarking with `dd`
- Support for up to 4 simultaneous USB storage devices (via hub)
## Supported Commands
| Command | Usage | Description |
|---------|--------------------|------------------------------------------------------|
| help | `help` | Print list of available commands |
| cat | `cat <file>` | Print file contents to the console |
| cd | `cd <dir>` | Change current working directory |
| cp | `cp <src> <dest>` | Copy a file |
| dd | `dd [count]` | Read sectors and report speed (default 1024 sectors) |
| ls | `ls [dir]` | List directory contents |
| pwd | `pwd` | Print current working directory |
| mkdir | `mkdir <dir>` | Create a directory |
| mv | `mv <src> <dest>` | Rename/move a file or directory |
| rm | `rm <file>` | Remove a file |
## Build
Build for a specific board using CMake (see [Getting Started](https://docs.tinyusb.org/en/latest/getting_started.html)):
```bash
# Example: build for STM32F407 Discovery board
cmake -B build -DBOARD=stm32f407disco -GNinja examples/host/msc_file_explorer_freertos
cmake --build build
```
## Usage
1. Flash the firmware to your board.
2. Open a serial terminal (e.g. `minicom`, `screen`, `PuTTY`) at 115200 baud.
3. Plug a USB flash drive into the board's USB host port.
4. The device is auto-mounted and the prompt appears:
```
TinyUSB MSC File Explorer Example
Device connected
Vendor : Kingston
Product : DataTraveler 2.0
Rev : 1.0
Capacity: 1.9 GB
0:/> _
```
### Browsing Files
```
0:/> ls
----a 1234 readme.txt
d---- 0 photos
d---- 0 docs
0:/> cd photos
0:/photos> ls
----a 520432 vacation.jpg
----a 312088 family.png
0:/> cat readme.txt
Hello from USB drive!
```
### Copying and Moving Files
```
0:/> cp readme.txt backup.txt
0:/> mv backup.txt docs/backup.txt
```
### Measuring Read Speed
```
0:/> dd
Reading 1024 sectors...
Data speed: 823 KB/s
```
### Multiple Devices
When using a USB hub, multiple drives are mounted as `0:`, `1:`, etc. Use the drive prefix to
navigate between them:
```
0:/> cd 1:
1:/> ls
```
## Testing
Build-time validation follows the standard TinyUSB host example flow. Runtime behavior should be
verified on hardware by attaching an MSC device and exercising CLI commands such as `ls`, `pwd`,
and `dd`.