usb_glue_esp.c 3.5 KB

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