Skip to content

feat(usb_host): esp sleep modes verification#445

Open
peter-marcisovsky wants to merge 2 commits intomasterfrom
feat/usb_host_light_sleep
Open

feat(usb_host): esp sleep modes verification#445
peter-marcisovsky wants to merge 2 commits intomasterfrom
feat/usb_host_light_sleep

Conversation

@peter-marcisovsky
Copy link
Copy Markdown
Collaborator

@peter-marcisovsky peter-marcisovsky commented Mar 20, 2026

Description

Verification of using light sleep and deep sleep in combination with USB Host application.

Light sleep

  • Using CONFIG_ESP_SLEEP_EVENT_CALLBACKS
  • Register enter and exit light sleep events
  • Calling root port suspend/resume (not public API) upon enter/exit light sleep to effectively suspend the peripheral and the connected device
  • Connected device enters suspended state during the light sleep
  • Connected device must not undergo a reset during light sleep

Light sleep log, with light sleep callbacks enabeld

Root port enters suspended mode before entering light sleep

Screenshot from 2026-03-27 12-31-41

Light sleep log, with light sleep callbacks disabled

Missing suspend/resume sequence from host. Host just stops sending SOFs without any "reason". This pattern only appears on HS port. The device does not enter suspended state

Screenshot from 2026-04-01 15-42-00

Enter light sleep latency

Added light sleep latency when entering and exiting light sleep with automatic suspend/resume of the peripheral

esp32s2 esp32s3 esp32p4 esp32h4
enter light sleep ~135 uS ~80 uS ~20 uS ~110 uS
exit light sleep ~65 uS ~50 uS ~10 uS ~35 uS

Deep sleep

  • Connected device shall be disconnected upon entering deep sleep and connected back after exiting deep sleep
  • RAM is unpowered during deep sleep and any information about the connected device (cached descriptors, device state.. ) is lost during deep sleep
  • Currently, HNP cap is enabled on all HNP capable targets by default, to prevent current leakage in deep sleep.

Testing

  • usb_sleep_modes target tests
    • light sleep functionality verification
    • deep sleep functionality verification
    • enter/exit light sleep latency
    • error handling inside the enter/exit light sleep callbacks

Limitations

  • Light sleep: a device must be present in order to call root port suspend in a light sleep callback
    • Known "issue" from the original suspend/resume implementation before HCLK clock gating was introduced
    • Will be fixed in a separate PR, which will remove the device presence obligation when entering light sleep

Related


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

Medium Risk
Adds sleep event callbacks that suspend/resume the USB root port from light-sleep critical sections and introduces new synchronization/error-handling paths, which could affect USB stability and wake latency on supported targets.

Overview
Adds optional automatic USB root-port suspend/resume around light sleep (guarded by CONFIG_ESP_SLEEP_EVENT_CALLBACKS) by registering sleep event callbacks in usb_host_install()/usb_host_uninstall() and coordinating async suspend completion via a new semaphore.

Extends the hub driver with a HUB_EVENT_SUSPEND_COMPLETED event and a hub_root_mark_light_sleep_auto_resume() flag to safely handle the case where a device disconnects during light sleep (HCD port in RECOVERY) before attempting resume.

Introduces a new usb_sleep_modes target test app (Unity + pytest) that exercises USB Host behavior across repeated light-sleep cycles and multi-stage deep-sleep resets, including an error-path test for sleep callback failures.

Reviewed by Cursor Bugbot for commit 7a8ee7a. Bugbot is set up for automated code reviews on this repo. Configure here.

@peter-marcisovsky peter-marcisovsky self-assigned this Mar 20, 2026
@peter-marcisovsky peter-marcisovsky added Status: In Progress Issue is being worked on Component: usb_host Issue affects usb_host component labels Mar 20, 2026
@peter-marcisovsky peter-marcisovsky force-pushed the feat/usb_host_light_sleep branch from aca937d to 4ad8b7d Compare March 25, 2026 14:10
@peter-marcisovsky peter-marcisovsky changed the title feat(usb_host): light sleep verification feat(usb_host): esp sleep modes verification Mar 26, 2026
@peter-marcisovsky peter-marcisovsky force-pushed the feat/usb_host_light_sleep branch 3 times, most recently from 5bcdbe4 to 1bce9cc Compare April 14, 2026 11:48
@peter-marcisovsky peter-marcisovsky force-pushed the feat/usb_host_light_sleep branch from 1bce9cc to e54f2ba Compare April 14, 2026 11:56
@peter-marcisovsky peter-marcisovsky force-pushed the feat/usb_host_light_sleep branch from e54f2ba to 2bc1e5f Compare April 14, 2026 13:09
@peter-marcisovsky peter-marcisovsky force-pushed the feat/usb_host_light_sleep branch from 2bc1e5f to 01d0528 Compare April 14, 2026 13:14
@peter-marcisovsky peter-marcisovsky force-pushed the feat/usb_host_light_sleep branch from 5b0dae3 to 8b5e16d Compare April 16, 2026 11:53
@tore-espressif tore-espressif self-requested a review April 16, 2026 14:13
@peter-marcisovsky peter-marcisovsky force-pushed the feat/usb_host_light_sleep branch from 70e0763 to 7a8ee7a Compare April 17, 2026 09:16
@peter-marcisovsky peter-marcisovsky marked this pull request as ready for review April 17, 2026 09:24
@peter-marcisovsky peter-marcisovsky added Status: Reviewing Issue is being reviewed and removed Status: In Progress Issue is being worked on labels Apr 17, 2026
Copy link
Copy Markdown

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 7a8ee7a. Configure here.

Comment thread host/usb/src/hub.c
Comment thread host/usb/test/target_test/usb_sleep_modes/main/test_usb_host_esp_sleep.c Outdated
@peter-marcisovsky peter-marcisovsky marked this pull request as draft April 17, 2026 11:56
@peter-marcisovsky peter-marcisovsky marked this pull request as draft April 17, 2026 11:56
@peter-marcisovsky
Copy link
Copy Markdown
Collaborator Author

Converted to draft, trying to lower the sleep latency even more.

    - using esp light sleep and esp deep sleep modes with USB Host application
    - verify USB Host functionality in both sleep modes
    - light sleep: added pm callbacks to automatically suspend and resume USB Host driver
    - Don't do the full suspend/resume when suspending/resuming from light sleep CBs
    - Added latency measurement test
@peter-marcisovsky peter-marcisovsky force-pushed the feat/usb_host_light_sleep branch from 66a9e6c to 519d3a4 Compare April 24, 2026 10:27
@peter-marcisovsky peter-marcisovsky marked this pull request as ready for review April 27, 2026 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: usb_host Issue affects usb_host component Status: Reviewing Issue is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant