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 / platform-integration

Platform integration

TinyUSB keeps USB behavior portable while adapting it to an MCU, a board, a build family, and a runtime. Platform integration makes these choices agree so USB work can start, receive hardware events, and run with or without an operating system.

Integration boundary

The integration boundary connects product firmware to TinyUSB. Board startup makes the USB hardware usable. TinyUSB then initializes a root-hub port (RHPort), routes controller events, and presents device or host behavior to the application.

ChoicePurpose
MCU and USB IPSelects the controller family and its limits
Board supportConnects clocks, pins, power, and board services to USB
Build-family dataConnects a board name to its MCU, sources, options, roles, and build metadata
OS abstractionGives USB tasks a common way to use time and synchronization

The flow turns these choices into USB events that the portable stack can process.

flowchart LR
    A[Board and family selection] --> B[MCU and controller setup]
    A --> C[Board startup and pins]
    B --> D[Controller driver]
    C --> E[RHPort initialization]
    D --> E
    E --> F[USB interrupt]
    F --> G[Device or host event path]
    G --> H[USB task and application]
    I[OS primitives] --> H

References: docs/integration.rst• L7-22 src/tusb.c• L65-145 src/tusb.h• L134-160

Controller adaptation

Controller adaptation maps an MCU USB peripheral or a software USB engine to TinyUSB's common device and host contracts. A device controller driver (DCD) serves a board as a USB peripheral. A host controller driver (HCD) serves it when it controls a USB bus. See USB controller drivers for the controller contracts and porting boundary.

Controller drivers absorb the limits, memory rules, timing rules, and event rules of each USB implementation. Their internal transfer engines can differ while their boundary stays stable: device paths report reset, setup, bus state, and transfer events; host paths report attach, remove, port state, and transfer results. The WCH family now separates USBFS, USBHS v1, and USBHS v2 controller paths because these hardware blocks have different register and transfer models. The Renesas RUSB2 path uses the same controller boundary across Synergy and RA USBFS or USBHS parts. The RA variant also records whether the MCU has one controller or a high-speed pair. ST FSDEV and Synopsys DWC2 keep packet memory, DMA, and endpoint rules inside their controller paths. A MUSB host path maps controller pipes and FIFO resources to the common HCD events.

Device and host roles

Device and host roles share initialization and interrupt routing at the platform boundary, but they have different bus duties. Device mode answers a host and exposes endpoints. Host mode detects devices and schedules transfers.

An RHPort receives its role during initialization. The role controls controller setup and the events sent upward. Family metadata records device, host, or dual support only when the matching controller paths are available. On Renesas RA, the available RHPorts and high-speed mode also depend on the MCU group and the board's USB reference clock.

The controller-specific work stays below the common event boundary. Host paths report attach, remove, and transfer results after they manage root-port state, endpoint resources, retries, or frame timing. Device paths report reset, setup, bus state, and transfer events after they manage endpoint responses and packet movement.

References: src/…/dcd.h• L99-157 src/…/hcd.h• L91-153 src/tusb.c• L93-145 src/…/hcd_musb.c• L542-610 src/…/rusb2_synergy.h• L54-91 src/…/rusb2_ra.h• L41-84 src/…/dcd_rusb2.c• L855-907 src/…/dcd_stm32_fsdev.c• L160-181 src/…/dcd_dwc2.c• L615-677 src/…/hcd_wch_usbfs.c• L628-722 src/…/hcd_wch_usbhs_v1.c• L753-878 src/…/dcd_wch_usbhs_v2.c• L245-300

Board startup

The board support package (BSP) prepares a development board for examples and hardware checks. It starts the MCU, configures USB clocks and pins, selects the PHY mode, handles board power, and exposes small services such as an LED, button, UART, and board identity.

Board startup runs before TinyUSB initialization. The board interrupt forwards controller events to TinyUSB. It does not implement USB protocol behavior. An embedded product can replace the BSP with its own startup and power code while keeping the same TinyUSB boundary. See Board support and build targets for board layout and target selection.

The WCH family ports show why startup is part of the USB contract. CH32V30x can route full-speed and high-speed controllers to separate RHPorts. CH583 starts USBFS as a device on RHPort 0 and enables the matching analog and pull-up signals. These choices are not interchangeable with the controller driver alone.

Renesas Synergy and RA startup use vendor vector and pin setup. Their startup can set interrupt priorities for FreeRTOS, provide a millisecond source for no-OS mode, and enable board-specific USB power after TinyUSB starts. Full-speed and high-speed vectors map to the RHPorts selected by the family rules.

The STM32H7S3 Nucleo startup also supports a split boot path. A small internal-flash boot section maps the external XSPI flash and copies the main image to RAM before normal board setup. The board then controls USB power through the TCPP0203 power switch.

Startup responsibilities

Board startup connects physical signals and system time to the stack. The common board API gives examples one interface so they can run across boards without knowing each pin map.

Startup areaSystem result
Clock and USB pinsThe controller can meet USB timing and reach the bus
PHY and USB powerThe selected role matches the board wiring
Interrupt routingHardware events enter the TinyUSB event path
Time sourceDelays and time-based work can progress
Board servicesExamples can report state without board-specific code
Memory descriptionThe image uses the board's flash and RAM layout

References: hw/…/board_api.h• L71-103 hw/…/family.c• L56-87 hw/…/family.c• L93-167 hw/…/board.h• L43-96 hw/…/family.c• L84-97 hw/…/family.c• L309-452 hw/…/xspi_boot.c• L27-38 hw/…/family.c• L55-109

Build-family metadata

Build-family metadata turns a board name into a complete supported target. The family layer joins the MCU choice, toolchain, startup inputs, board target, controller sources, options, and supported example roles.

Family rules provide the shared contract for a group of boards. Board rules provide the part and physical differences that the family cannot infer. The target must bind these choices to the same RHPort, speed, controller role, linker image, boot layout, and flash path.

The WCH family rules illustrate this binding. CH32V20x selects separate full-speed device and host paths by port. CH32V30x can select USBFS or USBHS for the host and use the other controller for the device. CH32F20x, CH32V10x, and CH583 select their MCU-specific startup, linker, and controller sources. Their board targets also set the controller role and speed that the startup code handles.

The Synergy family adds a licensed SSP package to this contract. A board must provide its linker image and generated SSP inputs. The family then selects RUSB2 device and host sources, maps RHPorts and speeds, and emits images for the supported toolchain and flash flow. The RA family follows the same board-to-family pattern with FSP inputs and derives the IAR memory definitions from each board's memory map.

The NXP LPC family rules use the same target shape across LPC11, LPC13, LPC15, LPC51, LPC54, and LPC55. The family still preserves each MCU core, SDK, startup image, and linker image. LPC54 and LPC55 targets bind device and host roles to full-speed or high-speed ports and select the matching host controller path.

The STM32H7RS family adds a board-selected boot layout. The STM32H7S3 Nucleo can run from RAM with its image in external XSPI flash, or use internal flash only. Its startup code, linker image, external loader, and flash command must describe the same choice.

Family and board selection

The build resolves a board under hw/bsp/*/boards, loads its family rules, and adds TinyUSB with the selected MCU identifier. Board settings refine the part variant, controller port, speed, linker image, boot layout, and role. The target then combines board startup, the portable stack, and the controller driver. The CMake path also records the selected board, family, MCU set, and build options for downstream build and analysis tools.

The family and board layers must agree on the physical USB path. A board preset must select a real board target. Family metadata must select controller sources and roles that match that target. The linker description must fit the selected MCU memory.

References: hw/…/family_support.cmake• L121-165 hw/…/family_support.cmake• L343-420 hw/…/family_support.cmake• L595-617 hw/…/family_support.cmake• L1031-1060 hw/…/family_rules.mk• L16-60 hw/…/family.cmake• L17-49 hw/…/family.cmake• L66-105 hw/…/family.mk• L17-57 hw/…/family.cmake• L13-21 hw/…/family.cmake• L10-18 hw/…/family.cmake• L10-18 hw/…/family.cmake• L11-30 hw/…/family.cmake• L17-35 hw/…/family.cmake• L117-141 hw/…/family.cmake• L16-38 hw/…/family.cmake• L127-147 hw/…/family.cmake• L18-59 hw/…/family.cmake• L113-145 hw/…/ra.icf• L1-27 hw/…/board.cmake• L1-27 hw/…/family.cmake• L42-66 hw/…/family.cmake• L117-180 hw/…/xspi_boot.c• L27-38 hw/…/sysview_manifest.cmake• L85-113

OS primitives

The operating-system abstraction layer (OSAL) gives TinyUSB one runtime interface for time, delays, critical sections, semaphores, mutexes, and event queues. The selected CFG_TUSB_OS adapter maps this interface to no OS, an RTOS, STK, or a custom runtime. See OS abstraction and runtime services for supported adapters and shared runtime services.

In no-OS mode, the application supplies the millisecond time source. Queue operations protect shared state by disabling interrupts, and semaphore waits do not provide a scheduler wait. This mode fits a single bare-metal execution context. It is not a substitute for hardware locking on a true multi-core system.

Runtime modes

TinyUSB treats runtime mode as a platform contract rather than a USB class feature. Configuration selects the adapter. The adapter then supplies the same event and synchronization behavior to the device and host stacks.

Runtime modeEvent coordination
No OSMain code and interrupts share queues with interrupt protection
RTOSTasks, queues, semaphores, and mutexes use the selected kernel
STKUSB time, task, lock, semaphore, mutex, and queue calls use STK services
Custom OSThe application supplies the OSAL contract
Multi-core MCUShared state needs a hardware or IPC lock

References: src/…/osal.h• L17-86 src/…/osal_none.h• L15-39 src/…/osal_none.h• L41-77 src/…/osal_none.h• L157-214 src/tusb_option.h• L587-610 hw/…/family.c• L144-157 hw/…/family.c• L346-360

Application boundary

An application supplies descriptors, class callbacks, product tasks, and product services. Platform integration supplies the started controller, correct interrupt delivery, a usable time source, and the event path that lets those callbacks run.

The device CDC and MSC example shows this boundary. It starts the board and RHPort, runs the device task in its main loop, and keeps product behavior in callbacks and application tasks. Host and dual-role applications use the same separation.

References: examples/…/main.c• L26-65 examples/…/main.c• L68-151 examples/…/tusb_config.h• L33-68

Change checklist

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

  • When an MCU or USB controller mapping changes, check controller source selection, endpoint and speed limits, memory rules, interrupt names, and the affected family metadata together.
  • When a board changes, check its clock source, USB interrupt, physical pins, MCU variant, linker image, RHPort, boot source, and board power path together.
  • When a family role changes, check that the selected DCD and HCD sources support the recorded device, host, or dual role on the selected RHPorts.
  • When a WCH USBFS, USBHS v1, USBHS v2, or CH583 target is added or changed, keep its controller register model, speed mode, buffer rules, endpoint limits, and IRQ mapping aligned.
  • When an LPC54 or LPC55 port changes, keep the port speed, controller choice, USB SRAM placement, and linker memory layout aligned.
  • When an RA target changes, keep FSP-generated vectors, the USB reference-clock setting, high-speed PHY mode, RHPort count, and IAR memory definitions aligned.
  • When a Synergy target changes, keep SSP availability, generated vector entries, pin setup, RUSB2 controller count, PHY clock selection, linker input, and RHPort speed settings aligned.
  • When the STM32H7RS external boot path changes, keep the boot code, image load and run regions, non-cacheable memory, startup entry, external loader, and flash artifact format aligned.
  • When a controller interrupt or event path changes, check the board ISR forwarding path and the selected OSAL's task-safe and ISR-safe operations.
  • When CFG_TUSB_OS changes, check the time source, scheduler, interrupt-priority, synchronization, and multi-core assumptions required by the adapter.
  • When controller-owned memory changes, check descriptor placement, buffer alignment, cache or DMA rules, memory barriers, and linker sections together.
  • When build options used by SystemView change, check that the recorded command-line values and linked image manifest still describe the image that CI captures.

Important failure modes

TriggerConsequenceGuard
USB clock, PHY, pins, or power are not ready before initializationThe controller may not enumerate or may miss bus timingComplete board startup before TinyUSB initialization
The board interrupt does not reach the matching RHPortDevice or host events do not reach the USB stackKeep controller interrupts mapped to the selected RHPort
The MCU mapping selects the wrong controller factsThe wrong driver or hardware limits are compiledKeep MCU mapping, controller data, family sources, and role metadata aligned
Device and host roles select the same WCH controller when the hardware cannot share itOne role can prevent the other role from starting or receiving eventsSelect distinct compatible controller paths for dual-role targets
WCH USBFS, USBHS v1, and USBHS v2 register models are mixedEndpoint transfers can use the wrong control bits or miss completion eventsMatch each WCH controller source to its hardware generation
RA USBHS uses an unsupported crystal settingThe PHY PLL may not lock and the high-speed port cannot startMatch the PHY clock encoding to the board oscillator
A Synergy SSP vector or generated pin input is missingThe target can fail to configure or USB interrupts can be absentKeep SSP package checks, generated inputs, vector entries, and RHPort selection aligned
The STM32H7RS XSPI boot image uses mismatched load and run regionsReset can fail before the application reaches board startupKeep the boot code, linker script, startup path, and external loader as one contract
The no-OS time source is missingDelays and time-based USB work cannot progressImplement tusb_time_millis_api() in the board or application
A no-OS queue or semaphore is used as multi-core synchronizationShared event state can race or corruptUse a hardware or IPC lock for multi-core execution
Controller DMA buffers violate alignment or address limitsTransfers can fail or overwrite unrelated memoryKeep buffer placement and controller DMA limits in the family contract
Board and family metadata name different targetsConfigure, build, or coverage flows can fail or describe the wrong boardKeep board names aligned across presets, family data, and board directories

Key files

FileWhy to read it
src/tusb.hDefines the platform-facing initialization and interrupt entry points
src/tusb.cShows how RHPort roles reach the device or host stack
src/…/dcd.hDefines the device-controller event boundary
src/…/hcd.hDefines the host-controller event boundary
hw/…/board_api.hDefines the common board startup and board-service interface
hw/bsp/family_support.cmakeResolves boards and applies common family, RTOS, TinyUSB, metadata, and output rules
hw/bsp/family_rules.mkProvides the shared Make build, artifact, and flash flow
hw/bsp/ch32v30x/family.cmakeShows WCH controller, RHPort, speed, and dual-role selection
hw/bsp/lpc55/family.cmakeShows NXP dual-port speed, memory placement, and host-controller selection
hw/bsp/ra/family.cmakeShows RA FSP inputs, RHPort selection, linker mapping, and RUSB2 sources
hw/…/family.cShows RA vector routing, USB time, power, and interrupt forwarding
hw/bsp/ra/linker/ra.icfShows how RA board memory definitions reach the IAR image
hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.cmakeShows board-selected RHPorts and the external boot and loader choice
hw/…/family.cShows STM32 USB setup, RHPort interrupt forwarding, power, and runtime time source
hw/…/xspi_boot.cShows the external-flash boot handoff to the RAM image
hw/bsp/stm32h7rs/linker/stm32h7s3xx_xspi.ldShows the split internal-flash, external-flash, RAM, and non-cacheable layout
hw/bsp/sysview_manifest.cmakeShows how build choices and linked-image identity are recorded for trace capture
src/…/dcd_rusb2.cShows the RUSB2 device event and transfer boundary
src/…/dcd_stm32_fsdev.cShows the ST FSDEV device boundary and packet-memory model
src/…/dcd_dwc2.cShows DWC2 device setup, FIFO or DMA choices, and interrupt events
src/…/osal.hSelects the runtime adapter and defines the shared OSAL contract
src/…/osal_none.hShows the no-OS time, lock, queue, and semaphore model
examples/…/main.cDemonstrates the application-to-platform boundary