How to enable both USB MSC (Mass Storage) and CDC simultaneously on ESP32 (Arduino framework)?

Hi everyone,

I’m trying to enable both USB Mass Storage (MSC) and USB CDC (Serial) simultaneously on an ESP32 using the Arduino framework in PlatformIO.

Currently, I have the following configuration in my platformio.ini:

build_flags =
-D ARDUINO_USB_MODE=1
-D ARDUINO_USB_CDC_ON_BOOT=0

With this setup, CDC and JTAG over USB work fine.
However, when I try to initialize the MSC interface like this:

msc.vendorID(“xxxxxx”);
msc.productID(“xxxxxx”);
msc.productRevision(“xxxxxx”);
msc.onRead(OnReadFunction);
msc.onWrite(OnWriteFunction);
msc.onStartStop(OnStartStopFunction);
msc.mediaPresent(true);
if (!msc.begin(SD.numSectors(), SD.sectorSize())) {
    LOG_E(“MSC initialization failed!”);
}
USBSerial.onEvent(USBEventCallbackFunction);
USBSerial.begin(115200);
vTaskDelay(pdMS_TO_TICKS(100));
if (!USB.begin()) {
    LOG_E(“USB initialization failed!”);
}

it seems that calling USB.begin() overrides the CDC interface — MSC works, but the serial port disappears.

If I remove USB.begin(), the CDC interface works again, but MSC stops working.

So my question is:
Is it possible to enable both USB MSC and CDC simultaneously on ESP32 in the Arduino environment?
And if so, should I use ARDUINO_USB_MODE=0 and ARDUINO_USB_CDC_ON_BOOT=0 to have full control of both interfaces manually?

Any guidance or working example would be greatly appreciated!

Thanks in advance.

Check the example!

It starts with

#ifndef ARDUINO_USB_MODE
#error This ESP32 SoC has no Native USB interface
#elif ARDUINO_USB_MODE == 1
#warning This sketch should be used when USB is in OTG mode
void setup() {}
void loop() {}
#else

As you can see -D ARDUINO_USB_MODE=1 won’t work. You have to use -D ARDUINO_USB_MODE=0.

I managed to find a solution.
The issue was caused by my terminal settings and the configuration

DARDUINO_USB_MODE=1

which was being set by the board configuration. Now, by configuring only with

-DARDUINO_USB_MODE=0

I can use the USBCDC class simultaneously with USBMSC. Excellent.

PS: The MSC seems to work with both -DARDUINO_USB_MODE=0 and -DARDUINO_USB_MODE=1. It’s a specific condition of that example, not a general one.