-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
shared/tinyusb: Use device event hook to schedule USB task. #17338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
shared/tinyusb: Use device event hook to schedule USB task. #17338
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #17338 +/- ##
=======================================
Coverage 98.54% 98.54%
=======================================
Files 169 169
Lines 21898 21898
=======================================
Hits 21579 21579
Misses 319 319 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code size report:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice find @projectgus ! That's much cleaner.
I do see that queue_event()
/tud_event_hook_cb()
is called from a number of places in the ISR for different types of events, my initial thought was maybe we only want to trigger on some events or prevent repeat calls if there are multiple events in one ISR.
But now I've confirmed that's not an issue, this new hook is queing up the usb task in the c-scheduler which has built in protection against re-scheduling of the same node, subsequent schedules are just ignored if it's already queued to be run (py/scheduler.c
-> mp_sched_schedule_node()
)
Tested on ARDUINO_NANO_33_BLE_SENSE with the following: >>> import time
>>> print('test');time.sleep(1)
test
>>> Prior to this PR, the "test" output would not appear until after the 1 second elapsed. With this PR it now works properly, "test" is printed immediately, and then it waits 1 second before the prompt appears. Great work @projectgus getting this hook merged to TinyUSB! |
Could we now get rid of |
71b180b
to
6de46d1
Compare
Oh, good catch! Have taken it out as well.
Huh, that's good! I see the nrf port of TinyUSB sometimes calls |
Previously MicroPython ports would linker-wrap dcd_event_handler in order to schedule the USB task callback to run when needed. TinyUSB 0.16 added proper support for an event hook to do the same thing without the hacky linker wrapping. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
This reverts commit 62e0fa0. Reverting as the only linker wrap needed for nrf port was removed in the parent commit. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
6de46d1
to
da7017f
Compare
Pushed one more small tweak, can remove the dcd.h include line from mp_usbd.c |
Very good! |
@dpgeorge I tried to replicate your test with |
No, I didn't change anything else. Just this PR on latest master. Maybe you need to |
I always start with |
TinyUSB should be at 0.17.0 on current master, and that's all that is needed for this PR to work. Can you please try with a fresh clone of this repo? It really should work without needing to update TinyUSB. |
If I make a fresh clone of MicroPython, run
So the output ought to be the same. But the firmware files differ in size and content. Do you have a hint for a reason? |
Do you have a |
Thank you for your answer that late in the night. |
Summary
Previously MicroPython ports would linker-wrap dcd_event_handler in order to schedule the USB task callback to run when needed.
TinyUSB 0.16 added proper support for an event hook to do the same thing without the hacky linker wrapping. (See hathach/tinyusb#2303)
(The vendored TinyUSB is 0.17.0 since 09fa90e, the version in ESP-IDF is 0.18.0 for both ESP-IDF V5.2 and V5.4.1.)
As a bonus:
This work was funded through GitHub Sponsors.
Testing
RPI_PICO
andESP32_GENERIC_S3
boards, interacted with the default REPL, and also ran theusb/device/mouse
example on both boards.