ESP32P4 Host mode

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

ESP32P4 Host mode

Postby robin2906 » Tue May 06, 2025 5:01 pm

Hi guys,

I try to use tinyusb as host with the P4. I don't use the espressif host lib because it do not support hub with different speed class device (it's not possible to connect an usb keyboard LS ans usbkey FS through a FS Hub)

So my question : is someone know where I can find an example of tinyusb host with esp32 please ? I tried to run stock tinyusb exemple but I didn't managed to complie.

I added espressif/tinyusb: ^0.18.0~2 to my idf_components.yml but i didn't find the bsp code for my target. In the exemple provided, it failed at the first line : #include "bsp/board_api.h" because the build system didn't find it.

Code: Select all

#include "bsp/board_api.h"
#include "tusb.h"

//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTOTYPES
//--------------------------------------------------------------------+
void led_blinking_task(void);
extern void cdc_app_task(void);
extern void hid_app_task(void);

/*------------- MAIN -------------*/
int main(void) {
  board_init();

  printf("TinyUSB Host CDC MSC HID Example\r\n");

  // init host stack on configured roothub port
  tusb_rhport_init_t host_init = {
    .role = TUSB_ROLE_HOST,
    .speed = TUSB_SPEED_AUTO
  };
  tusb_init(BOARD_TUH_RHPORT, &host_init);

  if (board_init_after_tusb) {
    board_init_after_tusb();
  }

#if CFG_TUH_ENABLED && CFG_TUH_MAX3421
  // FeatherWing MAX3421E use MAX3421E's GPIO0 for VBUS enable
  enum { IOPINS1_ADDR  = 20u << 3, /* 0xA0 */ };
  tuh_max3421_reg_write(BOARD_TUH_RHPORT, IOPINS1_ADDR, 0x01, false);
#endif

  while (1) {
    // tinyusb host task
    tuh_task();

    led_blinking_task();
    cdc_app_task();
    hid_app_task();
  }
}

//--------------------------------------------------------------------+
// TinyUSB Callbacks
//--------------------------------------------------------------------+

void tuh_mount_cb(uint8_t dev_addr) {
  // application set-up
  printf("A device with address %u is mounted\r\n", dev_addr);
}

void tuh_umount_cb(uint8_t dev_addr) {
  // application tear-down
  printf("A device with address %u is unmounted \r\n", dev_addr);
}


//--------------------------------------------------------------------+
// Blinking Task
//--------------------------------------------------------------------+
void led_blinking_task(void) {
  const uint32_t interval_ms = 1000;
  static uint32_t start_ms = 0;

  static bool led_state = false;

  // Blink every interval ms
  if (board_millis() - start_ms < interval_ms) {
    return;// not enough time
  }
  start_ms += interval_ms;

  board_led_write(led_state);
  led_state = 1 - led_state; // toggle
}
Can someone help me to get tinyusb working ?

Best regards,

chegewara
Posts: 2489
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32P4 Host mode

Postby chegewara » Wed May 07, 2025 6:23 am

As far as I know in tynyusb there is no support for espressif chips USB host mode, but I may be wrong.
In terms of low and high speed in usb hub, I can tell it's hardware limitation and P4 just can't support it.

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

Re: ESP32P4 Host mode

Postby robin2906 » Wed May 07, 2025 7:35 am

Hi, thank you for the answer but I think TinyUSB support P4 as host :

https://github.com/hathach/tinyusb/issues/3108

It do not go deep into details... But I've asked for clarifications.

Best regards,

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

Re: ESP32P4 Host mode

Postby role_espressif » Wed May 07, 2025 9:04 am

Hi @robin2906,

P4 has two peripherals, one is 2.0 (High-speed) and another one is 1.1 (Full-speed).

There are couple of things, that currently in progress, otherwise:
- High-speed peripheral has a hardware limitation, when implementing Transaction Translator (needs for Full- and Low-speed devices, connected via High-speed hub) is not possible. So, there will be no support for such thing in the current version of P4 chip.
- Full-speed peripheral doesn't have such limitation and both Full- and Low-speed devices could be connected via external Hub. Selection of the peripheral upon USB Host Library installation is almost done and soon will be available.

S2/S3:
- There was a limitation, when connecting Low-speed devices via external hub was not possible, but it is fixed at the moment (https://github.com/espressif/esp-idf/issues/15683) and soon will be available in all releases, starting from 5.2.x.

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

Re: ESP32P4 Host mode

Postby role_espressif » Wed May 07, 2025 11:59 am

[quote=role_espressif post_id=147398 time=1746608675 user_id=34548
- High-speed peripheral has a hardware limitation, when implementing Transaction Translator (needs for Full- and Low-speed devices,
[/quote]

I was not fully correct here: has a software limitation.

In espressif, we are using the Scatter-Gather DMA mode in the host driver. And in this mode split-transactions are not supported. And there are no plans to move to other mode ATM.

They are supported in two other modes: Slave (when the application initiates the data transfers for data fetch and store ) or Buffer DMA.

So, it is possible to attach Low-speed and Full-speed devices via external hubs to P4 USB High-speed peripheral, for example, with tinyUSB host stack.

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

Re: ESP32P4 Host mode

Postby robin2906 » Thu May 08, 2025 7:08 am

Thank you very very much for your answer, so if I understand correctly we can summarize the status like this :

This can work with TinyUSB as host stack, but will not work with Espressif Host stack (due to lack of STT support)
P4_with_USBHSOTG.jpg
P4_with_USBHSOTG.jpg (30.9 KiB) Viewed 51 times
This will work with both host stack ( Espressif Host stack and Tiny USB ) when espressif stack support the settings of which OTG host ( FS or HS) is used by the stack :
P4_with_USBFSOTG.jpg
P4_with_USBFSOTG.jpg (31.07 KiB) Viewed 51 times
But the USB drive will use FS speed not HS, correct ?

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

Re: ESP32P4 Host mode

Postby robin2906 » Thu May 08, 2025 2:39 pm

Hi Guys,

I keep digging with TinyUSB, I made some progress but now i'm stuck...

I am able to compile the TinyUSB component from espressif and I can include #include "tusb.h" in my main :D

But I am unable to include #include "bsp/board_api.h" in my main and iam not able to compile familiy.c because of #include "bsp/board_api.h" not found

My CMakeLists.txt next to my main.c is like this :

Code: Select all

idf_component_register(
    SRCS "main.c"
    PRIV_REQUIRES spi_flash espressif__tinyusb
    INCLUDE_DIRS "."
)

# espressif__tinyusb should match the current tinyusb dependency name
idf_component_get_property(tusb_lib espressif__tinyusb COMPONENT_LIB)
idf_component_get_property(tinyusb_dir espressif__tinyusb COMPONENT_DIR)
message(STATUS "tusb_lib = ${tusb_lib}")
message(STATUS "tinyusb_dir = ${tinyusb_dir}")


target_include_directories(${tusb_lib} PUBLIC "${COMPONENT_DIR}/tusb")
#target_sources(${tusb_lib} PUBLIC "${COMPONENT_DIR}/tusb/usb_descriptors.c")

# Ajoute les répertoires contenant board_api.h, board.h, etc.
target_include_directories(${tusb_lib} PUBLIC
  "${tinyusb_dir}/hw/bsp"
  "${tinyusb_dir}/hw/bsp/espressif"
  "${tinyusb_dir}/hw/bsp/espressif/boards"
)

# Ajoute le fichier family.c à la cible tinyusb
target_sources(${tusb_lib} PUBLIC
  "${tinyusb_dir}/hw/bsp/espressif/boards/family.c"
)
This gave me :
compile_error_familly.jpg
compile_error_familly.jpg (460.53 KiB) Viewed 42 times
So what is the proper way to compile the tinyusb bsp for the P4 ?

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

Re: ESP32P4 Host mode

Postby role_espressif » Fri May 09, 2025 8:46 am

Hi @robin2906 ,

Yes, you can attach external hub to both peripheral, but when you attach it to the Full-speed peripheral, the High-speed hub and any other High-speed downstream device will be Full-speed.
But the USB drive will use FS speed not HS, correct ?
So yes, this is correct. If the device supports USB 1.1.
Also, keep in mind that USB 1.1 perihipheral has only 8 channels, which means that you are able to keep 8 open endpoints.
External Hub uses 2 channels (one for CTRL and one for IN), so there is only 6 channels left. This might be not enough to support the Drive (usually 3 channels required), Keyboard (usually 2-3) and QR scanner.

I am able to compile the TinyUSB component from espressif and I can include #include "tusb.h" in my main
I would suggest to try the tinyUSB example directly from the upstream branch to test your board, if you have a proprietary one.

The description how to build it you may find here: https://docs.tinyusb.org/en/latest/refe ... arted.html

Indeed, you need to build the example under the esp-idf, so you need to install it in advance: "install.sh" and then "export.sh" (or another commands, according to the way and OS where you are doing it).


After building the example, you may create you own application with using espressif tinyusb component, but for the usb host application you have to provide "tusb_config.h" with all parameters.
iam not able to compile familiy.c because of #include "bsp/board_api.h" not found
There is another approach to solve this:
To enable USB peripheral, you need to enable PHY. You can do it without any files from tinyusb, but implement you own function, which does that.
For this you will need only "usb_phy.h" from the "idf/component/usb/include/esp_private".
Just the same way as in here: https://github.com/hathach/tinyusb/blob ... ily.c#L164

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

Re: ESP32P4 Host mode

Postby robin2906 » Fri May 09, 2025 9:51 am

Hi @role_espressif

Thank you very much for this valuables information about the limitations concerning the numbers of endpoints.

Yesterday I started to do what you explain in the last part of your message,

This is the global view of my project :
P4_prj_structure.jpg
P4_prj_structure.jpg (20.57 KiB) Viewed 20 times
idf_component.yml

Code: Select all

## IDF Component Manager Manifest File
dependencies:
  ## Required IDF version
  idf:
    version: '>=4.1.0'
  # # Put list of dependencies here
  # # For components maintained by Espressif:
  # component: "~1.0.0"
  # # For 3rd party components:
  # username/component: ">=1.0.0,<2.0.0"
  # username2/component2:
  #   version: "~1.0.0"
  #   # For transient dependencies `public` flag can be set.
  #   # `public` flag doesn't have an effect dependencies of the `main` component.
  #   # All dependencies of `main` are public by default.
  #   public: true
  espressif/tinyusb: ^0.18.0~2
CMakeLists.txt

I added usb in order to be able to include usb_phy.h in the main. And I followed https://docs.espressif.com/projects/esp ... ide.html# in order to make tusb_config.h visible by tinyusb component.

Code: Select all

idf_component_register(
    SRCS "main.c"
    PRIV_REQUIRES spi_flash usb espressif__tinyusb 
    INCLUDE_DIRS "."
)

# espressif__tinyusb should match the current tinyusb dependency name
idf_component_get_property(tusb_lib espressif__tinyusb COMPONENT_LIB)
#idf_component_get_property(tinyusb_dir espressif__tinyusb COMPONENT_DIR)
message(STATUS "tusb_lib = ${tusb_lib}")
message(STATUS "tinyusb_dir = ${tinyusb_dir}")

target_include_directories(${tusb_lib} PUBLIC "${COMPONENT_DIR}/tusb")
#target_sources(${tusb_lib} PUBLIC "${COMPONENT_DIR}/tusb/usb_descriptors.c")
And in my main I do what you said, i've striped the device_info example and implemented in the main the function provided by familly.c.

My code compile but do not link yet. I don't know why but the #if CFG_TUH_ENABLED is not set according to my tusb_config.h in the usbh.c file, so some functions are missing (ex : undefined reference to `tuh_rhport_init')
P4_TUH_ENABLED_notrecognized.jpg
P4_TUH_ENABLED_notrecognized.jpg (105.75 KiB) Viewed 20 times
despite :
P4_TUH_ENABLED.jpg
P4_TUH_ENABLED.jpg (167.77 KiB) Viewed 20 times
Do you have an idea of what can cause this behavior ? If i remove my custom tusb_config.h i am unable to compile tinyusb so i think my config file is used during tinyusb building so i do not understand why it is not used with usbh.c ...

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

Re: ESP32P4 Host mode

Postby role_espressif » Fri May 09, 2025 6:45 pm

HI @robin2906

unfortunately, espressif_tinyusb component doesn't support the usage of tinyUSB in the host mode (as it used in espressif for a device, its build system supports only device-related files).

And currently we don't have any plans to add such support.

I would suggest you to try to build the example from the upstream (for example, device_info) and then continue with building your own project.

Who is online

Users browsing this forum: Google [Bot] and 2 guests