Skip to content

Commit

Permalink
usb_phy: using usb phy api
Browse files Browse the repository at this point in the history
  • Loading branch information
leeebo committed Feb 22, 2022
1 parent e2523e5 commit 2ba36c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
23 changes: 18 additions & 5 deletions components/usb/esp_usbh_cdc/esp_usbh_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "hcd.h"
#include "usb/usb_types_stack.h"
#include "usb_private.h"
#include "esp_private/usb_phy.h"
#include "esp_usbh_cdc.h"

#define CDC_CHECK(a, str, ret) if(!(a)) { \
Expand Down Expand Up @@ -346,6 +347,8 @@ static bool _usb_pipe_callback(hcd_pipe_handle_t pipe_handle, hcd_pipe_event_t p

/************************************************************* USB Port API ***********************************************************/

static usb_phy_handle_t s_phy_handle = NULL;

/**
* @brief Initialize USB controler and USB port
*
Expand All @@ -361,16 +364,21 @@ static hcd_port_handle_t _usb_port_init(void *context, hcd_port_callback_t callb
hcd_port_handle_t port_hdl = NULL;

/* Router internal USB PHY to usb-otg instead of usb-serial-jtag (if it has) */
usb_hal_context_t hal = {
.use_external_phy = false
usb_phy_config_t phy_config = {
.controller = USB_PHY_CTRL_OTG,
.target = USB_PHY_TARGET_INT,
.otg_mode = USB_OTG_MODE_HOST,
.otg_speed = USB_PHY_SPEED_UNDEFINED, //In Host mode, the speed is determined by the connected device
.gpio_conf = NULL,
};
usb_hal_init(&hal);
ret = usb_new_phy(&phy_config, &s_phy_handle);
CDC_CHECK(ESP_OK == ret, "USB PHY init failed", NULL);
/* Initialize USB Peripheral */
hcd_config_t hcd_config = {
.intr_flags = ESP_INTR_FLAG_LEVEL2,
};
ret = hcd_install(&hcd_config);
CDC_CHECK(ESP_OK == ret, "HCD Install failed", NULL);
CDC_CHECK_GOTO(ESP_OK == ret, "HCD Install failed", hcd_init_err);

//TODO: create a usb port task to handle event
/* Initialize USB Port */
Expand All @@ -388,6 +396,8 @@ static hcd_port_handle_t _usb_port_init(void *context, hcd_port_callback_t callb

port_init_err:
hcd_uninstall();
hcd_init_err:
usb_del_phy(s_phy_handle);
return NULL;
}

Expand Down Expand Up @@ -461,7 +471,10 @@ static esp_err_t _usb_port_deinit(hcd_port_handle_t port_hdl)
if (ESP_OK != ret) {
ESP_LOGW(TAG, "hcd uninstall failed");
}

ret = usb_del_phy(s_phy_handle);
if (ESP_OK != ret) {
ESP_LOGW(TAG, "phy delete failed");
}
ESP_LOGI(TAG, "USB Port=%d deinit succeed", USB_PORT_NUM);
return ret;
}
Expand Down
23 changes: 19 additions & 4 deletions components/usb/uvc_stream/uvc_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,22 +414,29 @@ static esp_err_t _usb_port_event_wait(hcd_port_handle_t expected_port_hdl,
return ret;
}

static usb_phy_handle_t s_phy_handle = NULL;

static hcd_port_handle_t _usb_port_init(void *context, void *callback_arg)
{
UVC_CHECK(context != NULL && callback_arg != NULL, "invalid args", NULL);
esp_err_t ret = ESP_OK;
hcd_port_handle_t port_hdl = NULL;

usb_hal_context_t hal = {
.use_external_phy = false
usb_phy_config_t phy_config = {
.controller = USB_PHY_CTRL_OTG,
.target = USB_PHY_TARGET_INT,
.otg_mode = USB_OTG_MODE_HOST,
.otg_speed = USB_PHY_SPEED_UNDEFINED, //In Host mode, the speed is determined by the connected device
.gpio_conf = NULL,
};
usb_hal_init(&hal);
ret = usb_new_phy(&phy_config, &s_phy_handle);
UVC_CHECK(ESP_OK == ret, "USB PHY init failed", NULL);

hcd_config_t hcd_config = {
.intr_flags = ESP_INTR_FLAG_LEVEL2,
};
ret = hcd_install(&hcd_config);
UVC_CHECK(ESP_OK == ret, "HCD Install failed", NULL);
UVC_CHECK_GOTO(ESP_OK == ret, "HCD Install failed", hcd_init_err);

hcd_port_config_t port_cfg = {
.fifo_bias = HCD_PORT_FIFO_BIAS_BALANCED,
Expand All @@ -446,6 +453,8 @@ static hcd_port_handle_t _usb_port_init(void *context, void *callback_arg)

port_init_err:
hcd_uninstall();
hcd_init_err:
usb_del_phy(s_phy_handle);
return NULL;
}

Expand Down Expand Up @@ -479,6 +488,12 @@ static esp_err_t _usb_port_deinit(hcd_port_handle_t port_hdl)
ESP_LOGW(TAG, "hcd uninstall failed");
}

ret = usb_del_phy(s_phy_handle);

if (ESP_OK != ret) {
ESP_LOGW(TAG, "phy delete failed");
}

return ret;
}

Expand Down

0 comments on commit 2ba36c5

Please sign in to comment.