Skip to content

Commit 357e656

Browse files
committed
multiple cdc example: check for CFG_TUD_CDC to be at least 2
add note that esp32-sx is not supported yet (more works needed).
1 parent 489752a commit 357e656

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

examples/CDC/cdc_multi/cdc_multi.ino

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,45 @@
1515
*
1616
* Requirement:
1717
* The max number of CDC ports (CFG_TUD_CDC) has to be changed to at least 2.
18-
* Config file is located in Adafruit_TinyUSB_Arduino/src/arduino/ports/platform/tusb_config_platform.h
19-
* where platform is one of: esp32, nrf, rp2040, samd
18+
* Config file is located in Adafruit_TinyUSB_Arduino/src/arduino/ports/{platform}/tusb_config_{platform}.h
19+
* where platform is one of: nrf, rp2040, samd
20+
*
21+
* NOTE: Currnetly multiple CDCs on ESP32-Sx is not yet supported.
22+
* An PR to update core/esp32/USBCDC and/or pre-built libusb are needed.
23+
* We would implement this later when we could.
2024
*/
2125

2226
#include <Adafruit_TinyUSB.h>
2327

2428
#define LED LED_BUILTIN
2529

26-
// Create extra USB Serial Ports. "Serial" is already created.
27-
Adafruit_USBD_CDC USBSer1;
30+
// Create 2nd instance of CDC Ports.
31+
#ifdef ARDUINO_ARCH_ESP32
32+
#error "Currnetly multiple CDCs on ESP32-Sx is not yet supported. An PR to update core/esp32/USBCDC and/or pre-built libusb are needed."
33+
// for ESP32, we need to specify instance number when declaring object
34+
Adafruit_USBD_CDC USBSer1(1);
35+
#else
36+
Adafruit_USBD_CDC USBSer1;
37+
#endif
2838

2939
void setup() {
3040
pinMode(LED, OUTPUT);
3141

32-
// start up all of the USB Vitual ports, and wait for them to enumerate.
3342
Serial.begin(115200);
43+
44+
// check to see if multiple CDCs are enabled
45+
if ( CFG_TUD_CDC < 2 ) {
46+
digitalWrite(LED, HIGH); // LED on for error indicator
47+
48+
while(1) {
49+
Serial.printf("CFG_TUD_CDC must be at least 2, current value is %u\n", CFG_TUD_CDC);
50+
Serial.println(" Config file is located in Adafruit_TinyUSB_Arduino/src/arduino/ports/{platform}/tusb_config_{platform}.h");
51+
Serial.println(" where platform is one of: nrf, rp2040, samd");
52+
delay(1000);
53+
}
54+
}
55+
56+
// initialize 2nd CDC interface
3457
USBSer1.begin(115200);
3558

3659
while (!Serial || !USBSer1) {

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@
2727

2828
#include "Adafruit_TinyUSB_API.h"
2929

30-
#if defined(__cplusplus) && !defined(ARDUINO_ARCH_ESP32)
30+
#if defined(__cplusplus)
31+
32+
33+
#if defined(ARDUINO_ARCH_ESP32)
34+
35+
// For ESP32 use USBCDC as it is compatible
36+
#define Adafruit_USBD_CDC USBCDC
37+
38+
#else
3139

3240
#include "Adafruit_USBD_Interface.h"
3341
#include "Stream.h"
@@ -96,5 +104,7 @@ extern Adafruit_USBD_CDC Serial;
96104
extern Adafruit_USBD_CDC SerialTinyUSB;
97105
#endif
98106

107+
#endif // else of ESP32
99108
#endif // __cplusplus
109+
100110
#endif

0 commit comments

Comments
 (0)