Skip to content

Commit 8e823d2

Browse files
committed
Allow disabling CDC with -DCDC_DISABLED
Sometimes Arduino-based USB devices don't work because some hardware (like KVM switches) gets confused by the CDC sub-devices. This change makes it relatively easy to disable CDC at compiletime. Disabling it of course means that the serial console won't work anymore, so you need to use the reset button when flashing. CDC_DISABLED is also used in ArduinoCore-samd for the same purpose. based on https://github.com/gdsports/usb-metamorph/tree/master/USBSerPassThruLine See also NicoHood/HID#225 and arduino/Arduino#6387 and https://forum.arduino.cc/index.php?topic=545288.msg3717028#msg3717028
1 parent 60f0d0b commit 8e823d2

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

cores/arduino/CDC.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
#if defined(USBCON)
2424

25+
#ifndef CDC_ENABLED
26+
27+
#warning "! Disabled serial console via USB (CDC)!"
28+
#warning "! With this change you'll have to use the Arduino's reset button/pin to flash (upload)!"
29+
30+
#else // CDC not disabled
31+
2532
typedef struct
2633
{
2734
u32 dwDTERate;
@@ -299,4 +306,5 @@ int32_t Serial_::readBreak() {
299306

300307
Serial_ Serial;
301308

309+
#endif /* if defined(CDC_ENABLED) */
302310
#endif /* if defined(USBCON) */

cores/arduino/USBCore.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,18 @@ const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER;
6969
#define DEVICE_CLASS 0x02
7070

7171
// DEVICE DESCRIPTOR
72+
73+
#ifdef CDC_ENABLED
7274
const DeviceDescriptor USB_DeviceDescriptorIAD =
7375
D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
76+
#else // CDC_DISABLED
77+
// The default descriptor uses USB class OxEF, subclass 0x02 with protocol 1
78+
// which means "Interface Association Descriptor" - that's needed for the CDC,
79+
// but doesn't make much sense as a default for custom devices when CDC is disabled.
80+
// (0x00 means "Use class information in the Interface Descriptors" which should be generally ok)
81+
const DeviceDescriptor USB_DeviceDescriptorIAD =
82+
D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
83+
#endif
7484

7585
//==================================================================
7686
//==================================================================
@@ -328,10 +338,12 @@ int USB_Send(u8 ep, const void* d, int len)
328338
u8 _initEndpoints[USB_ENDPOINTS] =
329339
{
330340
0, // Control Endpoint
331-
341+
342+
#ifdef CDC_ENABLED
332343
EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
333344
EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
334345
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
346+
#endif
335347

336348
// Following endpoints are automatically initialized to 0
337349
};
@@ -373,10 +385,12 @@ void InitEndpoints()
373385
static
374386
bool ClassInterfaceRequest(USBSetup& setup)
375387
{
388+
#ifdef CDC_ENABLED
376389
u8 i = setup.wIndex;
377390

378391
if (CDC_ACM_INTERFACE == i)
379392
return CDC_Setup(setup);
393+
#endif
380394

381395
#ifdef PLUGGABLE_USB_ENABLED
382396
return PluggableUSB().setup(setup);
@@ -466,7 +480,9 @@ static u8 SendInterfaces()
466480
{
467481
u8 interfaces = 0;
468482

483+
#ifdef CDC_ENABLED
469484
CDC_GetInterface(&interfaces);
485+
#endif
470486

471487
#ifdef PLUGGABLE_USB_ENABLED
472488
PluggableUSB().getInterface(&interfaces);

cores/arduino/USBDesc.h

+17
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,25 @@
2626

2727
#define ISERIAL_MAX_LEN 20
2828

29+
// Uncomment the following line or pass -DCDC_DISABLED to the compiler
30+
// to disable CDC (serial console via USB).
31+
// That's useful if you want to create an USB device (like an USB Boot Keyboard)
32+
// that works even with problematic devices (like KVM switches).
33+
// Keep in mind that with this change you'll have to use the Arduino's
34+
// reset button to be able to flash it.
35+
//#define CDC_DISABLED
36+
37+
#ifndef CDC_DISABLED
38+
#define CDC_ENABLED
39+
#endif
40+
41+
#ifdef CDC_ENABLED
2942
#define CDC_INTERFACE_COUNT 2
3043
#define CDC_ENPOINT_COUNT 3
44+
#else // CDC_DISABLED
45+
#define CDC_INTERFACE_COUNT 0
46+
#define CDC_ENPOINT_COUNT 0
47+
#endif
3148

3249
#define CDC_ACM_INTERFACE 0 // CDC ACM
3350
#define CDC_DATA_INTERFACE 1 // CDC Data

0 commit comments

Comments
 (0)