|
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 |
|
@@ -63,6 +62,10 @@ typedef struct {
|
63 | 62 | bool external_phy;
|
64 | 63 | } tinyusb_config_t;
|
65 | 64 |
|
| 65 | +#if __has_include("hal/usb_hal.h") |
| 66 | + |
| 67 | +#include "hal/usb_hal.h" |
| 68 | + |
66 | 69 | static bool usb_otg_deinit(void *busptr) {
|
67 | 70 | // Once USB OTG is initialized, its GPIOs are assigned and it shall never be deinited
|
68 | 71 | // except when S3 swithicng usb from cdc to jtag while resetting to bootrom
|
@@ -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;
|
@@ -420,6 +480,7 @@ static void hw_cdc_reset_handler(void *arg) {
|
420 | 480 |
|
421 | 481 | static void usb_switch_to_cdc_jtag() {
|
422 | 482 | // Disable USB-OTG
|
| 483 | + deinit_usb_hal(); |
423 | 484 | periph_ll_reset(PERIPH_USB_MODULE);
|
424 | 485 | //periph_ll_enable_clk_clear_rst(PERIPH_USB_MODULE);
|
425 | 486 | periph_ll_disable_clk_set_rst(PERIPH_USB_MODULE);
|
|
0 commit comments