Skip to content

Conversation

@roma-jam
Copy link
Contributor

@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
@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.

@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
Copy link
Contributor Author

Hi
@tore-espressif
@peter-marcisovsky
@igi540

this one is ready, PTAL.

Copy link
Collaborator

@tore-espressif tore-espressif left a comment

Choose a reason for hiding this comment

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

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).

This is currently blocking limitation. If our implementation creates stack overflow in any system task, we should defer the handling to our tinyusb task.

Is it possible to signal the unplug event with dcd_event_bus_signal(port, DCD_EVENT_UNPLUGGED, false);? If yes, the DCD layer would propagate all callback to our tinyusb task

@roma-jam
Copy link
Contributor Author

Hi @tore-espressif,

this can be a blocking limitation for the release, but it is not blocking limitation for this PR.
This PR enables the mechanism of VBUS monitoring feature on the ECO5.

Resolve the limitation we can easily by moving the vbus monitor callback, added in this PR, to the dedicated task context.

This will take couple of lines and break the limitation easily. But for this, we need to move from the blocking tud_task() to non-blocking tud_task_ext().

Alternatives already were considered, such as:

  • Dedicated task for VBUS monitor feature only (rejected). As we don't need a separate task for such simple feature.
  • Using esp_timer instead of FreeRTOS timer as it has it's own dedicated task for the timer callback. (already discussed, rejected). This adds the additional task and also a dependency from esp_timer component.

As the feature with non-blocking tusb_task_ext() is still not reviewed, there is no other way right now to enable the VBUS monitoring for ECO5 in any other way.

So we can enable the feature with limitation to have it working on ECO5 and then (after moving to non-blocking tusb_task_ext()) remove this limitation with the simple follow-up PR.

…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)
…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 feature/vbus_monitor_esp32p4_part2 branch from 6b9d6ea to b79f873 Compare November 26, 2025 17:57
@roma-jam roma-jam force-pushed the fix/vbus_monitor_workaround_esp32p4_v4.30a branch from 4988969 to f4be682 Compare November 26, 2025 17:57
@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 26, 2025
@espressif-bot espressif-bot added the Status: Opened Issue is new label Nov 28, 2025
@github-actions github-actions bot 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 [WIP] (IEC-435) Nov 28, 2025
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 Status: Opened Issue is new

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants