2023-01-05 12:42:19 +02:00
|
|
|
/*
|
2024-03-21 15:55:57 +02:00
|
|
|
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
|
2023-01-05 12:42:19 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
#include "esp_check.h"
|
|
|
|
|
#include "esp_err.h"
|
|
|
|
|
#include "esp_private/periph_ctrl.h"
|
|
|
|
|
#include "esp_private/usb_phy.h"
|
|
|
|
|
#include "soc/usb_pins.h"
|
|
|
|
|
#include "tinyusb.h"
|
|
|
|
|
#include "descriptors_control.h"
|
|
|
|
|
#include "tusb.h"
|
|
|
|
|
#include "tusb_tasks.h"
|
|
|
|
|
|
|
|
|
|
const static char *TAG = "TinyUSB";
|
|
|
|
|
static usb_phy_handle_t phy_hdl;
|
|
|
|
|
|
|
|
|
|
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
|
|
|
|
|
{
|
2024-03-21 15:55:57 +02:00
|
|
|
ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "Config can't be NULL");
|
2023-01-05 12:42:19 +02:00
|
|
|
|
|
|
|
|
// Configure USB PHY
|
|
|
|
|
usb_phy_config_t phy_conf = {
|
|
|
|
|
.controller = USB_PHY_CTRL_OTG,
|
|
|
|
|
.otg_mode = USB_OTG_MODE_DEVICE,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// External PHY IOs config
|
|
|
|
|
usb_phy_ext_io_conf_t ext_io_conf = {
|
|
|
|
|
.vp_io_num = USBPHY_VP_NUM,
|
|
|
|
|
.vm_io_num = USBPHY_VM_NUM,
|
|
|
|
|
.rcv_io_num = USBPHY_RCV_NUM,
|
|
|
|
|
.oen_io_num = USBPHY_OEN_NUM,
|
|
|
|
|
.vpo_io_num = USBPHY_VPO_NUM,
|
|
|
|
|
.vmo_io_num = USBPHY_VMO_NUM,
|
|
|
|
|
};
|
|
|
|
|
if (config->external_phy) {
|
|
|
|
|
phy_conf.target = USB_PHY_TARGET_EXT;
|
|
|
|
|
phy_conf.ext_io_conf = &ext_io_conf;
|
|
|
|
|
} else {
|
|
|
|
|
phy_conf.target = USB_PHY_TARGET_INT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OTG IOs config
|
|
|
|
|
const usb_phy_otg_io_conf_t otg_io_conf = USB_PHY_SELF_POWERED_DEVICE(config->vbus_monitor_io);
|
|
|
|
|
if (config->self_powered) {
|
|
|
|
|
phy_conf.otg_io_conf = &otg_io_conf;
|
|
|
|
|
}
|
|
|
|
|
ESP_RETURN_ON_ERROR(usb_new_phy(&phy_conf, &phy_hdl), TAG, "Install USB PHY failed");
|
|
|
|
|
|
2024-03-21 15:55:57 +02:00
|
|
|
// Descriptors config
|
|
|
|
|
ESP_RETURN_ON_ERROR(tinyusb_set_descriptors(config), TAG, "Descriptors config failed");
|
2023-01-05 12:42:19 +02:00
|
|
|
|
2024-03-21 15:55:57 +02:00
|
|
|
// Init
|
|
|
|
|
#if !CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK
|
2023-01-05 12:42:19 +02:00
|
|
|
ESP_RETURN_ON_FALSE(tusb_init(), ESP_FAIL, TAG, "Init TinyUSB stack failed");
|
2024-03-21 15:55:57 +02:00
|
|
|
#endif
|
2023-01-05 12:42:19 +02:00
|
|
|
#if !CONFIG_TINYUSB_NO_DEFAULT_TASK
|
|
|
|
|
ESP_RETURN_ON_ERROR(tusb_run_task(), TAG, "Run TinyUSB task failed");
|
|
|
|
|
#endif
|
|
|
|
|
ESP_LOGI(TAG, "TinyUSB Driver installed");
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
2024-03-21 15:55:57 +02:00
|
|
|
|
|
|
|
|
esp_err_t tinyusb_driver_uninstall()
|
|
|
|
|
{
|
|
|
|
|
tinyusb_free_descriptors();
|
|
|
|
|
return usb_del_phy(phy_hdl);
|
|
|
|
|
}
|