2020-03-06 09:04:06 -05:00
|
|
|
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-08-06 08:41:32 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_rom_gpio.h"
|
2020-07-16 04:43:02 -04:00
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "driver/periph_ctrl.h"
|
2020-08-06 08:41:32 -04:00
|
|
|
#include "esp32s2/rom/gpio.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2020-07-16 04:43:02 -04:00
|
|
|
#include "hal/gpio_ll.h"
|
2020-03-06 09:04:06 -05:00
|
|
|
#include "hal/usb_hal.h"
|
2020-06-19 00:00:58 -04:00
|
|
|
#include "soc/gpio_periph.h"
|
2020-07-16 04:43:02 -04:00
|
|
|
#include "soc/usb_periph.h"
|
|
|
|
#include "tinyusb.h"
|
2020-08-06 08:41:32 -04:00
|
|
|
#include "descriptors_control.h"
|
|
|
|
#include "tusb.h"
|
|
|
|
#include "tusb_tasks.h"
|
|
|
|
#include "sdkconfig.h"
|
2020-11-05 23:00:07 -05:00
|
|
|
#include "esp_rom_gpio.h"
|
2020-08-06 08:41:32 -04:00
|
|
|
|
|
|
|
const static char *TAG = "TinyUSB";
|
2020-04-17 12:56:09 -04: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 00:00:58 -04:00
|
|
|
for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
|
2020-04-17 12:56:09 -04:00
|
|
|
if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_pad_select_gpio(iopin->pin);
|
2020-04-17 12:56:09 -04:00
|
|
|
if (iopin->is_output) {
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_connect_out_signal(iopin->pin, iopin->func, false, false);
|
2020-04-17 12:56:09 -04:00
|
|
|
} else {
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_connect_in_signal(iopin->pin, iopin->func, false);
|
2020-08-06 08:41:32 -04:00
|
|
|
if ((iopin->pin != GPIO_FUNC_IN_LOW) && (iopin->pin != GPIO_FUNC_IN_HIGH)) {
|
2020-07-16 04:43:02 -04:00
|
|
|
gpio_ll_input_enable(&GPIO, iopin->pin);
|
|
|
|
}
|
2020-04-17 12:56:09 -04:00
|
|
|
}
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_pad_unhold(iopin->pin);
|
2020-04-17 12:56:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!usb->use_external_phy) {
|
2020-06-01 08:17:15 -04:00
|
|
|
gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
|
2020-04-17 12:56:09 -04:00
|
|
|
gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 09:04:06 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes the tinyUSB driver.
|
|
|
|
*
|
|
|
|
* Note: Do not change any Custom descriptor, but
|
|
|
|
* if it used it is recomended to define: bDeviceClass = TUSB_CLASS_MISC,
|
|
|
|
* bDeviceSubClass = MISC_SUBCLASS_COMMON and bDeviceClass = TUSB_CLASS_MISC
|
|
|
|
* to match with Interface Association Descriptor (IAD) for CDC
|
|
|
|
*
|
|
|
|
* @param config if equal to NULL the default descriptor will be used
|
|
|
|
* @return esp_err_t Errors during the initialization
|
|
|
|
*/
|
|
|
|
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
|
|
|
|
{
|
|
|
|
tusb_desc_device_t *descriptor;
|
2020-08-06 08:41:32 -04:00
|
|
|
int res;
|
2020-03-06 09:04:06 -05:00
|
|
|
char **string_descriptor;
|
2020-08-06 08:41:32 -04:00
|
|
|
ESP_LOGI(TAG, "Driver installation...");
|
2020-03-06 09:04:06 -05:00
|
|
|
|
2020-04-17 12:56:09 -04:00
|
|
|
periph_module_reset(PERIPH_USB_MODULE);
|
|
|
|
periph_module_enable(PERIPH_USB_MODULE);
|
|
|
|
|
2020-03-06 09:04:06 -05:00
|
|
|
// Hal init
|
|
|
|
usb_hal_context_t hal = {
|
|
|
|
.use_external_phy = config->external_phy
|
|
|
|
};
|
|
|
|
usb_hal_init(&hal);
|
2020-04-17 12:56:09 -04:00
|
|
|
configure_pins(&hal);
|
2020-03-06 09:04:06 -05:00
|
|
|
|
|
|
|
if (config->descriptor == NULL) {
|
|
|
|
descriptor = &descriptor_kconfig;
|
|
|
|
} else {
|
|
|
|
descriptor = config->descriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (config->string_descriptor == NULL) {
|
|
|
|
string_descriptor = descriptor_str_kconfig;
|
|
|
|
} else {
|
|
|
|
string_descriptor = config->string_descriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
tusb_set_descriptor(descriptor,
|
|
|
|
string_descriptor);
|
|
|
|
|
2020-08-06 08:41:32 -04:00
|
|
|
res = tusb_init();
|
|
|
|
if (res != TUSB_ERROR_NONE) {
|
|
|
|
ESP_LOGE(TAG, "Can't initialize the TinyUSB stack. TinyUSB error: %d", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#if !CONFIG_USB_DO_NOT_CREATE_TASK
|
|
|
|
res = tusb_run_task();
|
|
|
|
if (res != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Can't create the TinyUSB task.");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ESP_LOGI(TAG, "Driver installed");
|
2020-03-06 09:04:06 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|