migrate midi_device to use edpt stream API#3337
Conversation
hathach
commented
Nov 12, 2025
- migrate midi_device to use edpt stream API
- also add tud_midi_n_packet_write/read_n()
0b6d508 to
9450f9f
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR migrates the MIDI device class driver from using direct FIFO management to using the new endpoint stream API. This simplifies the code by leveraging the shared stream infrastructure and adds new functions for reading/writing multiple MIDI packets at once.
- Replaced manual FIFO and endpoint management with
tu_edpt_stream_tAPI - Added
tud_midi_n_packet_read_n()andtud_midi_n_packet_write_n()functions for bulk packet operations - Updated style conventions throughout (const placement, spacing, formatting)
Reviewed Changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/tusb.c | Minor style updates to endpoint stream functions (const placement) |
| src/common/tusb_private.h | Added tu_edpt_stream_is_opened() helper, updated function signatures with const qualifiers, improved comment |
| src/common/tusb_fifo.h | Reorganized declarations, added const qualifiers, moved tu_fifo_empty() to inline implementation |
| src/common/tusb_fifo.c | Removed tu_fifo_empty() function (now inline), added const qualifiers to read-only functions |
| src/class/vendor/vendor_device.c | Added blank line between RX and TX stream operations |
| src/class/midi/midi_device.h | Added new packet batch read/write APIs, updated formatting, removed deprecated functions and comments |
| src/class/midi/midi_device.c | Complete refactor to use tu_edpt_stream_t instead of manual FIFO/endpoint management, implemented new batch packet functions |
| .clang-format | Added IndentPPDirectives: BeforeHash formatting rule |
💡 Codex Reviewtinyusb/src/class/midi/midi_device.c Lines 298 to 309 in 0b6d508 The new bulk send API advertises that it returns the number of packets written, mirroring tinyusb/src/class/midi/midi_device.h Lines 112 to 117 in 0b6d508 The single‑interface convenience wrapper for ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
9450f9f to
e04140b
Compare
There was a problem hiding this comment.
Code Health Improved
(1 files improve in Code Health)
Gates Failed
Enforce critical code health rules
(1 file with Bumpy Road Ahead)
Gates Passed
2 Quality Gates Passed
See analysis details in CodeScene
Reason for failure
| Enforce critical code health rules | Violations | Code Health Impact | |
|---|---|---|---|
| midi_device.c | 1 critical rule | 7.10 → 7.28 | Suppress |
View Improvements
| File | Code Health Impact | Categories Improved |
|---|---|---|
| midi_device.c | 7.10 → 7.28 | Complex Method, Complex Conditional, Bumpy Road Ahead, Overall Code Complexity, Deep, Nested Complexity |
Quality Gate Profile: Clean Code Collective
Want more control? Customize Code Health rules or catch issues early with our IDE extension and CLI tool.
51089b5 to
e7f8ec5
Compare
also add tud_midi_n_packet_write/read_n()
e7f8ec5 to
fc661ab
Compare
|
tannewt
left a comment
There was a problem hiding this comment.
FYI this change increased the CircuitPython arduino_zero build by 532 bytes. I can't easily update to 0.20.0 due to this because some languages now overflow (ru in this case).
It now includes tu_edpt_stream_write_xfer and tu_edpt_stream_read_xfer which are 280 bytes. midid_ init, open, reset and xfer_cb all grow in size as well.
|
@tannewt do you have a branch that have wip 0.20.0 (else I will just bump tinyusb submodule in cpy to test with). I will try to see if could reduce the size by dropping features that some smaller ports does not need. This refactor all common edpt stream code so it should reduce code size if e.g cdc and midi both enabled, but I haven't check. PS: I am trying to optimize the code size for tinyusb, new PR now will have a comment on code size change. |
|
I did some test in https://github.com/hathach/tinyusb/tree/size_tweak by:
While it's still larger but half-way done ;) Debug:
Release:
|
|
is due to extra log in edpt stream that cover all cases such as non-buffered (non-fifo) and xfer_fifo. I will try also to optimize further |
I was testing with adafruit/circuitpython#10736 but didn't end up needing to update TinyUSB. I had done it to the 0.20.0 tag. The tip of master yesterday had even less remaining space. I was doing the arduino_zero TRANSLATION=ru build in testing. It has CDC and MIDI so it should share the stream code. It is LTOd too. That may make a difference. |
|
@tannewt #3402 fine tune to reduce the tinyusb foot print. I also add the code metrics which basically sum average all boards/examples for each .c file. The metrics make it easier to follow the code size and make fine tuning later on. |




