Skip to content

Conversation

@roma-jam
Copy link
Collaborator

@roma-jam roma-jam commented Nov 10, 2025

Description

Added the vbus monitor state change callback to vbus_monitor module.
This callback is used to workaround the absence of UNPLUG event in tinyusb, because:

  • On the USB OTG 2.0 v4.30a, VBUSVALID, SRP_BVALID, SESSEND: these signals are not available
  • USB_SRP_BVALID_IN_IDX cannot be connected to GPIO via matrix

Thus, based on the VBUS IO state (in our case only relevant for DETACHED event), we notify the upper layer.

Limitation

  • As the storage mount/umount logic is handled in the FreeRTOS timer task context (Tmr SVC), the stack size should be not less then 2304 (2048 + 256) Bytes (Measured manually, as it is not working with 2048 default size. Starts to work at 2100, but I added 256 bytes in advance).

Related

Testing

Verified local test vbus_monitor (all scenarios) on:

  • ESP32-S3, USB OTG 1.1 (Limitation: No stable suspend event on test 3 before device detachment)
  • ESP32-P4 ECO4, USB OTG 1.1
  • ESP32-P4 ECO4, USB OTG 2.0 (Limitation: Several parasite SUSPEND events on test 7)
  • ESP32-P4 ECO5, USB OTG 1.1
  • ESP32-P4 ECO5, USB OTG 2.0 (Limitation: One parasite SUSPEND event on any test before device attach IEC-412)

Additional testing

  • [ECO5] Local run for esp-idf example esp-idf/examples/peripherals/usb/device/tusb_msc with self_powered flag (device attached/detached by the power button on the external hub, connected to the Laptop)
image
  • [ECO4] Local run for esp-idf example esp-idf/examples/peripherals/usb/device/tusb_msc with self_powered flag (device attached/detached by the power button on the external hub, connected to the Laptop)

Checklist

Before submitting a Pull Request, please ensure the following:

  • 🚨 This PR does not introduce breaking changes.
  • All CI checks (GH Actions) pass.
  • Documentation is updated as needed.
  • Tests are updated or added as necessary.
  • Code is well-commented, especially in complex areas.
  • Git history is clean — commits are squashed to the minimum necessary.

@roma-jam roma-jam self-assigned this Nov 10, 2025
@roma-jam roma-jam added the Component: usb_device Issue affects usb_device component label Nov 10, 2025
@roma-jam roma-jam added this to the esp_tinyusb v2.0.2 milestone Nov 10, 2025
@roma-jam roma-jam force-pushed the fix/vbus_monitor_workaround_esp32p4_v4.30a branch from a60a09a to 82702bb Compare November 10, 2025 12:22
@roma-jam roma-jam requested a review from Copilot November 10, 2025 12:22
@roma-jam roma-jam marked this pull request as ready for review November 10, 2025 12:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a workaround for VBUS monitoring on ESP32-P4 ECO5 (USB OTG 2.0 v4.30a), where hardware-based BValid override is not available. The solution introduces conditional compilation to handle different chip revisions and uses direct application layer notification as a fallback mechanism.

Key Changes:

  • Added conditional support for BValid override based on ESP32-P4 chip revision
  • Implemented workaround using tinyusb_device_detached() for newer revisions lacking hardware support
  • Refactored TinyUSB callbacks to use new attach/detach notification functions

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
device/esp_tinyusb/tinyusb_vbus_monitor.c Added conditional compilation for BValid override support and workaround for ECO5
device/esp_tinyusb/tinyusb.c Refactored mount/unmount callbacks to use new attach/detach notification functions
device/esp_tinyusb/test_apps/vbus_monitor/main/test_vbus_monitor.c Updated test to handle suspend events during attach/detach sequences
device/esp_tinyusb/include_private/tinyusb_vbus_monitor.h Updated documentation for init function error codes
device/esp_tinyusb/include_private/tinyusb_device.h New header declaring device detach notification function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@roma-jam roma-jam force-pushed the fix/vbus_monitor_workaround_esp32p4_v4.30a branch from 82702bb to 88e2689 Compare November 10, 2025 12:33
@roma-jam roma-jam requested a review from Copilot November 10, 2025 12:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@roma-jam roma-jam force-pushed the fix/vbus_monitor_workaround_esp32p4_v4.30a branch from 88e2689 to f156c98 Compare November 10, 2025 12:38
@roma-jam roma-jam changed the title fix(esp_tinyusb): VBUS monitoring feature on ESP32-P4, USB OTG 2.0 v4.30a [WIP] fix(esp_tinyusb): VBUS monitoring feature on ESP32-P4, USB OTG 2.0 v4.30a Nov 10, 2025
@roma-jam roma-jam force-pushed the fix/vbus_monitor_workaround_esp32p4_v4.30a branch from f156c98 to 15bec5d Compare November 10, 2025 14:04
@roma-jam roma-jam requested a review from Copilot November 10, 2025 14:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@roma-jam roma-jam force-pushed the fix/vbus_monitor_workaround_esp32p4_v4.30a branch from 15bec5d to ee8e16a Compare November 10, 2025 14:14
@roma-jam roma-jam changed the title fix(esp_tinyusb): VBUS monitoring feature on ESP32-P4, USB OTG 2.0 v4.30a fix(esp_tinyusb): VBUS monitoring feature on ESP32-P4, USB OTG 2.0 v4.30a [WIP] Nov 11, 2025
@roma-jam roma-jam marked this pull request as draft November 11, 2025 12:28
…lated verification of vbus monitor logic

- Added handling for tud_suspend_cb() to verify the sequence of callbacks during attach/detach
- Updated test case of emulated vbus handling for ESP32-P4 (ECO5) USB OTG 2.0, v4.30a (by adding emulated DETACH event and driving DCTL register)
@roma-jam roma-jam force-pushed the fix/vbus_monitor_workaround_esp32p4_v4.30a branch from 8750b74 to 11d6c03 Compare November 11, 2025 12:59
@roma-jam roma-jam requested a review from Copilot November 11, 2025 13:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…v4.30a

- Added the callback to vbus_monitor
- Applied workaround fix to replace the UNPLUG event with direct notification for USB OTG 2.0 v4.30a
@roma-jam roma-jam force-pushed the fix/vbus_monitor_workaround_esp32p4_v4.30a branch from 11d6c03 to 4988969 Compare November 11, 2025 13:16
@roma-jam roma-jam changed the title fix(esp_tinyusb): VBUS monitoring feature on ESP32-P4, USB OTG 2.0 v4.30a [WIP] fix(esp_tinyusb): VBUS monitoring feature on ESP32-P4, USB OTG 2.0 v4.30a Nov 11, 2025
@roma-jam roma-jam marked this pull request as ready for review November 11, 2025 13:16
@roma-jam roma-jam requested a review from igi540 November 11, 2025 20:48
@roma-jam
Copy link
Collaborator Author

Hi
@tore-espressif
@peter-marcisovsky
@igi540

this one is ready, PTAL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: usb_device Issue affects usb_device component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants