blob: 387587b48ff7613cc5295365f55c3b3e24594939 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
***********
USB Classes
***********
TinyUSB supports multiple USB device and host classes. This reference describes the features, capabilities, and requirements for each class.
Device Classes
==============
CDC (Communication Device Class)
--------------------------------
Implements USB CDC specification for serial communication.
**Supported Features:**
- CDC-ACM (Abstract Control Model) for virtual serial ports
- Data terminal ready (DTR) and request to send (RTS) control lines
- Line coding configuration (baud rate, parity, stop bits)
- Break signal support
**Configuration:**
- ``CFG_TUD_CDC``: Number of CDC interfaces (1-4)
- ``CFG_TUD_CDC_EP_BUFSIZE``: Endpoint buffer size (typically 512)
- ``CFG_TUD_CDC_RX_BUFSIZE``: Receive FIFO size
- ``CFG_TUD_CDC_TX_BUFSIZE``: Transmit FIFO size
**Key Functions:**
- ``tud_cdc_available()``: Check bytes available to read
- ``tud_cdc_read()``: Read data from host
- ``tud_cdc_write()``: Write data to host
- ``tud_cdc_write_flush()``: Flush transmit buffer
**Callbacks:**
- ``tud_cdc_line_coding_cb()``: Line coding changed
- ``tud_cdc_line_state_cb()``: DTR/RTS state changed
HID (Human Interface Device)
----------------------------
Implements USB HID specification for input devices.
**Supported Features:**
- Boot protocol (keyboard/mouse)
- Report protocol with custom descriptors
- Input, output, and feature reports
- Multiple HID interfaces
**Configuration:**
- ``CFG_TUD_HID``: Number of HID interfaces
- ``CFG_TUD_HID_EP_BUFSIZE``: Endpoint buffer size
**Key Functions:**
- ``tud_hid_ready()``: Check if ready to send report
- ``tud_hid_report()``: Send HID report
- ``tud_hid_keyboard_report()``: Send keyboard report
- ``tud_hid_mouse_report()``: Send mouse report
**Callbacks:**
- ``tud_hid_descriptor_report_cb()``: Provide report descriptor
- ``tud_hid_get_report_cb()``: Handle get report request
- ``tud_hid_set_report_cb()``: Handle set report request
MSC (Mass Storage Class)
------------------------
Implements USB mass storage for file systems.
**Supported Features:**
- SCSI transparent command set
- Multiple logical units (LUNs)
- Read/write operations
- Inquiry and capacity commands
**Configuration:**
- ``CFG_TUD_MSC``: Number of MSC interfaces
- ``CFG_TUD_MSC_EP_BUFSIZE``: Endpoint buffer size
**Key Functions:**
- Storage operations handled via callbacks
**Required Callbacks:**
- ``tud_msc_inquiry_cb()``: Device inquiry information
- ``tud_msc_test_unit_ready_cb()``: Test if LUN is ready
- ``tud_msc_capacity_cb()``: Get LUN capacity
- ``tud_msc_start_stop_cb()``: Start/stop LUN
- ``tud_msc_read10_cb()``: Read data from LUN
- ``tud_msc_write10_cb()``: Write data to LUN
Audio Class
-----------
Implements USB Audio Class 2.0 specification.
**Supported Features:**
- Audio streaming (input/output)
- Multiple sampling rates
- Volume and mute controls
- Feedback endpoints for asynchronous mode
**Configuration:**
- ``CFG_TUD_AUDIO``: Number of audio functions
- Multiple configuration options for channels, sample rates, bit depth
**Key Functions:**
- ``tud_audio_read()``: Read audio data
- ``tud_audio_write()``: Write audio data
- ``tud_audio_clear_ep_out_ff()``: Clear output FIFO
MIDI
----
Implements USB MIDI specification.
**Supported Features:**
- MIDI 1.0 message format
- Multiple virtual MIDI cables
- Standard MIDI messages
**Configuration:**
- ``CFG_TUD_MIDI``: Number of MIDI interfaces
- ``CFG_TUD_MIDI_RX_BUFSIZE``: Receive buffer size
- ``CFG_TUD_MIDI_TX_BUFSIZE``: Transmit buffer size
**Key Functions:**
- ``tud_midi_available()``: Check available MIDI messages
- ``tud_midi_read()``: Read MIDI packet
- ``tud_midi_write()``: Send MIDI packet
DFU (Device Firmware Update)
----------------------------
Implements USB DFU specification for firmware updates.
**Supported Modes:**
- DFU Mode: Device enters DFU for firmware update
- DFU Runtime: Request transition to DFU mode
**Configuration:**
- ``CFG_TUD_DFU``: Enable DFU mode
- ``CFG_TUD_DFU_RUNTIME``: Enable DFU runtime
**Key Functions:**
- Firmware update operations handled via callbacks
**Required Callbacks:**
- ``tud_dfu_download_cb()``: Receive firmware data
- ``tud_dfu_manifest_cb()``: Complete firmware update
Vendor Class
------------
Custom vendor-specific USB class implementation.
**Features:**
- Configurable endpoints
- Custom protocol implementation
- WebUSB support
- Microsoft OS descriptors
**Configuration:**
- ``CFG_TUD_VENDOR``: Number of vendor interfaces
- ``CFG_TUD_VENDOR_EPSIZE``: Endpoint size
**Key Functions:**
- ``tud_vendor_available()``: Check available data
- ``tud_vendor_read()``: Read vendor data
- ``tud_vendor_write()``: Write vendor data
Host Classes
============
CDC Host
--------
Connect to CDC devices (virtual serial ports).
**Supported Devices:**
- CDC-ACM devices
- FTDI USB-to-serial converters
- CP210x USB-to-serial converters
- CH34x USB-to-serial converters
**Configuration:**
- ``CFG_TUH_CDC``: Number of CDC host instances
- ``CFG_TUH_CDC_FTDI``: Enable FTDI support
- ``CFG_TUH_CDC_CP210X``: Enable CP210x support
**Key Functions:**
- ``tuh_cdc_available()``: Check available data
- ``tuh_cdc_read()``: Read from CDC device
- ``tuh_cdc_write()``: Write to CDC device
- ``tuh_cdc_set_baudrate()``: Configure serial settings
HID Host
--------
Connect to HID devices (keyboards, mice, etc.).
**Supported Devices:**
- Boot keyboards and mice
- Generic HID devices with report descriptors
- Composite HID devices
**Configuration:**
- ``CFG_TUH_HID``: Number of HID host instances
- ``CFG_TUH_HID_EPIN_BUFSIZE``: Input endpoint buffer size
**Key Functions:**
- ``tuh_hid_receive_report()``: Start receiving reports
- ``tuh_hid_send_report()``: Send report to device
- ``tuh_hid_parse_report_descriptor()``: Parse HID descriptors
MSC Host
--------
Connect to mass storage devices (USB drives).
**Supported Features:**
- SCSI transparent command set
- FAT file system support (with FatFS integration)
- Multiple LUNs per device
**Configuration:**
- ``CFG_TUH_MSC``: Number of MSC host instances
- ``CFG_TUH_MSC_MAXLUN``: Maximum LUNs per device
**Key Functions:**
- ``tuh_msc_ready()``: Check if device is ready
- ``tuh_msc_read10()``: Read sectors from device
- ``tuh_msc_write10()``: Write sectors to device
Hub
---
Support for USB hubs to connect multiple devices.
**Features:**
- Multi-level hub support
- Port power management
- Device connect/disconnect detection
**Configuration:**
- ``CFG_TUH_HUB``: Number of hub instances
- ``CFG_TUH_DEVICE_MAX``: Total connected devices
Class Implementation Guidelines
===============================
Descriptor Requirements
-----------------------
Each USB class requires specific descriptors:
1. **Interface Descriptor**: Defines the class type
2. **Endpoint Descriptors**: Define communication endpoints
3. **Class-Specific Descriptors**: Additional class requirements
4. **String Descriptors**: Human-readable device information
Callback Implementation
-----------------------
Most classes require callback functions:
- **Mandatory callbacks**: Must be implemented for class to function
- **Optional callbacks**: Provide additional functionality
- **Event callbacks**: Called when specific events occur
Performance Considerations
--------------------------
When implementing USB classes, match **buffer sizes** to expected data rates to avoid bottlenecks. Choose appropriate **transfer types** based on your application's requirements. Keep **callback processing** lightweight for optimal performance. Avoid **memory allocations in critical paths** where possible to maintain consistent performance.
Testing and Validation
----------------------
- **USB-IF Compliance**: Ensure descriptors meet USB standards
- **Host Compatibility**: Test with multiple operating systems
- **Performance Testing**: Verify transfer rates and latency
- **Error Handling**: Test disconnect/reconnect scenarios
Class-Specific Resources
========================
- **USB-IF Specifications**: Official USB class specifications
- **Example Code**: Reference implementations in ``examples/`` directory
- **Test Applications**: Host-side test applications for validation
- **Debugging Tools**: USB protocol analyzers and debugging utilities
|