Skip to content

Conversation

@roma-jam
Copy link
Collaborator

@roma-jam roma-jam commented Oct 30, 2025

Description

VBUS monitoring on ESP32-P4 - Part 1/2

  • refactored test app dconn_detection -> vbus_monitor, enabled for esp32s2
  • pinned the TinyUSB version to v0.18 (version: '^0.18.0')
  • added vbus_monitor API source/header + tinyusb config struct (vbus_debounce_ms, default 250).
  • updated CHANGELOG/README: how to set the vbus monitor feature on esp32p4; usage snippet; expected events.

Related

Testing

ESP32S2/S3

(1)	"Emulated VBUS, verify attach/detach events callback (via Bvalid signal)" [ci][dconn]
(2)	"Controlled VBUS, verify attach/detach events callback" [dconn]
(3)	"Real VBUS, verify attach/detach events callback (requires manual handling)" [dconn]

ESP32P4

(1)	"Emulated VBUS USB OTG 1.1, verify attach/detach events callback (via Bvalid signal)" [dconn]
(2)	"Emulated VBUS USB OTG 1.1, verify attach/detach events callback (via GOTGCTL register)" [dconn]
(3)	"Emulated VBUS USB OTG 2.0, verify attach/detach events callback (via GOTGCTL register)" [ci][dconn][ignore]
(4)	"Controlled VBUS USB OTG 1.1, verify attach/detach events callback" [dconn]
(5)	"Controlled VBUS USB OTG 2.0, verify attach/detach events callback" [dconn]
(6)	"Real VBUS USB OTG 1.1, verify attach/detach events callback (requires manual handling)" [dconn]
(7)	"Real VBUS USB OTG 2.0, verify attach/detach events callback (requires manual handling)" [dconn]

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.

Note

Introduces software VBUS monitoring for ESP32P4 USB OTG 2.0 (HS) with debounce config, integrates it into the driver/task, updates docs, pins TinyUSB to 0.18, and replaces dconn tests with a new vbus monitor test app.

  • Core (esp_tinyusb)
    • Add software VBUS monitor: tinyusb_vbus_monitor.c/.h and integrate init/deinit in tinyusb_task.c and tinyusb.c (pass tinyusb_vbus_monitor_config_t).
    • Extend tinyusb_phy_config_t with vbus_monitor_debounce_ms; set defaults in tinyusb_default_config.h.
    • Validate self-powered config and debounce for ESP32P4 HS; update tinyusb_task_start signature to accept VBUS monitor cfg.
    • CMake: include tinyusb_vbus_monitor.c only for esp32p4.
  • Docs
    • README: usage for ESP32-P4 VBUS monitoring, debounce, and ISR notes; CHANGELOG entry for new feature.
  • Dependencies
    • Pin tinyusb to ^0.18.0 in idf_component.yml.
  • Tests
    • Replace dconn_detection with vbus_monitor test app: new cases for emulated/controlled/real VBUS across ports/targets, helper setup/teardown, pytest runner, and config (enable CDC).

Written by Cursor Bugbot for commit d298a66. This will update automatically on new commits. Configure here.

@roma-jam roma-jam added this to the esp_tinyusb v2.0.2 milestone Oct 30, 2025
@roma-jam roma-jam self-assigned this Oct 30, 2025
@roma-jam roma-jam added the Component: usb_device Issue affects usb_device component label Oct 30, 2025
@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from c8dac7f to ed53b4e Compare October 30, 2025 04:00
@roma-jam roma-jam changed the base branch from master to fix/runtime_config_test_app_for_idf_v6.1.0 October 30, 2025 04:01
@roma-jam roma-jam force-pushed the fix/runtime_config_test_app_for_idf_v6.1.0 branch 2 times, most recently from a2cf813 to 649edbe Compare October 30, 2025 04:26
@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from ed53b4e to 888ae35 Compare October 30, 2025 06:31
@roma-jam roma-jam changed the base branch from fix/runtime_config_test_app_for_idf_v6.1.0 to master October 30, 2025 06:35
@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from 888ae35 to 03caa71 Compare October 30, 2025 07:02
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 introduces the first part of a software VBUS monitoring feature for ESP32P4, specifically targeting USB OTG 2.0 high-speed functionality. It addresses the limitation where ESP32P4's USB OTG 2.0 doesn't have hardware detection of VBUS BVALID signal by implementing a software-based solution with configurable debouncing.

  • Added VBUS monitor API infrastructure with debounce configuration
  • Refactored test application from dconn_detection to vbus_monitor with comprehensive test coverage
  • Updated documentation and configuration to support the new VBUS monitoring feature

Reviewed Changes

Copilot reviewed 19 out of 21 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tinyusb_vbus_monitor.c New VBUS monitoring implementation (currently stub returning ESP_ERR_NOT_SUPPORTED)
tinyusb_task.c Integration of VBUS monitor initialization and cleanup into TinyUSB task lifecycle
tinyusb.c Added VBUS monitor configuration validation and setup for different USB OTG versions
test_apps/vbus_monitor/* Comprehensive test suite replacing dconn_detection with extensive VBUS monitoring tests
include files Added VBUS monitor configuration structures and API declarations
README.md Documentation updates explaining VBUS monitoring setup and ESP32P4 limitations

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

@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from f439723 to fee7783 Compare November 2, 2025 14:50
@roma-jam roma-jam requested a review from Copilot November 2, 2025 14:58
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 19 out of 21 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 feature/vbus_monitor_esp32p4_part1 branch from fee7783 to 33acda8 Compare November 2, 2025 15:16
@roma-jam roma-jam requested a review from Copilot November 2, 2025 15:16
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 19 out of 21 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

device/esp_tinyusb/tinyusb.c:1

  • Line 106 has incorrect condition. It should check config->phy.vbus_monitor_io >= GPIO_NUM_0 && config->phy.vbus_monitor_io < GPIO_NUM_MAX instead of just config->phy.self_powered.
/*

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

@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch 4 times, most recently from f573418 to a8fbded Compare November 4, 2025 12:08
@roma-jam roma-jam changed the title feature(tinyusb): Software VBUS monitoring feature on ESP32P4 (part1/2) [WIP] feature(tinyusb): Software VBUS monitoring feature on ESP32P4 (part1/2) Nov 4, 2025
@roma-jam roma-jam marked this pull request as ready for review November 4, 2025 14:58
@roma-jam roma-jam changed the title feature(tinyusb): Software VBUS monitoring feature on ESP32P4 (part1/2) feature(tinyusb): Software VBUS monitoring feature on ESP32P4 (part1/2) [WIP] Nov 4, 2025
@roma-jam roma-jam marked this pull request as draft November 4, 2025 14:59
@roma-jam roma-jam requested a review from Copilot November 4, 2025 14:59
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 19 out of 21 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

device/esp_tinyusb/tinyusb.c:1

  • The condition logic is incorrect. It should check config->phy.vbus_monitor_io >= GPIO_NUM_0 && config->phy.vbus_monitor_io < GPIO_NUM_MAX instead of config->phy.self_powered.
/*

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

…L override driving for VBUS monitoring

- Added test cases for USB OTG 1.1 (esp32s2, esp32s3, esp32p4)
- Added test cases for USB OTG 2.0 (esp32p4)
- Enabled CI tests for esp32s2
@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from a8fbded to 652c0fb Compare November 4, 2025 15:09
@roma-jam roma-jam changed the title feature(tinyusb): Software VBUS monitoring feature on ESP32P4 (part1/2) [WIP] feature(tinyusb): Software VBUS monitoring feature on ESP32P4 (part1/2) Nov 4, 2025
@roma-jam roma-jam marked this pull request as ready for review November 4, 2025 15:10
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from 652c0fb to 057986d Compare November 5, 2025 10:30
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: Memory leak and state corruption in task start error path

Memory leak and state corruption in error handling path of tinyusb_task_start(). If task creation succeeds but the task fails to notify within the timeout (line 177-180), the error path at line 180-182 frees task_ctx but does not reset the _task_is_running flag. This has two problems:

  1. The _task_is_running flag remains true, preventing any future calls to tinyusb_task_start() from succeeding (they will fail with ESP_ERR_INVALID_STATE at line 138).

  2. If the task eventually starts and successfully calls xTaskNotifyGive() after the timeout, it will continue running with a freed task_ctx pointer that was already freed at line 181. While the current implementation doesn't access task_ctx after notification, this creates a dangerous dangling pointer situation.

The error path should reset _task_is_running to false within a critical section before freeing task_ctx.

device/esp_tinyusb/tinyusb_task.c#L179-L182

ESP_LOGE(TAG, "Task wasn't able to start TinyUSB stack");
ret = ESP_ERR_TIMEOUT;
goto err;
}

Fix in Cursor Fix in Web


@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from 057986d to d311914 Compare November 5, 2025 12:26
@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from d311914 to 623809e Compare November 5, 2025 12:31
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: Dangling _task_is_running Flag on Error Path

Inconsistent state on error: When the error path at label err: is reached (either from task creation failure at line 169 or timeout at line 180), the function frees task_ctx and returns, but does not reset the _task_is_running flag to false. This flag was set to true at line 140, but will remain set even though the task was never successfully started. This prevents subsequent calls to tinyusb_task_start from succeeding. The fix should reset _task_is_running to false within a critical section before freeing memory and returning.

device/esp_tinyusb/tinyusb_task.c#L185-L188

err:
heap_caps_free(task_ctx);
return ret;

Fix in Cursor Fix in Web


@roma-jam roma-jam force-pushed the feature/vbus_monitor_esp32p4_part1 branch from 623809e to d298a66 Compare November 6, 2025 12:44
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: Task Running Flag Not Reset on Error Path

The error path at label err: does not reset _task_is_running flag before returning. This error path is reached when task creation fails (line 176) or when the notification times out (line 183). Since _task_is_running was set to true at line 140, it must be cleared in the error path to prevent the system from being left in an inconsistent state where subsequent calls to tinyusb_task_start() will fail at the check on line 139.

device/esp_tinyusb/tinyusb_task.c#L185-L188

err:
heap_caps_free(task_ctx);
return ret;

Fix in Cursor Fix in Web


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