Skip to content

Commit 855523d

Browse files
authored
Merge pull request arduino#135 from hathach/tinyusb-midi
enable TinyUSB MIDI
2 parents 5c318aa + ff7b62d commit 855523d

28 files changed

+898
-478
lines changed

cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_CDC.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Adafruit_USBD_CDC::Adafruit_USBD_CDC(void)
4040
uint16_t Adafruit_USBD_CDC::getDescriptor(uint8_t* buf, uint16_t bufsize)
4141
{
4242
// CDC is mostly always existed for DFU
43+
// usb core will automatically update interface number and endpoint number
4344
uint8_t desc[] = { TUD_CDC_DESCRIPTOR(0, 0, EPIN, 8, EPOUT, EPIN, 64) };
4445
uint16_t const len = sizeof(desc);
4546

@@ -52,9 +53,13 @@ uint16_t Adafruit_USBD_CDC::getDescriptor(uint8_t* buf, uint16_t bufsize)
5253
// Baud and config is ignore in CDC
5354
void Adafruit_USBD_CDC::begin (uint32_t baud)
5455
{
56+
(void) baud;
5557
}
58+
5659
void Adafruit_USBD_CDC::begin (uint32_t baud, uint8_t config)
5760
{
61+
(void) baud;
62+
(void) config;
5863
}
5964

6065
void Adafruit_USBD_CDC::end(void)
@@ -128,6 +133,7 @@ extern "C"
128133
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
129134
{
130135
(void) itf; // interface ID, not used
136+
(void) rts;
131137

132138
// DTR = false is counted as disconnected
133139
if ( !dtr )

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/class/audio/audio.h

+39-5
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
* Currently only MIDI subclass is supported
3030
* @{ */
3131

32-
#ifndef _TUSB_CDC_H__
33-
#define _TUSB_CDC_H__
32+
#ifndef _TUSB_AUDIO_H__
33+
#define _TUSB_AUDIO_H__
3434

3535
#include "common/tusb_common.h"
3636

@@ -41,9 +41,9 @@
4141
/// Audio Interface Subclass Codes
4242
typedef enum
4343
{
44-
AUDIO_SUBCLASS_AUDIO_CONTROL = 0x01 , ///< Audio Control
45-
AUDIO_SUBCLASS_AUDIO_STREAMING , ///< Audio Streaming
46-
AUDIO_SUBCLASS_MIDI_STREAMING , ///< MIDI Streaming
44+
AUDIO_SUBCLASS_CONTROL = 0x01 , ///< Audio Control
45+
AUDIO_SUBCLASS_STREAMING , ///< Audio Streaming
46+
AUDIO_SUBCLASS_MIDI_STREAMING , ///< MIDI Streaming
4747
} audio_subclass_type_t;
4848

4949
/// Audio Protocol Codes
@@ -54,6 +54,40 @@ typedef enum
5454
AUDIO_PROTOCOL_V3 = 0x30, ///< Version 3.0
5555
} audio_protocol_type_t;
5656

57+
/// Audio Function Category Codes
58+
typedef enum
59+
{
60+
AUDIO_FUNC_DESKTOP_SPEAKER = 0x01,
61+
AUDIO_FUNC_HOME_THEATER = 0x02,
62+
AUDIO_FUNC_MICROPHONE = 0x03,
63+
AUDIO_FUNC_HEADSET = 0x04,
64+
AUDIO_FUNC_TELEPHONE = 0x05,
65+
AUDIO_FUNC_CONVERTER = 0x06,
66+
AUDIO_FUNC_SOUND_RECODER = 0x07,
67+
AUDIO_FUNC_IO_BOX = 0x08,
68+
AUDIO_FUNC_MUSICAL_INSTRUMENT = 0x09,
69+
AUDIO_FUNC_PRO_AUDIO = 0x0A,
70+
AUDIO_FUNC_AUDIO_VIDEO = 0x0B,
71+
AUDIO_FUNC_CONTROL_PANEL = 0x0C
72+
} audio_function_t;
73+
74+
/// Audio Class-Specific AC Interface Descriptor Subtypes
75+
typedef enum
76+
{
77+
AUDIO_CS_INTERFACE_HEADER = 0x01,
78+
AUDIO_CS_INTERFACE_INPUT_TERMINAL = 0x02,
79+
AUDIO_CS_INTERFACE_OUTPUT_TERMINAL = 0x03,
80+
AUDIO_CS_INTERFACE_MIXER_UNIT = 0x04,
81+
AUDIO_CS_INTERFACE_SELECTOR_UNIT = 0x05,
82+
AUDIO_CS_INTERFACE_FEATURE_UNIT = 0x06,
83+
AUDIO_CS_INTERFACE_EFFECT_UNIT = 0x07,
84+
AUDIO_CS_INTERFACE_PROCESSING_UNIT = 0x08,
85+
AUDIO_CS_INTERFACE_EXTENSION_UNIT = 0x09,
86+
AUDIO_CS_INTERFACE_CLOCK_SOURCE = 0x0A,
87+
AUDIO_CS_INTERFACE_CLOCK_SELECTOR = 0x0B,
88+
AUDIO_CS_INTERFACE_CLOCK_MULTIPLIER = 0x0C,
89+
AUDIO_CS_INTERFACE_SAMPLE_RATE_CONVERTER = 0x0D,
90+
} audio_cs_interface_subtype_t;
5791

5892
/** @} */
5993

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/class/cdc/cdc.h

+26-22
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
/** \defgroup ClassDriver_CDC_Common Common Definitions
4242
* @{ */
4343

44-
44+
// TODO remove
4545
/// CDC Pipe ID, used to indicate which pipe the API is addressing to (Notification, Out, In)
4646
typedef enum
4747
{
@@ -52,8 +52,9 @@ typedef enum
5252
}cdc_pipeid_t;
5353

5454
//--------------------------------------------------------------------+
55-
// CDC COMMUNICATION INTERFACE CLASS
55+
// CDC Communication Interface Class
5656
//--------------------------------------------------------------------+
57+
5758
/// Communication Interface Subclass Codes
5859
typedef enum
5960
{
@@ -117,7 +118,7 @@ typedef enum
117118
}cdc_func_desc_type_t;
118119

119120
//--------------------------------------------------------------------+
120-
// CDC DATA INTERFACE CLASS
121+
// CDC Data Interface Class
121122
//--------------------------------------------------------------------+
122123

123124
// SUBCLASS code of Data Interface is not used and should/must be zero
@@ -138,8 +139,9 @@ typedef enum{
138139
}cdc_data_protocol_type_t;
139140

140141
//--------------------------------------------------------------------+
141-
// MANAGEMENT ELEMENT REQUEST (CONTROL ENDPOINT)
142+
// Management Element Request (Control Endpoint)
142143
//--------------------------------------------------------------------+
144+
143145
/// Communication Interface Management Element Request Codes
144146
typedef enum
145147
{
@@ -189,8 +191,9 @@ typedef enum
189191
}cdc_management_request_t;
190192

191193
//--------------------------------------------------------------------+
192-
// MANAGEMENT ELEMENENT NOTIFICATION (NOTIFICATION ENDPOINT)
194+
// Management Elemenent Notification (Notification Endpoint)
193195
//--------------------------------------------------------------------+
196+
194197
/// Communication Interface Management Element Notification Codes
195198
typedef enum
196199
{
@@ -209,19 +212,20 @@ typedef enum
209212
}cdc_notification_request_t;
210213

211214
//--------------------------------------------------------------------+
212-
// FUNCTIONAL DESCRIPTOR (COMMUNICATION INTERFACE)
215+
// Class Specific Functional Descriptor (Communication Interface)
213216
//--------------------------------------------------------------------+
217+
214218
/// Header Functional Descriptor (Communication Interface)
215-
typedef struct ATTR_PACKED
219+
typedef struct TU_ATTR_PACKED
216220
{
217221
uint8_t bLength ; ///< Size of this descriptor in bytes.
218222
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
219-
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
223+
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUNC_DESC_
220224
uint16_t bcdCDC ; ///< CDC release number in Binary-Coded Decimal
221225
}cdc_desc_func_header_t;
222226

223227
/// Union Functional Descriptor (Communication Interface)
224-
typedef struct ATTR_PACKED
228+
typedef struct TU_ATTR_PACKED
225229
{
226230
uint8_t bLength ; ///< Size of this descriptor in bytes.
227231
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
@@ -231,7 +235,7 @@ typedef struct ATTR_PACKED
231235
}cdc_desc_func_union_t;
232236

233237
#define cdc_desc_func_union_n_t(no_slave)\
234-
struct ATTR_PACKED { \
238+
struct TU_ATTR_PACKED { \
235239
uint8_t bLength ;\
236240
uint8_t bDescriptorType ;\
237241
uint8_t bDescriptorSubType ;\
@@ -240,17 +244,17 @@ typedef struct ATTR_PACKED
240244
}
241245

242246
/// Country Selection Functional Descriptor (Communication Interface)
243-
typedef struct ATTR_PACKED
247+
typedef struct TU_ATTR_PACKED
244248
{
245249
uint8_t bLength ; ///< Size of this descriptor in bytes.
246250
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
247251
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
248252
uint8_t iCountryCodeRelDate ; ///< Index of a string giving the release date for the implemented ISO 3166 Country Codes.
249-
uint16_t wCountryCode[] ; ///< Country code in the format as defined in [ISO3166], release date as specified inoffset 3 for the first supported country.
253+
uint16_t wCountryCode ; ///< Country code in the format as defined in [ISO3166], release date as specified inoffset 3 for the first supported country.
250254
}cdc_desc_func_country_selection_t;
251255

252256
#define cdc_desc_func_country_selection_n_t(no_country) \
253-
struct ATTR_PACKED {\
257+
struct TU_ATTR_PACKED {\
254258
uint8_t bLength ;\
255259
uint8_t bDescriptorType ;\
256260
uint8_t bDescriptorSubType ;\
@@ -264,7 +268,7 @@ typedef struct ATTR_PACKED
264268

265269
/// \brief Call Management Functional Descriptor
266270
/// \details This functional descriptor describes the processing of calls for the Communications Class interface.
267-
typedef struct ATTR_PACKED
271+
typedef struct TU_ATTR_PACKED
268272
{
269273
uint8_t bLength ; ///< Size of this descriptor in bytes.
270274
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
@@ -280,7 +284,7 @@ typedef struct ATTR_PACKED
280284
}cdc_desc_func_call_management_t;
281285

282286

283-
typedef struct ATTR_PACKED
287+
typedef struct TU_ATTR_PACKED
284288
{
285289
uint8_t support_comm_request : 1; ///< Device supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and Get_Comm_Feature.
286290
uint8_t support_line_request : 1; ///< Device supports the request combination of Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State.
@@ -293,7 +297,7 @@ TU_VERIFY_STATIC(sizeof(cdc_acm_capability_t) == 1, "mostly problem with compile
293297

294298
/// \brief Abstract Control Management Functional Descriptor
295299
/// \details This functional descriptor describes the commands supported by by the Communications Class interface with SubClass code of \ref CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL
296-
typedef struct ATTR_PACKED
300+
typedef struct TU_ATTR_PACKED
297301
{
298302
uint8_t bLength ; ///< Size of this descriptor in bytes.
299303
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
@@ -303,7 +307,7 @@ typedef struct ATTR_PACKED
303307

304308
/// \brief Direct Line Management Functional Descriptor
305309
/// \details This functional descriptor describes the commands supported by the Communications Class interface with SubClass code of \ref CDC_FUNC_DESC_DIRECT_LINE_MANAGEMENT
306-
typedef struct ATTR_PACKED
310+
typedef struct TU_ATTR_PACKED
307311
{
308312
uint8_t bLength ; ///< Size of this descriptor in bytes.
309313
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
@@ -319,7 +323,7 @@ typedef struct ATTR_PACKED
319323
/// \brief Telephone Ringer Functional Descriptor
320324
/// \details The Telephone Ringer functional descriptor describes the ringer capabilities supported by the Communications Class interface,
321325
/// with the SubClass code of \ref CDC_COMM_SUBCLASS_TELEPHONE_CONTROL_MODEL
322-
typedef struct ATTR_PACKED
326+
typedef struct TU_ATTR_PACKED
323327
{
324328
uint8_t bLength ; ///< Size of this descriptor in bytes.
325329
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
@@ -331,7 +335,7 @@ typedef struct ATTR_PACKED
331335
/// \brief Telephone Operational Modes Functional Descriptor
332336
/// \details The Telephone Operational Modes functional descriptor describes the operational modes supported by
333337
/// the Communications Class interface, with the SubClass code of \ref CDC_COMM_SUBCLASS_TELEPHONE_CONTROL_MODEL
334-
typedef struct ATTR_PACKED
338+
typedef struct TU_ATTR_PACKED
335339
{
336340
uint8_t bLength ; ///< Size of this descriptor in bytes.
337341
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
@@ -347,7 +351,7 @@ typedef struct ATTR_PACKED
347351
/// \brief Telephone Call and Line State Reporting Capabilities Descriptor
348352
/// \details The Telephone Call and Line State Reporting Capabilities functional descriptor describes the abilities of a
349353
/// telephone device to report optional call and line states.
350-
typedef struct ATTR_PACKED
354+
typedef struct TU_ATTR_PACKED
351355
{
352356
uint8_t bLength ; ///< Size of this descriptor in bytes.
353357
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
@@ -371,7 +375,7 @@ static inline uint8_t cdc_functional_desc_typeof(uint8_t const * p_desc)
371375
//--------------------------------------------------------------------+
372376
// Requests
373377
//--------------------------------------------------------------------+
374-
typedef struct ATTR_PACKED
378+
typedef struct TU_ATTR_PACKED
375379
{
376380
uint32_t bit_rate;
377381
uint8_t stop_bits; ///< 0: 1 stop bit - 1: 1.5 stop bits - 2: 2 stop bits
@@ -381,7 +385,7 @@ typedef struct ATTR_PACKED
381385

382386
TU_VERIFY_STATIC(sizeof(cdc_line_coding_t) == 7, "size is not correct");
383387

384-
typedef struct ATTR_PACKED
388+
typedef struct TU_ATTR_PACKED
385389
{
386390
uint16_t dte_is_present : 1; ///< Indicates to DCE if DTE is presentor not. This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR.
387391
uint16_t half_duplex_carrier_control : 1;

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/class/cdc/cdc_device.c

+12-13
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,18 @@ typedef struct
7373
//--------------------------------------------------------------------+
7474
CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
7575

76-
// TODO will be replaced by dcd_edpt_busy()
77-
bool pending_read_from_host;
7876
static void _prep_out_transaction (uint8_t itf)
7977
{
8078
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
8179

8280
// skip if previous transfer not complete
83-
// dcd_edpt_busy() doesn't work, probably transfer is complete but not properly handled by the stack
84-
// if ( dcd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_out) ) return;
85-
if (pending_read_from_host) return;
81+
if ( usbd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_out) ) return;
8682

8783
// Prepare for incoming data but only allow what we can store in the ring buffer.
8884
uint16_t max_read = tu_fifo_remaining(&p_cdc->rx_ff);
8985
if ( max_read >= CFG_TUD_CDC_EPSIZE )
9086
{
91-
dcd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE);
92-
pending_read_from_host = true;
87+
usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE);
9388
}
9489
}
9590

@@ -183,13 +178,13 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
183178
bool tud_cdc_n_write_flush (uint8_t itf)
184179
{
185180
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
186-
TU_VERIFY( !dcd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_in) ); // skip if previous transfer not complete
181+
TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_in) ); // skip if previous transfer not complete
187182

188183
uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].tx_ff, p_cdc->epin_buf, CFG_TUD_CDC_EPSIZE);
189184
if ( count )
190185
{
191186
TU_VERIFY( tud_cdc_n_connected(itf) ); // fifo is empty if not connected
192-
TU_ASSERT( dcd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, p_cdc->epin_buf, count) );
187+
TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, p_cdc->epin_buf, count) );
193188
}
194189

195190
return true;
@@ -267,7 +262,7 @@ bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t
267262
(*p_length) = sizeof(tusb_desc_interface_t);
268263

269264
// Communication Functional Descriptors
270-
while ( TUSB_DESC_CLASS_SPECIFIC == tu_desc_type(p_desc) )
265+
while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) )
271266
{
272267
(*p_length) += tu_desc_len(p_desc);
273268
p_desc = tu_desc_next(p_desc);
@@ -298,7 +293,6 @@ bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t
298293
}
299294

300295
// Prepare for incoming data
301-
pending_read_from_host = false;
302296
_prep_out_transaction(cdc_id);
303297

304298
return true;
@@ -394,11 +388,16 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
394388
if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf);
395389

396390
// prepare for OUT transaction
397-
pending_read_from_host = false;
398391
_prep_out_transaction(itf);
399392
}
400393

401-
// nothing to do with in and notif endpoint for now
394+
// sent data
395+
// if ( ep_addr == p_cdc->ep_in )
396+
// {
397+
//
398+
// }
399+
400+
// nothing to do with notif endpoint for now
402401

403402
return true;
404403
}

0 commit comments

Comments
 (0)