Limit events processed by tud_task_ext() / tuh_task_ext()#3478
Merged
Conversation
Contributor
Author
|
Please wait, I'm retesting everything with the missing breaks... |
MemBrowse Memory Reportat32f402_405-at_start_f402-cdc_msc
at32f403a_407-at32f403a_weact_blackpill-cdc_msc
at32f413-at_start_f413-cdc_msc
at32f423-at_start_f423-cdc_msc
at32f425-at_start_f425-cdc_msc
ch32v10x-ch32v103r_r1_1v0-cdc_msc
ch32v20x-ch32v203c_r0_1v0-cdc_msc
da1469x-da14695_dk_usb-cdc_msc
hpmicro-hpm6750evk2-cdc_msc
kinetis_k-frdm_k64f-cdc_msc
kinetis_k32l2-frdm_k32l2a4s-cdc_msc
kinetis_kl-frdm_kl25z-cdc_msc
lpc11-lpcxpresso11u37-cdc_msc
lpc13-lpcxpresso1347-cdc_msc
lpc15-lpcxpresso1549-cdc_msc
lpc17-lpcxpresso1769-cdc_msc
lpc40-ea4088_quickstart-cdc_msc
lpc43-ea4357-cdc_msc
maxim-apard32690-cdc_msc
mm32-mm32f327x_mb39-cdc_msc
msp430-msp_exp430f5529lp-cdc_msc
nuc121_125-nutiny_sdk_nuc121-cdc_msc
nuc126-nutiny_nuc126v-cdc_msc
ra-portenta_c33-cdc_msc
samd11-cynthion_d11-cdc_dual_ports
stm32c0-stm32c071nucleo-cdc_msc
stm32f1-stm32f103_bluepill-cdc_msc
stm32f2-stm32f207nucleo-cdc_msc
stm32f3-stm32f303disco-cdc_msc
stm32l4-stm32l412nucleo-cdc_msc
No memory changes detected for: |
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request adds configuration options to limit the number of events processed in a single call to tud_task_ext() and tuh_task_ext(), preventing these functions from blocking for arbitrarily long periods when USB traffic is high or the CPU is slow.
Changes:
- Added
CFG_TUD_TASK_EVENTS_PER_RUNandCFG_TUH_TASK_EVENTS_PER_RUNconfiguration options with a default value of 16 - Modified the event processing loops in both device and host stacks to check the event limit and exit early when reached
- Added debug logging when the event limit is reached
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/tusb_option.h | Added two new configuration macros for limiting events per task run (default: 16, 0 for unlimited) |
| src/host/usbh.c | Converted infinite while loop to bounded for loop with conditional event limit check |
| src/device/usbd.c | Converted infinite while loop to bounded for loop with conditional event limit check |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the PR
Add CFG_TUD_TASK_EVENTS_PER_RUN and CFG_TUH_TASK_EVENTS_PER_RUN options to limit the number of events processed in a single call to tud_task_ext() or tuh_task_ext().
This is a simplified replacement for pull request #3432 after hathach last comment.
Additional context
An application main loop (without os) with tinyusb host and device stack look like:
Unfortunately, without patching, tud_task() and tuh_task() can take arbitrarily long, if the CPU is slow and USB traffic fast.
the other day I got tud_task() running many seconds mounting a FATFS partition using a msc device on slow full speed USB.
Infinite loop can also happen in weird situations (bugs or stuff like usbh.c line 626).
This PR solves all of these by adding two new simple configuration options:
Once this limit is reach, tud_task() or tuh_task() exit, ensuring the other stack and application_code run quickly. The leftover events will be processed on the next call.
The default value of 16 is high enough that it should not be reached in healthy situations. A value of 0 disable the feature and go back to infinite number of events processed by call.