ESP32-S3 ESP-IDF Arduino as a Component USB + Debug

Hi,
I’ve recently transitioned a project away from purely ESP-IDF to integrate a small number of Arduino specific libraries and am hitting some roadblocks regarding USB configuration. What I need to achieve is:
Adafruit TinyUSB (specifically USB MIDI, HID, and CDC). I’m also using raw esp-idf for USB host handling, however that is not configured dynamically, but is selected at boot by non-volatile configuration (in case it impacts solutions).
I would like to retain TX0 and RX0 serial both for debug/log output, and also as an application uart.

Combining PIO management, Arduino quirks, and the IDF sensibilities around compilation and configuration is proving a problem with some combination of Serial.xxxz and/or Serial0.xxxx being flagged as compilation errors.

I’m happy to perform log/debug output functions using built in idf calls such as printf or log_e, however, several Arduino libraries get upset if Serial is not declared etc…

I’m hoping for some help in what combination of ARDUINO_USB_MODE and other related build flags, menu config settings, and tinyusb configuration can allow everything to play nicely.
Cheers

Sound’s like you’re looking for these settings:

build_flags = 
  -DARDUINO_USB_MODE=0
  -DARDUINO_USB_CDC_ON_BOOT=0

Explanation:

ARDUINO_USB_MODE = 0:
USB Software Stack (like TinyUSB)

ARDUINO_USB_CDC_ON_BOOT = 0:
The Serial object will use UART (RX & TX Pins).

If your board manifest file already defines these settings, you must first undefine them:

build_unflags = 
  -DARDUINO_USB_MODE
  -DARDUINO_USB_CDC_ON_BOOT

Hmm, I’ve tried that and run into some compilation errors previously, I must’ve missed something. Thanks for that. Now I’m having an absolute hell of a time getting it to find my tusb_config file.

I’ve tried adding it to both the include folder, and src folder, by using:

-I include/
'-D CFG_TUSB_CONFIG_FILE = "include/tusb_config.h"'

Along with what feels like every possible combination.

Why do you need the (complicated) external Adafruit library?
You can use the builtin libraries.

All you need (and much more) is available:

  • Keyboard
  • Mouse
  • MIDI

Check the examples at arduino-esp32/libraries/USB/examples at master · espressif/arduino-esp32 · GitHub

I would agree if I was starting from scratch, however I’d like to reuse substantial portions from a previous, well tested code base.

I’m not adverse to switching if it’s the only way, but given it seems like a reasonable use case to tie in with existing dependencies, wrangling PIO into playing nicely would be great.

Okay, reasonable point.

I can’t say anything more about the Adafruit libraries.
I try to avoid them wherever possible.

Another point to consider is that when compiling with the Arduino core as a component, adding the tinyusb IDF components causes significant conflicts when compiling due to the various tinyusb declarations. It does not seem easily solvable to use the ESP-IDF tinyusb device stack with the Arduino ESP32 core as well.

In my opinion, an “Arduino as ESP-IDF Component Project” only makes sense if you want to change the precompiled libraries or the project settings (sdkconfig), which is not possible with a pure Arduino project. I would not mix the project with other ESP-IDF components and keep the code in Arduino style with Arduino compatible libraries.

Otherwise, the project should be set up as a pure ESP-IDF project and do without Arduino code and libraries completely.

– All this is my opinion –

Again, there are some valid reasons to mix them. One thing I haven’t done yet is gone down the avenue of recompiling the Arduino core as I was able to get some progress testing purely with the Arduino framework, but need a larger CONFIG_USB_HOST_CONTROL_TRANSFER_MAX_SIZE than the standard 256.

Either that or being able to get past the Adafruit TinyUSB compilation and include issues.
@maxgerhardt any thoughts?