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 / class-serial-and-human-interface

Serial and human-interface classes

CDC and HID give TinyUSB two common ways to move application data over USB. CDC presents a serial-style byte stream with line control, while HID presents structured input and output reports for devices such as keyboards, mice, gamepads, and vendor-specific controls. The same class support can run in device mode, where the firmware is a USB peripheral, or host mode, where the firmware discovers and drives an attached peripheral. See the USB class drivers page for the wider class-driver model.

Class roles

Communication Device Class

The Communication Device Class (CDC) provides a serial-like channel between an application and a USB device. TinyUSB targets the CDC Abstract Control Model for standard CDC serial devices, with a communication interface for control and notification and a data interface for the byte stream. The host side also exposes one CDC API for several serial chip families that reuse the same application model, even when those devices are not formally CDC devices. These host drivers include common vendor-specific serial devices such as FTDI, CP210x, CH34x, and PL2303 hardware.

CDC has two kinds of state. Line coding describes the serial format, such as baud rate, stop bits, parity, and data width. Line state carries connection signals such as DTR and RTS. These controls can affect when an application treats the stream as connected, but the data path remains a buffered byte stream. Host applications can read cached line state without starting a new USB request, while changes to line state or line coding use control transfers and can complete asynchronously or through the synchronous control API.

References: src/…/cdc.h• L8-11 src/…/cdc_host.h• L45-215 src/…/cdc_host.c• L101-153 src/…/cdc_host.c• L736-763 src/…/cdc_host.c• L134-168 src/…/cdc_device.h• L85-138

Human Interface Device

The Human Interface Device (HID) class carries structured reports for human controls and other low-latency controls. A report descriptor defines the meaning and shape of input, output, and feature reports. HID can use standard boot keyboard and mouse protocols, or a report protocol for custom layouts such as gamepads and vendor-defined controls.

The report descriptor is the contract between an HID device and its host. Simple reports can use the built-in report description helpers and parser. A custom or complex report needs application code that matches the declared report IDs, usages, fields, and lengths. The host parser identifies top-level report usage information and stops when a report item is truncated instead of reading beyond the descriptor. Short descriptor values are treated as new values rather than as leftover bytes from an earlier item.

References: src/…/hid.h• L27-82 src/…/hid_device.h• L35-64 src/…/hid_device.h• L109-137 src/…/hid_host.h• L36-71 src/…/hid_host.c• L701-839

Device mode

Device interface configuration

In device mode, the application selects CDC and HID instances through class configuration and describes each exposed interface in the USB configuration descriptor. The descriptor is the public shape of the device. It declares interface numbers, endpoint directions and sizes, polling or notification behavior, and the HID report descriptor associated with each HID interface. The application must keep interface order and instance use aligned.

CDC normally exposes a paired communication and data interface. A composite device can place that pair beside other classes. The CDC receive buffer must hold a complete bulk OUT packet when the controller uses software endpoint memory; otherwise the class rejects the interface during configuration. HID can expose several independent interfaces, each with its own report descriptor and endpoint. Its output buffer must hold an OUT packet. On controllers that receive whole packets, the driver arms only complete packets that fit this buffer, so a report that spans more than the armed space can reach the application in more than one transfer. The multiple-interface HID example uses one keyboard interface and one mouse interface to show this separation.

References: src/tusb_option.h• L678-721 examples/…/usb_descriptors.c• L101-110 examples/…/usb_descriptors.c• L63-118 examples/…/usb_descriptors.c• L43-47 examples/…/usb_descriptors.c• L67-117 src/…/cdc_device.c• L315-358 src/…/hid_device.c• L205-270

Device application integration

The device application owns the meaning of data after the class driver has moved it into or out of USB buffers. CDC applications check connection and buffer state, read received bytes, write response bytes, and flush output when they want the data sent. CDC callbacks provide notification when data arrives, output space becomes available, or the host changes line state or line coding. An application can also register a wanted-character trigger for receive data.

CDC receive handling keeps the stream ready for later transfers. It can notify the application when buffered data remains and can call the wanted-character callback once when the configured character appears in a received transfer. After transmit completion, the transmit callback gives the application a chance to add more data before the next transfer.

HID applications provide the report descriptor and handle host requests for reports. They send input reports when the device state changes and receive output reports through the set-report callback. The convenience helpers cover common keyboard, mouse, gamepad, and stylus layouts, while the generic report API supports application-defined layouts. If the interface has an output endpoint, the class driver keeps that receive path ready after each transfer and reports success or failure to the application. When the configured output transfer space holds fewer complete packets than one report needs, the application must use the transfer length and assemble the report before decoding it.

The device-side data path connects USB transfer events to application callbacks and buffers. A device application normally runs the TinyUSB task and then performs class work in its main loop or task context. The examples use this pattern to forward CDC bytes to another class and to create keyboard and mouse reports from board input.

The following diagram shows the two device-side paths from USB traffic to application data.

flowchart LR
  Host[USB host]
  CDC_USB[CDC control and data interfaces]
  HID_USB[HID endpoints and control requests]
  CDC_APP[CDC byte-stream application]
  HID_APP[HID report application]
  Host --> CDC_USB --> CDC_APP
  Host --> HID_USB --> HID_APP
  CDC_APP --> CDC_USB
  HID_APP --> HID_USB

References: src/…/cdc_device.h• L100-138 src/…/cdc_device.h• L250-276 src/…/cdc_device.c• L63-94 src/…/cdc_device.c• L445-509 src/…/hid_device.h• L44-64 src/…/hid_device.h• L109-137 src/…/hid_device.c• L88-112 src/…/hid_device.c• L253-366 src/…/hid_device.c• L391-422 examples/…/main.c• L47-92 examples/…/main.c• L113-169

Host mode

Host interface configuration

In host mode, TinyUSB discovers attached interfaces during enumeration and assigns each supported CDC or HID interface to a class driver. The application enables the needed class capacity and provides buffers for the expected transfer sizes. CDC stream buffers must be at least as large as the host stack's maximum bulk packet size. Small control responses also use storage sized for the controller's control-transfer rules. A mounted interface is identified by the host's device address and an interface index, rather than by a fixed global device object.

HID host setup reads the interface's HID descriptor and report descriptor. It supports descriptors where the HID descriptor appears directly after the interface or after its endpoint descriptors. Boot interfaces can use the standard keyboard or mouse layout. Only those two defined boot protocol values are treated as boot interfaces; reserved protocol values are treated as non-boot interfaces. Other interfaces expose their report descriptor to the application for parsing or custom decoding. If the report descriptor is larger than the host enumeration buffer, the interface can still mount but the descriptor is not passed to the application. An interrupt IN endpoint that exceeds the configured HID input buffer is left unopened, so the interface may mount without a usable report-polling path.

CDC host setup can apply initial line state and line coding when the device is mounted. The interface becomes available only after that setup sequence completes successfully. During enumeration, a CDC slot is reserved for the interface that is currently being opened. If that attempt fails, TinyUSB releases that reserved slot without clearing an earlier accepted alternate setting for the same device and interface number.

References: src/tusb_option.h• L756-805 examples/…/tusb_config.h• L98-133 src/…/hid_host.c• L501-570 src/…/hid_host.c• L585-674 src/…/hid_host.h• L20-31 src/…/cdc_host.c• L81-92 src/…/cdc_host.c• L365-383 src/…/cdc_host.c• L726-742 src/…/cdc_host.c• L774-787 src/…/cdc_host.c• L766-881

Host application integration

CDC host applications use mount and unmount callbacks to track availability. They use read and write operations for the byte stream, then use control requests when they must change DTR, RTS, baud rate, or data format. One common host API covers both standard CDC ACM devices and supported vendor serial devices. Control changes can run asynchronously through a completion callback or synchronously through the matching sync API. Some vendor devices need separate control steps for baud rate and data format, so applications must use the completion result before treating a change as active. The host keeps control-request success separate from the device's encoded baud value, so a valid rate that uses an encoded zero is not treated as a failed request. Vendor-specific serial drivers also reject baud rates outside the range that their device encoding can represent. The host example bridges an attached CDC interface to the board console in both directions.

HID host applications start report reception after the interface mounts and request the next report after each receive callback. They select a decoder from the interface protocol or the parsed report usage. Output and feature reports use the matching interrupt or control path when the application must send state back to the device. The host also reports completion for control transfers such as get-report, set-report, and set-protocol so the application can update its state. An interface without a usable interrupt IN endpoint can mount without providing a polling path, so the application must treat receive readiness as a capability rather than assume that every mounted interface produces input.

When an interrupt IN report transfer fails, HID calls the report-failed callback instead of the report-received callback. Its default behavior re-arms polling for transient transfer errors, but it does not re-arm after a stalled or aborted transfer. An application that changes this policy must clear a halt or decide how to stop polling before it resumes input.

The host-side data path starts at enumeration and ends at an application-specific byte or report consumer. The application must keep receiving reports or reading bytes after each completed transfer. Otherwise the class driver has no new application work to deliver.

The following diagram shows how host discovery becomes application data and how output returns to the peripheral.

flowchart LR
  Peripheral[Attached USB peripheral]
  Enum[Enumeration and interface match]
  CDC_HOST[CDC host interface]
  HID_HOST[HID host interface]
  SerialApp[Serial application]
  InputApp[Input or control application]
  Peripheral --> Enum
  Enum --> CDC_HOST --> SerialApp
  Enum --> HID_HOST --> InputApp
  SerialApp --> CDC_HOST
  InputApp --> HID_HOST
  CDC_HOST --> Peripheral
  HID_HOST --> Peripheral

References: src/…/cdc_host.h• L45-215 src/…/cdc_host.c• L455-623 src/…/cdc_host.c• L884-928 src/…/cdc_host.c• L1879-1928 examples/…/cdc_app.c• L65-108 src/…/hid_host.h• L90-162 src/…/hid_host.c• L257-338 src/…/hid_host.c• L374-444 src/…/hid_host.c• L459-482 src/…/hid_host.c• L657-674 examples/…/hid_app.c• L54-112 examples/…/hid_app.c• L189-231 examples/…/hid_app.c• L311-321

Data paths

CDC byte stream

CDC separates control from payload. The control side carries line settings and connection state. The data side carries application bytes through receive and transmit buffers. Device and host APIs use the same read, write, availability, and flush concepts so an application can treat the class as a stream without handling USB packets directly.

The stream is asynchronous from the application point of view. A write can remain buffered until a flush or later transfer, and a read only returns bytes already available. Applications therefore check readiness and available space rather than assuming that one call moves all requested data. On the device side, the transmit callback can refill the buffer after a completed transfer, and the receive path can trigger application work for buffered data or a configured wanted character. The CDC host also keeps serial control state separate from stream state, even when a vendor driver maps one public request into several device-specific control requests.

References: src/…/cdc_device.h• L85-138 src/…/cdc_device.c• L115-225 src/…/cdc_device.c• L315-358 src/…/cdc_device.c• L455-509 src/…/cdc_host.h• L89-215 src/…/cdc_host.c• L455-510 src/…/cdc_host.c• L578-623 src/…/cdc_host.c• L670-705 src/…/cdc_device.h• L24-67 src/…/cdc_host.h• L21-39

HID report exchange

HID exchanges complete reports rather than an untyped stream. Input reports move device state to the host. Output reports move host state to the device. Feature reports use control transfers for state that does not belong to the normal input or output stream. Report IDs distinguish several layouts carried by one interface.

HID timing is driven by the interface endpoint and the application's readiness checks. A device sends only when its interface is ready. A host queues the next receive request after it handles the current report. If a receive transfer fails, the failure callback decides whether polling continues; the default continues after transient errors but stops after a stall or abort. The report length and layout come from the descriptor, so a decoder must not assume that every HID device has a boot keyboard or mouse format. Host output reports require an interrupt OUT endpoint; otherwise the application must use the control-report path. On the device side, an OUT report can be delivered in transfer-sized pieces when the endpoint buffer does not hold all of its packets, so the application must preserve enough state to decode the declared report.

References: src/…/hid.h• L64-82 src/…/hid_host.h• L90-162 src/…/hid_device.h• L35-64 src/…/hid_device.c• L88-112 src/…/hid_device.c• L205-270 src/…/hid_device.c• L391-422 src/…/hid_host.c• L374-444 src/…/hid_host.c• L459-482 examples/…/hid_app.c• L183-245

Application patterns

Composite applications

CDC and HID can operate beside other class drivers in one composite device or host application. The class driver keeps each interface's transfer contract separate, while the application can connect those interfaces to a shared product function. The printer-to-CDC example forwards bytes between two class interfaces, and the host composite example services CDC, HID, and mass storage together.

When several instances are enabled, interface or instance identity is part of the application contract. A device selects the intended HID instance when it sends a report. A host uses the mounted CDC or HID index in callbacks and data calls. Descriptor order and host discovery results must therefore stay aligned with the application mapping.

References: examples/…/main.c• L26-36 examples/…/cdc_app.c• L74-108 examples/…/main.c• L37-41 src/…/cdc_host.h• L45-59 src/…/hid_host.h• L46-66

Related pages: USB class drivers, Media and connectivity classes, USB host stack, and Specialized peripheral classes.

Change checklist

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

  • Keep CDC control and data interface numbers, the union relationship, endpoint addresses, and class instance count aligned when changing a device descriptor.
  • Keep CDC device receive buffers large enough for the bulk OUT packet and CDC host stream buffers large enough for the host bulk packet limit. A descriptor or controller change can otherwise make class opening fail.
  • Keep each HID report descriptor, endpoint capacity, report ID, and application decoder aligned. A valid USB descriptor with a mismatched decoder still produces unusable input.
  • Keep host class counts, HID endpoint buffers, and enumeration buffer capacity large enough for the attached CDC or HID interfaces and the data that the application must receive or parse.
  • Keep mount, unmount, receive, failure, and transmit callback contracts aligned with application state. A stale device address or interface index can send data to a different mounted interface.
  • Keep failed CDC host opens tied to the slot reserved by the current open attempt. Do not clear a previously accepted alternate setting when a later alternate setting fails to open.
  • When changing CDC line setup on host enumeration, check the attached serial driver's supported control requests and completion result before treating the connection or new settings as usable.
  • When changing vendor CDC baud-rate encoding, keep request success separate from the encoded device value. A zero encoded divisor can be a valid rate, and rates below a driver's representable minimum must be rejected rather than truncated.
  • When changing HID report parsing or descriptor limits, check both the report metadata passed at mount and the decoder's assumptions about report length and usage fields.
  • When changing HID endpoint buffer sizes, check both whether the device can arm each OUT packet and whether the host can open its IN endpoint. Also check whether the application can assemble reports that span more than one transfer.
  • When changing HID error recovery, keep the retry policy aligned with endpoint state. A stalled endpoint needs recovery before polling can resume, while an aborted transfer must not be treated as a new report.
  • When changing device-side CDC receive or transmit callbacks, keep the wanted-character, buffered-data, and refill behavior aligned with the application's stream state.

Important failure modes

TriggerConsequenceGuard
CDC communication and data interfaces are not described as a matching pairThe host may not bind the CDC serial function or may expose no usable byte streamKeep the CDC descriptor pair and its interface association consistent
A CDC device receive buffer is smaller than its bulk OUT packetClass opening fails and USB configuration can stallSize the buffer for the endpoint packet or use a controller-backed FIFO
CDC write data is not flushed or the transmit buffer is fullData stays pending or the application drops part of a transferCheck write availability and flush after the application has queued the intended bytes
CDC line setup uses an unsupported rate or ignores a failed device control requestThe interface may use settings that are not active on the peripheralUse a rate supported by the attached serial driver and check the completion result; reject rates below the CH34x minimum instead of allowing divisor truncation
CDC receive or transmit callbacks do not account for buffered dataInput work can remain pending or output can stop after one transferUse the receive callback while data remains available and refill output from the transmit-complete path
A CDC host open fails after reserving a slot and cleanup finds an interface by device and interface numberCleanup can clear an accepted alternate setting or leave the failed reservation in useRelease the exact slot reserved by the current open attempt
HID report ID or report layout does not match the report descriptorInput fields decode as the wrong controls or the report is rejectedUse the descriptor as the source of report length, IDs, and field meaning
A HID device output buffer is smaller than its interrupt OUT packetClass opening fails and USB configuration can stallSize the buffer for the endpoint packet
A HID output report spans more transfer space than the device arms at onceThe application receives pieces that do not form a complete reportPreserve partial report state or size the buffer and report path for the report
A HID interface uses a reserved boot protocol value as if it were keyboard or mouseThe host selects a boot decoder for an interface that does not have a defined boot layoutTreat only the defined keyboard and mouse boot values as boot protocols
HID host code does not queue another receive requestThe first report is handled, but later input stopsQueue the next receive operation from the receive-complete path
HID report polling retries after a stall or abort without recoveryThe endpoint remains halted or the application treats cancelled input as validRe-arm only transient failures; clear the halt or restart the interface before retrying a stalled or aborted transfer
HID input endpoint is larger than the configured host bufferThe interface can mount without a usable input polling pathSize the HID input buffer for the endpoint and check receive readiness
HID output is sent through an interrupt path that the interface does not provideThe report cannot be queued to the deviceCheck for an OUT endpoint and use the control-report path when needed
Host capacity or enumeration buffer is too smallA supported interface is not mounted or a large HID report descriptor is unavailableSet host counts and enumeration storage for the attached device set
Application uses a stale mounted index after unmountA later transfer targets the wrong interface or failsClear per-interface application state in the unmount callback

Key files

FileWhy to read it
src/…/cdc.hDefines the CDC control model, line state, line coding, and notifications.
src/…/cdc_device.hShows the device-side CDC stream API and application callbacks.
src/…/cdc_device.cShows device-side CDC interface setup, packet-fit checks, buffered stream flow, line controls, and callback timing.
src/…/cdc_host.hShows host-side CDC interface identity, stream operations, serial controls, cached state, lifecycle callbacks, and synchronous control wrappers.
src/…/cdc_host.cShows how standard and vendor-specific serial devices share host stream and control behavior, including endpoint buffer limits, safe cleanup when interface opening fails, and validation of vendor baud-rate limits.
src/…/hid.hDefines HID report types, protocols, usages, and common report shapes.
src/…/hid_device.hShows device-side report sending, descriptor ownership, and report callbacks.
src/…/hid_device.cShows device-side HID report transfer, output packet sizing, control requests, output reception, and transfer callbacks.
src/…/hid_host.hShows host-side report discovery, parsing, receive, send, error, and lifecycle callbacks.
src/…/hid_host.cShows host HID enumeration, boot protocol classification, report descriptor handling, report transfer paths, parser behavior, endpoint capability checks, and transfer recovery.
examples/…/usb_descriptors.cIllustrates CDC interface configuration inside a composite device.
examples/…/main.cIllustrates CDC stream integration with another device class.
examples/…/usb_descriptors.cIllustrates separate report descriptors and endpoints for multiple HID interfaces.
examples/…/main.cIllustrates device-side keyboard and mouse report production.
examples/…/cdc_app.cIllustrates host CDC forwarding and serial-control setup.
examples/…/hid_app.cIllustrates boot and generic HID report handling.
examples/…/hid_app.cIllustrates device-specific HID input and output reports.
examples/…/tusb_config.hShows host class capacity and CDC/HID setup choices.