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 / application-examples

Application examples

The example applications are working guides for using TinyUSB on supported boards. They show how a USB scenario selects its device, host, dual-role, or Type-C behavior, how that behavior connects to board support, and how the build system produces firmware that can be deployed to the board.

Example goals

USB application patterns

The examples turn TinyUSB features into small, observable applications. Device examples present USB functions to a computer. Host examples discover and use attached USB devices. Dual-role examples use both sides of the link. Type-C examples control connection and power behavior.

GroupSystem behaviorStarting point
DeviceThe board appears as a USB device to a host computer.Device application examples
HostThe board enumerates and uses attached USB devices.Host, dual-role, and Type-C examples
Dual roleThe board uses device and host roles through role switching or simultaneous links.Host, dual-role, and Type-C examples
Type-CThe board manages Type-C connection state and Power Delivery.Host, dual-role, and Type-C examples

Choose an example as a starting point for a product behavior. Treat it as a focused guide rather than as a complete product design.

References: examples/CMakeLists.txt• L7-16 examples/…/CMakeLists.txt• L7-47 examples/…/CMakeLists.txt• L7-24 examples/…/CMakeLists.txt• L7-21 examples/…/CMakeLists.txt• L5-9

Configuration intent

Each application has a tusb_config.h file that states the USB contract for that scenario. It selects the active stack, the USB classes, the board port and speed defaults, and the resource limits needed by the application.

The configuration follows the role of the application:

  • Device examples enable CFG_TUD_* settings for functions such as CDC, MSC, HID, audio, video, MIDI, networking, DFU, MTP, printer, or USBTMC.
  • Host examples enable CFG_TUH_* settings for the attached classes and set limits for enumeration, devices, hubs, and transfers.
  • Dual-role examples enable both stacks and keep the device and host ports and speeds aligned.
  • Type-C examples enable the Type-C stack without enabling a device or host USB class stack.

The configuration is part of the application behavior, not only a build option. A class setting must agree with the descriptors and application callbacks. Board support supplies the hardware port and speed when the example does not override them. Some configurations also select FreeRTOS, memory placement, or larger buffers for audio, video, network, and throughput cases.

The dynamic-configuration device is a special case. It selects one of several complete USB profiles at run time, so its mode choice must agree with the device identity and descriptors returned to the host.

References: examples/…/tusb_config.h• L33-108 examples/…/tusb_config.h• L94-136 examples/…/tusb_config.h• L87-156 examples/…/tusb_config.h• L52-97 examples/…/tusb_config.h• L33-146 examples/…/tusb_config.h• L33-58 examples/…/tusb_config.h• L33-105

Board and build selection

Board selection

The BOARD choice connects an application to a physical board and its MCU family. The shared board support layer resolves the board name, selects the family support, and filters examples that do not match the board's USB hardware or resource needs.

Select the board before judging whether an example is supported. Family, MCU, board, and host-controller restrictions are part of the example contract. A board that supports one USB scenario may not support another.

Use board support and targets for the full board and target model. This page only needs the main rule: the board supplies hardware context, while the application supplies the USB scenario.

References: hw/…/family_support.cmake• L14-46 hw/…/family_support.cmake• L119-131 hw/…/family_support.cmake• L169-225

CMake builds

The CMake path starts with an example group and a selected board. The top-level project registers the device, host, dual-role, and Type-C groups. Group rules then include only examples that are valid for the selected family.

The shared build rules join the application with TinyUSB, board support, the selected operating-system support, compiler settings, and board-specific output or flash targets. An application therefore describes its USB behavior while the shared build layer supplies the board link and deployment details. The lwIP web server and Espressif FreeRTOS examples add their own dependency or operating-system settings to this common path.

This diagram shows how a scenario and board become a deployable firmware image.

flowchart TD
    A[Choose USB scenario] --> B[Set BOARD]
    B --> C[Resolve board family]
    C --> D[Filter supported examples]
    D --> E[Apply USB role and class configuration]
    E --> F[Add TinyUSB and board support]
    F --> G[Build firmware]
    G --> H[Flash or deploy to board]

References: examples/CMakeLists.txt• L1-16 examples/…/CMakeLists.txt• L1-36 examples/…/CMakeLists.txt• L6-24 examples/…/CMakeLists.txt• L1-30 hw/…/family_support.cmake• L400-513

Make builds

The Make path uses the same board-driven model with a smaller per-example description. An example provides its source and include paths. Shared board rules then derive the family and add the TinyUSB, board, toolchain, and optional RTOS parts.

Use Make when an existing workflow is based on make or when a small source list is easier to inspect there. The network example shows that extra services such as lwIP remain an explicit dependency in this path.

References: examples/…/Makefile• L1-15 examples/…/Makefile• L1-14 examples/…/Makefile• L1-67 hw/…/family_support.mk• L8-34 hw/…/family_support.mk• L64-101

Deployment patterns

Device applications

Device applications make the board visible to a USB host. They cover single-class and composite products, data transfer, storage, input, audio, video, networking, firmware update requests, media transfer, printer links, test and measurement, and controller validation.

These examples also show why a USB profile is more than a class name. The descriptors define what the host sees, while the application supplies the data or control behavior. Some examples use generated data such as audio samples, MIDI notes, or video color bars. Others connect USB to storage, lwIP services, or command handling. The device examples use both non-RTOS and FreeRTOS forms where task scheduling is part of the scenario.

See Device application examples for the device class and flow map.

References: examples/…/CMakeLists.txt• L9-43 examples/…/tusb_config.h• L33-115 examples/…/usb_descriptors.c• L29-35 examples/…/tusb_config.h• L87-156 examples/…/tusb_config.h• L63-75 examples/…/tusb_config.h• L86-140

Host applications

Host applications make the board control an attached USB device. They demonstrate a common host pattern: detect a device, learn its supported interfaces and formats, open the needed class path, and then move application data.

The examples cover inspection, audio, HID input, MIDI, serial links, mass storage, and composite devices. The bare API example also shows a lower-level host pattern: it reads descriptors, opens matching HID input paths, and keeps report transfers active. Its application work stays outside host callbacks so descriptor reads and report handling have a clear execution boundary. Their configurations state how many devices and hubs the application can handle and which class drivers it can use. FreeRTOS variants keep the same USB purpose while moving application work into scheduled tasks.

See Host, dual-role, and Type-C examples for the host scenarios and their attached-device flows.

References: examples/…/tusb_config.h• L52-97 examples/…/tusb_config.h• L66-110 examples/…/tusb_config.h• L61-112 examples/…/main.c• L72-126 examples/…/main.c• L129-173 examples/…/main.c• L186-218 examples/…/main.c• L253-327 examples/…/tusb_config.h• L94-129 examples/…/tusb_config.h• L94-111 examples/…/tusb_config.h• L56-96

Dual-role applications

Dual-role applications use the board as both a USB device and a USB host. One pattern switches roles at run time. Another keeps both links active and passes information from an attached host-side device to a device-side computer connection.

Role choice affects the active stack, port, speed, and application callbacks. The dynamic switch example marks the old role inactive, stops and deinitializes its stack, then starts the other role. This boundary keeps device and host tasks from using a stack during the transition. Simultaneous examples must keep the two port assignments distinct. RP2040 scenarios can also require Pico-PIO-USB support for the host side.

References: examples/…/tusb_config.h• L33-146 examples/…/main.c• L203-257 examples/…/main.c• L266-339 examples/…/tusb_config.h• L33-135 examples/…/tusb_config.h• L33-131 examples/…/CMakeLists.txt• L7-20

Type-C applications

Type-C applications demonstrate connection control and USB Power Delivery negotiation without presenting a USB device or host class. The power-delivery example lets the user control the port, reads source capabilities, and requests a power profile within the board's safe limits.

Type-C deployment is a distinct pattern because connection and power state are the product behavior. Espressif Type-C examples use the Espressif build path rather than the common family path.

References: examples/…/tusb_config.h• L33-58 examples/…/main.c• L37-152 examples/…/CMakeLists.txt• L7-29

Application workflow

Build and deploy sequence

An engineer normally moves from a USB scenario to a board-specific image in a short sequence. The scenario selects the behavior. The board supplies the hardware context. The build system produces the image and the board-specific deployment targets.

  1. Choose a device, host, dual-role, or Type-C scenario.
  2. Choose a board supported by the example and its USB hardware path.
  3. Configure with CMake or invoke the example's Make path with BOARD=<board>.
  4. Build the application and inspect the available board-specific flash targets.
  5. Flash or deploy the image.
  6. Connect the USB peer required by the scenario and check the expected host, device, or Type-C behavior.

CMake presets provide repeatable board names. Most boards use the multi-config preset. Espressif boards use the single-config preset. The exact flash command belongs to the selected board family, not to the USB scenario.

References: examples/…/README.md• L35-49 examples/…/README.md• L34-76 examples/…/README.md• L47-65 hw/…/BoardPresets.json• L3-24 hw/…/family_support.cmake• L538-761

Change checklist

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

  • Keep the top-level example groups aligned with the child-page links and family filters.
  • When a role or class changes, update the matching tusb_config.h, descriptors, and application behavior as one contract.
  • When board, MCU, or host-controller limits change, update the example's target filters and the related configuration limits.
  • When a FreeRTOS configuration changes, keep its OS selection, scheduler assumptions, memory defaults, and USB transfer timing aligned.
  • When a device profile changes, keep its mode choice aligned with its identity, configuration descriptors, speed descriptors, and host re-enumeration behavior.
  • When a host configuration changes, keep its enabled classes, device and hub limits, port, speed, and transfer buffers aligned with the attached-device flow.
  • When a host application changes its callback or transfer flow, keep descriptor work outside host callbacks and keep buffer ownership valid until each transfer completes.
  • When a dual-role configuration changes, keep both role ports, speeds, stack state, and role lifecycle aligned. Keep application tasks from calling the inactive stack during a role transition.
  • When a Type-C power limit changes, review the requested profile against the board's safe voltage and current limits.
  • When a build path changes, preserve the board-specific image and flash target names used by deployment workflows.

Important failure modes

TriggerConsequenceGuard
BOARD is missing or unsupportedThe family cannot be resolved, or the example is filtered out before build.Select a board from the board support and preset lists.
An example's target filter does not match its hardware or resource needsThe build can fail, or the image can run without the required USB path.Check the example's only.txt and skip.txt rules before choosing a board.
Stack or class settings do not match descriptors and callbacksThe host can reject enumeration or expose the wrong interfaces.Treat tusb_config.h, descriptors, and class behavior as one contract.
A host configuration enables too few devices, hubs, buffers, or endpointsAn attached device is not opened, or transfers stop after enumeration.Match host limits to the supported attached-device flow.
Host callbacks perform synchronous descriptor workThe host callback can block or use a helper from an unsafe execution context.Defer descriptor reads and other synchronous work to the main loop or a task.
The host HID buffer pool has no free bufferA matching HID interface is not opened, or its reports do not reach the application.Size the pool for the number of concurrent HID input paths.
A dual-role build uses the wrong port, or a task uses the old stack during a switchOne side does not enumerate, or a task accesses a deinitialized stack.Mark the role inactive, stop and deinitialize it, then start the new role.
An Espressif example uses the common family build pathThe expected target or deployment flow is not created.Use the build path defined for the selected family.
A Type-C request exceeds the board's safe power limitsPower negotiation can damage the board.Keep the requested voltage and current within tested limits.
A dynamic profile returns mixed-mode descriptorsThe host can retain the old driver match or enumerate the wrong classes.Use one mode decision for the device identity and all descriptors.

Key files

FileWhy to read it
examples/CMakeLists.txtShows the four top-level application groups.
examples/…/CMakeLists.txtShows device scenario registration and family filtering.
examples/…/CMakeLists.txtShows host scenario entry points.
examples/…/CMakeLists.txtShows dual-role registration and host-support gating.
examples/…/CMakeLists.txtShows the Type-C application entry point.
hw/bsp/family_support.cmakeDefines board resolution, filtering, common target setup, and deployment targets.
hw/bsp/family_support.mkDefines the equivalent board and source flow for Make builds.
hw/…/BoardPresets.jsonMaps board names to repeatable CMake configure and build presets.
examples/…/tusb_config.hShows a basic composite device configuration.
examples/…/usb_descriptors.cShows run-time selection between complete device profiles.
examples/…/tusb_config.hShows a board-sensitive USB network configuration.
examples/…/tusb_config.hShows a validation device with bulk, interrupt, and isochronous paths.
examples/…/tusb_config.hShows host class, protocol, port, speed, and transfer limits.
examples/…/tusb_config.hShows a composite host configuration.
examples/…/tusb_config.hShows the host storage limits used by a file-system application.
examples/…/main.cShows descriptor-led host discovery and continuous HID input handling.
examples/…/tusb_config.hShows both stacks and the shared role configuration.
examples/…/main.cShows the role transition boundary and the device and host application paths.
examples/…/tusb_config.hShows Type-C enabled without a device or host class stack.