usb_glue_esp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "esp_idf_version.h"
  8. #include "esp_intr_alloc.h"
  9. #include "esp_private/usb_phy.h"
  10. #include "soc/periph_defs.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "usbd_core.h"
  13. #include "usbh_core.h"
  14. #ifdef CONFIG_IDF_TARGET_ESP32S2
  15. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ
  16. #define DEFAULT_USB_INTR_SOURCE ETS_USB_INTR_SOURCE
  17. #elif CONFIG_IDF_TARGET_ESP32S3
  18. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
  19. #define DEFAULT_USB_INTR_SOURCE ETS_USB_INTR_SOURCE
  20. #elif CONFIG_IDF_TARGET_ESP32P4
  21. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ
  22. #define DEFAULT_USB_INTR_SOURCE ETS_USB_OTG_INTR_SOURCE
  23. #else
  24. #define DEFAULT_CPU_FREQ_MHZ 160
  25. #endif
  26. uint32_t SystemCoreClock = (DEFAULT_CPU_FREQ_MHZ * 1000 * 1000);
  27. static usb_phy_handle_t s_phy_handle = NULL;
  28. static intr_handle_t s_interrupt_handle = NULL;
  29. static void usb_dc_interrupt_cb(void *arg_pv)
  30. {
  31. extern void USBD_IRQHandler(uint8_t busid);
  32. USBD_IRQHandler(0);
  33. }
  34. void usb_dc_low_level_init(uint8_t busid)
  35. {
  36. usb_phy_config_t phy_config = {
  37. .controller = USB_PHY_CTRL_OTG,
  38. .otg_mode = USB_OTG_MODE_DEVICE,
  39. .target = USB_PHY_TARGET_INT,
  40. };
  41. esp_err_t ret = usb_new_phy(&phy_config, &s_phy_handle);
  42. if (ret != ESP_OK) {
  43. USB_LOG_ERR("USB Phy Init Failed!\r\n");
  44. return;
  45. }
  46. // TODO: Check when to enable interrupt
  47. ret = esp_intr_alloc(DEFAULT_USB_INTR_SOURCE, ESP_INTR_FLAG_LOWMED, usb_dc_interrupt_cb, 0, &s_interrupt_handle);
  48. if (ret != ESP_OK) {
  49. USB_LOG_ERR("USB Interrupt Init Failed!\r\n");
  50. return;
  51. }
  52. USB_LOG_INFO("cherryusb, version: "CHERRYUSB_VERSION_STR"\r\n");
  53. }
  54. void usb_dc_low_level_deinit(uint8_t busid)
  55. {
  56. if (s_interrupt_handle) {
  57. esp_intr_free(s_interrupt_handle);
  58. s_interrupt_handle = NULL;
  59. }
  60. if (s_phy_handle) {
  61. usb_del_phy(s_phy_handle);
  62. s_phy_handle = NULL;
  63. }
  64. }
  65. uint32_t usbd_get_dwc2_gccfg_conf(uint32_t reg_base)
  66. {
  67. return 0;
  68. }
  69. static void usb_hc_interrupt_cb(void *arg_pv)
  70. {
  71. extern void USBH_IRQHandler(uint8_t busid);
  72. USBH_IRQHandler(0);
  73. }
  74. void usb_hc_low_level_init(struct usbh_bus *bus)
  75. {
  76. // Host Library defaults to internal PHY
  77. usb_phy_config_t phy_config = {
  78. .controller = USB_PHY_CTRL_OTG,
  79. .target = USB_PHY_TARGET_INT,
  80. .otg_mode = USB_OTG_MODE_HOST,
  81. .otg_speed = USB_PHY_SPEED_UNDEFINED, // In Host mode, the speed is determined by the connected device
  82. .ext_io_conf = NULL,
  83. .otg_io_conf = NULL,
  84. };
  85. esp_err_t ret = usb_new_phy(&phy_config, &s_phy_handle);
  86. if (ret != ESP_OK) {
  87. USB_LOG_ERR("USB Phy Init Failed!\r\n");
  88. return;
  89. }
  90. // TODO: Check when to enable interrupt
  91. ret = esp_intr_alloc(DEFAULT_USB_INTR_SOURCE, ESP_INTR_FLAG_LOWMED, usb_hc_interrupt_cb, 0, &s_interrupt_handle);
  92. if (ret != ESP_OK) {
  93. USB_LOG_ERR("USB Interrupt Init Failed!\r\n");
  94. return;
  95. }
  96. USB_LOG_INFO("cherryusb, version: "CHERRYUSB_VERSION_STR"\r\n");
  97. }
  98. void usb_hc_low_level_deinit(struct usbh_bus *bus)
  99. {
  100. if (s_interrupt_handle) {
  101. esp_intr_free(s_interrupt_handle);
  102. s_interrupt_handle = NULL;
  103. }
  104. if (s_phy_handle) {
  105. usb_del_phy(s_phy_handle);
  106. s_phy_handle = NULL;
  107. }
  108. }
  109. uint32_t usbh_get_dwc2_gccfg_conf(uint32_t reg_base)
  110. {
  111. return 0;
  112. }
  113. void usbd_dwc2_delay_ms(uint8_t ms)
  114. {
  115. vTaskDelay(pdMS_TO_TICKS(ms));
  116. }