From 22d7fc91eb7e146913c2ca02a155c33e938acf09 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Tue, 27 Feb 2024 17:32:19 +0200 Subject: [PATCH] Enable USB Serial only if selected (#9294) * Enable USB Serial only if selected * fix examples --------- Co-authored-by: Rodrigo Garcia --- cores/esp32/HWCDC.cpp | 2 +- cores/esp32/HWCDC.h | 2 +- cores/esp32/USBCDC.cpp | 2 +- cores/esp32/USBCDC.h | 2 +- libraries/USB/examples/CompositeDevice/CompositeDevice.ino | 4 ++++ libraries/USB/examples/FirmwareMSC/FirmwareMSC.ino | 1 - libraries/USB/examples/USBMSC/USBMSC.ino | 1 - libraries/USB/examples/USBSerial/USBSerial.ino | 4 ++++ 8 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index f0539d69b5d..79ff84dcc91 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -436,7 +436,7 @@ void HWCDC::setDebugOutput(bool en) } } -#if ARDUINO_USB_MODE // Hardware JTAG CDC selected +#if ARDUINO_USB_MODE && ARDUINO_USB_CDC_ON_BOOT // Hardware JTAG CDC selected // USBSerial is always available to be used HWCDC HWCDCSerial; #endif diff --git a/cores/esp32/HWCDC.h b/cores/esp32/HWCDC.h index 30a1a8fbb56..d09962861ec 100644 --- a/cores/esp32/HWCDC.h +++ b/cores/esp32/HWCDC.h @@ -102,7 +102,7 @@ class HWCDC: public Stream uint32_t baudRate(){return 115200;} }; -#if ARDUINO_USB_MODE // Hardware JTAG CDC selected +#if ARDUINO_USB_MODE && ARDUINO_USB_CDC_ON_BOOT // Hardware JTAG CDC selected #ifndef HWCDC_SERIAL_IS_DEFINED #define HWCDC_SERIAL_IS_DEFINED 1 #endif diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index c99659dadac..85eb5f77855 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -455,7 +455,7 @@ USBCDC::operator bool() const return connected; } -#if !ARDUINO_USB_MODE // Native USB CDC selected +#if !ARDUINO_USB_MODE && ARDUINO_USB_CDC_ON_BOOT // Native USB CDC selected // USBSerial is always available to be used USBCDC USBSerial(0); #endif diff --git a/cores/esp32/USBCDC.h b/cores/esp32/USBCDC.h index 4005f23bc1e..17c782671b2 100644 --- a/cores/esp32/USBCDC.h +++ b/cores/esp32/USBCDC.h @@ -141,7 +141,7 @@ class USBCDC: public Stream }; -#if !ARDUINO_USB_MODE // Native USB CDC selected +#if !ARDUINO_USB_MODE && ARDUINO_USB_CDC_ON_BOOT // Native USB CDC selected #ifndef USB_SERIAL_IS_DEFINED #define USB_SERIAL_IS_DEFINED 1 #endif diff --git a/libraries/USB/examples/CompositeDevice/CompositeDevice.ino b/libraries/USB/examples/CompositeDevice/CompositeDevice.ino index 5a42662877e..e8711feb34e 100644 --- a/libraries/USB/examples/CompositeDevice/CompositeDevice.ino +++ b/libraries/USB/examples/CompositeDevice/CompositeDevice.ino @@ -18,6 +18,10 @@ void loop(){} FirmwareMSC MSC_Update; #endif +#if !ARDUINO_USB_CDC_ON_BOOT +USBCDC USBSerial; +#endif + USBHID HID; USBHIDKeyboard Keyboard; USBHIDMouse Mouse; diff --git a/libraries/USB/examples/FirmwareMSC/FirmwareMSC.ino b/libraries/USB/examples/FirmwareMSC/FirmwareMSC.ino index 31007068a91..3259c702030 100644 --- a/libraries/USB/examples/FirmwareMSC/FirmwareMSC.ino +++ b/libraries/USB/examples/FirmwareMSC/FirmwareMSC.ino @@ -65,7 +65,6 @@ void setup() { USB.onEvent(usbEventCallback); MSC_Update.onEvent(usbEventCallback); MSC_Update.begin(); - USBSerial.begin(); USB.begin(); } diff --git a/libraries/USB/examples/USBMSC/USBMSC.ino b/libraries/USB/examples/USBMSC/USBMSC.ino index 9dde1da3c0a..e19822449bd 100644 --- a/libraries/USB/examples/USBMSC/USBMSC.ino +++ b/libraries/USB/examples/USBMSC/USBMSC.ino @@ -182,7 +182,6 @@ void setup() { MSC.onWrite(onWrite); MSC.mediaPresent(true); MSC.begin(DISK_SECTOR_COUNT, DISK_SECTOR_SIZE); - USBSerial.begin(); USB.begin(); } diff --git a/libraries/USB/examples/USBSerial/USBSerial.ino b/libraries/USB/examples/USBSerial/USBSerial.ino index 9e8de7c8f36..0dcc4fc3ff6 100644 --- a/libraries/USB/examples/USBSerial/USBSerial.ino +++ b/libraries/USB/examples/USBSerial/USBSerial.ino @@ -7,6 +7,10 @@ void loop(){} #else #include "USB.h" +#if !ARDUINO_USB_CDC_ON_BOOT +USBCDC USBSerial; +#endif + static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){ if(event_base == ARDUINO_USB_EVENTS){ arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;