|
22 | 22 |
|
23 | 23 | #include "rom/gpio.h"
|
24 | 24 |
|
25 |
| -#include "hal/usb_hal.h" |
26 | 25 | #include "hal/gpio_ll.h"
|
27 | 26 | #include "hal/clk_gate_ll.h"
|
28 | 27 |
|
@@ -73,6 +72,10 @@ static bool usb_otg_deinit(void *busptr) {
|
73 | 72 | #endif
|
74 | 73 | }
|
75 | 74 |
|
| 75 | +#if __has_include("hal/usb_hal.h") |
| 76 | + |
| 77 | +#include "hal/usb_hal.h" |
| 78 | + |
76 | 79 | static void configure_pins(usb_hal_context_t *usb) {
|
77 | 80 | for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
|
78 | 81 | if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
|
@@ -101,10 +104,67 @@ static void configure_pins(usb_hal_context_t *usb) {
|
101 | 104 | }
|
102 | 105 | }
|
103 | 106 |
|
104 |
| -esp_err_t tinyusb_driver_install(const tinyusb_config_t *config) { |
105 |
| - usb_hal_context_t hal = {.use_external_phy = config->external_phy}; |
| 107 | +esp_err_t init_usb_hal(bool external_phy) { |
| 108 | + usb_hal_context_t hal = {.use_external_phy = external_phy}; |
106 | 109 | usb_hal_init(&hal);
|
107 | 110 | configure_pins(&hal);
|
| 111 | + return ESP_OK; |
| 112 | +} |
| 113 | + |
| 114 | +esp_err_t deinit_usb_hal() { |
| 115 | + return ESP_OK; |
| 116 | +} |
| 117 | + |
| 118 | +#elif __has_include("esp_private/usb_phy.h") |
| 119 | + |
| 120 | +#include "esp_private/usb_phy.h" |
| 121 | + |
| 122 | +static usb_phy_handle_t phy_handle = NULL; |
| 123 | + |
| 124 | +esp_err_t init_usb_hal(bool external_phy) { |
| 125 | + esp_err_t ret = ESP_OK; |
| 126 | + usb_phy_config_t phy_config = { |
| 127 | + .controller = USB_PHY_CTRL_OTG, |
| 128 | + .target = USB_PHY_TARGET_INT, |
| 129 | + .otg_mode = USB_OTG_MODE_DEVICE, |
| 130 | + .otg_speed = USB_PHY_SPEED_FULL, |
| 131 | + .ext_io_conf = NULL, |
| 132 | + .otg_io_conf = NULL, |
| 133 | + }; |
| 134 | + |
| 135 | + ret = usb_new_phy(&phy_config, &phy_handle); |
| 136 | + if (ret != ESP_OK) { |
| 137 | + log_e("Failed to init USB PHY"); |
| 138 | + } |
| 139 | + return ret; |
| 140 | +} |
| 141 | + |
| 142 | +esp_err_t deinit_usb_hal() { |
| 143 | + esp_err_t ret = ESP_OK; |
| 144 | + if (phy_handle) { |
| 145 | + ret = usb_del_phy(phy_handle); |
| 146 | + if (ret != ESP_OK) { |
| 147 | + log_e("Failed to deinit USB PHY"); |
| 148 | + } |
| 149 | + } |
| 150 | + return ret; |
| 151 | +} |
| 152 | + |
| 153 | +#else |
| 154 | + |
| 155 | +#error No way to initialize USP PHY |
| 156 | + |
| 157 | +void init_usb_hal(bool external_phy) { |
| 158 | + return ESP_OK; |
| 159 | +} |
| 160 | + |
| 161 | +void deinit_usb_hal() { |
| 162 | + return ESP_OK; |
| 163 | +} |
| 164 | +#endif |
| 165 | + |
| 166 | +esp_err_t tinyusb_driver_install(const tinyusb_config_t *config) { |
| 167 | + init_usb_hal(config->external_phy); |
108 | 168 | if (!tusb_init()) {
|
109 | 169 | log_e("Can't initialize the TinyUSB stack.");
|
110 | 170 | return ESP_FAIL;
|
@@ -416,6 +476,7 @@ static void hw_cdc_reset_handler(void *arg) {
|
416 | 476 |
|
417 | 477 | static void usb_switch_to_cdc_jtag() {
|
418 | 478 | // Disable USB-OTG
|
| 479 | + deinit_usb_hal(); |
419 | 480 | periph_ll_reset(PERIPH_USB_MODULE);
|
420 | 481 | //periph_ll_enable_clk_clear_rst(PERIPH_USB_MODULE);
|
421 | 482 | periph_ll_disable_clk_set_rst(PERIPH_USB_MODULE);
|
|
0 commit comments