Skip to content

Conversation

@ceedriic
Copy link
Contributor

@ceedriic ceedriic commented Feb 1, 2026

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:

      while(1) { // the mainloop
        application_code();
        tud_task(); // tinyusb device task
        tuh_task(); // tinyusb host task
      }

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:

#define CFG_TUD_TASK_EVENTS_PER_RUN  16
#define CFG_TUH_TASK_EVENTS_PER_RUN  16

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.

@ceedriic
Copy link
Contributor Author

ceedriic commented Feb 1, 2026

Please wait, I'm retesting everything with the missing breaks...

@github-actions
Copy link

github-actions bot commented Feb 2, 2026

MemBrowse Memory Report

at32f402_405-at_start_f402-cdc_msc

  • FLASH: .text +4 B (+0.0%, 18,200 B / 262,144 B, total: 7% used)

at32f403a_407-at32f403a_weact_blackpill-cdc_msc

  • FLASH: .text +4 B (+0.0%, 18,492 B / 262,144 B, total: 7% used)

at32f413-at_start_f413-cdc_msc

  • FLASH: .text +4 B (+0.0%, 18,464 B / 262,144 B, total: 7% used)

at32f423-at_start_f423-cdc_msc

  • FLASH: .text +4 B (+0.0%, 20,024 B / 262,144 B, total: 8% used)

at32f425-at_start_f425-cdc_msc

  • FLASH: .text +4 B (+0.0%, 27,672 B / 65,536 B, total: 42% used)

ch32v10x-ch32v103r_r1_1v0-cdc_msc

  • FLASH: .text +4 B (+0.0%, 14,604 B / 65,536 B, total: 22% used)

ch32v20x-ch32v203c_r0_1v0-cdc_msc

  • FLASH: .text +8 B (+0.0%, 16,844 B / 65,536 B, total: 26% used)

da1469x-da14695_dk_usb-cdc_msc

  • FLASH: .text +8 B (+0.1%, 14,324 B / 1,048,576 B, total: 1% used)

hpmicro-hpm6750evk2-cdc_msc

  • FLASH: .text +8 B (+0.0%, 29,464 B / 16,777,216 B, total: 0% used)

kinetis_k-frdm_k64f-cdc_msc

  • m_text: .text +8 B (+0.1%, 15,156 B / 1,047,536 B, total: 1% used)

kinetis_k32l2-frdm_k32l2a4s-cdc_msc

  • m_text: .text +16 B (+0.1%, 15,008 B / 523,248 B, total: 3% used)

kinetis_kl-frdm_kl25z-cdc_msc

  • m_text: .text +16 B (+0.1%, 22,260 B / 130,032 B, total: 17% used)

lpc11-lpcxpresso11u37-cdc_msc

  • MFlash128: .text +16 B (+0.1%, 20,448 B / 131,072 B, total: 16% used)

lpc13-lpcxpresso1347-cdc_msc

  • MFlash64: .text +4 B (+0.0%, 20,404 B / 65,536 B, total: 31% used)

lpc15-lpcxpresso1549-cdc_msc

  • MFlash256: .text +4 B (+0.0%, 21,824 B / 262,144 B, total: 8% used)

lpc17-lpcxpresso1769-cdc_msc

  • MFlash512: .text +8 B (+0.1%, 12,524 B / 524,288 B, total: 2% used)

lpc40-ea4088_quickstart-cdc_msc

  • MFlash512: .text +8 B (+0.1%, 12,564 B / 524,288 B, total: 2% used)

lpc43-ea4357-cdc_msc

  • MFlashA512: .text +4 B (+0.0%, 15,766 B / 524,288 B, total: 3% used)

maxim-apard32690-cdc_msc

  • FLASH: .text +4 B (+0.0%, 18,864 B / 3,407,872 B, total: 1% used)

mm32-mm32f327x_mb39-cdc_msc

  • FLASH: .text +8 B (+0.1%, 13,356 B / 524,288 B, total: 3% used)

msp430-msp_exp430f5529lp-cdc_msc

  • ROM: .text +28 B (+0.1%, 23,782 B / 48,000 B, total: 50% used)

nuc121_125-nutiny_sdk_nuc121-cdc_msc

  • FLASH: .text +16 B (+0.1%, 20,984 B / 32,768 B, total: 64% used)

nuc126-nutiny_nuc126v-cdc_msc

  • FLASH: .text +16 B (+0.1%, 12,812 B / 262,144 B, total: 5% used)

ra-portenta_c33-cdc_msc

  • FLASH: .text +4 B (+0.0%, 16,268 B / 2,097,152 B, total: 1% used)

samd11-cynthion_d11-cdc_dual_ports

  • rom: .text +16 B (+0.2%, 9,468 B / 14,336 B, total: 66% used)

stm32c0-stm32c071nucleo-cdc_msc

  • FLASH: .text +16 B (+0.1%, 17,124 B / 131,072 B, total: 13% used)

stm32f1-stm32f103_bluepill-cdc_msc

  • FLASH: .text +4 B (+0.0%, 16,480 B / 65,536 B, total: 25% used)

stm32f2-stm32f207nucleo-cdc_msc

  • FLASH: .text +4 B (+0.0%, 17,876 B / 1,048,576 B, total: 2% used)

stm32f3-stm32f303disco-cdc_msc

  • FLASH: .text +4 B (+0.0%, 17,372 B / 262,144 B, total: 7% used)

stm32l4-stm32l412nucleo-cdc_msc

  • FLASH: .text +8 B (+0.0%, 19,616 B / 131,072 B, total: 15% used)

No memory changes detected for:

Copy link
Contributor

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 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_RUN and CFG_TUH_TASK_EVENTS_PER_RUN configuration 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant