2021-10-25 17:13:46 +08:00
|
|
|
/*
|
2022-01-05 16:17:12 +08:00
|
|
|
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
|
2021-10-25 17:13:46 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-03-06 15:04:06 +01:00
|
|
|
|
2021-07-09 11:15:26 +08:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_check.h"
|
2021-09-24 14:22:47 +08:00
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_private/periph_ctrl.h"
|
|
|
|
#include "esp_private/usb_phy.h"
|
|
|
|
#include "soc/usb_pins.h"
|
2020-07-16 10:43:02 +02:00
|
|
|
#include "tinyusb.h"
|
2020-08-06 14:41:32 +02:00
|
|
|
#include "descriptors_control.h"
|
2022-02-10 14:49:11 +01:00
|
|
|
#include "usb_descriptors.h"
|
2020-08-06 14:41:32 +02:00
|
|
|
#include "tusb.h"
|
|
|
|
#include "tusb_tasks.h"
|
|
|
|
|
|
|
|
const static char *TAG = "TinyUSB";
|
2021-09-24 14:22:47 +08:00
|
|
|
static usb_phy_handle_t phy_hdl;
|
2020-04-17 18:56:09 +02:00
|
|
|
|
2020-03-06 15:04:06 +01:00
|
|
|
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
|
|
|
|
{
|
2022-02-10 14:49:11 +01:00
|
|
|
const tusb_desc_device_t *dev_descriptor;
|
2021-07-09 11:15:26 +08:00
|
|
|
const char **string_descriptor;
|
2022-02-10 14:49:11 +01:00
|
|
|
const uint8_t *cfg_descriptor;
|
2021-07-09 11:15:26 +08:00
|
|
|
ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
2021-09-24 14:22:47 +08:00
|
|
|
|
|
|
|
// Configure USB PHY
|
|
|
|
usb_phy_config_t phy_conf = {
|
|
|
|
.controller = USB_PHY_CTRL_OTG,
|
|
|
|
.otg_mode = USB_OTG_MODE_DEVICE,
|
2020-03-06 15:04:06 +01:00
|
|
|
};
|
2022-09-22 12:33:33 +02:00
|
|
|
|
|
|
|
// External PHY IOs config
|
2022-10-06 09:04:53 +02:00
|
|
|
usb_phy_ext_io_conf_t ext_io_conf = {
|
2022-07-21 15:42:48 +02:00
|
|
|
.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,
|
2022-06-29 17:40:19 +02:00
|
|
|
};
|
2021-09-24 14:22:47 +08:00
|
|
|
if (config->external_phy) {
|
|
|
|
phy_conf.target = USB_PHY_TARGET_EXT;
|
2022-10-06 09:04:53 +02:00
|
|
|
phy_conf.ext_io_conf = &ext_io_conf;
|
2021-09-24 14:22:47 +08:00
|
|
|
} else {
|
|
|
|
phy_conf.target = USB_PHY_TARGET_INT;
|
|
|
|
}
|
2022-09-22 12:33:33 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2021-09-24 14:22:47 +08:00
|
|
|
ESP_RETURN_ON_ERROR(usb_new_phy(&phy_conf, &phy_hdl), TAG, "Install USB PHY failed");
|
2020-03-06 15:04:06 +01:00
|
|
|
|
2022-07-21 15:42:48 +02:00
|
|
|
#if (CONFIG_TINYUSB_HID_COUNT > 0)
|
|
|
|
// For HID device, configuration descriptor must be provided
|
|
|
|
ESP_RETURN_ON_FALSE(config->configuration_descriptor, ESP_ERR_INVALID_ARG, TAG, "Configuration descriptor must be provided for HID device");
|
|
|
|
#endif
|
2022-02-10 14:49:11 +01:00
|
|
|
dev_descriptor = config->device_descriptor ? config->device_descriptor : &descriptor_dev_kconfig;
|
2021-07-09 11:15:26 +08:00
|
|
|
string_descriptor = config->string_descriptor ? config->string_descriptor : descriptor_str_kconfig;
|
2022-02-10 14:49:11 +01:00
|
|
|
cfg_descriptor = config->configuration_descriptor ? config->configuration_descriptor : descriptor_cfg_kconfig;
|
2020-03-06 15:04:06 +01:00
|
|
|
|
2022-02-10 14:49:11 +01:00
|
|
|
tusb_set_descriptor(dev_descriptor, string_descriptor, cfg_descriptor);
|
2020-03-06 15:04:06 +01:00
|
|
|
|
2021-07-09 11:15:26 +08:00
|
|
|
ESP_RETURN_ON_FALSE(tusb_init(), ESP_FAIL, TAG, "Init TinyUSB stack failed");
|
|
|
|
#if !CONFIG_TINYUSB_NO_DEFAULT_TASK
|
|
|
|
ESP_RETURN_ON_ERROR(tusb_run_task(), TAG, "Run TinyUSB task failed");
|
2020-08-06 14:41:32 +02:00
|
|
|
#endif
|
2021-07-09 11:15:26 +08:00
|
|
|
ESP_LOGI(TAG, "TinyUSB Driver installed");
|
2020-03-06 15:04:06 +01:00
|
|
|
return ESP_OK;
|
|
|
|
}
|