|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | +#include "esp_log.h" |
| 9 | +#include "esp_check.h" |
| 10 | +#include "driver/gpio.h" |
| 11 | +#include "esp_timer.h" |
| 12 | + |
| 13 | +#include "tinyusb.h" |
| 14 | +#include "sdkconfig.h" |
| 15 | + |
| 16 | +const static char *TAG = "VBUS mon"; |
| 17 | + |
| 18 | +#define DEBOUNCE_DELAY_MS 500 /* TODO: make configurable */ |
| 19 | + |
| 20 | +#if (CONFIG_IDF_TARGET_ESP32P4) |
| 21 | +// On ESP32-P4 OTG signals from USB-DWC are not wired to GPIO matrix |
| 22 | +// So we need to override the Bvalid signal from PHY |
| 23 | +#define ENABLE_BVALID_OVERRIDE 1 |
| 24 | +#endif // CONFIG_IDF_TARGET_ESP32P4 |
| 25 | + |
| 26 | +#if (ENABLE_BVALID_OVERRIDE) |
| 27 | +#include "soc/usb_dwc_struct.h" |
| 28 | +#endif // ENABLE_BVALID_OVERRIDE |
| 29 | + |
| 30 | +static esp_timer_handle_t _vbus_debounce_timer = NULL; |
| 31 | + |
| 32 | +static void vbus_io_cb(void *arg) |
| 33 | +{ |
| 34 | + int vbus_io_num = (int) arg; |
| 35 | + // disable interrupts for a while to debounce |
| 36 | + gpio_intr_disable(vbus_io_num); |
| 37 | + // enable debounce timer |
| 38 | + esp_timer_start_once(_vbus_debounce_timer, DEBOUNCE_DELAY_MS * 1000); |
| 39 | +} |
| 40 | + |
| 41 | +static void vbus_debounce_timer_cb(void *arg) |
| 42 | +{ |
| 43 | + int vbus_io_num = (int) arg; |
| 44 | + |
| 45 | + if (gpio_get_level(vbus_io_num)) { |
| 46 | + // If the VBUS is still present |
| 47 | +#if (ENABLE_BVALID_OVERRIDE) |
| 48 | + USB_DWC_HS.gotgctl_reg.bvalidovval = 1; |
| 49 | +#endif // ENABLE_BVALID_OVERRIDE |
| 50 | + } else { |
| 51 | + // VBUS is not present |
| 52 | +#if (ENABLE_BVALID_OVERRIDE) |
| 53 | + USB_DWC_HS.gotgctl_reg.bvalidovval = 0; |
| 54 | +#endif // ENABLE_BVALID_OVERRIDE |
| 55 | + |
| 56 | + // Note: |
| 57 | + // When device stayed connected in the USB Host port, we need to disable the pull-up resistor on D+ D- first |
| 58 | + // Disable pull-up resistor on D+ D- |
| 59 | + // But this creates a breaking change, so we can call it in the notification callback |
| 60 | + } |
| 61 | + |
| 62 | + // Re-enable GPIO interrupt |
| 63 | + gpio_intr_enable(vbus_io_num); |
| 64 | +} |
| 65 | + |
| 66 | +esp_err_t tinyusb_vbus_monitor_init(int vbus_io_num) |
| 67 | +{ |
| 68 | + esp_err_t ret; |
| 69 | + |
| 70 | + // There could be only one instance of VBUS monitoring |
| 71 | + if (_vbus_debounce_timer) { |
| 72 | + ESP_LOGE(TAG, "Already initialized"); |
| 73 | + return ESP_ERR_INVALID_STATE; |
| 74 | + } |
| 75 | + |
| 76 | + // VBUS Debounce timer |
| 77 | + const esp_timer_create_args_t vbus_timer_args = { |
| 78 | + .callback = &vbus_debounce_timer_cb, |
| 79 | + .arg = (void *) vbus_io_num, |
| 80 | + }; |
| 81 | + ret = esp_timer_create(&vbus_timer_args, &_vbus_debounce_timer); |
| 82 | + if (ret != ESP_OK) { |
| 83 | + ESP_LOGE(TAG, "Create VBUS debounce timer failed"); |
| 84 | + return ret; |
| 85 | + } |
| 86 | + |
| 87 | + // Init gpio IRQ for VBUS monitoring |
| 88 | + const gpio_config_t vbus_io_cfg = { |
| 89 | + .pin_bit_mask = BIT64(vbus_io_num), |
| 90 | + .mode = GPIO_MODE_INPUT, |
| 91 | + .pull_down_en = GPIO_PULLDOWN_ENABLE, |
| 92 | + .intr_type = GPIO_INTR_ANYEDGE, |
| 93 | + }; |
| 94 | + |
| 95 | + ret = gpio_config(&vbus_io_cfg); |
| 96 | + if (ret != ESP_OK) { |
| 97 | + ESP_LOGE(TAG, "Config VBUS GPIO failed"); |
| 98 | + goto gpio_fail; |
| 99 | + } |
| 100 | + |
| 101 | + ret = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED); |
| 102 | + if (ret != ESP_OK) { |
| 103 | + ESP_LOGE(TAG, "Install GPIO ISR service failed"); |
| 104 | + goto isr_fail; |
| 105 | + } |
| 106 | + |
| 107 | + ret = gpio_isr_handler_add(vbus_io_num, vbus_io_cb, (void *) vbus_io_num); |
| 108 | + if (ret != ESP_OK) { |
| 109 | + ESP_LOGE(TAG, "Add GPIO ISR handler failed"); |
| 110 | + goto add_isr_hdl_fail; |
| 111 | + } |
| 112 | + |
| 113 | + // Device could be already connected, check the status and start the timer if needed |
| 114 | + if (gpio_get_level(vbus_io_num)) { |
| 115 | + esp_timer_start_once(_vbus_debounce_timer, DEBOUNCE_DELAY_MS * 1000); |
| 116 | + } |
| 117 | + |
| 118 | + ESP_LOGD(TAG, "Configured via GPIO%d", vbus_io_num); |
| 119 | + return ESP_OK; |
| 120 | + |
| 121 | +add_isr_hdl_fail: |
| 122 | + gpio_uninstall_isr_service(); |
| 123 | +isr_fail: |
| 124 | + gpio_reset_pin(vbus_io_num); |
| 125 | +gpio_fail: |
| 126 | + if (_vbus_debounce_timer) { |
| 127 | + esp_timer_delete(_vbus_debounce_timer); |
| 128 | + _vbus_debounce_timer = NULL; |
| 129 | + } |
| 130 | + return ret; |
| 131 | +} |
| 132 | + |
| 133 | +void tinyusb_vbus_monitor_enable(void) |
| 134 | +{ |
| 135 | + if (_vbus_debounce_timer == NULL) { |
| 136 | + return; |
| 137 | + } |
| 138 | +#if (ENABLE_BVALID_OVERRIDE) |
| 139 | + // Enable to override the signal from PHY |
| 140 | + USB_DWC_HS.gotgctl_reg.bvalidoven = 1; |
| 141 | + // Wait 1 microsecond (sufficient for >5 PHY clocks) |
| 142 | + esp_rom_delay_us(1); |
| 143 | + // Set Bvalid signal to 0 initially |
| 144 | + USB_DWC_HS.gotgctl_reg.bvalidovval = 0; |
| 145 | +#endif // ENABLE_BVALID_OVERRIDE |
| 146 | + ESP_LOGD(TAG, "Enabled"); |
| 147 | +} |
| 148 | + |
| 149 | +void tinyusb_vbus_monitor_deinit(int vbus_io_num) |
| 150 | +{ |
| 151 | + // Deinit gpio IRQ for VBUS monitoring |
| 152 | + if (esp_timer_is_active(_vbus_debounce_timer)) { |
| 153 | + ESP_ERROR_CHECK(esp_timer_stop(_vbus_debounce_timer)); |
| 154 | + } |
| 155 | + esp_timer_delete(_vbus_debounce_timer); |
| 156 | + _vbus_debounce_timer = NULL; |
| 157 | + |
| 158 | + // Deinit gpio IRQ for VBUS monitoring |
| 159 | + ESP_ERROR_CHECK(gpio_isr_handler_remove(vbus_io_num)); |
| 160 | + gpio_intr_disable(vbus_io_num); |
| 161 | + gpio_uninstall_isr_service(); |
| 162 | + |
| 163 | + ESP_LOGD(TAG, "Deinit"); |
| 164 | +} |
0 commit comments