feat(usb/host): Add option to choose peripheral for USB host library

Starting with ESP32-P4 we can have targets that have more than 1 USB-OTG peripheral.
This commit adds an option to choose which peripherals will be used by USB Host lib.

Internally, we will still have only 1 Root HUB but with multiple Root ports.
This commit is contained in:
Tomas Rezucha
2024-12-03 08:52:48 +01:00
parent 66e5cc8801
commit b961f42e8b
14 changed files with 169 additions and 75 deletions

View File

@@ -25,9 +25,24 @@
// ----------------------------------------------------- Macros --------------------------------------------------------
#define TEST_P4_OTG11 0 // Change this to 1 to test on OTG1.1 peripheral - only for ESP32-P4
// --------------------- Constants -------------------------
#define PORT_NUM 1
#if TEST_P4_OTG11
#define TEST_PHY USB_PHY_TARGET_INT
#define TEST_PERIPHERAL_MAP BIT1
#define TEST_PORT_NUM 1
#else
#if CONFIG_IDF_TARGET_ESP32P4
#define TEST_PHY USB_PHY_TARGET_UTMI
#else
#define TEST_PHY USB_PHY_TARGET_INT
#endif
#define TEST_PERIPHERAL_MAP BIT0
#define TEST_PORT_NUM 0
#endif // TEST_P4_OTG11
#define EVENT_QUEUE_LEN 5
#define ENUM_ADDR 1 // Device address to use for tests that enumerate the device
#define ENUM_CONFIG 1 // Device configuration number to use for tests that enumerate the device
@@ -149,7 +164,7 @@ int test_hcd_get_num_pipe_events(hcd_pipe_handle_t pipe_hdl)
hcd_port_handle_t test_hcd_setup(void)
{
test_setup_usb_phy();
test_setup_usb_phy(TEST_PHY);
// Create a queue for port callback to queue up port events
QueueHandle_t port_evt_queue = xQueueCreate(EVENT_QUEUE_LEN, sizeof(port_event_msg_t));
@@ -157,6 +172,7 @@ hcd_port_handle_t test_hcd_setup(void)
// Install HCD
hcd_config_t hcd_config = {
.intr_flags = ESP_INTR_FLAG_LEVEL1,
.peripheral_map = TEST_PERIPHERAL_MAP,
};
TEST_ASSERT_EQUAL(ESP_OK, hcd_install(&hcd_config));
// Initialize a port
@@ -166,7 +182,7 @@ hcd_port_handle_t test_hcd_setup(void)
.context = (void *)port_evt_queue,
};
hcd_port_handle_t port_hdl;
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_init(PORT_NUM, &port_config, &port_hdl));
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_init(TEST_PORT_NUM, &port_config, &port_hdl));
TEST_ASSERT_NOT_NULL(port_hdl);
return port_hdl;
}

View File

@@ -20,7 +20,7 @@
static usb_phy_handle_t phy_hdl = NULL;
void test_setup_usb_phy(void)
void test_setup_usb_phy(usb_phy_target_t phy_target)
{
// Deinitialize PHY from previous failed test
if (phy_hdl != NULL) {
@@ -31,11 +31,7 @@ void test_setup_usb_phy(void)
// Initialize the internal USB PHY to connect to the USB OTG peripheral
usb_phy_config_t phy_config = {
.controller = USB_PHY_CTRL_OTG,
#if CONFIG_IDF_TARGET_ESP32P4 // ESP32-P4 has 2 USB-DWC peripherals, each with its dedicated PHY. We support HS+UTMI only ATM.
.target = USB_PHY_TARGET_UTMI,
#else
.target = USB_PHY_TARGET_INT,
#endif
.target = phy_target,
.otg_mode = USB_OTG_MODE_HOST,
.otg_speed = USB_PHY_SPEED_UNDEFINED, // In Host mode, the speed is determined by the connected device
.ext_io_conf = NULL,

View File

@@ -4,10 +4,15 @@
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "hal/usb_phy_types.h"
/**
* @brief Install USB PHY separately from the usb_host_install()
*
* @param[in] phy_target USB PHY target
*/
void test_setup_usb_phy(void);
void test_setup_usb_phy(usb_phy_target_t phy_target);
/**
* @brief Uninstall PHY

View File

@@ -13,17 +13,36 @@
#include "phy_common.h"
#include "usb/usb_host.h"
// ----------------------------------------------------- Macros --------------------------------------------------------
#define TEST_P4_OTG11 0 // Change this to 1 to test on OTG1.1 peripheral - only for ESP32-P4
// --------------------- Constants -------------------------
#if TEST_P4_OTG11
#define TEST_PHY USB_PHY_TARGET_INT
#define TEST_PERIPHERAL_MAP BIT1
#else
#if CONFIG_IDF_TARGET_ESP32P4
#define TEST_PHY USB_PHY_TARGET_UTMI
#else
#define TEST_PHY USB_PHY_TARGET_INT
#endif
#define TEST_PERIPHERAL_MAP BIT0
#endif // TEST_P4_OTG11
void setUp(void)
{
unity_utils_record_free_mem();
dev_msc_init();
// Install PHY separately
test_setup_usb_phy();
test_setup_usb_phy(TEST_PHY);
// Install USB Host
usb_host_config_t host_config = {
.skip_phy_setup = true,
.root_port_unpowered = false,
.intr_flags = ESP_INTR_FLAG_LEVEL1,
.peripheral_map = TEST_PERIPHERAL_MAP,
};
ESP_ERROR_CHECK(usb_host_install(&host_config));
printf("USB Host installed\n");