Introducing a new way to review your PRs.

Read the announcement

hathach / tinyusb

An open source cross-platform USB stack for embedded system

View on GitHub

18 documents · Updated Nov 23, 2026

docs / host-dual-and-typec-examples

Host, dual-role, and Type-C examples

These examples show how an application uses TinyUSB for three related USB roles: a host that controls attached devices, a dual-role system that connects host and device behavior, and a Type-C system that negotiates power. They are small product patterns rather than one end-to-end application. For the wider example set, see Application examples, and for device-side patterns see Device application examples.

Example map

The examples map a user task to one or more TinyUSB stacks. A host example discovers and serves an external USB device. A dual-role example joins host-side input or descriptor flow to device-side output. A Type-C example manages connection and power without replacing the USB host or device data stack.

ScenarioStack boundaryIntegration pattern
Host inspectionUSB hostDiscover a device and expose its descriptors as application data
Host class useUSB host plus a class driverTurn device events into storage, input, serial, audio, or MIDI behavior
Host-to-device bridgeUSB host plus USB deviceConsume an external device on one port and publish the result through another
Runtime role switchUSB host or USB deviceStop one role and start the other on the same root-hub port
Type-C power negotiationType-C and Power DeliveryInspect source power options and request a safe operating point

Host examples

The host examples start with the same lifecycle: the board provides host power, detects an attached device, enumerates it, and reports a mount or removal event. Each example then adds the class behavior needed for its product task. The USB host stack page describes the shared host model.

Device discovery

The device information and bare API examples show the host boundary before a class driver is added. The device-information flow saves enumeration data and defers descriptor reads and reporting to application context. The bare API variant now also keeps descriptor inspection and HID input handling in that application flow. It reads configuration descriptors, opens HID input endpoints, and continuously reports raw HID data. These patterns support bring-up, device qualification, and support tools because they keep device-specific work outside the first connection callback.

References: examples/…/main.c• L66-82 examples/…/main.c• L121-152 examples/…/main.c• L204-220 examples/…/tusb_config.h• L66-110 examples/…/main.c• L67-113 examples/…/main.c• L120-174 examples/…/main.c• L186-218 examples/…/main.c• L251-327 examples/…/main.c• L333-351 examples/…/tusb_config.h• L61-112

USB class applications

The class examples turn mount events into useful work after enumeration. The combined CDC, MSC, and HID example reports serial, storage, and input activity. Its HID path handles boot keyboard and mouse reports and can also map generic reports by their parsed usage and report ID. The Mass Storage Class path starts an inquiry after a device mounts, then reports vendor data and disk capacity when the inquiry completes. The FreeRTOS variant keeps the same product behavior while placing the inquiry response in USB/DMA-capable memory. This shows that the application contract stays stable while memory and scheduling needs can differ.

The file explorer adds a file-system and command interface over MSC. It mounts a usable volume after inquiry completes, connects disk reads and writes to asynchronous host transfers, and exposes commands for browsing and file operations. Its FreeRTOS variant defers the file-system mount to an application task after the host callback marks it pending. This keeps file-system work in the task context while the host stack continues to own USB completion events.

The audio host example adds a continuous stream path. It discovers capture and playback streams, chooses a supported PCM format, sample rate, and channel count, then runs capture, playback, or echo phases. Its FIFO boundary separates application audio work from isochronous USB transfers, while the host configuration enables both UAC1 and UAC2 with bounded transfer buffers. This pattern requires steady host service and enough buffering when audio moves in either direction.

Example familyProduct behaviorIntegration lesson
CDC/MSC/HIDSerial bridge, drive information, and keyboard or mouse reportsSeveral class drivers can share one host and one user-facing monitor
MSC file explorerMount, browse, copy, move, and measure USB storageA class driver can feed a higher-level file system and command interface
MIDI and MIDI 2.0Receive raw or decoded musical messagesThe application can choose raw transport data or decoded events
HID controllerDecode controller state and send rumble outputHost traffic can flow in both directions
USB AudioDiscover streams, control volume, capture, play, and echo audioStreaming work needs a supported format and steady host service and buffering

References: examples/…/README.md• L1-14 examples/…/hid_app.c• L54-112 examples/…/hid_app.c• L184-246 examples/…/msc_app.c• L32-67 examples/…/tusb_config.h• L94-129 examples/…/hid_app.c• L60-119 examples/…/msc_app.c• L31-73 examples/…/tusb_config.h• L99-131 examples/…/README.md• L1-13 examples/…/msc_app.c• L77-105 examples/…/msc_app.c• L111-163 examples/…/tusb_config.h• L94-111 examples/…/msc_app.c• L102-177 examples/…/msc_app.c• L183-235 examples/…/msc_app.c• L253-316 examples/…/tusb_config.h• L99-116 examples/…/README.md• L1-13 examples/…/main.c• L44-60 examples/…/main.c• L91-118 examples/…/tusb_config.h• L98-109 examples/…/README.md• L1-13 examples/…/tusb_config.h• L84-96 examples/…/README.md• L1-16 examples/…/hid_app.c• L164-232 examples/…/tusb_config.h• L94-112 examples/…/README.md• L1-13 examples/…/tusb_config.h• L74-97

Host target coverage

Each host or dual example has a support manifest that names the families, MCUs, or boards selected for that scenario. The manifests are not identical. The audio host manifest is limited to targets with the needed isochronous host path; for example, some LPC55 targets require the host port that supports ISO transfers. FreeRTOS variants also use their own target sets. Board-level skip files further limit where a scenario is selected, including boards that cannot fit the multi-class or storage examples within their available resources. Treat this coverage as a target-selection boundary for the example rather than as a claim that every host class works the same way on every board.

References: examples/…/only.txt• L1-39 examples/…/only.txt• L1-38 examples/…/only.txt• L1-39 examples/…/only.txt• L1-29 examples/…/only.txt• L1-40 examples/…/only.txt• L1-36 examples/…/only.txt• L1-40 examples/…/only.txt• L1-38 examples/…/only.txt• L1-29 examples/…/only.txt• L1-11 examples/…/only.txt• L1-13 examples/…/only.txt• L1-14 examples/…/skip.txt• L1 examples/…/skip.txt• L1-5 examples/…/skip.txt• L1-5 examples/…/skip.txt• L1-6 examples/…/skip.txt• L1-5 examples/…/skip.txt• L1-5 examples/…/skip.txt• L1-5 examples/…/skip.txt• L1-7 examples/…/skip.txt• L1-7

Dual-role examples

Dual-role means that a USB root-hub port can operate as a host or as a device. Concurrent bridge examples use two usable USB ports so the board can consume data from one side and present a stable interface to a PC on the other side. The USB Type-C and Power Delivery page covers a different concern: electrical role and power negotiation are not the same as USB data role.

Concurrent host and device bridge

The host-information bridge collects descriptors from a device on its host port and prints them through a CDC interface on its device port. A related HID bridge receives keyboard and mouse reports on the host side and translates them into text or event lines on the device-side CDC interface. The host side records or processes input after enumeration, while the device-side CDC service publishes the result to a PC. The same application can run the two stack lifecycles in separate RTOS tasks or service both stacks in one loop. This pattern shows how an application joins host events to a PC-facing device service without merging the two stack roles.

The data path is summarized below.

flowchart LR
    A[External USB device] --> B[Host port and host stack]
    B --> C[Application bridge]
    C --> D[Device stack and CDC interface]
    D --> E[PC serial terminal]

The board needs one host connection for the external device and one device connection for the PC. A board with only one usable port cannot use this concurrent pattern without a different product design.

References: examples/…/README.md• L1-9 examples/…/README.md• L32-35 examples/…/main.c• L72-84 examples/…/main.c• L107-125 examples/…/main.c• L130-181 examples/…/main.c• L213-235 examples/…/main.c• L291-307 examples/…/tusb_config.h• L37-85 examples/…/usb_descriptors.c• L29-60 examples/…/README.md• L1-9 examples/…/main.c• L75-100 examples/…/main.c• L145-176 examples/…/main.c• L198-275 examples/…/tusb_config.h• L37-85 examples/…/usb_descriptors.c• L29-60

Runtime role switching

The dynamic-switch example uses one port and changes its role when the user presses a board button. Device mode provides a CDC echo service. Host mode enumerates attached devices and reports their information. The handover is controlled: the current role becomes inactive before its stack stops, and the new role starts only after the old role has stopped.

The handover is coordinated by the execution model. In a direct main loop, the button check performs the switch and the loop runs only the active role. With FreeRTOS, the button task asks the USB task to stop its current role. The USB task then performs deinitialization and starts the other role in the same task context. If initialization of the new role fails, the example keeps the role invalid rather than using an unstarted stack.

The role transition follows this sequence:

  1. Detect a button press and request a role stop.
  2. Mark the current role inactive so other application work leaves the old-role path.
  3. Stop and deinitialize the current USB stack.
  4. Start the other role on the same port.
  5. Process only the task and callbacks for the new role.

This pattern trades simultaneous connections for a smaller hardware footprint and a user-controlled mode change.

References: examples/…/README.md• L1-11 examples/…/main.c• L125-190 examples/…/main.c• L200-259 examples/…/main.c• L266-316 examples/…/main.c• L322-344 examples/…/main.c• L379-426 examples/…/tusb_config.h• L37-54 examples/…/tusb_config.h• L82-146 examples/…/usb_descriptors.c• L29-57 examples/…/usb_descriptors.c• L96-127

Type-C and Power Delivery examples

The Power Delivery example focuses on Type-C connection state and power selection. It does not enumerate USB data devices. Instead, it connects or disconnects the Type-C port, reads source capabilities, and requests a fixed power option within the board's declared voltage and current limits. The USB Type-C and Power Delivery page describes this power and connection boundary.

Power negotiation

A Power Data Object (PDO) is one power option advertised by a source. When the source sends its PDO list, the example checks fixed-voltage options and selects one within 5 V and 500 mA. It requests 300 mA for normal operation. The source then reports whether it accepted the request and when the new power level is ready.

The Type-C and Power Delivery flow is summarized below.

sequenceDiagram
    participant App as Application
    participant Port as Type-C port
    participant Source as Power source
    App->>Port: Connect port
    Port->>Source: Receive source capabilities
    Source-->>App: Fixed PDO list
    App->>Source: Request safe PDO and operating current
    Source-->>App: Accept or reject
    Source-->>App: Power ready

The selected voltage and current must match the board hardware. A source with no acceptable PDO does not produce a power request. See Board support and targets when checking whether a target has the required Type-C port and power path.

References: examples/…/main.c• L37-71 examples/…/main.c• L84-160 examples/…/main.c• L163-185 examples/…/tusb_config.h• L37-58

Integration boundaries

The examples separate three kinds of responsibility. USB host and device stacks move data on a USB bus. The application maps those transfers to a user task. The Type-C stack decides connection and power state. It can support USB data operation but does not replace the host or device stack.

ConcernPrimary example groupApplication responsibility
External device controlHostHandle discovery, class events, transfers, and removal
PC-facing serviceDevice side of dual bridgesPresent a stable interface and publish bridge output
Port role choiceDynamic switchMake role changes explicit and isolate old-role work
Power contractType-C and Power DeliverySelect only power that the hardware can safely use

These boundaries help select an example. Start with a host example when the board must control a peripheral. Start with a dual bridge when the board must translate peripheral data for a PC. Start with the Type-C example when the main question is power negotiation or connection state.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep host mount callbacks small. Defer synchronous descriptor reads and other work that depends on application context.
  • Keep descriptor and HID transfer buffers in memory that the host controller can use. Release device-owned buffers when the device is removed.
  • Keep the Mass Storage inquiry response storage suitable for the USB transfer and DMA rules in both bare-loop and FreeRTOS variants.
  • Keep file-system mount work aligned with the execution context. Host callbacks should not bypass the task boundary used by the scheduler-based explorer.
  • Keep the host class configuration aligned with the application service. A device can enumerate without the class behavior that the application expects.
  • Keep audio stream selection, volume control, FIFO watermarks, and transfer buffer limits aligned with the device formats and target memory.
  • Keep target manifests and skip rules aligned with the resources and USB features required by each example.
  • Keep both sides of a dual bridge aligned. The host input format must match the device-side CDC output contract.
  • Preserve the runtime switch boundary. Mark the old role inactive and stop its stack before callbacks or application tasks use the new role.
  • Recheck Type-C PDO limits against the board power path before changing voltage or current values.
  • Keep Type-C connection state separate from USB data-role state. Power Delivery alone does not enable host or device data transfer.

Important failure modes

TriggerConsequenceGuard
A host callback performs synchronous descriptor reads or other long application workThe host event path can block or use an unsafe execution contextSet a pending flag or save event data, then process it in the application task
A descriptor or HID buffer is not in memory usable by the host transfer pathEnumeration or repeated HID input can fail on targets with strict USB or DMA rulesUse the target's required USB memory section and free buffers on unmount
The Mass Storage inquiry response is not in memory usable by the transfer pathInquiry data can be invalid or unavailable on targets with strict USB/DMA memory rulesKeep the response buffer in the required USB memory section
File-system mounting runs in the host callback when the scheduler variant expects task contextStorage state can cross execution boundaries and make the explorer unreliableMark the mount pending and process it in the application task
A host class is not enabled for the application taskThe device may mount, but the expected storage, input, serial, audio, or MIDI behavior is absentTreat host configuration and class behavior as one contract
An audio device has no supported stream format or matching playback rateThe example can capture only or fail to start the requested streamSelect from the device's reported configurations and accept capture-only operation when no match exists
A target is selected even though its memory or USB support cannot meet the example's needsThe example can fail to build or cannot provide its promised class behaviorUse the example's support and skip rules as the target boundary
A concurrent bridge has only one usable USB portEither the external device or the PC-facing CDC link is unavailableUse two independent USB connections for the bridge pattern
A role switch leaves old-role work active or the new role fails to startStale callbacks can use a stopped stack or later work can call an uninitialized stackMark the role inactive before deinitialization and keep it invalid until initialization succeeds
The source advertises no PDO within the allowed limitsThe example skips the power requestKeep a safe voltage ceiling and handle the no-match case
The requested power exceeds the board's ratingThe board can be damagedSet limits from the real board power path, not from the source capability list

Key files

FileWhy to read it
examples/…/README.mdSmallest example of host enumeration and descriptor reporting
examples/…/main.cShows how host events hand descriptor work to the application task
examples/…/tusb_config.hDefines the host-only discovery boundary
examples/…/main.cShows direct descriptor parsing, HID endpoint setup, and repeating HID input transfer
examples/…/tusb_config.hShows the low-level endpoint-transfer API boundary
examples/…/hid_app.cShows boot and generic HID report handling in a multi-class host application
examples/…/msc_app.cShows a host class callback that turns Mass Storage inquiry data into device information
examples/…/tusb_config.hShows how one host enables several class services
examples/…/msc_app.cShows the scheduler variant's USB/DMA-capable response storage
examples/…/msc_app.cShows how host storage becomes a file and command interface in a direct loop
examples/…/skip.txtShows board limits for the storage explorer example
examples/…/msc_app.cShows task-owned mount work and scheduler-based storage access
examples/…/README.mdDefines audio stream discovery, format choice, controls, and stream phases
examples/…/tusb_config.hShows the audio host class and transfer buffer contract
examples/…/only.txtShows the target boundary for the isochronous audio host example
examples/…/only.txtShows which families, MCUs, or boards each host scenario targets
examples/…/README.mdDefines the concurrent host-to-device bridge shape and port needs
examples/…/main.cShows separate host and device initialization and the bridge event flow
examples/…/tusb_config.hShows the two-stack configuration for the bridge
examples/…/README.mdShows input translation from a host HID device to a PC-facing CDC link
examples/…/main.cShows concurrent HID input handling and CDC publication
examples/…/README.mdDefines the user-controlled host/device role handover
examples/…/main.cShows the role boundary, task handoff, and safe switch order
examples/…/tusb_config.hShows host and device stacks sharing one root-hub port
examples/…/main.cShows Type-C connection handling and Power Delivery selection
examples/…/tusb_config.hShows that the Type-C example is separate from host and device data stacks