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 / usb-typec-power-delivery

USB Type-C and Power Delivery

TinyUSB’s Type-C subsystem gives an application a small interface for USB Type-C attach detection and USB Power Delivery (PD) message exchange. It connects a Type-C controller to application policy: the controller reports cable and message events, the stack turns them into callbacks, and the application decides which power contract to request. For related USB role examples, see Host, Dual-Role, and Type-C Examples, Platform Integration, and USB Host Stack.

System role

The Type-C subsystem sits beside the USB host and device stacks. It manages the electrical Type-C connection and the PD control channel, but it does not decide the product’s power policy. An application selects a safe power offer and reacts to the result.

The subsystem has three broad boundaries:

  • The Type-C controller interface connects TinyUSB to MCU or external controller hardware.
  • The Type-C stack tracks attach state and routes PD messages.
  • The application policy selects power and handles attach, accept, reject, and ready events.

The normal USB data role is a separate concern. A product can use Type-C and PD without enabling the TinyUSB host or device stack, as shown by the power-delivery example.

Type-C controller boundary

Controller interface

The Type-C controller interface, named the TCD interface in the code, is the hardware boundary for CC-pin sensing and PD wire traffic. A controller adapter initializes a port, enables or disables its interrupts, enables or disables Type-C terminations, and transfers PD messages. It reports only events the common Type-C stack needs: CC changes, completed receives, and completed sends.

This split lets the common stack keep one application API while each supported controller handles its own signal setup and transfer rules. The STM32 UCPD adapter is one such controller adapter. It detects the active CC pin, receives SOP messages, sends GoodCRC, and reports transfer errors to the stack.

References: src/…/tcd.h• L25-76 src/…/tcd.h• L78-124 src/…/typec_stm32.c• L338-490

Port states

The stack treats a port as initialized, connected, or disconnected. rhport means the TinyUSB root-hub port identifier used to select the Type-C port. Initialization creates the shared event path and starts the controller; tuc_connect() enables the Type-C terminations so the controller can detect a partner; tuc_disconnect() stops detection and clears active transfers.

CC state changes drive the connection state. A non-zero state on either CC pin marks the port as attached, and a later loss of both states marks it as detached. The stack calls tuc_attach_changed_cb() only when the Boolean attached state changes. The hardware adapter also selects the active CC pin for a flipped cable and starts PD traffic only after attachment.

The public type list includes source, sink, dual-role power, and disconnected port types. The STM32 adapter currently implements sink behavior in its attach path, so an application must confirm that its selected controller adapter supports the requested role before treating the role enum as a complete product policy.

References: src/…/pd_types.h• L23-32 src/…/usbc.c• L29-35 src/…/usbc.c• L77-119 src/…/usbc.c• L122-169 src/…/typec_stm32.c• L233-246 src/…/typec_stm32.c• L261-310

Power Delivery model

PD message types

Power Delivery messages describe the available power and the requested power. A PD header identifies the message kind, PD revision, data role, power role, message ID, and number of data objects. A control message has no data objects; a data message carries one or more four-byte objects.

The main objects used by a sink application are:

ObjectPurpose
Source PDOAdvertises one source power option. Options can be fixed, battery, variable, or augmented programmable supply.
Request RDOSelects one advertised option and states the requested and operating current or power.
Control messageReports protocol results such as accept, reject, or power ready.

The object fields use fixed units defined by PD: fixed voltage uses 50 mV steps and fixed current uses 10 mA steps. The application must convert these values before comparing them with board limits.

References: src/…/pd_types.h• L34-60 src/…/pd_types.h• L62-105 src/…/pd_types.h• L117-176 src/…/pd_types.h• L178-208

Power negotiation

The common stack exposes PD messages to application callbacks instead of selecting a power contract itself. The application examines the source capabilities, chooses an object that the board can safely accept, sends a request, and then waits for the source’s result.

This diagram shows the normal sink-side negotiation path.

sequenceDiagram
    participant Source as PD source
    participant Controller as Type-C controller
    participant Stack as Type-C stack
    participant App as Application policy

    Source->>Controller: Source Capabilities
    Controller->>Stack: Receive complete
    Stack->>App: tuc_pd_data_received_cb()
    App->>Stack: tuc_msg_request(selected RDO)
    Stack->>Controller: Request
    Controller->>Source: Request
    Source-->>Controller: Accept or Reject
    Controller-->>Stack: Control message
    Stack-->>App: tuc_pd_control_received_cb()
    Source-->>Controller: PS Ready
    Controller-->>Stack: Control message
    Stack-->>App: tuc_pd_control_received_cb()

The controller answers valid received messages with GoodCRC at the protocol boundary. The application therefore handles policy decisions, not the low-level acknowledgement. The current controller path accepts SOP traffic, ignores SOP-star traffic, and rearms reception after a hard reset or completed message.

References: src/…/usbc.c• L143-180 src/…/usbc.c• L187-212 src/…/typec_stm32.c• L398-455 examples/…/main.c• L84-185

Power contract behavior

The power contract is not active when the application only sees a source capability. It becomes usable after the source accepts the request and reports PS_READY. A reject means that the application must keep the existing safe state or choose another supported offer.

The example limits both voltage and current before it builds a request. It skips unsupported battery, variable, and augmented offers, and it does not request a voltage above its board limit. This safety check belongs to the application because the stack cannot know the limits of the board’s power path.

References: examples/…/main.c• L37-42 examples/…/main.c• L92-152 examples/…/main.c• L163-185

Application integration

Type-C configuration

An application enables the Type-C stack with CFG_TUC_ENABLED. When enabled, tusb.h exposes the Type-C API through typec/usbc.h; when disabled, the Type-C interrupt hook becomes a no-op. The application must also provide a controller adapter for the target hardware.

Type-C uses its own task queue. The default queue size is eight events, and the application can change it with CFG_TUC_TASK_QUEUE_SZ. In a bare-metal program, the application calls tuc_task() from its main loop. An RTOS application can call tuc_task_ext() with a wait time that fits its task model. The in_isr argument is part of the public call but does not change task processing in the current stack.

References: src/tusb_option.h• L922-930 src/tusb.h• L18-25 src/…/usbc.h• L18-53 src/…/usbc.c• L124-129 examples/…/tusb_config.h• L42-50

Public lifecycle APIs

The lifecycle API controls one Type-C port at a time. A typical integration initializes the port once, enables terminations when the product is ready to detect a partner, runs the task function, and disables terminations before shutdown or a controlled power change.

APIRole in an application
tuc_init(rhport, port_type)Initialize the common stack and controller for a port.
tuc_inited(rhport)Check that the common stack and selected port are initialized.
tuc_connect(rhport)Enable Type-C terminations and begin attach detection.
tuc_disconnect(rhport)Disable terminations and stop the port.
tuc_task()Process queued Type-C and PD events with an indefinite wait.
tuc_task_ext(timeout_ms, in_isr)Process events with an explicit wait and call context.
tuc_msg_request(rhport, rdo)Send a sink request built from a fixed or variable RDO.
tuc_msg_soft_reset(rhport)Send a PD soft-reset control message when the application needs to restart protocol state.

The calls that change port state return false if the port is not initialized. The task function must run regularly after initialization because controller events are queued for stack processing and application callbacks.

References: src/…/usbc.h• L28-60 src/…/usbc.h• L74-75 src/…/usbc.c• L77-119 src/…/usbc.c• L122-169 src/…/usbc.c• L200-231

Power reset API

tuc_msg_soft_reset() gives the application a public way to request PD protocol recovery without changing the Type-C attach state. It sends a control message on an initialized port, so the application can use it when its policy detects a protocol state that needs to restart.

References: src/…/usbc.h• L74-75 src/…/usbc.c• L218-231

Application callbacks

Callbacks are the application’s policy boundary. The data callback receives a PD header and a bounded data-object range. The control callback receives a header for messages such as accept, reject, and power ready. The attach callback reports the change between connected and disconnected states.

CallbackApplication responsibility
tuc_pd_data_received_cb()Inspect data messages and, when appropriate, create a safe request.
tuc_pd_control_received_cb()Track the result of a request and update the product power state.
tuc_attach_changed_cb()Start or stop product behavior that depends on a cable connection.

The callbacks have weak default handlers, so an application can implement only the events it needs. A callback should treat all received capability values as peer data and enforce the board’s voltage and current limits before requesting power.

References: src/…/usbc.h• L62-75 src/…/usbc.c• L45-72 src/…/usbc.c• L171-181 examples/…/main.c• L82-185

Integration sequence

The application owns the order between product readiness, Type-C detection, event processing, and power use. The power-delivery example uses a button to toggle connection detection and uses the main loop to run both product work and tuc_task().

The integration sequence is:

  1. Enable CFG_TUC_ENABLED and select a controller-supported port role.
  2. Call tuc_init() after board hardware is ready.
  3. Call tuc_connect() when the product is ready to detect or negotiate with a partner.
  4. Run tuc_task() or tuc_task_ext() in the main or RTOS task loop.
  5. Use the callbacks to choose a safe PDO and send its RDO. Use tuc_msg_soft_reset() only when the application needs to restart PD protocol state.
  6. Start high-power product functions only after the control callback reports power ready.
  7. Call tuc_disconnect() before removing the Type-C connection from product use.

References: examples/…/main.c• L58-71 examples/…/main.c• L188-213 src/…/usbc.c• L122-169 src/…/usbc.c• L218-231

Change checklist

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

  • Keep the CFG_TUC_ENABLED build gate aligned with the public include and interrupt-handler behavior in src/tusb.h and src/tusb_option.h.
  • Keep controller event IDs and payloads aligned between src/…/tcd.h, the controller adapter, and the event processing in src/…/usbc.c.
  • Keep PDO and RDO field units aligned with the application’s voltage and current limits when changing src/…/pd_types.h or the power policy example.
  • Keep the controller adapter’s supported port roles aligned with the tusb_typec_port_type_t values exposed to applications.
  • Keep the task queue capacity adequate for attach, receive, and transmit events when changing event production or task scheduling.
  • Keep public PD control-message helpers aligned with the control-message handling and the policy callbacks that consume their results.

Important failure modes

TriggerConsequenceGuard
The application requests a PDO above the board’s voltage or current limit.The board power path can be damaged.Compare every offered PDO with board limits before calling tuc_msg_request().
The application does not run tuc_task() or tuc_task_ext().Attach changes and PD results remain queued, so policy does not advance.Run the Type-C task in the main or RTOS loop after initialization.
tuc_connect() runs before tuc_init() succeeds.No Type-C terminations are enabled.Check the initialization result before enabling the port.
A source offers only unsupported PDO forms.The application cannot make a valid request.Handle the supported PDO types and keep the port at a safe state when none match.
The controller adapter does not support the requested role.Attach detection or power negotiation does not match the product design.Verify the adapter’s role support before selecting TUSB_TYPEC_PORT_SRC, SNK, or DRP.
The PD receive queue fills faster than the application drains it.Events can be delayed or lost at the controller-to-stack boundary.Size CFG_TUC_TASK_QUEUE_SZ for the event rate and task wake-up pattern.
The application sends a soft reset without handling the resulting control messages.The product policy can lose track of protocol recovery.Use tuc_pd_control_received_cb() to track the reset exchange and restore the expected power state.

Key files

FileWhy to read it
src/…/usbc.hDefines the public Type-C lifecycle, task, callback, and PD control-message APIs.
src/…/usbc.cShows the common state and event flow from controller events to application callbacks.
src/…/tcd.hDefines the controller contract and the events that hardware adapters report.
src/…/pd_types.hDefines Type-C roles, PD message kinds, source capabilities, and request objects.
src/…/typec_stm32.cIllustrates how an STM32 UCPD controller detects CC state and moves PD messages.
examples/…/main.cShows a complete sink-side policy that selects a safe source offer and handles negotiation results.
examples/…/tusb_config.hShows the minimum configuration for a Type-C-only application.