ESP32S3 - Use TinyUSB with arduino and espidf frameworks

Hey,

I have an Arduino UNO R4 WiFi board with RA4M1 and ESP32S3 on board.
I’m trying to replicate this project GitHub - vshymanskyy/UNO-R4-WiFi-freedom: UNO R4 WiFi set free! while using both espidf and arduino frameworks, as shown in this example: platform-espressif32/examples/espidf-arduino-blink at develop · platformio/platform-espressif32 · GitHub.

The reason I want to do that is I want to have access to ESP-IDF framework for using Bluepad32, which I hope to connect with RA4M1 using the serial communication between two MCUs. However, I had some trouble with using ESP-IDF’s serial connection directly, and I figured I don’t want to reinvent the wheel, and just use Arduino’s implementations, which are way easier to use.

As the Freedom library is using TinyUSB, I tried enabling it step-by-step in my example project. As soon as I add -DCONFIG_TINYUSB_ENABLED to my CMakeLists.txt file, I get this error:

In file included from /home/seba/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-tinyusb.c:36:
/home/seba/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-tinyusb.h:24:10: fatal error: tusb.h: No such file or directory

**************************************************************
* Looking for tusb.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:tusb.h"
* Web  > https://registry.platformio.org/search?q=header:tusb.h
*
**************************************************************

 #include "tusb.h"
          ^~~~~~~~
compilation terminated.
Compiling .pio/build/esp32s3/Tone.cpp.o
*** [.pio/build/esp32s3/esp32-hal-tinyusb.c.o] Error 1

I don’t have the slightest idea why this is not included. In main.cpp, I can #include "tusb.h" and call tusb_init, and it compiles, but for some reason this header is not visible from the esp32-hal-tinyusb.c file.

This is my platformio.ini file:

[env]
platform = espressif32
framework = arduino, espidf
monitor_speed = 115200

[env:esp32s3]
board = esp32-s3-devkitc-1

CMakeLists.txt in root folder:

cmake_minimum_required(VERSION 3.16.0)

add_definitions(-DCONFIG_TINYUSB_ENABLED)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp-tests-1)

I’ve tried also using the esp32_uno_r4 board included in the Freedom project, but nothing changes.

Any help would be appreciated.

I don’t think that’s the right way; Instead, you will want to configure that in the “menuconfig” project task and menu: https://docs.platformio.org/en/latest/frameworks/espidf.html#configuration.

To get you started, you should delete your current sdkconfig.esp32s3, copy over the example sdkconfig.defaults into your project (same level as your `platformio.ini), and then run the menuconfig. The config will then have those default options plus TinyUSB if you configure it so.

@maxgerhardt I have tried doing all configurations via the menuconfig command. However, enabling TinyUSB support only seems to set CONFIG_TINYUSB=y in the sdkconfig file.

I manually set CONFIG_TINYUSB_ENABLED in CMakeLists.txt because there are libraries that use this definition, like USB.h in framework-arduinoespressif32:

/ ** /
#include "sdkconfig.h"

#if CONFIG_TINYUSB_ENABLED

#include "esp_event.h"
#include "USBCDC.h"

#define ARDUINO_USB_ON_BOOT (ARDUINO_USB_CDC_ON_BOOT|ARDUINO_USB_MSC_ON_BOOT|ARDUINO_USB_DFU_ON_BOOT)

ESP_EVENT_DECLARE_BASE(ARDUINO_USB_EVENTS);
/ ** /

That helped with some of the compilation steps.
This variable is also used in CMakeLists.txt of the mentioned library:

# CMakeLists.txt of framework-arduinoespressif32
if(IDF_TARGET MATCHES "esp32s2|esp32s3" AND CONFIG_TINYUSB_ENABLED)
    maybe_add_component(arduino_tinyusb)
endif()

When I tried adding some debug messages though, it does not seem to be picked up by CMake here:

# CMakeLists.txt of framework-arduinoespressif32
if(IDF_TARGET MATCHES "esp32s2|esp32s3")
    message("IDF_TARGET Matched: ${IDF_TARGET}")
else()
    message("IDF_TARGET NOT Matched: ${IDF_TARGET}")
endif()

if(CONFIG_TINYUSB_ENABLED)
    message("CONFIG_TINYUSB_ENABLED is true")
else()
    message("CONFIG_TINYUSB_ENABLED is NOT true")
endif()
# pio run -e esp32s3 --verbose
IDF_TARGET Matched: esp32s3
CONFIG_TINYUSB_ENABLED is NOT true

And this is what I see in menuconfig:
image

I tried also removing the checks for maybe_add_component(arduino_tinyusb), however it wasn’t added as well, because arduino_tinyusb is not included in BUILD_COMPONENTS checked in maybe_add_component function:

# message("${BUILD_COMPONENTS}")
esp_ringbuf
efuse
esp_timer
esp_ipc
driver
esp_pm
mbedtls
bootloader
esptool_py
partition_table
app_update
bootloader_support
spi_flash
nvs_flash
pthread
esp_gdbstub
espcoredump
esp_phy
esp_system
xtensa
esp32s3
esp_common
esp_rom
hal
vfs
esp_eth
tcpip_adapter
esp_netif
esp_event
wpa_supplicant
esp_wifi
ieee802154
console
openthread
lwip
log
heap
soc
esp_hw_support
app_trace
freertos
newlib
cxx
__pio_env
asio
bt
btstack
cbor
unity
cmock
coap
nghttp
esp-tls
esp_adc_cal
esp_hid
tcp_transport
esp_http_client
esp_http_server
esp_https_ota
esp_https_server
esp_lcd
protobuf-c
protocomm
mdns
esp_local_ctrl
sdmmc
esp_serial_slave_link
esp_websocket_client
expat
wear_levelling
fatfs
freemodbus
idf_test
jsmn
json
libsodium
mqtt
openssl
perfmon
spiffs
usb
tinyusb
ulp
wifi_provisioning
src
framework-arduinoespressif32

So maybe menuconfig sets the invalid variable, or does not also set the required one?