Skip to content

Commit f156c98

Browse files
committed
feature(esp_tinyusb): Applied fix for UNPLUG event for v4.30a
1 parent 097fe98 commit f156c98

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

device/esp_tinyusb/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ To enable Mass Storage Device:
349349
- select the option from `menuconfig`
350350
- configure storage for MSC Device class: SPI Flash or SD/MMC (when supported by the hardware).
351351
352+
> **🔧 Self-powered Flash Drive**:
353+
>
354+
> When VBUS monitoring is enabled with `TINYUSB_PORT_HIGH_SPEED_0` (ESP32-P4 USB OTG 2.0), the storage filesystem is mounted and unmounted between the application and the USB Host based on software VBUS monitoring events. In this scenario, the Timer Task Stack Size (`configTIMER_TASK_STACK_DEPTH`) must be large enough to handle filesystem operations during detach events. It is recommended to set this value to at least 2304 bytes (2048 + 256).
355+
352356
**SPI-Flash Storage**
353357
354358
```c

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: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@
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
2426
// On ESP32-P4 USB OTG 2.0 signals are not wired to GPIO matrix
2527
// So we need to override the Bvalid signal from PHY
26-
#define USB_DWC_REG USB_DWC_HS
28+
#define BVALID_OVERRIDE_SUPPORT_ENABLE 1
29+
#endif // CONFIG_ESP32P4_REV_MIN_300
30+
#else
31+
#error "VBUS monitoring is supported only on ESP32-P4, USB OTG 2.0"
2732
#endif // CONFIG_IDF_TARGET_ESP32P4
2833

2934
/**
@@ -47,6 +52,7 @@ static vbus_monitor_context_t _vbus_ctx = {
4752
// Additional low-level USB DWC functions, which are not present in the IDF USB DWC HAL
4853
//
4954

55+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
5056
// --------------- GOTGCTL register ------------------
5157

5258
static void usb_dwc_ll_gotgctl_set_bvalid_override_value(usb_dwc_dev_t *hw, uint8_t value)
@@ -58,6 +64,7 @@ static void usb_dwc_ll_gotgctl_enable_bvalid_override(usb_dwc_dev_t *hw, bool en
5864
{
5965
hw->gotgctl_reg.bvalidoven = enable ? 1 : 0;
6066
}
67+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
6168

6269
// ------------------ DCTL register --------------------
6370

@@ -74,7 +81,9 @@ static void usb_dwc_ll_dctl_set_soft_disconnect(usb_dwc_dev_t *hw, bool enable)
7481
static void vbus_appeared(void)
7582
{
7683
ESP_LOGD(TAG, "Appeared");
84+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
7785
usb_dwc_ll_gotgctl_set_bvalid_override_value(&USB_DWC_REG, 1);
86+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
7887
usb_dwc_ll_dctl_set_soft_disconnect(&USB_DWC_REG, false);
7988
}
8089

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

91107
/**
@@ -153,6 +169,14 @@ esp_err_t tinyusb_vbus_monitor_init(tinyusb_vbus_monitor_config_t *config)
153169
return ESP_ERR_INVALID_STATE;
154170
}
155171

172+
#if (CONFIG_TINYUSB_MSC_ENABLED)
173+
// When MSC is enabled, timer task stack size must be sufficient to handle FS operations during detach events
174+
#if (configTIMER_TASK_STACK_DEPTH < 2300)
175+
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");
176+
return ESP_ERR_INVALID_ARG;
177+
#endif // (configTIMER_TASK_STACK_DEPTH)
178+
#endif // (CONFIG_TINYUSB_MSC_ENABLED)
179+
156180
_vbus_ctx.gpio_num = config->gpio_num;
157181
_vbus_ctx.prev_state = false;
158182

@@ -189,12 +213,15 @@ esp_err_t tinyusb_vbus_monitor_init(tinyusb_vbus_monitor_config_t *config)
189213
}
190214
// Disable GPIO interrupt
191215
gpio_intr_disable(_vbus_ctx.gpio_num);
216+
217+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
192218
// Set initial Bvalid override value and enable override
193219
usb_dwc_ll_gotgctl_set_bvalid_override_value(&USB_DWC_REG, 0);
194220
// Wait 1 microsecond (sufficient for >5 PHY clocks)
195221
esp_rom_delay_us(1);
196222
// Enable to override the signal from PHY
197223
usb_dwc_ll_gotgctl_enable_bvalid_override(&USB_DWC_REG, true);
224+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
198225

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

215242
timer_err:
216243
gpio_isr_handler_remove(_vbus_ctx.gpio_num);
244+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
217245
usb_dwc_ll_gotgctl_enable_bvalid_override(&USB_DWC_REG, false);
246+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
218247
isr_err:
219248
gpio_reset_pin(_vbus_ctx.gpio_num);
220249
_vbus_ctx.gpio_num = GPIO_NUM_NC;
@@ -248,8 +277,11 @@ esp_err_t tinyusb_vbus_monitor_deinit(void)
248277
xTimerDelete(_vbus_ctx.debounce_timer, 0);
249278
_vbus_ctx.debounce_timer = NULL;
250279

280+
#if (BVALID_OVERRIDE_SUPPORT_ENABLE)
251281
// Disable to override the signal from PHY
252282
usb_dwc_ll_gotgctl_enable_bvalid_override(&USB_DWC_REG, false);
283+
#endif // BVALID_OVERRIDE_SUPPORT_ENABLE
284+
253285
ESP_LOGD(TAG, "Deinit");
254286
return ESP_OK;
255287
}

0 commit comments

Comments
 (0)