shared/tinyusb: Add macro to override TinyUSB callbacks. #18382
+14
−4
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.
Summary
I need to redirect some of the TinyUSB callbacks, but MicroPython implements them exclusively. So far I've been using linker
--wrapwith great success, sadly TinyUSB 0.19.0 adds weak symbols for callbacks which breaks the--wraptrick.This patch adds
MICROPY_TINYUSB_CALLBACKmacro 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_CALLBACKmacro expands to the callback name unchanged, I've double checked the map file and in both cases (default, or defined tomp_) 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:
--wrapno longer works with TinyUSB 0.19.0.objcopyto 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.MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDCseems 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_x2- Implement
tud_cdc_as yet another weak symbol that callsmp_tud_cdc_x3- Now we can provide strong symbols that override them, and call
mp_tud_cdc_xif needed.This depends on the link order, I think the shared MP objects always come first though.