ESP32P4 Host mode

robin2906
Posts: 9
Joined: Tue May 06, 2025 4:50 pm

Re: ESP32P4 Host mode

Postby robin2906 » Mon May 12, 2025 8:10 am

Hi @role_espressif

Thank you for your message,

I am a bit disappointed by your answer, espressif/tinyusb is a fork of tinyusb, modified to include the necessary instructions to compile with esp-idf rigth ?

As tinyusb stock library support esp32p4 in host mode it should be possible to compile with esp-idf, it's just a matter of include the relevant files no ?

I made a diff between espressif/tinyusb and /tinyusb, I saw that Espressif add a CMakeLists.txt at the root.

Code: Select all

idf_build_get_property(target IDF_TARGET)

if(${target} STREQUAL "esp32s3")
    set(tusb_mcu "OPT_MCU_ESP32S3")
    set(tusb_family "esp32sx")
elseif(${target} STREQUAL "esp32s2")
    set(tusb_mcu "OPT_MCU_ESP32S2")
    set(tusb_family "esp32sx")
elseif(${target} STREQUAL "esp32p4")
    set(tusb_mcu "OPT_MCU_ESP32P4")
    set(tusb_family "esp32px")
endif()

set(compile_options
    "-DCFG_TUSB_MCU=${tusb_mcu}"
    )

idf_component_get_property(freertos_include freertos ORIG_INCLUDE_PATH)

set(includes_private
    "src/"
    "src/device"
    "lib/networking" # For RNDIS definitions
    )

set(includes_public
    "src/"
    # The FreeRTOS API include convention in tinyusb is different from esp-idf
    "${freertos_include}"
    )

set(srcs
    "src/class/cdc/cdc_device.c"
    "src/class/hid/hid_device.c"
    "src/class/midi/midi_device.c"
    "src/class/msc/msc_device.c"
    "src/class/vendor/vendor_device.c"
    "src/class/audio/audio_device.c"
    "src/class/video/video_device.c"
    "src/class/bth/bth_device.c"
    # NET class
    "src/class/net/ecm_rndis_device.c"
    "lib/networking/rndis_reports.c"
    "src/class/net/ncm_device.c"
    # DFU
    "src/class/dfu/dfu_device.c"
    "src/class/dfu/dfu_rt_device.c"
    # Common, device-mode related
    "src/portable/synopsys/dwc2/dcd_dwc2.c"
    "src/portable/synopsys/dwc2/dwc2_common.c"
    "src/common/tusb_fifo.c"
    "src/device/usbd_control.c"
    "src/device/usbd.c"
    "src/tusb.c"
    )

set(requirements_private
    esp_netif   # required by rndis_reports.c: #include "netif/ethernet.h"
    )

if(${target} STREQUAL "esp32p4")
    list(APPEND requirements_private
                esp_mm # required by dcd_dwc2.c: #include "esp_cache.h"
        )
endif()

idf_component_register(SRCS ${srcs}
                       INCLUDE_DIRS ${includes_public}
                       PRIV_INCLUDE_DIRS ${includes_private}
                       PRIV_REQUIRES  ${requirements_private}
                       )

target_compile_options(${COMPONENT_LIB} PUBLIC ${compile_options})

# when no builtin class driver is enabled, an uint8_t data compared with `BUILTIN_DRIVER_COUNT` will always be false
set_source_files_properties("src/device/usbd.c" PROPERTIES COMPILE_FLAGS "-Wno-type-limits")
As stated in your message i saw that files relative to host are not included, so i modifed a bit :

Code: Select all

set(includes_private
    "src/"
    "src/host"
    "src/device"
    "lib/networking" # For RNDIS definitions
    )
and here :

Code: Select all

set(srcs
    "src/class/cdc/cdc_device.c"
    "src/class/hid/hid_device.c"
    "src/class/midi/midi_device.c"
    "src/class/msc/msc_device.c"
    "src/class/vendor/vendor_device.c"
    "src/class/audio/audio_device.c"
    "src/class/video/video_device.c"
    "src/class/bth/bth_device.c"
    # NET class
    "src/class/net/ecm_rndis_device.c"
    "lib/networking/rndis_reports.c"
    "src/class/net/ncm_device.c"
    # DFU
    "src/class/dfu/dfu_device.c"
    "src/class/dfu/dfu_rt_device.c"
    # Common, device-mode related
    "src/portable/synopsys/dwc2/dcd_dwc2.c"
    "src/portable/synopsys/dwc2/dwc2_common.c"
    "src/common/tusb_fifo.c"
    "src/device/usbd_control.c"
    "src/device/usbd.c"
    "src/host/usbh.c"
    "src/host/hub.c"
    "src/tusb.c"
    )
now the linker can saw all the functions that was missing before. Now it fail in usbh.c it cannot find function defined in ..managed_components\espressif__tinyusb\src\portable\synopsys\dwc2\hcd_dwc2.c because some #define are not available in this file :
P4_TUH_ENABLED_in_usbhc.jpg
P4_TUH_ENABLED_in_usbhc.jpg (294.58 KiB) Viewed 23 times
Do you have an idea how to make the definie available in the dwc2 file ? Do you think i am on a good path with my approche ?

Best regards,

role_espressif
Espressif staff
Espressif staff
Posts: 6
Joined: Tue Jan 23, 2024 10:12 pm

Re: ESP32P4 Host mode

Postby role_espressif » Mon May 12, 2025 11:52 am

Hi @robin2906
I am a bit disappointed by your answer
Sorry for the inconvenience, we didn't plan to support tinyUSB in host mode as there is a esp-idf implementation available.
As tinyusb stock library support esp32p4 in host mode it should be possible to compile with esp-idf, it's just a matter of include the relevant files no ?
Yes, absolutely.
Do you have an idea how to make the definie available in the dwc2 file ?
For the host stack you need hcd_dwc2.c (not dcd_dwc2.c) file, and I didn't see this file in the srsc. Please, check that this file present in sources.
Do you think i am on a good path with my approche ?
Yes, this is a good path. Generally speaking, you have to change the build-files with providing all necessary files to build Host stack and everything should work.

Please, feel free to let me know if you have any other questions.

robin2906
Posts: 9
Joined: Tue May 06, 2025 4:50 pm

Re: ESP32P4 Host mode

Postby robin2906 » Mon May 12, 2025 3:45 pm

Sorry for the inconvenience, we didn't plan to support tinyUSB in host mode as there is a esp-idf implementation available.
No problem I fully understand that USB is a huge part on your side. The actual limitations of the espressif implementation of the host with hub is not acceptable for my application. I really want to implement hub and multiples devices with the P4, that's why I asked about tinyusb.

I just want to said thank you very much, with your help I managed to get the cdc_msc_hid_freertos exmple from tinyusb compile, link and work on the P4 with Tinyusb. Now I have a mouse, a keyboard and an USB drive working through a HUB with the P4 and the USB OTG 2.0. :D

As you said, I had to change the files in espressif/tinyusb, do you know a way to continue to use the original files from espressif and patch the relevant files with my modifications before compiling ?

Also I need to fix some issues with performances ( sometime the system stuck and become unresponsive ), I will investigate a bit further and ask for help if needed.
Edit : It was just a problem with the task memory stack, increased a bit the stack allocated to the host task and everything works like a charm now :D

Thanks again !

Best regards,

Who is online

Users browsing this forum: Amazon [Bot], Baidu [Spider], Bing [Bot], Google [Bot] and 2 guests