Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@iabdalkader
Copy link
Contributor

@iabdalkader iabdalkader commented Nov 7, 2025

Summary

I need to redirect some of the TinyUSB callbacks, but MicroPython implements them exclusively. So far I've been using linker --wrap with great success, sadly TinyUSB 0.19.0 adds weak symbols for callbacks which breaks the --wrap trick.
This patch adds MICROPY_TINYUSB_CALLBACK macro that by default expands to the callback name unchanged. Users can define this macro to add a prefix (e.g., mp_) to callback implementations, to redirect or completely override MicroPython's TinyUSB callbacks.

Other changes

Add hook to mp_usbd_task. This adds an empty hook that gets called when running the USB task.

Testing

MICROPY_TINYUSB_CALLBACK macro expands to the callback name unchanged, I've double checked the map file and in both cases (default, or defined to mp_) the symbols have the right names. Let me know if more testing needed.

Trade-offs and Alternatives

This is the most direct way I could think of, however:

  • --wrap no longer works with TinyUSB 0.19.0.
  • objcopy to rename symbols. I had a hard time trying to get this to work, it requires complex rules somewhere, but might be a viable option if this is closed.
  • Hooks?
  // In mp_usbd_cdc.c
  void tud_cdc_rx_cb(uint8_t itf) {
      // Call user hook if registered
      if (mp_usbd_cdc_rx_hook != NULL) {
          if (mp_usbd_cdc_rx_hook(itf)) {
              return; // Hook handled it
          }
      }
      // Default implementation
      mp_usbd_cdc_rx_cb_default(itf);
  }
  • MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDC seems to have been needed at some point, but it requires a full implementation of those callbacks. I only need to redirect/override some of them.

  • Make mp callbacks weak?
    1- Rename those callbacks to mp_tud_cdc_x
    2- Implement tud_cdc_ as yet another weak symbol that calls mp_tud_cdc_x
    3- Now we can provide strong symbols that override them, and call mp_tud_cdc_x if needed.

This depends on the link order, I think the shared MP objects always come first though.

Add MICROPY_TINYUSB_CALLBACK macro that by default expands to the
callback name unchanged. Users can define this macro to add a prefix
(e.g., mp_) to callback implementations, to redirect or completely
override MicroPython's TinyUSB callbacks.

Signed-off-by: iabdalkader <[email protected]>
@codecov
Copy link

codecov bot commented Nov 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.38%. Comparing base (2762fe6) to head (e39a240).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #18382   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files         171      171           
  Lines       22294    22294           
=======================================
  Hits        21933    21933           
  Misses        361      361           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link

github-actions bot commented Nov 7, 2025

Code size report:

Reference:  tests/serial_test.py: Allow up to 2 seconds between bytes. [2762fe6]
Comparison: shared/tinyusb: Add hook to mp_usbd_task. [merge of e39a240]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

This adds MICROPY_USBD_TASK_HOOK which is called after tud_task_ext()
completes in mp_usbd_task(). This allows port code to run custom logic
after USB device task processing, such as handling port-specific USB
events or updating port state.

Signed-off-by: iabdalkader <[email protected]>
@dpgeorge dpgeorge added the shared Relates to shared/ directory in source label Nov 13, 2025
@iabdalkader
Copy link
Contributor Author

Does this look okay to you, or do think there's a better approach to do this? We're looking to do a production firmware release, which requires TinyUSB 0.19.x, which in turn requires that I somehow fix this broken wrap issue.

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

Labels

shared Relates to shared/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants