Files
tinyusb/examples/device/dynamic_configuration
hathach 4b1c8d16f7 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>
2026-06-29 10:16:25 +07:00
..
2025-02-09 00:25:38 +01:00
2025-10-14 17:53:55 +07:00
2024-06-21 18:50:44 +07:00

Dynamic Configuration

Demonstrates returning different device and configuration descriptors at enumeration time, selected by the on-board button. The button is sampled when the host requests the device descriptor, so the device enumerates as one of two completely different personalities depending on whether the button is held while plugging in.

What it does

  • Reads the on-board button when the host fetches the device descriptor to choose the active configuration.
  • Button not pressed — enumerates as CDC + MIDI:
    • CDC echoes back received data (and appends a newline after each carriage return); prints a banner when the terminal connects.
    • MIDI continuously plays a fixed note sequence, and drains/discards any incoming MIDI.
  • Button pressed — enumerates as MSC: presents a small read/write FAT12 RAM disk containing a README.TXT file.
  • Blinks the on-board LED to reflect bus state: 250 ms unmounted, 1000 ms mounted, 2500 ms suspended.

USB Descriptors

Two alternate configurations are served, chosen by the button at enumeration.

Button not pressed (CDC + MIDI):

Interface Class driver
01 CDC (virtual serial)
23 MIDI

Button pressed (MSC):

Interface Class driver
0 MSC (mass storage)

Configuration

Notable tusb_config.h settings (both personalities share one config):

#define CFG_TUD_CDC              1
#define CFG_TUD_MSC              1
#define CFG_TUD_MIDI             1
#define CFG_TUD_CDC_RX_BUFSIZE   (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_TX_BUFSIZE   (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_MIDI_RX_BUFSIZE  (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_MIDI_TX_BUFSIZE  (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_MSC_EP_BUFSIZE   512

Building

CMake:

mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .

Make:

make BOARD=raspberry_pi_pico all

Try it

Plug the board in normally: a serial port and a MIDI device appear on the host. Now hold the on-board button while plugging in (or while resetting): the device instead enumerates as a removable drive with a README.TXT file. The two personalities use different product IDs so the host treats them as distinct devices.