Files
tinyusb/examples/dual/dynamic_switch
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
..
2026-02-27 11:46:54 +01:00
2026-02-27 11:46:54 +01:00
2026-02-27 11:46:54 +01:00
2026-05-10 14:17:00 +02:00

Dynamic Switch Example

This example demonstrates TinyUSB's dual-role capability by allowing runtime switching between USB device and host modes.

Features

  • Button-triggered mode switching: Press the board button to switch between device and host modes
  • Device Mode: Acts as a USB CDC (Virtual Serial Port) that echoes all received data
  • Host Mode: Enumerates connected USB devices and prints device information
  • Dynamic switching: Deinitializes the current stack and reinitializes in the new mode

Configuration

Notable tusb_config.h settings:

#define CFG_TUD_CDC 1
#define CFG_TUH_HUB 1
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1)
#define CFG_TUH_ENUMERATION_BUFSIZE 256

Usage

  1. Build and flash the example to your board
  2. Default behavior: The board starts in Device mode
  3. Device mode:
    • Connect the board to a PC
    • Open a serial terminal (e.g., screen /dev/ttyACM0 or PuTTY)
    • Type characters - they will be echoed back to you
  4. Switch to Host mode:
    • Press the board button
    • Connect a USB device to the board
    • The board will enumerate the device and print its descriptors to the debug console
  5. Switch back to Device mode: Press the button again

LED Patterns

The onboard LED indicates the USB connection status:

  • Fast blink (250ms): Not mounted/connected
  • Slow blink (1000ms): Successfully mounted/connected
  • Very slow blink (2500ms): Suspended (device mode only)

Serial Output

The example prints status messages to the debug UART:

======================================
TinyUSB Dynamic Switch Example
Press button to switch between device and host modes
Starting in DEVICE mode...
======================================

[DEVICE] Mounted

--- Switching USB mode ---
Stopping DEVICE mode...
Starting HOST mode...
Mode switch complete!

[HOST] Device attached, address = 1
Device 1: ID 1234:5678 SN ABC123
Device Descriptor:
  bLength             18
  bDescriptorType     1
  bcdUSB              0200
  bDeviceClass        239
  ...