@@ -25,8 +25,7 @@ ESP_EVENT_DEFINE_BASE(ARDUINO_USB_CDC_EVENTS);
25
25
esp_err_t arduino_usb_event_post (esp_event_base_t event_base, int32_t event_id, void *event_data, size_t event_data_size, TickType_t ticks_to_wait);
26
26
esp_err_t arduino_usb_event_handler_register_with (esp_event_base_t event_base, int32_t event_id, esp_event_handler_t event_handler, void *event_handler_arg);
27
27
28
- #define MAX_USB_CDC_DEVICES 2
29
- USBCDC *devices[MAX_USB_CDC_DEVICES] = {NULL , NULL };
28
+ USBCDC *devices[CFG_TUD_CDC];
30
29
31
30
static uint16_t load_cdc_descriptor (uint8_t *dst, uint8_t *itf) {
32
31
uint8_t str_index = tinyusb_add_string_descriptor (" TinyUSB CDC" );
@@ -38,23 +37,42 @@ static uint16_t load_cdc_descriptor(uint8_t *dst, uint8_t *itf) {
38
37
return TUD_CDC_DESC_LEN;
39
38
}
40
39
40
+ static uint16_t load_cdc_descriptor2 (uint8_t *dst, uint8_t *itf) {
41
+ uint8_t str_index = tinyusb_add_string_descriptor (" TinyUSB CDC2" );
42
+ uint8_t ep_ntfy = tinyusb_get_free_in_endpoint ();
43
+ TU_VERIFY (ep_ntfy != 0 );
44
+ uint8_t ep_in = tinyusb_get_free_in_endpoint ();
45
+ TU_VERIFY (ep_in != 0 );
46
+ uint8_t ep_out = tinyusb_get_free_out_endpoint ();
47
+ TU_VERIFY (ep_out != 0 );
48
+ uint8_t descriptor[TUD_CDC_DESC_LEN] = {// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
49
+ TUD_CDC_DESCRIPTOR (*itf, str_index, (uint8_t )(0x80 | ep_ntfy), CFG_TUD_ENDOINT_SIZE, ep_out, (uint8_t )(0x80 | ep_in), CFG_TUD_ENDOINT_SIZE)
50
+ };
51
+ *itf += 2 ;
52
+ memcpy (dst, descriptor, TUD_CDC_DESC_LEN);
53
+ return TUD_CDC_DESC_LEN;
54
+ }
55
+
41
56
// Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE
42
57
void tud_cdc_line_state_cb (uint8_t itf, bool dtr, bool rts) {
43
- if (itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL ) {
58
+ // log_v("ITF: %u, DTR: %u, RTS: %u", itf, dtr, rts);
59
+ if (itf < CFG_TUD_CDC && devices[itf] != NULL ) {
44
60
devices[itf]->_onLineState (dtr, rts);
45
61
}
46
62
}
47
63
48
64
// Invoked when line coding is change via SET_LINE_CODING
49
65
void tud_cdc_line_coding_cb (uint8_t itf, cdc_line_coding_t const *p_line_coding) {
50
- if (itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL ) {
66
+ // log_v("ITF: %u, BITRATE: %lu, STOP_BITS: %u, PARITY: %u, DATA_BITS: %u", itf, p_line_coding->bit_rate, p_line_coding->stop_bits, p_line_coding->parity, p_line_coding->data_bits);
67
+ if (itf < CFG_TUD_CDC && devices[itf] != NULL ) {
51
68
devices[itf]->_onLineCoding (p_line_coding->bit_rate , p_line_coding->stop_bits , p_line_coding->parity , p_line_coding->data_bits );
52
69
}
53
70
}
54
71
55
72
// Invoked when received new data
56
73
void tud_cdc_rx_cb (uint8_t itf) {
57
- if (itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL ) {
74
+ // log_v("ITF: %u", itf);
75
+ if (itf < CFG_TUD_CDC && devices[itf] != NULL ) {
58
76
devices[itf]->_onRX ();
59
77
}
60
78
}
@@ -66,13 +84,13 @@ void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms) {
66
84
67
85
// Invoked when space becomes available in TX buffer
68
86
void tud_cdc_tx_complete_cb (uint8_t itf) {
69
- if (itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL ) {
87
+ if (itf < CFG_TUD_CDC && devices[itf] != NULL ) {
70
88
devices[itf]->_onTX ();
71
89
}
72
90
}
73
91
74
92
static void ARDUINO_ISR_ATTR cdc0_write_char (char c) {
75
- if (devices[0 ] != NULL ) {
93
+ if (CFG_TUD_CDC && devices[0 ] != NULL ) {
76
94
tud_cdc_n_write_char (0 , c);
77
95
}
78
96
}
@@ -84,9 +102,15 @@ static void usb_unplugged_cb(void *arg, esp_event_base_t event_base, int32_t eve
84
102
USBCDC::USBCDC (uint8_t itfn)
85
103
: itf(itfn), bit_rate(0 ), stop_bits(0 ), parity(0 ), data_bits(0 ), dtr(false ), rts(false ), connected(false ), reboot_enable(true ), rx_queue(NULL ), tx_lock(NULL ),
86
104
tx_timeout_ms(250 ) {
87
- tinyusb_enable_interface (USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
88
- if (itf < MAX_USB_CDC_DEVICES) {
105
+ if (itf < CFG_TUD_CDC) {
106
+ if (itf == 0 ) {
107
+ tinyusb_enable_interface (USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
108
+ } else {
109
+ tinyusb_enable_interface (USB_INTERFACE_CDC2, TUD_CDC_DESC_LEN, load_cdc_descriptor2);
110
+ }
89
111
arduino_usb_event_handler_register_with (ARDUINO_USB_EVENTS, ARDUINO_USB_STOPPED_EVENT, usb_unplugged_cb, this );
112
+ } else {
113
+ log_e (" Maximum of %u CDC devices are supported" , CFG_TUD_CDC);
90
114
}
91
115
}
92
116
@@ -142,6 +166,9 @@ size_t USBCDC::setRxBufferSize(size_t rx_queue_len) {
142
166
}
143
167
144
168
void USBCDC::begin (unsigned long baud) {
169
+ if (itf >= CFG_TUD_CDC) {
170
+ return ;
171
+ }
145
172
if (tx_lock == NULL ) {
146
173
tx_lock = xSemaphoreCreateMutex ();
147
174
}
@@ -153,6 +180,9 @@ void USBCDC::begin(unsigned long baud) {
153
180
}
154
181
155
182
void USBCDC::end () {
183
+ if (itf >= CFG_TUD_CDC) {
184
+ return ;
185
+ }
156
186
connected = false ;
157
187
devices[itf] = NULL ;
158
188
setRxBufferSize (0 );
@@ -298,14 +328,14 @@ bool USBCDC::rebootEnabled(void) {
298
328
}
299
329
300
330
int USBCDC::available (void ) {
301
- if (itf >= MAX_USB_CDC_DEVICES || rx_queue == NULL ) {
331
+ if (itf >= CFG_TUD_CDC || rx_queue == NULL ) {
302
332
return -1 ;
303
333
}
304
334
return uxQueueMessagesWaiting (rx_queue);
305
335
}
306
336
307
337
int USBCDC::peek (void ) {
308
- if (itf >= MAX_USB_CDC_DEVICES || rx_queue == NULL ) {
338
+ if (itf >= CFG_TUD_CDC || rx_queue == NULL ) {
309
339
return -1 ;
310
340
}
311
341
uint8_t c;
@@ -316,7 +346,7 @@ int USBCDC::peek(void) {
316
346
}
317
347
318
348
int USBCDC::read (void ) {
319
- if (itf >= MAX_USB_CDC_DEVICES || rx_queue == NULL ) {
349
+ if (itf >= CFG_TUD_CDC || rx_queue == NULL ) {
320
350
return -1 ;
321
351
}
322
352
uint8_t c = 0 ;
@@ -327,7 +357,7 @@ int USBCDC::read(void) {
327
357
}
328
358
329
359
size_t USBCDC::read (uint8_t *buffer, size_t size) {
330
- if (itf >= MAX_USB_CDC_DEVICES || rx_queue == NULL ) {
360
+ if (itf >= CFG_TUD_CDC || rx_queue == NULL ) {
331
361
return -1 ;
332
362
}
333
363
uint8_t c = 0 ;
@@ -339,7 +369,7 @@ size_t USBCDC::read(uint8_t *buffer, size_t size) {
339
369
}
340
370
341
371
void USBCDC::flush (void ) {
342
- if (itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || !tud_cdc_n_connected (itf)) {
372
+ if (itf >= CFG_TUD_CDC || tx_lock == NULL || !tud_cdc_n_connected (itf)) {
343
373
return ;
344
374
}
345
375
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS) {
@@ -350,7 +380,7 @@ void USBCDC::flush(void) {
350
380
}
351
381
352
382
int USBCDC::availableForWrite (void ) {
353
- if (itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || !tud_cdc_n_connected (itf)) {
383
+ if (itf >= CFG_TUD_CDC || tx_lock == NULL || !tud_cdc_n_connected (itf)) {
354
384
return 0 ;
355
385
}
356
386
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS) {
@@ -362,7 +392,7 @@ int USBCDC::availableForWrite(void) {
362
392
}
363
393
364
394
size_t USBCDC::write (const uint8_t *buffer, size_t size) {
365
- if (itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || buffer == NULL || size == 0 || !tud_cdc_n_connected (itf)) {
395
+ if (itf >= CFG_TUD_CDC || tx_lock == NULL || buffer == NULL || size == 0 || !tud_cdc_n_connected (itf)) {
366
396
return 0 ;
367
397
}
368
398
if (xPortInIsrContext ()) {
@@ -415,6 +445,9 @@ uint32_t USBCDC::baudRate() {
415
445
}
416
446
417
447
void USBCDC::setDebugOutput (bool en) {
448
+ if (itf) {
449
+ return ;
450
+ }
418
451
if (en) {
419
452
uartSetDebug (NULL );
420
453
ets_install_putc2 ((void (*)(char )) & cdc0_write_char);
@@ -424,7 +457,7 @@ void USBCDC::setDebugOutput(bool en) {
424
457
}
425
458
426
459
USBCDC::operator bool () const {
427
- if (itf >= MAX_USB_CDC_DEVICES ) {
460
+ if (itf >= CFG_TUD_CDC ) {
428
461
return false ;
429
462
}
430
463
return connected;
0 commit comments