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 */
5963TEST_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
0 commit comments