initial commit

This commit is contained in:
shell committed 2026-10-11 21:48:59 +08:00
commit b44ce7bd77
2716 files changed
+559005

No files matched your search

@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.20)
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
project(midi2_host 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
)
# 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_host_example(${PROJECT_NAME} noos)
@@ -0,0 +1,6 @@
{
"version": 6,
"include": [
"../../../hw/bsp/BoardPresets.json"
]
}
+13
View File
@@ -0,0 +1,13 @@
include ../../../hw/bsp/family_support.mk
INC += \
src \
# Example source
EXAMPLE_SOURCE += \
src/main.c
SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE))
include ../../../hw/bsp/family_rules.mk
+45
View File
@@ -0,0 +1,45 @@
# Host: MIDI 2.0
A minimal USB host example that receives MIDI from a connected USB-MIDI device and prints it over the debug UART, using the USB-MIDI 2.0 host driver (UMP).
## What it does
- Enumerates USB-MIDI devices (`CFG_TUH_MIDI2`) and, on mount, prints the negotiated protocol (MIDI 1.0 / MIDI 2.0) and the number of RX/TX cables.
- Receives Universal MIDI Packets (UMP) and prints each one, decoding common Channel Voice messages — Note On/Off, Control Change, Program Change, Channel Pressure, Pitch Bend — for both MIDI 2.0 and MIDI 1.0 message types, and falling back to a raw hex dump for anything else.
## Requirements
The board must support USB host mode (provide VBUS to the connected device); some boards need an external USB-A port / host adapter.
## Configuration
Notable `tusb_config.h` settings:
```c
#define CFG_TUH_ENABLED 1
#define CFG_TUH_HUB 1
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
#define CFG_TUH_MIDI2 CFG_TUH_DEVICE_MAX
#define CFG_TUH_MIDI2_RX_BUFSIZE 512
#define CFG_TUH_MIDI2_TX_BUFSIZE 512
```
## Building
CMake:
```bash
mkdir build && cd build
cmake -DBOARD=raspberry_pi_pico ..
cmake --build .
```
Make:
```bash
make BOARD=raspberry_pi_pico all
```
## How to use
Plug a USB-MIDI device (keyboard, controller, or interface) into the board's USB host port. On attach the debug UART prints the mount/descriptor info; then playing notes or moving controls on the device prints the decoded MIDI messages.
+34
View File
@@ -0,0 +1,34 @@
family:hpmicro
family:samd21
family:samd5x_e5x
mcu:CH32V20X
mcu:ESP32P4
mcu:ESP32S2
mcu:ESP32S3
mcu:KINETIS_KL
mcu:LPC175X_6X
mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
mcu:LPC54
mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
mcu:MIMXRT1XXX
mcu:MSP432E4
mcu:RAXXX
mcu:RP2040
mcu:RW61X
mcu:RX65X
mcu:STM32C0
mcu:STM32F4
mcu:STM32F7
mcu:STM32G0
mcu:STM32H5
mcu:STM32H7
mcu:STM32H7RS
mcu:STM32N6
mcu:STM32U3
mcu:STM32U5
+1
View File
@@ -0,0 +1 @@
board:lpcxpresso54114
+163
View File
@@ -0,0 +1,163 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2026 Saulo Verissimo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Minimal USB-MIDI 2.0 host example.
// Receives UMP from any MIDI 2.0 device, prints each packet to stdout.
#include <stdio.h>
#include <string.h>
#include "bsp/board_api.h"
#include "tusb.h"
#include "class/midi/midi2_host.h"
//--------------------------------------------------------------------+
// State
//--------------------------------------------------------------------+
TU_ATTR_UNUSED static uint8_t midi2_idx = 0xFF;
//--------------------------------------------------------------------+
// UMP printer - shows MT and word(s) in hex; decodes Channel Voice
//--------------------------------------------------------------------+
static void print_ump(const uint32_t* words, uint8_t wc) {
uint8_t mt = (uint8_t)((words[0] >> 28) & 0x0F);
uint8_t group = (uint8_t)((words[0] >> 24) & 0x0F);
if (mt == 0x4 && wc >= 2) {
// MIDI 2.0 Channel Voice
uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F);
uint8_t channel = (uint8_t)((words[0] >> 16) & 0x0F);
uint8_t data1 = (uint8_t)((words[0] >> 8) & 0x7F);
switch (status) {
case 0x9:
printf("[g%u ch%u] M2 NoteOn n=%u vel=%04X attr=%04X\r\n",
group, channel, data1,
(unsigned)((words[1] >> 16) & 0xFFFF),
(unsigned)(words[1] & 0xFFFF));
break;
case 0x8:
printf("[g%u ch%u] M2 NoteOff n=%u vel=%04X\r\n",
group, channel, data1,
(unsigned)((words[1] >> 16) & 0xFFFF));
break;
case 0xB:
printf("[g%u ch%u] M2 CC#%u = %08lX\r\n",
group, channel, data1, (unsigned long)words[1]);
break;
case 0xC:
printf("[g%u ch%u] M2 ProgChg %u\r\n",
group, channel, (unsigned)((words[1] >> 24) & 0x7F));
break;
case 0xD:
printf("[g%u ch%u] M2 ChanPress %08lX\r\n",
group, channel, (unsigned long)words[1]);
break;
case 0xE:
printf("[g%u ch%u] M2 PitchBend %08lX\r\n",
group, channel, (unsigned long)words[1]);
break;
default:
printf("[g%u ch%u] M2 status=0x%X w0=%08lX w1=%08lX\r\n",
group, channel, status,
(unsigned long)words[0], (unsigned long)words[1]);
break;
}
} else if (mt == 0x2 && wc == 1) {
// MIDI 1.0 Channel Voice
uint8_t status = (uint8_t)((words[0] >> 16) & 0xFF);
uint8_t data1 = (uint8_t)((words[0] >> 8) & 0x7F);
uint8_t data2 = (uint8_t)(words[0] & 0x7F);
printf("[g%u] M1 %02X %02X %02X\r\n", group, status, data1, data2);
} else {
printf("UMP MT=0x%X wc=%u w0=%08lX\r\n",
mt, wc, (unsigned long)words[0]);
}
}
//--------------------------------------------------------------------+
// MIDI 2.0 Host Callbacks
//--------------------------------------------------------------------+
void tuh_midi2_descriptor_cb(uint8_t idx, const tuh_midi2_descriptor_cb_t* d) {
(void)idx;
printf("MIDI2 descriptor: %s, RX cables=%u TX cables=%u\r\n",
d->protocol_version ? "MIDI 2.0" : "MIDI 1.0",
d->rx_cable_count, d->tx_cable_count);
}
void tuh_midi2_mount_cb(uint8_t idx, const tuh_midi2_mount_cb_t* m) {
midi2_idx = idx;
printf("MIDI2 mounted: idx=%u protocol=%s\r\n",
idx, m->protocol_version ? "MIDI 2.0" : "MIDI 1.0");
}
void tuh_midi2_rx_cb(uint8_t idx, uint32_t xferred_bytes) {
(void)xferred_bytes;
uint32_t words[16];
while (1) {
uint32_t n = tuh_midi2_ump_read(idx, words, 16);
if (n == 0) break;
uint32_t i = 0;
while (i < n) {
uint8_t mt = (uint8_t)((words[i] >> 28) & 0x0F);
uint8_t wc = midi2_ump_word_count(mt);
if (i + wc > n) break;
print_ump(&words[i], wc);
i += wc;
}
}
}
void tuh_midi2_tx_cb(uint8_t idx, uint32_t xferred_bytes) {
(void)idx; (void)xferred_bytes;
}
void tuh_midi2_umount_cb(uint8_t idx) {
(void)idx;
midi2_idx = 0xFF;
printf("MIDI2 unmounted\r\n");
}
//--------------------------------------------------------------------+
// Main
//--------------------------------------------------------------------+
int main(void) {
board_init();
printf("\r\nTinyUSB Host MIDI 2.0 Example\r\n");
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();
}
}
@@ -0,0 +1,106 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2026 Saulo Verissimo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef TUSB_CONFIG_H_
#define TUSB_CONFIG_H_
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------+
// Common Configuration
//--------------------------------------------------------------------+
#ifndef CFG_TUSB_MCU
#error CFG_TUSB_MCU must be defined
#endif
#ifdef ESP_PLATFORM
#define CFG_TUSB_OS_INC_PATH freertos/
#endif
#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_NONE
#endif
#ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 0
#endif
#ifndef CFG_TUH_MEM_SECTION
#define CFG_TUH_MEM_SECTION
#endif
#ifndef CFG_TUH_MEM_ALIGN
#define CFG_TUH_MEM_ALIGN __attribute__ ((aligned(4)))
#endif
//--------------------------------------------------------------------+
// Host Configuration
//--------------------------------------------------------------------+
#define CFG_TUH_ENABLED 1
#if CFG_TUSB_MCU == OPT_MCU_RP2040
// #define CFG_TUH_RPI_PIO_USB 1 // use pio-usb as host controller
// #define CFG_TUH_MAX3421 1 // use max3421 as host controller
// host roothub port is 1 if using either pio-usb or max3421
#if (defined(CFG_TUH_RPI_PIO_USB) && CFG_TUH_RPI_PIO_USB) || (defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421)
#define BOARD_TUH_RHPORT 1
#endif
#endif
#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED
//------------------------- Board Specific --------------------------+
#ifndef BOARD_TUH_RHPORT
#define BOARD_TUH_RHPORT 0
#endif
#ifndef BOARD_TUH_MAX_SPEED
#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED
#endif
//--------------------------------------------------------------------+
// Driver Configuration
//--------------------------------------------------------------------+
#define CFG_TUH_ENUMERATION_BUFSIZE 256
#define CFG_TUH_HUB 1
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
// USB-MIDI 2.0 Host
#define CFG_TUH_MIDI2 CFG_TUH_DEVICE_MAX
#define CFG_TUH_MIDI2_RX_BUFSIZE 512
#define CFG_TUH_MIDI2_TX_BUFSIZE 512
#ifdef __cplusplus
}
#endif
#endif