mirror of
https://github.com/hathach/tinyusb.git
synced 2026-08-18 11:02:16 +00:00
- src/tusb.c: extend tu_str_xfer_result[] with "ABORTED" and "INVALID" to match the new enum size. Not reachable today (no HCD posts those values through hcd_event_xfer_complete), but keeps the enum/table invariant intact so future HCDs that surface ABORTED don't index OOB. - examples/dual/dynamic_switch/src/main.c: apply the same while(1) hoist already done for cdc_task / print_devinfo_task to led_blinking_task. On OS_NONE the loop returned mid-iteration, which on first call could fire multiple back-to-back toggles while start_ms (initially 0) caught up to uptime. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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
Usage
- Build and flash the example to your board
- Default behavior: The board starts in Device mode
- Device mode:
- Connect the board to a PC
- Open a serial terminal (e.g.,
screen /dev/ttyACM0or PuTTY) - Type characters - they will be echoed back to you
- 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
- 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
...