2021-10-25 17:13:46 +08:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* 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"
|
2020-07-16 10:43:02 +02:00
|
|
|
#include "driver/gpio.h"
|
2021-10-25 17:13:46 +08:00
|
|
|
#include "esp_private/periph_ctrl.h"
|
2021-07-09 11:15:26 +08:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_check.h"
|
|
|
|
#include "esp_rom_gpio.h"
|
2020-07-16 10:43:02 +02:00
|
|
|
#include "hal/gpio_ll.h"
|
2020-03-06 15:04:06 +01:00
|
|
|
#include "hal/usb_hal.h"
|
2020-06-19 12:00:58 +08:00
|
|
|
#include "soc/gpio_periph.h"
|
2020-07-16 10:43:02 +02:00
|
|
|
#include "soc/usb_periph.h"
|
|
|
|
#include "tinyusb.h"
|
2020-08-06 14:41:32 +02:00
|
|
|
#include "descriptors_control.h"
|
|
|
|
#include "tusb.h"
|
|
|
|
#include "tusb_tasks.h"
|
|
|
|
|
|
|
|
const static char *TAG = "TinyUSB";
|
2020-04-17 18:56:09 +02:00
|
|
|
|
|
|
|
static void configure_pins(usb_hal_context_t *usb)
|
|
|
|
{
|
|
|
|
/* usb_periph_iopins currently configures USB_OTG as USB Device.
|
|
|
|
* Introduce additional parameters in usb_hal_context_t when adding support
|
|
|
|
* for USB Host.
|
|
|
|
*/
|
2020-06-19 12:00:58 +08:00
|
|
|
for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
|
2020-04-17 18:56:09 +02:00
|
|
|
if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
|
2020-06-19 12:00:58 +08:00
|
|
|
esp_rom_gpio_pad_select_gpio(iopin->pin);
|
2020-04-17 18:56:09 +02:00
|
|
|
if (iopin->is_output) {
|
2020-06-19 12:00:58 +08:00
|
|
|
esp_rom_gpio_connect_out_signal(iopin->pin, iopin->func, false, false);
|
2020-04-17 18:56:09 +02:00
|
|
|
} else {
|
2020-06-19 12:00:58 +08:00
|
|
|
esp_rom_gpio_connect_in_signal(iopin->pin, iopin->func, false);
|
2020-08-06 14:41:32 +02:00
|
|
|
if ((iopin->pin != GPIO_FUNC_IN_LOW) && (iopin->pin != GPIO_FUNC_IN_HIGH)) {
|
2020-07-16 10:43:02 +02:00
|
|
|
gpio_ll_input_enable(&GPIO, iopin->pin);
|
|
|
|
}
|
2020-04-17 18:56:09 +02:00
|
|
|
}
|
2020-06-19 12:00:58 +08:00
|
|
|
esp_rom_gpio_pad_unhold(iopin->pin);
|
2020-04-17 18:56:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!usb->use_external_phy) {
|
2020-06-01 14:17:15 +02:00
|
|
|
gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
|
2020-04-17 18:56:09 +02:00
|
|
|
gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:04:06 +01:00
|
|
|
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
|
|
|
|
{
|
2021-07-09 11:15:26 +08:00
|
|
|
tusb_desc_device_t *dev_descriptor;
|
|
|
|
const char **string_descriptor;
|
|
|
|
ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
// Enable APB CLK to USB peripheral
|
2020-04-17 18:56:09 +02:00
|
|
|
periph_module_enable(PERIPH_USB_MODULE);
|
2021-07-09 11:15:26 +08:00
|
|
|
periph_module_reset(PERIPH_USB_MODULE);
|
|
|
|
// Initialize HAL layer
|
2020-03-06 15:04:06 +01:00
|
|
|
usb_hal_context_t hal = {
|
|
|
|
.use_external_phy = config->external_phy
|
|
|
|
};
|
|
|
|
usb_hal_init(&hal);
|
2020-04-17 18:56:09 +02:00
|
|
|
configure_pins(&hal);
|
2020-03-06 15:04:06 +01:00
|
|
|
|
2021-07-09 11:15:26 +08:00
|
|
|
dev_descriptor = config->descriptor ? config->descriptor : &descriptor_kconfig;
|
|
|
|
string_descriptor = config->string_descriptor ? config->string_descriptor : descriptor_str_kconfig;
|
2020-03-06 15:04:06 +01:00
|
|
|
|
2021-07-09 11:15:26 +08:00
|
|
|
tusb_set_descriptor(dev_descriptor, string_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;
|
|
|
|
}
|