Skip to content

Commit a60a09a

Browse files
committed
feature(esp_tinyusb): Applied fix for UNPLUG event for v4.30a
1 parent 249222c commit a60a09a

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

device/esp_tinyusb/include_private/tinyusb_vbus_monitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct {
2929
* @param config VBUS monitoring configuration
3030
*
3131
* @return
32-
* - ESP_ERR_INVALID_ARG if config is NULL
32+
* - ESP_ERR_INVALID_ARG if config is NULL or Timer Task Stack Depth is insufficient when MSC is enabled
3333
* - ESP_ERR_INVALID_STATE if VBUS monitoring is already initialized
3434
* - ESP_ERR_NO_MEM if debounce timer creation failed
3535
* - ESP_OK if VBUS monitoring was initialized successfully

device/esp_tinyusb/tinyusb_vbus_monitor.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@
1212
#include "freertos/timers.h"
1313
#include "driver/gpio.h"
1414
#include "tinyusb_vbus_monitor.h"
15-
<<<<<<< HEAD
16-
=======
1715
#include "tinyusb_device.h"
18-
>>>>>>> a562322070 (refactor(esp_tinyusb): Made attached and dettached functions available as private)
1916

2017
const static char *TAG = "VBUS mon";
2118

2219
#if (CONFIG_IDF_TARGET_ESP32P4)
23-
#include "soc/usb_dwc_struct.h"
20+
#include "soc/usb_dwc_struct.h" // For GOTGCTL and DCTL registers access
21+
#define USB_DWC_REG USB_DWC_HS
22+
23+
#if (CONFIG_ESP32P4_REV_MIN_300)
24+
#define BVALID_OVERRIDE_SUPPORT_ENABLE 0
25+
#else
26+
#warning "Bvalid override is supported only on ESP32-P4 ECO4 and earlier revisions"
2427
// On ESP32-P4 USB OTG 2.0 signals are not wired to GPIO matrix
2528
// So we need to override the Bvalid signal from PHY
26-
#define USB_DWC_REG USB_DWC_HS
29+
#define BVALID_OVERRIDE_SUPPORT_ENABLE 1
30+
#endif // CONFIG_ESP32P4_REV_MIN_300
31+
#else
32+
#error "VBUS monitoring is supported only on ESP32-P4, USB OTG 2.0"
2733
#endif // CONFIG_IDF_TARGET_ESP32P4
2834

2935
/**
@@ -47,6 +53,7 @@ static vbus_monitor_context_t _vbus_ctx = {
4753
// Additional low-level USB DWC functions, which are not present in the IDF USB DWC HAL
4854
//
4955

56+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
5057
// --------------- GOTGCTL register ------------------
5158

5259
static void usb_dwc_ll_gotgctl_set_bvalid_override_value(usb_dwc_dev_t *hw, uint8_t value)
@@ -58,6 +65,7 @@ static void usb_dwc_ll_gotgctl_enable_bvalid_override(usb_dwc_dev_t *hw, bool en
5865
{
5966
hw->gotgctl_reg.bvalidoven = enable ? 1 : 0;
6067
}
68+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
6169

6270
// ------------------ DCTL register --------------------
6371

@@ -74,7 +82,9 @@ static void usb_dwc_ll_dctl_set_soft_disconnect(usb_dwc_dev_t *hw, bool enable)
7482
static void vbus_appeared(void)
7583
{
7684
ESP_LOGD(TAG, "Appeared");
85+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
7786
usb_dwc_ll_gotgctl_set_bvalid_override_value(&USB_DWC_REG, 1);
87+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
7888
usb_dwc_ll_dctl_set_soft_disconnect(&USB_DWC_REG, false);
7989
}
8090

@@ -84,8 +94,15 @@ static void vbus_appeared(void)
8494
static void vbus_disappeared(void)
8595
{
8696
ESP_LOGD(TAG, "Disappeared");
87-
usb_dwc_ll_gotgctl_set_bvalid_override_value(&USB_DWC_REG, 0);
8897
usb_dwc_ll_dctl_set_soft_disconnect(&USB_DWC_REG, true);
98+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
99+
usb_dwc_ll_gotgctl_set_bvalid_override_value(&USB_DWC_REG, 0);
100+
#else
101+
// Workaround for ESP32-P4 ECO5, USB-OTG peripheral v4.30a
102+
// We are not able to detect the disconnection event from the PHY after VBUS goes low
103+
// So we need to notify the upper logic about the disconnection event manually
104+
tinyusb_device_detached();
105+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
89106
}
90107

91108
/**
@@ -153,6 +170,14 @@ esp_err_t tinyusb_vbus_monitor_init(tinyusb_vbus_monitor_config_t *config)
153170
return ESP_ERR_INVALID_STATE;
154171
}
155172

173+
#if (CONFIG_TINYUSB_MSC_ENABLED)
174+
// When MSC is enabled, timer task stack size must be sufficient to handle FS operations during detach events
175+
#if (configTIMER_TASK_STACK_DEPTH < 2300)
176+
ESP_LOGE(TAG, "When MSC is enabled, configTIMER_TASK_STACK_DEPTH must be at least 2300 bytes to handle FS operations during attach/detach events");
177+
return ESP_ERR_INVALID_ARG;
178+
#endif // (configTIMER_TASK_STACK_DEPTH)
179+
#endif // (CONFIG_TINYUSB_MSC_ENABLED)
180+
156181
_vbus_ctx.gpio_num = config->gpio_num;
157182
_vbus_ctx.prev_state = false;
158183

@@ -189,12 +214,15 @@ esp_err_t tinyusb_vbus_monitor_init(tinyusb_vbus_monitor_config_t *config)
189214
}
190215
// Disable GPIO interrupt
191216
gpio_intr_disable(_vbus_ctx.gpio_num);
217+
218+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
192219
// Set initial Bvalid override value and enable override
193220
usb_dwc_ll_gotgctl_set_bvalid_override_value(&USB_DWC_REG, 0);
194221
// Wait 1 microsecond (sufficient for >5 PHY clocks)
195222
esp_rom_delay_us(1);
196223
// Enable to override the signal from PHY
197224
usb_dwc_ll_gotgctl_enable_bvalid_override(&USB_DWC_REG, true);
225+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
198226

199227
// Device could be already connected, check the status and start the timer if needed
200228
if (gpio_get_level(_vbus_ctx.gpio_num)) {
@@ -214,7 +242,9 @@ esp_err_t tinyusb_vbus_monitor_init(tinyusb_vbus_monitor_config_t *config)
214242

215243
timer_err:
216244
gpio_isr_handler_remove(_vbus_ctx.gpio_num);
245+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
217246
usb_dwc_ll_gotgctl_enable_bvalid_override(&USB_DWC_REG, false);
247+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
218248
isr_err:
219249
gpio_reset_pin(_vbus_ctx.gpio_num);
220250
_vbus_ctx.gpio_num = GPIO_NUM_NC;
@@ -248,8 +278,11 @@ esp_err_t tinyusb_vbus_monitor_deinit(void)
248278
xTimerDelete(_vbus_ctx.debounce_timer, 0);
249279
_vbus_ctx.debounce_timer = NULL;
250280

281+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
251282
// Disable to override the signal from PHY
252283
usb_dwc_ll_gotgctl_enable_bvalid_override(&USB_DWC_REG, false);
284+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
285+
253286
ESP_LOGD(TAG, "Deinit");
254287
return ESP_OK;
255288
}

0 commit comments

Comments
 (0)