1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdbool.h>
8 #include "freertos/FreeRTOS.h"
9 #include "freertos/task.h"
10 #include "esp_err.h"
11 #include "hal/usb_phy_types.h"
12 #include "esp_private/usb_phy.h"
13 #include "test_usb_common.h"
14 
15 static usb_phy_handle_t phy_hdl = NULL;
16 
test_usb_init_phy(void)17 void test_usb_init_phy(void)
18 {
19     //Initialize the internal USB PHY to connect to the USB OTG peripheral
20     usb_phy_config_t phy_config = {
21         .controller = USB_PHY_CTRL_OTG,
22         .target = USB_PHY_TARGET_INT,
23         .otg_mode = USB_OTG_MODE_HOST,
24         .otg_speed = USB_PHY_SPEED_UNDEFINED,   //In Host mode, the speed is determined by the connected device
25         .gpio_conf = NULL,
26     };
27     ESP_ERROR_CHECK(usb_new_phy(&phy_config, &phy_hdl));
28 }
29 
test_usb_deinit_phy(void)30 void test_usb_deinit_phy(void)
31 {
32     //Deinitialize the internal USB PHY
33     ESP_ERROR_CHECK(usb_del_phy(phy_hdl));
34     phy_hdl = NULL;
35 }
36 
test_usb_set_phy_state(bool connected,TickType_t delay_ticks)37 void test_usb_set_phy_state(bool connected, TickType_t delay_ticks)
38 {
39     if (delay_ticks > 0) {
40         //Delay of 0 ticks causes a yield. So skip if delay_ticks is 0.
41         vTaskDelay(delay_ticks);
42     }
43     ESP_ERROR_CHECK(usb_phy_action(phy_hdl, (connected) ? USB_PHY_ACTION_HOST_ALLOW_CONN : USB_PHY_ACTION_HOST_FORCE_DISCONN));
44 }
45