-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: Add USB-MIDI 2.0 Device and Host class drivers #3571
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
Open
sauloverissimo
wants to merge
7
commits into
hathach:master
Choose a base branch
from
sauloverissimo:feat/midi2-device-host-driver
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f48bbcb
feat: add MIDI 2.0 Device class driver (USB-MIDI 2.0)
sauloverissimo 2fcb5db
feat: add MIDI 2.0 Host class driver (USB-MIDI 2.0)
sauloverissimo 11f7bb5
test: add MIDI 2.0 Device and Host unit tests
sauloverissimo fd304f9
example: add MIDI 2.0 Device example (midi2_device)
sauloverissimo 5d5c50a
example: add MIDI 2.0 Host example with display (midi2_host)
sauloverissimo 9f1001e
fix: address PR review feedback for MIDI 2.0 drivers
sauloverissimo b8fa96a
fix: CI build failures on non-RP2040 platforms
sauloverissimo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,316 @@ | ||
| *************** | ||
| Class Drivers | ||
| *************** | ||
|
|
||
| USB Class Drivers implement specific USB device classes (CDC, HID, MSC, MIDI, Audio, etc.) and are the main interface between the USB core and application code. | ||
|
|
||
| MIDI 2.0 Device Driver | ||
| ======================= | ||
|
|
||
| Overview | ||
| -------- | ||
|
|
||
| The MIDI 2.0 Device driver enables TinyUSB to act as a USB MIDI 2.0 device. It implements both Alt Setting 0 (MIDI 1.0 fallback) and Alt Setting 1 (native UMP) as required by the USB-MIDI 2.0 specification. | ||
|
|
||
| **Key Features:** | ||
|
|
||
| - **Dual Alt Settings**: Alt 0 (MIDI 1.0) and Alt 1 (UMP native) per USB-MIDI 2.0 spec | ||
| - **Protocol Negotiation**: Endpoint Discovery, Config Request/Notify, Function Block Discovery | ||
| - **Group Terminal Block**: Served via GET_DESCRIPTOR automatically | ||
| - **Atomic UMP Framing**: Read/write with correct message boundaries | ||
| - **Memory Safe**: No dynamic allocation, static instances | ||
|
|
||
| Configuration | ||
| ------------- | ||
|
|
||
| Enable MIDI 2.0 Device support in ``tusb_config.h``: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| #define CFG_TUD_ENABLED 1 | ||
| #define CFG_TUD_MIDI2 1 | ||
|
|
||
| Optional configuration: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| #define CFG_TUD_MIDI2_TX_BUFSIZE 256 | ||
| #define CFG_TUD_MIDI2_RX_BUFSIZE 256 | ||
| #define CFG_TUD_MIDI2_TX_EPSIZE 64 | ||
| #define CFG_TUD_MIDI2_RX_EPSIZE 64 | ||
| #define CFG_TUD_MIDI2_NUM_GROUPS 1 // 1..16 | ||
| #define CFG_TUD_MIDI2_NUM_FUNCTION_BLOCKS 1 // 1..32 | ||
| #define CFG_TUD_MIDI2_EP_NAME "TinyUSB MIDI 2.0" | ||
| #define CFG_TUD_MIDI2_PRODUCT_ID "TinyUSB-MIDI2" | ||
|
|
||
| Public API | ||
| ---------- | ||
|
|
||
| Query Functions | ||
| ^^^^^^^^^^^^^^^ | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| bool tud_midi2_mounted(void); | ||
| uint32_t tud_midi2_available(void); | ||
| uint8_t tud_midi2_alt_setting(void); | ||
| bool tud_midi2_negotiated(void); | ||
| uint8_t tud_midi2_protocol(void); | ||
|
|
||
| I/O Functions | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| uint32_t tud_midi2_ump_read(uint32_t* words, uint32_t max_words); | ||
| uint32_t tud_midi2_ump_write(const uint32_t* words, uint32_t count); | ||
| bool tud_midi2_packet_read(uint8_t packet[4]); | ||
| bool tud_midi2_packet_write(const uint8_t packet[4]); | ||
|
|
||
| Callbacks | ||
| ^^^^^^^^^ | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| void tud_midi2_rx_cb(uint8_t itf); | ||
| void tud_midi2_set_itf_cb(uint8_t itf, uint8_t alt); | ||
| bool tud_midi2_get_req_itf_cb(uint8_t rhport, const tusb_control_request_t* request); | ||
|
|
||
| MIDI 2.0 Host Driver | ||
| ===================== | ||
|
|
||
| Overview | ||
| -------- | ||
|
|
||
| The MIDI 2.0 Host driver enables TinyUSB to enumerate and communicate with USB MIDI 2.0 devices. It implements the USB MIDI 2.0 specification, supporting both MIDI 1.0 legacy devices and modern MIDI 2.0 devices with UMP (Universal MIDI Packet) protocol. | ||
|
|
||
| **Key Features:** | ||
|
|
||
| - **Reactive Architecture**: Auto-detects Alt Setting 1 (MIDI 2.0) capability during enumeration | ||
| - **Auto-Selection**: Automatically selects the highest available protocol (MIDI 2.0 preferred) | ||
| - **Transparent Stream Messages**: All data (UMP packets + Stream Messages) flow through callbacks | ||
| - **Memory Safe**: No dynamic allocation, fixed-size instances per device | ||
|
|
||
| Configuration | ||
| ------------- | ||
|
|
||
| Enable MIDI 2.0 Host support in ``tusb_config.h``: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| #define CFG_TUH_ENABLED 1 | ||
| #define CFG_TUH_MIDI2 4 // Number of MIDI 2.0 devices to support | ||
|
|
||
| Optional buffer configuration: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| #define CFG_TUH_MIDI2_RX_BUFSIZE (4 * TUH_EPSIZE_BULK_MAX) | ||
| #define CFG_TUH_MIDI2_TX_BUFSIZE (4 * TUH_EPSIZE_BULK_MAX) | ||
|
|
||
| Enumeration Lifecycle | ||
| --------------------- | ||
|
|
||
| When a MIDI 2.0 device is connected, the host stack invokes callbacks in this order: | ||
|
|
||
| .. code-block:: none | ||
|
|
||
| Device Connected | ||
| | | ||
| [Host detects Alt 0 and Alt 1 descriptors] | ||
| | | ||
| tuh_midi2_descriptor_cb() <- Device detected, NOT yet ready | ||
| | | ||
| [Auto-select highest protocol] | ||
| | | ||
| tuh_midi2_mount_cb() <- Device ready to use | ||
| | | ||
| [Application can read/write data] | ||
| | | ||
| tuh_midi2_rx_cb() <- Data arrived | ||
| tuh_midi2_tx_cb() <- TX buffer space available | ||
| | | ||
| [Device disconnects] | ||
| | | ||
| tuh_midi2_umount_cb() <- Device removed | ||
|
|
||
| Public API | ||
| ---------- | ||
|
|
||
| Query Functions | ||
| ^^^^^^^^^^^^^^^ | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| bool tuh_midi2_mounted(uint8_t idx); | ||
| uint8_t tuh_midi2_get_protocol_version(uint8_t idx); // 0=MIDI 1.0, 1=MIDI 2.0 | ||
| uint8_t tuh_midi2_get_alt_setting_active(uint8_t idx); // 0 or 1 | ||
| uint8_t tuh_midi2_get_cable_count(uint8_t idx); | ||
|
|
||
| I/O Functions | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| Read and write UMP (Universal MIDI Packet) data: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| uint32_t tuh_midi2_ump_read(uint8_t idx, uint32_t* words, uint32_t max_words); | ||
| uint32_t tuh_midi2_ump_write(uint8_t idx, const uint32_t* words, uint32_t count); | ||
| uint32_t tuh_midi2_write_flush(uint8_t idx); | ||
|
|
||
| Callbacks | ||
| --------- | ||
|
|
||
| Application can define weak callback implementations to respond to device events. | ||
|
|
||
| Descriptor Callback | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Invoked when device is detected but not yet ready for I/O: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| void tuh_midi2_descriptor_cb(uint8_t idx, const tuh_midi2_descriptor_cb_t *desc_cb_data) { | ||
| printf("MIDI %s device detected\r\n", | ||
| desc_cb_data->protocol_version == 0 ? "1.0" : "2.0"); | ||
| } | ||
|
|
||
| Mount Callback | ||
| ^^^^^^^^^^^^^^ | ||
|
|
||
| Invoked when device is ready for I/O: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| void tuh_midi2_mount_cb(uint8_t idx, const tuh_midi2_mount_cb_t *mount_cb_data) { | ||
| printf("Device mounted at idx=%u, protocol=%u, alt_setting=%u\r\n", | ||
| idx, mount_cb_data->protocol_version, mount_cb_data->alt_setting_active); | ||
| } | ||
|
|
||
| RX Callback | ||
| ^^^^^^^^^^^ | ||
|
|
||
| Invoked when data arrives from device (both UMP packets and Stream Messages): | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| void tuh_midi2_rx_cb(uint8_t idx, uint32_t xferred_bytes) { | ||
| uint32_t words[4]; | ||
| uint32_t n = tuh_midi2_ump_read(idx, words, 4); | ||
|
|
||
| for (uint32_t i = 0; i < n; i++) { | ||
| uint8_t mt = (words[i] >> 28) & 0x0F; | ||
| if (mt == 0x0F) { | ||
| // Stream Message - app handles discovery, negotiation, etc. | ||
| } else { | ||
| // Regular MIDI UMP packet | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TX Callback | ||
| ^^^^^^^^^^^ | ||
|
|
||
| Invoked when TX buffer space becomes available: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| void tuh_midi2_tx_cb(uint8_t idx, uint32_t xferred_bytes) { | ||
| // Buffer space available for writing | ||
| } | ||
|
|
||
| Unmount Callback | ||
| ^^^^^^^^^^^^^^^^ | ||
|
|
||
| Invoked when device is disconnected: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| void tuh_midi2_umount_cb(uint8_t idx) { | ||
| printf("Device at idx=%u disconnected\r\n", idx); | ||
| } | ||
|
|
||
| Complete Example | ||
| ---------------- | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| #include "tusb.h" | ||
|
|
||
| void tuh_midi2_mount_cb(uint8_t idx, const tuh_midi2_mount_cb_t *mount_cb_data) { | ||
| printf("MIDI 2.0 device mounted\r\n"); | ||
| } | ||
|
|
||
| void tuh_midi2_rx_cb(uint8_t idx, uint32_t xferred_bytes) { | ||
| uint32_t words[4]; | ||
| uint32_t n = tuh_midi2_ump_read(idx, words, 4); | ||
|
|
||
| for (uint32_t i = 0; i < n; i++) { | ||
| printf("RX: 0x%08lx\r\n", words[i]); | ||
| } | ||
| } | ||
|
|
||
| void tuh_midi2_umount_cb(uint8_t idx) { | ||
| printf("MIDI 2.0 device disconnected\r\n"); | ||
| } | ||
|
|
||
| int main(void) { | ||
| board_init(); | ||
|
|
||
| tusb_rhport_init_t host_init = {.role = TUSB_ROLE_HOST, .speed = TUSB_SPEED_AUTO}; | ||
| tusb_init(BOARD_TUH_RHPORT, &host_init); | ||
|
|
||
| while (1) { | ||
| tuh_task(); | ||
| } | ||
| } | ||
|
|
||
| Architecture | ||
| ------------ | ||
|
|
||
| The MIDI 2.0 Host driver uses a **reactive, callback-driven architecture** that mirrors the proven patterns in TinyUSB's existing device drivers (CDC, HID, etc.): | ||
|
|
||
| - **Auto-Detection**: Host automatically detects Alt Setting 1 capability | ||
| - **Auto-Selection**: Selects highest protocol available (MIDI 2.0 preferred) | ||
| - **Application Control**: App makes protocol behavior decisions via callbacks | ||
| - **Transparent I/O**: Stream Messages and UMP packets flow transparently | ||
|
|
||
| Differences from MIDI 1.0 Host | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
|
|
||
| * - Aspect | ||
| - MIDI 1.0 Host | ||
| - MIDI 2.0 Host | ||
| * - Alt Settings | ||
| - Parses only Alt 0 | ||
| - Parses Alt 0 + Alt 1 | ||
| * - Data Format | ||
| - 4-byte MIDI packets | ||
| - UMP (32/64/128-bit) | ||
| * - Version Detection | ||
| - None | ||
| - bcdMSC from descriptor | ||
| * - GTB | ||
| - N/A | ||
| - Presence detection | ||
| * - Stream Messages | ||
| - N/A | ||
| - Transparent passthrough | ||
| * - Callbacks | ||
| - descriptor_cb, mount_cb, rx_cb, umount_cb | ||
| - descriptor_cb, mount_cb, rx_cb, tx_cb, umount_cb | ||
| * - Public API | ||
| - tuh_midi_* | ||
| - tuh_midi2_* | ||
|
|
||
| Implementation Notes | ||
| -------------------- | ||
|
|
||
| - All internal state is statically allocated (no dynamic allocation) | ||
| - Endpoint streams use TinyUSB's tu_edpt_stream_t for buffered I/O | ||
| - Protocol version detection via bcdMSC field | ||
| - Alt Setting is automatically selected during mount | ||
| - Compatible with all TinyUSB-supported MCU families | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| cmake_minimum_required(VERSION 3.20) | ||
|
|
||
| include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) | ||
|
|
||
| project(midi2_device C CXX ASM) | ||
|
|
||
| # Checks this example is valid for the family and initializes the project | ||
| family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}) | ||
|
|
||
| # Espressif has its own cmake build system | ||
| if(FAMILY STREQUAL "espressif") | ||
| return() | ||
| endif() | ||
|
|
||
| add_executable(${PROJECT_NAME}) | ||
|
|
||
| # Example source | ||
| target_sources(${PROJECT_NAME} PUBLIC | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c | ||
| ) | ||
|
|
||
| # Example include | ||
| target_include_directories(${PROJECT_NAME} PUBLIC | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/src | ||
| ) | ||
|
|
||
| # Configure compilation flags and libraries for the example without RTOS. | ||
| # See the corresponding function in hw/bsp/FAMILY/family.cmake for details. | ||
| family_configure_device_example(${PROJECT_NAME} noos) | ||
|
|
||
| # Suppress pre-existing warning in usbd.c (uint8_t comparison always true/false) | ||
| target_compile_options(${PROJECT_NAME} PRIVATE -Wno-type-limits) |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
The host driver documentation states it will "Auto-Select" the highest available protocol, which matches the current implementation (midih2_auto_select_alt_setting). However, the PR description says the driver makes no protocol decisions and the application controls everything. Please align the PR description and public API/docs with the actual behavior (either document the auto-selection as the supported behavior, or add an application-controlled selection mechanism and update the code/docs accordingly).
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.
Fixed in 9f1001e. Documentation now reflects the actual behavior: auto-selects highest protocol and issues SET_INTERFACE to activate Alt Setting 1 when MIDI 2.0 is detected. PR description aligned accordingly.