TinyUSB definition errors on ESP32s3

There seems to be a PlatformIO problem with the order of includes – the file “tusb_config.h” in the Adafruit library wants to

#elif defined(ARDUINO_ARCH_ESP32)
  // Use the BSP sdk/include/arduino_tinyusb/include/tusb_config.h
  #include <tusb_config.h>

but the include order in PlatformIO is so that libraries always come first before the core, so it will try to include itself but is then stopped by the include guards, leaving all the critical macros undefined.

I could temporarily fix that with

build_unflags = -DARDUINO_USB_MODE=1
build_flags =
   -DUSE_TINYUSB
   -DARDUINO_USB_MODE=0 
   '-DCFG_TUSB_CONFIG_FILE="C:\\Users\\Max\\.platformio\\packages\\framework-arduinoespressif32\\tools\\sdk\\esp32s3\\include\\arduino_tinyusb\\include\\tusb_config.h"'

but that requires a full path. So instead I decided to just copy the tusb_config.h file in the include/ folder of the project as tusb_config_esp32.h and do

build_flags =
   -DUSE_TINYUSB
   -DARDUINO_USB_MODE=0 
   -Iinclude/
   '-DCFG_TUSB_CONFIG_FILE="tusb_config_esp32.h"'

The only compile error is then

.pio/libdeps/esp32-s3-devkitc-1/Adafruit TinyUSB Library/src/arduino/webusb/Adafruit_USBD_WebUSB.cpp: In constructor 'Adafruit_USBD_WebUSB::Adafruit_USBD_WebUSB(const void*)':
.pio/libdeps/esp32-s3-devkitc-1/Adafruit TinyUSB Library/src/arduino/webusb/Adafruit_USBD_WebUSB.cpp:151:3: error: 'USB' was not declared in this scope
   USB.usbVersion(0x0210);
   ^~~

because apparently even in the latest TinyUSB version, they forget to include the USB.h header so that the USB object is declared. So I added that include to the Adafruit_USBD_WebUSB.cpp file.

And then…

Linking .pio\build\esp32-s3-devkitc-1\firmware.elf
Retrieving maximum program size .pio\build\esp32-s3-devkitc-1\firmware.elf
Checking size .pio\build\esp32-s3-devkitc-1\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.2% (used 13900 bytes from 327680 bytes)
Flash: [=         ]   6.6% (used 219865 bytes from 3342336 bytes)
Building .pio\build\esp32-s3-devkitc-1\firmware.bin
esptool.py v3.3
Creating esp32s3 image...
Merged 2 ELF sections
Successfully created esp32s3 image.
=== [SUCCESS] Took 30.96 seconds ===

I reported the fix in Fix compile error for missing "USB" definition by maxgerhardt · Pull Request #202 · adafruit/Adafruit_TinyUSB_Arduino · GitHub and you can use the reference project GitHub - maxgerhardt/pio-esp32s3-tinyusb: PlatformIO example that uses ESP32S3 with TinyUSB..

It only compiles, since I don’t have a board, I can’t test further.