Skip to content

Commit d512a29

Browse files
committed
Merge branch 'refactor/usb_merge_p4_phy_backport_v5.4' into 'release/v5.4'
refactor(usb/phy): Merge P4 and S2/S3 PHYs into one module (backport 5.4) See merge request espressif/esp-idf!36449
2 parents 86ace15 + f1fa7b1 commit d512a29

File tree

14 files changed

+206
-125
lines changed

14 files changed

+206
-125
lines changed

components/usb/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ if(CONFIG_SOC_USB_OTG_SUPPORTED)
1919
"usb_helpers.c"
2020
"usb_host.c"
2121
"usb_private.c"
22-
"usbh.c")
23-
if(NOT ${target} STREQUAL "esp32p4")
24-
list(APPEND srcs "usb_phy.c")
25-
else()
26-
list(APPEND srcs "usb_phy_p4.c")
27-
endif()
22+
"usbh.c"
23+
"usb_phy.c"
24+
)
2825
list(APPEND include "include")
2926
list(APPEND priv_includes "private_include")
3027
endif()

components/usb/include/esp_private/usb_phy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "soc/soc_caps.h"
1212
#include "hal/usb_phy_types.h"
1313

14+
#define USB_PHY_SUPPORTS_P4_OTG11 1 // This version of usb_phy supports P4 OTG1.1 PHY
15+
1416
#ifdef __cplusplus
1517
extern "C" {
1618
#endif

components/usb/include/usb/usb_host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/

components/usb/test_apps/.build-test-rules.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ components/usb/test_apps:
2121
components/usb/test_apps/phy:
2222
enable:
2323
- if: SOC_USB_OTG_SUPPORTED == 1
24-
disable_test:
25-
- if: IDF_TARGET in ["esp32p4"]
26-
temporary: true
27-
reason: ESP32-P4 PHY driver not yet migrated
2824
depends_components:
2925
- usb
3026
depends_filepatterns:

components/usb/test_apps/hcd/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake)
66
set(EXTRA_COMPONENT_DIRS "../common")
77

88
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
9-
set(COMPONENTS main)
9+
idf_build_set_property(MINIMAL_BUILD ON)
1010

1111
project(test_app_usb_host)

components/usb/test_apps/hcd/main/test_hcd_common.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -153,7 +153,11 @@ hcd_port_handle_t test_hcd_setup(void)
153153
// Initialize the internal USB PHY to connect to the USB OTG peripheral
154154
usb_phy_config_t phy_config = {
155155
.controller = USB_PHY_CTRL_OTG,
156+
#if CONFIG_IDF_TARGET_ESP32P4 // ESP32-P4 has 2 USB-DWC peripherals, each with its dedicated PHY. We support HS+UTMI only ATM.
157+
.target = USB_PHY_TARGET_UTMI,
158+
#else
156159
.target = USB_PHY_TARGET_INT,
160+
#endif
157161
.otg_mode = USB_OTG_MODE_HOST,
158162
.otg_speed = USB_PHY_SPEED_UNDEFINED, // In Host mode, the speed is determined by the connected device
159163
.ext_io_conf = NULL,

components/usb/test_apps/phy/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ cmake_minimum_required(VERSION 3.16)
44
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
55

66
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
7-
set(COMPONENTS main)
7+
idf_build_set_property(MINIMAL_BUILD ON)
88

99
project(test_app_usb_phy)

components/usb/test_apps/phy/main/test_app_main.c

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "esp_private/usb_phy.h"
1111
#include "hal/usb_wrap_ll.h" // For USB_WRAP_LL_EXT_PHY_SUPPORTED symbol
1212
#include "soc/soc_caps.h" // For SOC_USB_UTMI_PHY_NUM symbol
13+
#include "sdkconfig.h" // For CONFIG_IDF_TARGET_***
1314

1415
#if USB_WRAP_LL_EXT_PHY_SUPPORTED
1516
#define EXT_PHY_SUPPORTED 1
@@ -55,9 +56,13 @@ void app_main(void)
5556

5657
/**
5758
* Test init and deinit of internal FSLS PHY
59+
*
60+
* 1. Init + deinit in Host mode
61+
* 2. Init + deinit in Device mode
5862
*/
5963
TEST_CASE("Init internal FSLS PHY", "[phy]")
6064
{
65+
// Host mode
6166
usb_phy_handle_t phy_handle = NULL;
6267
const usb_phy_config_t phy_config = {
6368
.controller = USB_PHY_CTRL_OTG,
@@ -70,6 +75,20 @@ TEST_CASE("Init internal FSLS PHY", "[phy]")
7075
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config, &phy_handle));
7176
TEST_ASSERT_NOT_NULL(phy_handle);
7277
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_handle));
78+
79+
// Device mode
80+
usb_phy_handle_t phy_handle_2 = NULL;
81+
const usb_phy_config_t phy_config_2 = {
82+
.controller = USB_PHY_CTRL_OTG,
83+
.target = USB_PHY_TARGET_INT,
84+
.otg_mode = USB_OTG_MODE_DEVICE,
85+
.otg_speed = USB_PHY_SPEED_FULL,
86+
.ext_io_conf = NULL,
87+
.otg_io_conf = NULL,
88+
};
89+
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config_2, &phy_handle_2));
90+
TEST_ASSERT_NOT_NULL(phy_handle_2);
91+
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_handle_2));
7392
}
7493

7594
/**
@@ -137,44 +156,85 @@ TEST_CASE("Init internal UTMI PHY", "[phy]")
137156
}
138157

139158
/**
140-
* Test init and deinit of all PHYs at the same time
159+
* Test init and deinit of all PHYs at the same time multiple times
141160
*/
142-
TEST_CASE("Init all PHYs", "[phy]")
161+
TEST_CASE("Init all PHYs in a loop", "[phy]")
143162
{
163+
for (int i = 0; i < 2; i++) {
164+
usb_phy_handle_t phy_handle = NULL;
165+
usb_phy_handle_t phy_handle_2 = NULL;
166+
usb_phy_config_t phy_config = {
167+
.controller = USB_PHY_CTRL_OTG,
168+
.target = USB_PHY_TARGET_INT,
169+
.otg_mode = USB_OTG_MODE_HOST,
170+
.otg_speed = USB_PHY_SPEED_UNDEFINED,
171+
.ext_io_conf = NULL,
172+
.otg_io_conf = NULL,
173+
};
174+
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config, &phy_handle));
175+
TEST_ASSERT_NOT_NULL(phy_handle);
176+
177+
// Our current targets support either UTMI or external PHY
178+
// so if/else suffice here
179+
#if UTMI_PHY_SUPPORTED
180+
phy_config.target = USB_PHY_TARGET_UTMI;
181+
#else
182+
phy_config.target = USB_PHY_TARGET_EXT;
183+
const usb_phy_ext_io_conf_t ext_io_conf = { // Some random values
184+
.vp_io_num = 1,
185+
.vm_io_num = 1,
186+
.rcv_io_num = 1,
187+
.suspend_n_io_num = 1,
188+
.oen_io_num = 1,
189+
.vpo_io_num = 1,
190+
.vmo_io_num = 1,
191+
.fs_edge_sel_io_num = 1,
192+
};
193+
phy_config.ext_io_conf = &ext_io_conf;
194+
#endif
195+
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config, &phy_handle_2));
196+
TEST_ASSERT_NOT_NULL(phy_handle_2);
197+
198+
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_handle));
199+
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_handle_2));
200+
}
201+
}
202+
203+
#if CONFIG_IDF_TARGET_ESP32P4
204+
/**
205+
* Test backward compatibility of ESP32-P4 PHY
206+
*
207+
* Initial P4 device support was for USB-DWC HS and UTMI PHY. To maintain backward compatibility on ESP32-P4 in USB Device mode,
208+
* we select UTMI PHY in case otg_speed is UNDEFINED or HIGH
209+
*/
210+
TEST_CASE("ESP32-P4 TinyUSB backward compatibility", "[phy]")
211+
{
212+
// This configuration is used in esp_tinyusb
144213
usb_phy_handle_t phy_handle = NULL;
145-
usb_phy_handle_t phy_handle_2 = NULL;
146214
usb_phy_config_t phy_config = {
147215
.controller = USB_PHY_CTRL_OTG,
148216
.target = USB_PHY_TARGET_INT,
149-
.otg_mode = USB_OTG_MODE_HOST,
217+
.otg_mode = USB_OTG_MODE_DEVICE,
150218
.otg_speed = USB_PHY_SPEED_UNDEFINED,
151219
.ext_io_conf = NULL,
152220
.otg_io_conf = NULL,
153221
};
154222
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config, &phy_handle));
155223
TEST_ASSERT_NOT_NULL(phy_handle);
224+
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_handle));
156225

157-
// Our current targets support either UTMI or external PHY
158-
// so if/else suffice here
159-
#if UTMI_PHY_SUPPORTED
160-
phy_config.target = USB_PHY_TARGET_UTMI;
161-
#else
162-
const usb_phy_ext_io_conf_t ext_io_conf = { // Some random values
163-
.vp_io_num = 1,
164-
.vm_io_num = 1,
165-
.rcv_io_num = 1,
166-
.suspend_n_io_num = 1,
167-
.oen_io_num = 1,
168-
.vpo_io_num = 1,
169-
.vmo_io_num = 1,
170-
.fs_edge_sel_io_num = 1,
226+
// This configuration is used in upstream tinyusb examples
227+
usb_phy_handle_t phy_handle_2 = NULL;
228+
usb_phy_config_t phy_config_2 = {
229+
.controller = USB_PHY_CTRL_OTG,
230+
.target = USB_PHY_TARGET_INT,
231+
.otg_mode = USB_OTG_MODE_DEVICE,
232+
.otg_speed = USB_PHY_SPEED_HIGH,
233+
.ext_io_conf = NULL,
234+
.otg_io_conf = NULL,
171235
};
172-
phy_config.target = USB_PHY_TARGET_EXT;
173-
phy_config.ext_io_conf = &ext_io_conf;
174-
#endif
175-
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config, &phy_handle_2));
236+
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config_2, &phy_handle_2));
176237
TEST_ASSERT_NOT_NULL(phy_handle_2);
177-
178-
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_handle));
179238
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_handle_2));
180239
}
240+
#endif

components/usb/test_apps/phy/pytest_usb_phy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
@pytest.mark.esp32s3
99
@pytest.mark.esp32p4
1010
@pytest.mark.generic
11-
@pytest.mark.temp_skip_ci(targets=['esp32p4'], reason='P4 PHY not yet migrated')
1211
def test_usb_phy(dut: Dut) -> None:
1312
dut.run_all_single_board_cases(group='phy')

components/usb/test_apps/usb_host/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake)
66
set(EXTRA_COMPONENT_DIRS "../common")
77

88
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
9-
set(COMPONENTS main)
9+
idf_build_set_property(MINIMAL_BUILD ON)
1010

1111
project(test_app_usb_host)

0 commit comments

Comments
 (0)