tinyusb.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "tinyusb.h"
  15. #include "hal/usb_hal.h"
  16. #include "soc/usb_periph.h"
  17. #include "driver/periph_ctrl.h"
  18. #include "driver/gpio.h"
  19. #include "esp32s2/rom/gpio.h"
  20. static void configure_pins(usb_hal_context_t *usb)
  21. {
  22. /* usb_periph_iopins currently configures USB_OTG as USB Device.
  23. * Introduce additional parameters in usb_hal_context_t when adding support
  24. * for USB Host.
  25. */
  26. for (const usb_iopin_dsc_t* iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
  27. if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
  28. gpio_pad_select_gpio(iopin->pin);
  29. if (iopin->is_output) {
  30. gpio_matrix_out(iopin->pin, iopin->func, false, false);
  31. } else {
  32. gpio_matrix_in(iopin->pin, iopin->func, false);
  33. gpio_pad_input_enable(iopin->pin);
  34. }
  35. gpio_pad_unhold(iopin->pin);
  36. }
  37. }
  38. if (!usb->use_external_phy) {
  39. gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
  40. gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
  41. }
  42. }
  43. /**
  44. * @brief Initializes the tinyUSB driver.
  45. *
  46. * Note: Do not change any Custom descriptor, but
  47. * if it used it is recomended to define: bDeviceClass = TUSB_CLASS_MISC,
  48. * bDeviceSubClass = MISC_SUBCLASS_COMMON and bDeviceClass = TUSB_CLASS_MISC
  49. * to match with Interface Association Descriptor (IAD) for CDC
  50. *
  51. * @param config if equal to NULL the default descriptor will be used
  52. * @return esp_err_t Errors during the initialization
  53. */
  54. esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
  55. {
  56. tusb_desc_device_t *descriptor;
  57. char **string_descriptor;
  58. periph_module_reset(PERIPH_USB_MODULE);
  59. periph_module_enable(PERIPH_USB_MODULE);
  60. // Hal init
  61. usb_hal_context_t hal = {
  62. .use_external_phy = config->external_phy
  63. };
  64. usb_hal_init(&hal);
  65. configure_pins(&hal);
  66. if (config->descriptor == NULL) {
  67. descriptor = &descriptor_kconfig;
  68. } else {
  69. descriptor = config->descriptor;
  70. }
  71. if (config->string_descriptor == NULL) {
  72. string_descriptor = descriptor_str_kconfig;
  73. } else {
  74. string_descriptor = config->string_descriptor;
  75. }
  76. tusb_set_descriptor(descriptor,
  77. string_descriptor);
  78. ESP_ERROR_CHECK(tusb_init());
  79. return ESP_OK;
  80. }