MTP¶
Role: device only. Media Transfer Protocol (MTP) presents objects and object metadata rather than a host-mounted block device. It is a better fit than MSC when device firmware and the host must access managed files concurrently.
Configuration¶
Set CFG_TUD_MTP to 1 and add TUD_MTP_DESCRIPTOR. Configure the endpoint
and control buffers, then advertise only operations, events, properties, and
formats that the application actually implements:
#define CFG_TUD_MTP 1
#define CFG_TUD_MTP_EP_BUFSIZE 512
#define CFG_TUD_MTP_EP_CONTROL_BUFSIZE 16
#define CFG_TUD_MTP_DEVICEINFO_EXTENSIONS "microsoft.com: 1.0; "
#define CFG_TUD_MTP_DEVICEINFO_SUPPORTED_OPERATIONS \
MTP_OP_GET_DEVICE_INFO, MTP_OP_OPEN_SESSION, MTP_OP_CLOSE_SESSION
#define CFG_TUD_MTP_DEVICEINFO_SUPPORTED_EVENTS \
MTP_EVENT_OBJECT_ADDED
#define CFG_TUD_MTP_DEVICEINFO_SUPPORTED_DEVICE_PROPERTIES \
MTP_DEV_PROP_DEVICE_FRIENDLY_NAME
#define CFG_TUD_MTP_DEVICEINFO_CAPTURE_FORMATS \
MTP_OBJ_FORMAT_UNDEFINED, MTP_OBJ_FORMAT_ASSOCIATION
#define CFG_TUD_MTP_DEVICEINFO_PLAYBACK_FORMATS \
MTP_OBJ_FORMAT_UNDEFINED, MTP_OBJ_FORMAT_ASSOCIATION
Option |
Default |
What it controls |
|---|---|---|
|
Required |
Shared bulk data buffer and maximum data chunk, at most 65535 bytes. Larger values improve throughput but consume static RAM. |
|
Required |
Staging buffer for MTP class control requests and responses. |
|
Required |
MTP extension string returned by GetDeviceInfo; use an empty string when no extension is implemented. |
|
Required |
Operation codes the host is told it may issue. |
|
Required |
Event codes the device may send on the interrupt endpoint. |
|
Required |
Device property codes supported by the application. |
|
Required |
Object formats the device can create or expose for playback. |
The CFG_TUD_MTP_DEVICEINFO_* lists form the GetDeviceInfo response and are
a contract with the host.
Application flow¶
API or callback |
What it does |
|---|---|
|
Tests whether the bulk OUT and bulk IN endpoints are open. |
|
Delivers the operation container and starts the application transaction state machine. A negative return stalls both bulk endpoints. |
|
Starts or continues the operation’s data phase. Returns |
|
Queues the final response container with the current transaction ID.
Returns |
|
Copies and queues one asynchronous event. Send the next event only
after the previous one has gone out: a call while the event endpoint is
busy overwrites the event still in flight before returning |
|
Supplies or consumes the next chunk of a multi-packet data phase. A negative return stalls both bulk endpoints. |
|
Advances application state after the entire data phase. A negative return stalls both bulk endpoints. |
|
Advances application state after the response phase. |
|
Handles cancel, reset, status, extended-event, and vendor control
requests. Return |
tud_mtp_command_received_cb() receives an operation container. The
application performs any data phase with tud_mtp_data_send() or
tud_mtp_data_receive(), then completes the transaction with
tud_mtp_response_send(). Use tud_mtp_event_send() for asynchronous
events such as ObjectAdded.
The tud_mtp_data_xfer_cb(), tud_mtp_data_complete_cb(), and
tud_mtp_response_complete_cb() callbacks advance multi-stage transfers.
Validate container lengths, object handles, property codes, and storage bounds
before using them.
The MTP example is the recommended template. It implements a small in-memory object store, core session/object operations, and an upload. Replace its storage functions while preserving the command/data/response state machine.
TinyUSB supplies the USB transport and MTP containers; it does not provide a filesystem, object database, stable handle allocation, or access arbitration. Those remain application responsibilities.