tinyusb.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "driver/gpio.h"
  8. #include "esp_private/periph_ctrl.h"
  9. #include "esp_log.h"
  10. #include "esp_check.h"
  11. #include "esp_rom_gpio.h"
  12. #include "hal/gpio_ll.h"
  13. #include "hal/usb_hal.h"
  14. #include "soc/gpio_periph.h"
  15. #include "soc/usb_periph.h"
  16. #include "soc/gpio_pins.h"
  17. #include "tinyusb.h"
  18. #include "descriptors_control.h"
  19. #include "tusb.h"
  20. #include "tusb_tasks.h"
  21. const static char *TAG = "TinyUSB";
  22. static void configure_pins(usb_hal_context_t *usb)
  23. {
  24. /* usb_periph_iopins currently configures USB_OTG as USB Device.
  25. * Introduce additional parameters in usb_hal_context_t when adding support
  26. * for USB Host.
  27. */
  28. for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
  29. if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
  30. esp_rom_gpio_pad_select_gpio(iopin->pin);
  31. if (iopin->is_output) {
  32. esp_rom_gpio_connect_out_signal(iopin->pin, iopin->func, false, false);
  33. } else {
  34. esp_rom_gpio_connect_in_signal(iopin->pin, iopin->func, false);
  35. if ((iopin->pin != GPIO_MATRIX_CONST_ZERO_INPUT) && (iopin->pin != GPIO_MATRIX_CONST_ONE_INPUT)) {
  36. gpio_ll_input_enable(&GPIO, iopin->pin);
  37. }
  38. }
  39. esp_rom_gpio_pad_unhold(iopin->pin);
  40. }
  41. }
  42. if (!usb->use_external_phy) {
  43. gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
  44. gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
  45. }
  46. }
  47. esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
  48. {
  49. tusb_desc_device_t *dev_descriptor;
  50. const char **string_descriptor;
  51. ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  52. // Enable APB CLK to USB peripheral
  53. periph_module_enable(PERIPH_USB_MODULE);
  54. periph_module_reset(PERIPH_USB_MODULE);
  55. // Initialize HAL layer
  56. usb_hal_context_t hal = {
  57. .use_external_phy = config->external_phy
  58. };
  59. usb_hal_init(&hal);
  60. configure_pins(&hal);
  61. dev_descriptor = config->descriptor ? config->descriptor : &descriptor_kconfig;
  62. string_descriptor = config->string_descriptor ? config->string_descriptor : descriptor_str_kconfig;
  63. tusb_set_descriptor(dev_descriptor, string_descriptor);
  64. ESP_RETURN_ON_FALSE(tusb_init(), ESP_FAIL, TAG, "Init TinyUSB stack failed");
  65. #if !CONFIG_TINYUSB_NO_DEFAULT_TASK
  66. ESP_RETURN_ON_ERROR(tusb_run_task(), TAG, "Run TinyUSB task failed");
  67. #endif
  68. ESP_LOGI(TAG, "TinyUSB Driver installed");
  69. return ESP_OK;
  70. }