feat(usb): Add usb_host_lib_set_root_port_power()

This commit adds the usb_host_lib_set_root_port_power() function. This provides
a public API for users to power the root port OFF or ON at runtime, thus trigger
a disconnection or allow connections respectively.

In addition, the usb_host_config_t.root_port_unpowered install configuration is
provided to allow users to install the USB Host Library without automatically
powering ON the root port.
This commit is contained in:
Darian Leung
2024-05-30 02:45:23 +08:00
committed by Tomas Rezucha
parent 620966389c
commit 318c69a671
2 changed files with 42 additions and 9 deletions

View File

@@ -101,10 +101,13 @@ typedef void (*usb_host_client_event_cb_t)(const usb_host_client_event_msg_t *ev
* Configuration structure of the USB Host Library. Provided in the usb_host_install() function * Configuration structure of the USB Host Library. Provided in the usb_host_install() function
*/ */
typedef struct { typedef struct {
bool skip_phy_setup; /**< If set, the USB Host Library will not configure the USB PHY thus allowing the user bool skip_phy_setup; /**< If set, the USB Host Library will not configure the USB PHY thus allowing
to manually configure the USB PHY before calling usb_host_install(). Users should the user to manually configure the USB PHY before calling usb_host_install().
set this if they want to use an external USB PHY. Otherwise, the USB Host Library Users should set this if they want to use an external USB PHY. Otherwise,
will automatically configure the internal USB PHY */ the USB Host Library will automatically configure the internal USB PHY */
bool root_port_unpowered; /**< If set, the USB Host Library will not power on the root port on installation.
This allows users to power on the root port manually by calling
usb_host_lib_set_root_port_power(). */
int intr_flags; /**< Interrupt flags for the underlying ISR used by the USB Host stack */ int intr_flags; /**< Interrupt flags for the underlying ISR used by the USB Host stack */
usb_host_enum_filter_cb_t enum_filter_cb; /**< Enumeration filter callback. Enable CONFIG_USB_HOST_ENABLE_ENUM_FILTER_CALLBACK usb_host_enum_filter_cb_t enum_filter_cb; /**< Enumeration filter callback. Enable CONFIG_USB_HOST_ENABLE_ENUM_FILTER_CALLBACK
to use this feature. Set to NULL otherwise. */ to use this feature. Set to NULL otherwise. */
@@ -187,6 +190,21 @@ esp_err_t usb_host_lib_unblock(void);
*/ */
esp_err_t usb_host_lib_info(usb_host_lib_info_t *info_ret); esp_err_t usb_host_lib_info(usb_host_lib_info_t *info_ret);
/**
* @brief Power the root port ON or OFF
*
* - Powering ON the root port will allow device connections to occur
* - Powering OFF the root port will disconnect all devices downstream off the root port and prevent
* any further device connections.
*
* @note If 'usb_host_config_t.root_port_unpowered' was set on USB Host Library installation, users must call this
* function to power ON the root port before any device connections can occur.
*
* @param enable True to power the root port ON, false to power OFF
* @return esp_err_t
*/
esp_err_t usb_host_lib_set_root_port_power(bool enable);
// ------------------------------------------------ Client Functions --------------------------------------------------- // ------------------------------------------------ Client Functions ---------------------------------------------------
/** /**

View File

@@ -540,8 +540,11 @@ esp_err_t usb_host_install(const usb_host_config_t *config)
p_host_lib_obj = host_lib_obj; p_host_lib_obj = host_lib_obj;
HOST_EXIT_CRITICAL(); HOST_EXIT_CRITICAL();
if (!config->root_port_unpowered) {
// Start the root hub // Start the root hub
ESP_ERROR_CHECK(hub_root_start()); ESP_ERROR_CHECK(hub_root_start());
}
ret = ESP_OK; ret = ESP_OK;
return ret; return ret;
@@ -694,6 +697,18 @@ esp_err_t usb_host_lib_info(usb_host_lib_info_t *info_ret)
return ESP_OK; return ESP_OK;
} }
esp_err_t usb_host_lib_set_root_port_power(bool enable)
{
esp_err_t ret;
if (enable) {
ret = hub_root_start();
} else {
ret = hub_root_stop();
}
return ret;
}
// ------------------------------------------------ Client Functions --------------------------------------------------- // ------------------------------------------------ Client Functions ---------------------------------------------------
// ----------------------- Private ------------------------- // ----------------------- Private -------------------------