Skip to content

Conversation

@roma-jam
Copy link
Contributor

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

Description

This is the third and the final part of the plan: Make the TinyUSB task deletion notification driven

Implement the mechanism:

  • User task sends a stop notification to TinyUSB task via xTaskNotify.
  • TinyUSB task acknowledges start/stop completion via the start/stop semaphore so the user task knows when cleanup is safe.
  • Use tud_task_ext(timeout_ms, false) with the configured timeout.
  • Replace the existing stop/deletion logic with this notification-driven flow, keeping behavior identical when timeout_ms == 0 (legacy way of task deletion).

Additional tasks

  • Solved _task_is_running bug (feature(tinyusb): Software VBUS monitoring feature on ESP32P4 (part1/2) (IEC-437) #305 (review))
  • Compared CPU load in both cases (Both Run Time and CPU load in default mode (1s blocking) and legacy mode (indefinite blocking) are the same)
  • Update the result table with the actual results based on the last changes
  • Update CHANGELOG.md and README
  • Profile the precise percentage calculation to find the calculation error (the total sum of percentage should be exactly 100%, find and eliminate the problem)
  • Re-measure the results for CPU Load with precise percentage

CPU Load value has been excluded from this PR as it is re-calculated Run Time value.
The TinyUSB task CPU load is measured via the Run Time value instead.

The CPU Load calculation add on is in follow-up PR: [TBC]

Test description

CPU load test case was used as a measurement tool.

As the results vary from the launch, the test was launched three times in a row.
Test steps:

  1. Re-power the test board
  2. Perform launch1, get result1
  3. Perform launch2, get result2
  4. Perform launch3, get result3

After each launch, the result was placed in the table: result1 / result2 / result 3, for example 461 / 458 / 462

CPU Load all the time less than 1%, because tasks IDLE0 and IDLE1 use most of the CPU:

Name                   Run time CPU load
TinyUSB                    1667   0.09%
main                       8492   0.50%
IDLE1                    846330  50.06%
IDLE0                    844577  49.96%
ipc1                          0   0.00%
ipc0                          0   0.00%

Run Time and CPU Load of TinyUSB task for ESP32-S3

Case Run Time
non-blocking ( timeout_ms = 0) 2276 / 2449 / 2417
default blocking (timeout_ms = 2000) 2122 / 1726 / 1769
blocking indefinitely (legacy mode, timeout_ms = UINT32_MAX) 1702 / 1700 / 1738

Run Time and CPU Load of TinyUSB task for ESP32-P4 (ECO5)

Case Run Time (ticks)
non-blocking ( timeout_ms = 0) 1390 / 893 / 848
default blocking (timeout_ms = 1000) 967 / 469 / 458
blocking indefinitely (legacy mode, timeout_ms = UINT32_MAX) 968 / 473 / 460

Notes

  • On ESP32-S2 the non-blocking Run Time is ~ 4200.
  • On ESP32-S3 the non-blocking Run Time is ~ 1700.

Default timeout values may differ per CPU/target to match CPU load observed in legacy (block-forever) mode versus non-blocking mode. To minimize the difference with legacy mode the default timeout was selected based on the Run Time value according to the table above.

Related

Testing


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 Sep 30, 2025
@roma-jam roma-jam added the Component: usb_device Issue affects usb_device component label Sep 30, 2025
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from 7c7d2a9 to 399a3e4 Compare September 30, 2025 10:42
@roma-jam roma-jam changed the title refactor(tinyusb): Changed the tinyusb task deletion to notification [WIP] refactor(tinyusb): Made the tinyusb task deletion notification driven [WIP] Sep 30, 2025
@roma-jam roma-jam added this to the esp_tinyusb v2.0.2 milestone Oct 2, 2025
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from b0d1b6b to 4636e24 Compare November 7, 2025 13:19
@roma-jam roma-jam changed the title refactor(tinyusb): Made the tinyusb task deletion notification driven [WIP] feature(esp_tinyusb): TinyUSB task deletion notification driven (part 2/3) [WIP] Nov 14, 2025
@roma-jam roma-jam changed the title feature(esp_tinyusb): TinyUSB task deletion notification driven (part 2/3) [WIP] feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) [WIP] Nov 14, 2025
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from 4636e24 to 88051eb Compare November 17, 2025 21:49
@roma-jam roma-jam changed the base branch from master to feature/tinyusb_task_deletion_part2 November 17, 2025 21:50
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from 88051eb to d2250d4 Compare November 18, 2025 12:28
@roma-jam roma-jam force-pushed the feature/tinyusb_task_deletion_part2 branch 2 times, most recently from 31fd0b0 to d14fb88 Compare November 18, 2025 13:14
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from d2250d4 to 4fdd710 Compare November 18, 2025 13:48
@roma-jam roma-jam force-pushed the feature/tinyusb_task_deletion_part2 branch from d14fb88 to 1619f26 Compare November 18, 2025 13:55
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch 2 times, most recently from fa9ddc5 to 8cf8bf8 Compare November 18, 2025 14:34
@roma-jam roma-jam force-pushed the feature/tinyusb_task_deletion_part2 branch from 1619f26 to 019c18f Compare November 18, 2025 15:31
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from de9b405 to 2edb940 Compare November 18, 2025 15:40
@roma-jam roma-jam force-pushed the feature/tinyusb_task_deletion_part2 branch from 019c18f to fd54244 Compare November 20, 2025 12:51
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from 2edb940 to 4c78584 Compare November 20, 2025 13:08
@roma-jam
Copy link
Contributor Author

I moved out the CPU Load improvements into another PR to unblock this one.

As the CPU Load value it re-calculated Run Time, we can make a decision based on the Run Time value.

CPU Load in percentage is nice to have feature and more user-friendly, but it is not important ATM.

@roma-jam roma-jam changed the title feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) [WIP] feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) Nov 20, 2025
@roma-jam roma-jam marked this pull request as ready for review November 20, 2025 13:10
@roma-jam roma-jam force-pushed the feature/tinyusb_task_deletion_part2 branch from fd54244 to adc2129 Compare November 20, 2025 13:33
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from 4c78584 to 6dc6c0d Compare November 20, 2025 13:34
@roma-jam roma-jam force-pushed the feature/tinyusb_task_deletion_part2 branch from adc2129 to 73b4ab0 Compare November 20, 2025 17:02
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from 6dc6c0d to e78d3b8 Compare November 21, 2025 12:55
@roma-jam roma-jam force-pushed the feature/tinyusb_task_deletion_part2 branch 4 times, most recently from eb0fbec to 6dca07a Compare November 24, 2025 14:21
@roma-jam roma-jam changed the title feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) [WIP] Nov 24, 2025
@roma-jam roma-jam marked this pull request as draft November 24, 2025 16:26
@roma-jam roma-jam force-pushed the feature/tinyusb_task_deletion_part2 branch 2 times, most recently from eae16ec to e3b601d Compare November 24, 2025 20:25
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch 2 times, most recently from b6b9dc5 to 4d3bb52 Compare November 25, 2025 11:16
@roma-jam roma-jam changed the title feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) [WIP] feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) Nov 25, 2025
@roma-jam roma-jam marked this pull request as ready for review November 25, 2025 11:20
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch 2 times, most recently from 370a8e2 to c33acc5 Compare November 27, 2025 09:18
} else {
uint32_t response_len = ctrl_xfer->actual_num_bytes - USB_SETUP_PACKET_SIZE;
if (response_len <= req->wLength) {
memcpy(req->data,

Check warning

Code scanning / clang-tidy

Call to function 'memcpy' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memcpy_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling] Warning

Call to function 'memcpy' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memcpy_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling]
Base automatically changed from feature/tinyusb_task_deletion_part2 to master November 27, 2025 11:11
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from c33acc5 to 01f9d68 Compare November 27, 2025 11:32
- Fixed the _task_pending status change on error during tinyusb_task_start()
- Kept the legace way of deleting task when blocking time is UINT32_MAX (task deleted in the user context)
- Added blocking_timeout_ms parameter to the driver config, tinyusb task parameters
@roma-jam roma-jam force-pushed the refactor/tinyusb_task_deletion branch from 01f9d68 to 15a5ed5 Compare November 27, 2025 11:40
@espressif-bot espressif-bot added the Status: Opened Issue is new label Nov 28, 2025
@github-actions github-actions bot changed the title feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) feature(esp_tinyusb): TinyUSB task deletion notification driven (part 3/3) (IEC-441) 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.

3 participants