From d5c5ac586bfdbf53e18a8cdbb11b978f53b6d059 Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Tue, 24 Mar 2026 23:38:36 -0300 Subject: example: add MIDI 2.0 Host example with display (midi2_host) Add Host example that receives UMP from a MIDI 2.0 Device via PIO-USB and displays received messages on a SSD1306 OLED (I2C, 128x64). Features: - Boot checklist on display (PWR, TinyUSB, USB bus, Device, Descriptor, Alt Setting UMP, Mount, Receiving) - Decode and display all MIDI 2.0 Channel Voice messages - PIO-USB Host on rhport 1 (GP12/GP13 for Waveshare RP2350-USB-A) - SSD1306 display via I2C0 (GP4=SDA, GP5=SCL) Also add board definition for Waveshare RP2350-USB-A. Tested: Waveshare RP2350-USB-A (Host) receiving UMP from Raspberry Pi Pico (Device) via USB cable, notes displayed on SSD1306 OLED --- examples/host/CMakeLists.txt | 1 + examples/host/midi2_host/CMakeLists.txt | 37 ++++ examples/host/midi2_host/src/display.c | 220 +++++++++++++++++++ examples/host/midi2_host/src/display.h | 54 +++++ examples/host/midi2_host/src/font5x7.h | 107 ++++++++++ examples/host/midi2_host/src/main.c | 326 +++++++++++++++++++++++++++++ examples/host/midi2_host/src/tusb_config.h | 91 ++++++++ 7 files changed, 836 insertions(+) create mode 100644 examples/host/midi2_host/CMakeLists.txt create mode 100644 examples/host/midi2_host/src/display.c create mode 100644 examples/host/midi2_host/src/display.h create mode 100644 examples/host/midi2_host/src/font5x7.h create mode 100644 examples/host/midi2_host/src/main.c create mode 100644 examples/host/midi2_host/src/tusb_config.h (limited to 'examples/host') diff --git a/examples/host/CMakeLists.txt b/examples/host/CMakeLists.txt index 70e0427ab..7c74e3c73 100644 --- a/examples/host/CMakeLists.txt +++ b/examples/host/CMakeLists.txt @@ -13,6 +13,7 @@ set(EXAMPLE_LIST device_info hid_controller midi_rx + midi2_host msc_file_explorer msc_file_explorer_freertos ) diff --git a/examples/host/midi2_host/CMakeLists.txt b/examples/host/midi2_host/CMakeLists.txt new file mode 100644 index 000000000..221de7adf --- /dev/null +++ b/examples/host/midi2_host/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.20) + +include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) + +project(midi2_host C CXX ASM) + +family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}) + +if(FAMILY STREQUAL "espressif") + return() +endif() + +add_executable(${PROJECT_NAME}) + +target_sources(${PROJECT_NAME} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c + ${CMAKE_CURRENT_SOURCE_DIR}/src/display.c +) + +target_include_directories(${PROJECT_NAME} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src +) + +family_configure_host_example(${PROJECT_NAME} noos) + +# Waveshare RP2350-USB-A: PIO-USB on GP12/GP13 +target_compile_definitions(${PROJECT_NAME} PRIVATE + PIO_USB_DP_PIN_DEFAULT=12 +) +target_compile_options(${PROJECT_NAME} PRIVATE + -Wno-type-limits +) + +# SSD1306 display (I2C) +target_link_libraries(${PROJECT_NAME} PUBLIC + hardware_i2c +) diff --git a/examples/host/midi2_host/src/display.c b/examples/host/midi2_host/src/display.c new file mode 100644 index 000000000..6d81bcd5c --- /dev/null +++ b/examples/host/midi2_host/src/display.c @@ -0,0 +1,220 @@ +/* + * 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. + */ + +// SSD1306 OLED display driver (128x64, I2C) for MIDI 2.0 Host example. +// Minimal text-only implementation, no graphics library. +// I2C0: SDA = GP4, SCL = GP5, Address = 0x3C + +#include "display.h" +#include +#include +#include "pico/stdlib.h" +#include "hardware/i2c.h" +#include "hardware/gpio.h" + +#define I2C_PORT i2c0 +#define I2C_SDA 4 +#define I2C_SCL 5 +#define I2C_FREQ 400000 +#define SSD1306_ADDR 0x3C + +#define SCR_W 128 +#define SCR_H 64 +#define PAGES (SCR_H / 8) // 8 pages +#define CHARS_PER_LINE 21 // 128 / 6 = 21 chars + +// Framebuffer +static uint8_t fb[SCR_W * PAGES]; + +// Log buffer +#define LOG_LINES 3 +static char log_lines[LOG_LINES][CHARS_PER_LINE + 1]; +static int log_count = 0; + +// Status line +static char status_text[CHARS_PER_LINE + 1] = ""; + +//--------------------------------------------------------------------+ +// Minimal 5x7 font (ASCII 32-126) +//--------------------------------------------------------------------+ +#include "font5x7.h" + +//--------------------------------------------------------------------+ +// SSD1306 I2C commands +//--------------------------------------------------------------------+ + +static void ssd_cmd(uint8_t cmd) { + uint8_t buf[2] = { 0x00, cmd }; // Co=0, D/C=0 + i2c_write_blocking(I2C_PORT, SSD1306_ADDR, buf, 2, false); +} + +static void ssd_data(const uint8_t* data, size_t len) { + uint8_t buf[SCR_W + 1]; + buf[0] = 0x40; // Co=0, D/C=1 + size_t chunk = (len > SCR_W) ? SCR_W : len; + memcpy(buf + 1, data, chunk); + i2c_write_blocking(I2C_PORT, SSD1306_ADDR, buf, chunk + 1, false); +} + +static void ssd_flush(void) { + ssd_cmd(0x21); ssd_cmd(0); ssd_cmd(127); // Column range + ssd_cmd(0x22); ssd_cmd(0); ssd_cmd(7); // Page range + for (int page = 0; page < PAGES; page++) { + ssd_data(&fb[page * SCR_W], SCR_W); + } +} + +//--------------------------------------------------------------------+ +// Framebuffer drawing +//--------------------------------------------------------------------+ + +static void fb_clear(void) { + memset(fb, 0, sizeof(fb)); +} + +static void fb_char(int x, int y, char c) { + if (c < 32 || c > 126) c = '?'; + const uint8_t* glyph = font5x7 + (c - 32) * 5; + int page = y / 8; + int bit_offset = y % 8; + + if (page >= PAGES || x + 5 > SCR_W) return; + + for (int col = 0; col < 5; col++) { + uint8_t column_data = glyph[col]; + fb[(page * SCR_W) + x + col] |= (uint8_t)(column_data << bit_offset); + if (bit_offset > 0 && page + 1 < PAGES) { + fb[((page + 1) * SCR_W) + x + col] |= (uint8_t)(column_data >> (8 - bit_offset)); + } + } +} + +static void fb_string(int x, int y, const char* str) { + while (*str) { + fb_char(x, y, *str); + x += 6; + if (x + 6 > SCR_W) break; + str++; + } +} + +//--------------------------------------------------------------------+ +// Display API +//--------------------------------------------------------------------+ + +void display_init(void) { + i2c_init(I2C_PORT, I2C_FREQ); + gpio_set_function(I2C_SDA, GPIO_FUNC_I2C); + gpio_set_function(I2C_SCL, GPIO_FUNC_I2C); + gpio_pull_up(I2C_SDA); + gpio_pull_up(I2C_SCL); + + sleep_ms(100); + + // SSD1306 init sequence + ssd_cmd(0xAE); // Display off + ssd_cmd(0xD5); ssd_cmd(0x80); // Clock div + ssd_cmd(0xA8); ssd_cmd(0x3F); // Multiplex 64 + ssd_cmd(0xD3); ssd_cmd(0x00); // Display offset + ssd_cmd(0x40); // Start line 0 + ssd_cmd(0x8D); ssd_cmd(0x14); // Charge pump on + ssd_cmd(0x20); ssd_cmd(0x00); // Horizontal addressing + ssd_cmd(0xA1); // Segment remap + ssd_cmd(0xC8); // COM scan direction + ssd_cmd(0xDA); ssd_cmd(0x12); // COM pins + ssd_cmd(0x81); ssd_cmd(0xCF); // Contrast + ssd_cmd(0xD9); ssd_cmd(0xF1); // Pre-charge + ssd_cmd(0xDB); ssd_cmd(0x40); // VCOMH deselect + ssd_cmd(0xA4); // Display from RAM + ssd_cmd(0xA6); // Normal display + ssd_cmd(0xAF); // Display on + + fb_clear(); + fb_string(0, 0, "MIDI 2.0 Host"); + ssd_flush(); +} + +void display_checklist_update(const checklist_t* ck) { + struct { const char* label; bool ok; } items[] = { + { "PWR", ck->pwr_on }, + { "TinyUSB", ck->tusb_init }, + { "USB bus", ck->bus_active }, + { "Device", ck->device_connected }, + { "Descript", ck->descriptor_parsed }, + { "Alt1 UMP", ck->alt_setting_ok }, + { "Mount", ck->mounted }, + { "RX UMP", ck->receiving }, + }; + + // Clear checklist area (lines 1-4, two columns) + for (int p = 1; p <= 4; p++) { + memset(&fb[p * SCR_W], 0, SCR_W); + } + + for (int i = 0; i < 8; i++) { + int col = (i < 4) ? 0 : 64; + int row = (i < 4) ? i : i - 4; + int y = 9 + row * 8; + char line[12]; + snprintf(line, sizeof(line), "%s %s", items[i].ok ? "OK" : "..", items[i].label); + fb_string(col, y, line); + } + + ssd_flush(); +} + +void display_log(const char* text, uint16_t color) { + (void)color; // SSD1306 is monochrome + + if (log_count >= LOG_LINES) { + for (int i = 0; i < LOG_LINES - 1; i++) { + strncpy(log_lines[i], log_lines[i + 1], CHARS_PER_LINE); + } + log_count = LOG_LINES - 1; + } + + strncpy(log_lines[log_count], text, CHARS_PER_LINE); + log_lines[log_count][CHARS_PER_LINE] = '\0'; + log_count++; + + // Draw log area (pages 5-6, y=40-55) + memset(&fb[5 * SCR_W], 0, SCR_W); + memset(&fb[6 * SCR_W], 0, SCR_W); + + for (int i = 0; i < log_count && i < LOG_LINES; i++) { + fb_string(0, 41 + i * 8, log_lines[i]); + } + + ssd_flush(); +} + +void display_status(const char* text) { + strncpy(status_text, text, CHARS_PER_LINE); + status_text[CHARS_PER_LINE] = '\0'; + + // Status on last page (y=56) + memset(&fb[7 * SCR_W], 0, SCR_W); + fb_string(0, 56, status_text); + ssd_flush(); +} diff --git a/examples/host/midi2_host/src/display.h b/examples/host/midi2_host/src/display.h new file mode 100644 index 000000000..a8b0a4b5e --- /dev/null +++ b/examples/host/midi2_host/src/display.h @@ -0,0 +1,54 @@ +/* + * 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 DISPLAY_H_ +#define DISPLAY_H_ + +#include +#include + +typedef struct { + bool pwr_on; + bool tusb_init; + bool bus_active; + bool device_connected; + bool descriptor_parsed; + bool alt_setting_ok; + bool mounted; + bool receiving; +} checklist_t; + +// Initialize SSD1306 display (I2C on GP4/GP5) +void display_init(void); + +// Redraw the checklist area (top portion) +void display_checklist_update(const checklist_t* ck); + +// Add a line to the scrolling log area +void display_log(const char* text, uint16_t color); + +// Update the status bar (bottom line) +void display_status(const char* text); + +#endif diff --git a/examples/host/midi2_host/src/font5x7.h b/examples/host/midi2_host/src/font5x7.h new file mode 100644 index 000000000..e515faac1 --- /dev/null +++ b/examples/host/midi2_host/src/font5x7.h @@ -0,0 +1,107 @@ +// Minimal 5x7 font, ASCII 32-126. Public domain. +// 5 bytes per character (5 columns, 7 rows, LSB=top row) + +#ifndef FONT5X7_H_ +#define FONT5X7_H_ + +#include + +static const uint8_t font5x7[] = { + 0x00,0x00,0x00,0x00,0x00, // 32 (space) + 0x00,0x00,0x5F,0x00,0x00, // 33 ! + 0x00,0x07,0x00,0x07,0x00, // 34 " + 0x14,0x7F,0x14,0x7F,0x14, // 35 # + 0x24,0x2A,0x7F,0x2A,0x12, // 36 $ + 0x23,0x13,0x08,0x64,0x62, // 37 % + 0x36,0x49,0x55,0x22,0x50, // 38 & + 0x00,0x05,0x03,0x00,0x00, // 39 ' + 0x00,0x1C,0x22,0x41,0x00, // 40 ( + 0x00,0x41,0x22,0x1C,0x00, // 41 ) + 0x08,0x2A,0x1C,0x2A,0x08, // 42 * + 0x08,0x08,0x3E,0x08,0x08, // 43 + + 0x00,0x50,0x30,0x00,0x00, // 44 , + 0x08,0x08,0x08,0x08,0x08, // 45 - + 0x00,0x60,0x60,0x00,0x00, // 46 . + 0x20,0x10,0x08,0x04,0x02, // 47 / + 0x3E,0x51,0x49,0x45,0x3E, // 48 0 + 0x00,0x42,0x7F,0x40,0x00, // 49 1 + 0x42,0x61,0x51,0x49,0x46, // 50 2 + 0x21,0x41,0x45,0x4B,0x31, // 51 3 + 0x18,0x14,0x12,0x7F,0x10, // 52 4 + 0x27,0x45,0x45,0x45,0x39, // 53 5 + 0x3C,0x4A,0x49,0x49,0x30, // 54 6 + 0x01,0x71,0x09,0x05,0x03, // 55 7 + 0x36,0x49,0x49,0x49,0x36, // 56 8 + 0x06,0x49,0x49,0x29,0x1E, // 57 9 + 0x00,0x36,0x36,0x00,0x00, // 58 : + 0x00,0x56,0x36,0x00,0x00, // 59 ; + 0x00,0x08,0x14,0x22,0x41, // 60 < + 0x14,0x14,0x14,0x14,0x14, // 61 = + 0x41,0x22,0x14,0x08,0x00, // 62 > + 0x02,0x01,0x51,0x09,0x06, // 63 ? + 0x32,0x49,0x79,0x41,0x3E, // 64 @ + 0x7E,0x11,0x11,0x11,0x7E, // 65 A + 0x7F,0x49,0x49,0x49,0x36, // 66 B + 0x3E,0x41,0x41,0x41,0x22, // 67 C + 0x7F,0x41,0x41,0x22,0x1C, // 68 D + 0x7F,0x49,0x49,0x49,0x41, // 69 E + 0x7F,0x09,0x09,0x01,0x01, // 70 F + 0x3E,0x41,0x41,0x51,0x32, // 71 G + 0x7F,0x08,0x08,0x08,0x7F, // 72 H + 0x00,0x41,0x7F,0x41,0x00, // 73 I + 0x20,0x40,0x41,0x3F,0x01, // 74 J + 0x7F,0x08,0x14,0x22,0x41, // 75 K + 0x7F,0x40,0x40,0x40,0x40, // 76 L + 0x7F,0x02,0x04,0x02,0x7F, // 77 M + 0x7F,0x04,0x08,0x10,0x7F, // 78 N + 0x3E,0x41,0x41,0x41,0x3E, // 79 O + 0x7F,0x09,0x09,0x09,0x06, // 80 P + 0x3E,0x41,0x51,0x21,0x5E, // 81 Q + 0x7F,0x09,0x19,0x29,0x46, // 82 R + 0x46,0x49,0x49,0x49,0x31, // 83 S + 0x01,0x01,0x7F,0x01,0x01, // 84 T + 0x3F,0x40,0x40,0x40,0x3F, // 85 U + 0x1F,0x20,0x40,0x20,0x1F, // 86 V + 0x7F,0x20,0x18,0x20,0x7F, // 87 W + 0x63,0x14,0x08,0x14,0x63, // 88 X + 0x03,0x04,0x78,0x04,0x03, // 89 Y + 0x61,0x51,0x49,0x45,0x43, // 90 Z + 0x00,0x00,0x7F,0x41,0x41, // 91 [ + 0x02,0x04,0x08,0x10,0x20, // 92 backslash + 0x41,0x41,0x7F,0x00,0x00, // 93 ] + 0x04,0x02,0x01,0x02,0x04, // 94 ^ + 0x40,0x40,0x40,0x40,0x40, // 95 _ + 0x00,0x01,0x02,0x04,0x00, // 96 ` + 0x20,0x54,0x54,0x54,0x78, // 97 a + 0x7F,0x48,0x44,0x44,0x38, // 98 b + 0x38,0x44,0x44,0x44,0x20, // 99 c + 0x38,0x44,0x44,0x48,0x7F, // 100 d + 0x38,0x54,0x54,0x54,0x18, // 101 e + 0x08,0x7E,0x09,0x01,0x02, // 102 f + 0x08,0x14,0x54,0x54,0x3C, // 103 g + 0x7F,0x08,0x04,0x04,0x78, // 104 h + 0x00,0x44,0x7D,0x40,0x00, // 105 i + 0x20,0x40,0x44,0x3D,0x00, // 106 j + 0x00,0x7F,0x10,0x28,0x44, // 107 k + 0x00,0x41,0x7F,0x40,0x00, // 108 l + 0x7C,0x04,0x18,0x04,0x78, // 109 m + 0x7C,0x08,0x04,0x04,0x78, // 110 n + 0x38,0x44,0x44,0x44,0x38, // 111 o + 0x7C,0x14,0x14,0x14,0x08, // 112 p + 0x08,0x14,0x14,0x18,0x7C, // 113 q + 0x7C,0x08,0x04,0x04,0x08, // 114 r + 0x48,0x54,0x54,0x54,0x20, // 115 s + 0x04,0x3F,0x44,0x40,0x20, // 116 t + 0x3C,0x40,0x40,0x20,0x7C, // 117 u + 0x1C,0x20,0x40,0x20,0x1C, // 118 v + 0x3C,0x40,0x30,0x40,0x3C, // 119 w + 0x44,0x28,0x10,0x28,0x44, // 120 x + 0x0C,0x50,0x50,0x50,0x3C, // 121 y + 0x44,0x64,0x54,0x4C,0x44, // 122 z + 0x00,0x08,0x36,0x41,0x00, // 123 { + 0x00,0x00,0x7F,0x00,0x00, // 124 | + 0x00,0x41,0x36,0x08,0x00, // 125 } + 0x10,0x08,0x08,0x10,0x08, // 126 ~ +}; + +#endif diff --git a/examples/host/midi2_host/src/main.c b/examples/host/midi2_host/src/main.c new file mode 100644 index 000000000..e23fbfc87 --- /dev/null +++ b/examples/host/midi2_host/src/main.c @@ -0,0 +1,326 @@ +/* + * 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. + */ + +// MIDI 2.0 Host Receiver Example +// +// Receives UMP from a MIDI 2.0 Device via PIO-USB Host. +// SSD1306 OLED (I2C, 128x64) shows boot checklist then received UMP messages. + +#include +#include +#include "bsp/board_api.h" +#include "tusb.h" +#include "class/midi/midi2_host.h" +#include "class/midi/midi.h" +#include "display.h" + +//--------------------------------------------------------------------+ +// Checklist state +//--------------------------------------------------------------------+ + +static checklist_t ck = { 0 }; +static uint32_t note_count = 0; +static uint8_t midi2_idx = 0xFF; // invalid until mount + +//--------------------------------------------------------------------+ +// Note name helper +//--------------------------------------------------------------------+ + +static const char* NOTE_NAMES[] = { + "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" +}; + +static void note_name(uint8_t pitch, char* buf, size_t len) { + int octave = (pitch / 12) - 1; + snprintf(buf, len, "%s%d", NOTE_NAMES[pitch % 12], octave); +} + +//--------------------------------------------------------------------+ +// UMP Message Type names +//--------------------------------------------------------------------+ + +static const char* mt_name(uint8_t mt) { + switch (mt) { + case 0x0: return "Utility"; + case 0x1: return "System"; + case 0x2: return "M1 CVM"; + case 0x3: return "SysEx7"; + case 0x4: return "M2 CVM"; + case 0x5: return "SysEx8"; + case 0xD: return "Flex"; + case 0xF: return "Stream"; + default: return "?"; + } +} + +//--------------------------------------------------------------------+ +// UMP decoder - extract and display MIDI 2.0 messages +//--------------------------------------------------------------------+ + +static void decode_ump(const uint32_t* words, uint8_t word_count) { + uint8_t mt = (uint8_t)((words[0] >> 28) & 0x0F); + char line[64]; + + if (mt == 0x4 && word_count >= 2) { + // MIDI 2.0 Channel Voice Message + uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F); + uint8_t channel = (uint8_t)((words[0] >> 16) & 0x0F); + + switch (status) { + case 0x9: { // Note On + uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); + uint16_t vel = (uint16_t)((words[1] >> 16) & 0xFFFF); + char nn[6]; + note_name(pitch, nn, sizeof(nn)); + snprintf(line, sizeof(line), "NoteOn %s ch%u vel=0x%04X", nn, channel, vel); + display_log(line, 0x07E0); // green + note_count++; + break; + } + case 0x8: { // Note Off + uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); + char nn[6]; + note_name(pitch, nn, sizeof(nn)); + snprintf(line, sizeof(line), "NoteOff %s ch%u", nn, channel); + display_log(line, 0x8410); // grey + break; + } + case 0xB: { // CC + uint8_t idx = (uint8_t)((words[0] >> 8) & 0x7F); + snprintf(line, sizeof(line), "CC%-3u = 0x%08lX", idx, (unsigned long)words[1]); + display_log(line, 0x001F); // blue + break; + } + case 0xC: { // Program Change + uint8_t prog = (uint8_t)((words[1] >> 24) & 0x7F); + snprintf(line, sizeof(line), "ProgChg %u", prog); + display_log(line, 0xFFE0); // yellow + break; + } + case 0xE: { // Pitch Bend + snprintf(line, sizeof(line), "PBend = 0x%08lX", (unsigned long)words[1]); + display_log(line, 0xF81F); // magenta + break; + } + case 0xD: { // Channel Pressure + snprintf(line, sizeof(line), "CPress = 0x%08lX", (unsigned long)words[1]); + display_log(line, 0xFC10); // orange + break; + } + case 0xA: { // Poly Pressure + uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); + char nn[6]; + note_name(pitch, nn, sizeof(nn)); + snprintf(line, sizeof(line), "PolyP %s = 0x%08lX", nn, (unsigned long)words[1]); + display_log(line, 0xFC10); + break; + } + case 0xF: { // Per-Note Management + uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); + uint8_t flags = (uint8_t)(words[0] & 0xFF); + snprintf(line, sizeof(line), "PN-Mgmt note=%u flags=0x%02X", pitch, flags); + display_log(line, 0x07FF); // cyan + break; + } + default: { + snprintf(line, sizeof(line), "M2CVM status=0x%X", status); + display_log(line, 0xFFFF); + break; + } + } + } else if (mt == 0x0) { + // Utility (JR Timestamp, NOOP) + uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F); + if (status == 0x2) { + uint16_t ts = words[0] & 0xFFFF; + snprintf(line, sizeof(line), "JR-TS = 0x%04X", ts); + display_log(line, 0x8410); + } + } else { + snprintf(line, sizeof(line), "MT=0x%X (%s) w0=0x%08lX", + mt, mt_name(mt), (unsigned long)words[0]); + display_log(line, 0xFFFF); + } +} + +//--------------------------------------------------------------------+ +// MIDI 2.0 Host Callbacks +//--------------------------------------------------------------------+ + +void tuh_midi2_descriptor_cb(uint8_t idx, const tuh_midi2_descriptor_cb_t* d) { + (void)idx; + ck.descriptor_parsed = true; + char line[48]; + + snprintf(line, sizeof(line), "bcdMSC=0x%02X%02X proto=%s", + d->bcdMSC_hi, d->bcdMSC_lo, + d->protocol_version ? "MIDI2" : "MIDI1"); + display_log(line, 0x07E0); + + snprintf(line, sizeof(line), "Cables: RX=%u TX=%u", + d->rx_cable_count, d->tx_cable_count); + display_log(line, 0x07E0); + + display_checklist_update(&ck); +} + +void tuh_midi2_mount_cb(uint8_t idx, const tuh_midi2_mount_cb_t* m) { + midi2_idx = idx; + ck.alt_setting_ok = true; + ck.mounted = true; + + char line[48]; + snprintf(line, sizeof(line), "Mounted addr=%u alt=%u", + m->daddr, m->alt_setting_active); + display_log(line, 0x07E0); + + if (m->protocol_version) { + display_log("MIDI 2.0 ready", 0x07E0); + } else { + display_log("MIDI 1.0 only", 0xFFE0); + } + + display_checklist_update(&ck); +} + +void tuh_midi2_rx_cb(uint8_t idx, uint32_t xferred_bytes) { + (void)xferred_bytes; + if (!ck.receiving) { + ck.receiving = true; + display_checklist_update(&ck); + } + + uint32_t words[16]; + while (1) { + uint32_t n = tuh_midi2_ump_read(idx, words, 16); + if (n == 0) break; + + // Decode complete UMP messages + 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; + decode_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; + ck.device_connected = false; + ck.descriptor_parsed = false; + ck.alt_setting_ok = false; + ck.mounted = false; + ck.receiving = false; + note_count = 0; + + display_log("Device disconnected", 0xF800); + display_checklist_update(&ck); +} + +//--------------------------------------------------------------------+ +// Main +//--------------------------------------------------------------------+ + +int main(void) { + board_init(); + + display_init(); + + ck.pwr_on = true; + display_checklist_update(&ck); + + // Init TinyUSB Host (BSP handles PIO-USB configuration via tuh_configure) + tusb_rhport_init_t host_init = { + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_FULL, + }; + tusb_init(BOARD_TUH_RHPORT, &host_init); + + ck.tusb_init = true; + ck.bus_active = true; + display_checklist_update(&ck); + + char dbg[40]; + snprintf(dbg, sizeof(dbg), "RHPORT=%u PIO_USB=%u", + BOARD_TUH_RHPORT, CFG_TUH_RPI_PIO_USB); + display_log(dbg, 0xFFE0); // yellow + display_status("Waiting for device..."); + + uint32_t last_status_ms = 0; + static uint32_t loop_count = 0; + + while (1) { + tuh_task(); + loop_count++; + + // Update status every 2 seconds + uint32_t now = tusb_time_millis_api(); + if (now - last_status_ms > 2000) { + last_status_ms = now; + char line[22]; + if (ck.receiving) { + snprintf(line, sizeof(line), "Notes:%lu", (unsigned long)note_count); + } else if (ck.mounted) { + snprintf(line, sizeof(line), "Mounted OK!"); + } else if (ck.device_connected) { + snprintf(line, sizeof(line), "Dev found, mounting.."); + } else { + snprintf(line, sizeof(line), "Wait.. t=%lu", (unsigned long)(now/1000)); + } + display_status(line); + } + } + + return 0; +} + +// Generic USB device mount/unmount (any class) +void tuh_mount_cb(uint8_t daddr) { + char line[32]; + uint16_t vid, pid; + tuh_vid_pid_get(daddr, &vid, &pid); + snprintf(line, sizeof(line), "USB %04X:%04X a%u", vid, pid, daddr); + display_log(line, 0x07E0); + ck.device_connected = true; + display_checklist_update(&ck); +} + +void tuh_umount_cb(uint8_t daddr) { + (void)daddr; + display_log("USB disconnected", 0xF800); + ck.device_connected = false; + ck.descriptor_parsed = false; + ck.alt_setting_ok = false; + ck.mounted = false; + ck.receiving = false; + display_checklist_update(&ck); +} diff --git a/examples/host/midi2_host/src/tusb_config.h b/examples/host/midi2_host/src/tusb_config.h new file mode 100644 index 000000000..650df97e1 --- /dev/null +++ b/examples/host/midi2_host/src/tusb_config.h @@ -0,0 +1,91 @@ +/* + * 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 + +#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 +// Waveshare RP2350-USB-A: PIO-USB on GP12/GP13 (USB-A Host port) +//-------------------------------------------------------------------- + +#define CFG_TUH_ENABLED 1 + +// PIO-USB Host on rhport 1 (USB-A connector on GP12/GP13) +#define CFG_TUH_RPI_PIO_USB 1 +#define BOARD_TUH_RHPORT 1 + +#ifndef BOARD_TUH_MAX_SPEED +#define BOARD_TUH_MAX_SPEED OPT_MODE_FULL_SPEED +#endif + +#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED + +//-------------------------------------------------------------------- +// Driver Configuration +//-------------------------------------------------------------------- + +#define CFG_TUH_ENUMERATION_BUFSIZE 256 + +#define CFG_TUH_HUB 0 +#define CFG_TUH_DEVICE_MAX 1 + +// MIDI 2.0 Host +#define CFG_TUH_MIDI2 1 +#define CFG_TUH_MIDI2_RX_BUFSIZE 512 +#define CFG_TUH_MIDI2_TX_BUFSIZE 512 + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.3.1 From fa9edeff9c00ee1fc4ee7ab9938b1a5955a6281a Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Wed, 25 Mar 2026 07:05:56 -0300 Subject: fix: address PR review feedback for MIDI 2.0 drivers Host driver (midi2_host.c): - midih2_open() now returns actual parsed length instead of max_len, preventing composite device interface conflicts - Parsers (alt0/alt1) refactored to return const uint8_t* end pointer following midi_host.c switch/case pattern - Alt 1 CS Endpoint now parses MIDI 2.0 layout (bNumGrpTrmBlk at offset 3 with MIDI_CS_ENDPOINT_GENERAL_2_0 subtype check) instead of reusing MIDI 1.0 struct (bNumEmbMIDIJack) - midih2_set_config() now issues SET_INTERFACE control request via tuh_interface_set() before completing configuration. Falls back to alt 0 if SET_INTERFACE fails - Extracted midih2_set_config_complete() and midih2_set_interface_cb() for async SET_INTERFACE handling Device driver (midi2_device.c): - midi2d_open() skip loop now checks bInterfaceNumber, stopping at interfaces that belong to other functions in composite devices - SET_INTERFACE handler now rejects alt > 1 (returns false/stall) - Named constants for GTB descriptor types and MIDI protocol values Descriptor macros (usbd.h): - TUD_MIDI2_DESC_ALT1_HEAD: iInterface set to 0 (consistent with Alt 0), wTotalLength now uses TUD_MIDI2_DESC_ALT1_CS_LEN to cover all Alt 1 class-specific descriptors - TUD_MIDI2_DESC_ALT1_EP: now accepts GTB ID list via variadic args, emitting complete CS endpoint descriptor Host example: - CMakeLists.txt restricted to rp2040 family (display.c requires Pico SDK headers) - display.c: null terminator after strncpy in log scroll Documentation: - class_drivers.rst updated to reflect SET_INTERFACE behavior and auto-select with fallback Addresses: Codex P1 (#1, #2, #3), Copilot (#4-#9) --- docs/reference/class_drivers.rst | 8 +- examples/host/midi2_host/CMakeLists.txt | 3 +- examples/host/midi2_host/src/display.c | 1 + src/class/midi/midi2_device.c | 14 ++- src/class/midi/midi2_host.c | 213 ++++++++++++++++++++------------ src/device/usbd.h | 24 ++-- 6 files changed, 166 insertions(+), 97 deletions(-) (limited to 'examples/host') diff --git a/docs/reference/class_drivers.rst b/docs/reference/class_drivers.rst index 9ed332acb..3ac0d8d4e 100644 --- a/docs/reference/class_drivers.rst +++ b/docs/reference/class_drivers.rst @@ -87,7 +87,7 @@ The MIDI 2.0 Host driver enables TinyUSB to enumerate and communicate with USB M **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) +- **Auto-Selection**: Automatically selects the highest available protocol and issues SET_INTERFACE to activate Alt Setting 1 when MIDI 2.0 is detected - **Transparent Stream Messages**: All data (UMP packets + Stream Messages) flow through callbacks - **Memory Safe**: No dynamic allocation, fixed-size instances per device @@ -271,9 +271,9 @@ 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 +- **Auto-Selection**: Selects highest protocol available and issues SET_INTERFACE +- **Transparent I/O**: Stream Messages and UMP packets flow through callbacks +- **Callback-Driven**: App receives events via callbacks (descriptor, mount, rx, tx, unmount) Differences from MIDI 1.0 Host ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/examples/host/midi2_host/CMakeLists.txt b/examples/host/midi2_host/CMakeLists.txt index 221de7adf..cd70d122a 100644 --- a/examples/host/midi2_host/CMakeLists.txt +++ b/examples/host/midi2_host/CMakeLists.txt @@ -6,7 +6,8 @@ project(midi2_host C CXX ASM) family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}) -if(FAMILY STREQUAL "espressif") +# This example requires PIO-USB and Pico SDK (I2C, SSD1306 display) +if(NOT FAMILY STREQUAL "rp2040") return() endif() diff --git a/examples/host/midi2_host/src/display.c b/examples/host/midi2_host/src/display.c index 6d81bcd5c..4745c2961 100644 --- a/examples/host/midi2_host/src/display.c +++ b/examples/host/midi2_host/src/display.c @@ -190,6 +190,7 @@ void display_log(const char* text, uint16_t color) { if (log_count >= LOG_LINES) { for (int i = 0; i < LOG_LINES - 1; i++) { strncpy(log_lines[i], log_lines[i + 1], CHARS_PER_LINE); + log_lines[i][CHARS_PER_LINE] = '\0'; } log_count = LOG_LINES - 1; } diff --git a/src/class/midi/midi2_device.c b/src/class/midi/midi2_device.c index 9363aac11..aecbda4c5 100644 --- a/src/class/midi/midi2_device.c +++ b/src/class/midi/midi2_device.c @@ -505,12 +505,19 @@ uint16_t midi2d_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint } // Skip remaining descriptors (alt setting 1, CS endpoints, GTB) + // Stop at any interface descriptor that is not our MIDI Streaming alt setting while (tu_desc_in_bounds(p_desc, desc_end)) { uint8_t dtype = tu_desc_type(p_desc); - if (dtype != TUSB_DESC_CS_INTERFACE && dtype != TUSB_DESC_CS_ENDPOINT && - dtype != TUSB_DESC_INTERFACE && dtype != TUSB_DESC_ENDPOINT) { + + if (dtype == TUSB_DESC_INTERFACE) { + const tusb_desc_interface_t* next_itf = (const tusb_desc_interface_t*) p_desc; + // Continue only if this is an alternate setting of our own interface + if (next_itf->bInterfaceNumber != desc_midi->bInterfaceNumber) break; + } else if (dtype != TUSB_DESC_CS_INTERFACE && dtype != TUSB_DESC_CS_ENDPOINT && + dtype != TUSB_DESC_ENDPOINT) { break; } + p_desc = tu_desc_next(p_desc); } @@ -528,6 +535,9 @@ bool midi2d_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_re uint8_t itf_num = tu_u16_low(request->wIndex); uint8_t alt = tu_u16_low(request->wValue); + // Only Alt Setting 0 (MIDI 1.0) and 1 (UMP) are valid + if (alt > 1) return false; + uint8_t idx = find_midi2_itf_by_num(itf_num); if (idx >= CFG_TUD_MIDI2) return false; diff --git a/src/class/midi/midi2_host.c b/src/class/midi/midi2_host.c index beb441b56..5b9f98f8b 100644 --- a/src/class/midi/midi2_host.c +++ b/src/class/midi/midi2_host.c @@ -125,9 +125,10 @@ static inline uint8_t get_idx_by_ep_addr(uint8_t daddr, uint8_t ep_addr) { // Descriptor parsing //--------------------------------------------------------------------+ -static void midih2_parse_descriptors_alt0(midih2_interface_t *p_midi, +// Parse Alt Setting 0 (MIDI 1.0) descriptors. Returns pointer past last consumed descriptor. +static const uint8_t* midih2_parse_descriptors_alt0(midih2_interface_t *p_midi, const tusb_desc_interface_t *desc_itf, const uint8_t *desc_end) { - TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass,); + TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass, NULL); p_midi->bInterfaceNumber = desc_itf->bInterfaceNumber; @@ -136,93 +137,113 @@ static void midih2_parse_descriptors_alt0(midih2_interface_t *p_midi, uint8_t rx_cable_count = 0; uint8_t tx_cable_count = 0; + bool found_new_interface = false; - while (tu_desc_in_bounds(p_desc, desc_end)) { - if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) { - break; - } - - if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT) { - const tusb_desc_endpoint_t *p_ep = (const tusb_desc_endpoint_t *) p_desc; - - // Open endpoint and stream - TU_ASSERT(tuh_edpt_open(p_midi->daddr, p_ep),); - if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_IN) { - tu_edpt_stream_open(&p_midi->ep_stream.rx, p_midi->daddr, p_ep, tu_edpt_packet_size(p_ep)); - tu_edpt_stream_clear(&p_midi->ep_stream.rx); - } else { - tu_edpt_stream_open(&p_midi->ep_stream.tx, p_midi->daddr, p_ep, tu_edpt_packet_size(p_ep)); - tu_edpt_stream_clear(&p_midi->ep_stream.tx); - } - - p_desc = tu_desc_next(p_desc); + while (tu_desc_in_bounds(p_desc, desc_end) && !found_new_interface) { + switch (tu_desc_type(p_desc)) { + case TUSB_DESC_INTERFACE: + found_new_interface = true; + break; - if (tu_desc_in_bounds(p_desc, desc_end) && tu_desc_type(p_desc) == TUSB_DESC_CS_ENDPOINT) { - const midi_desc_cs_endpoint_t *p_csep = (const midi_desc_cs_endpoint_t *) p_desc; + case TUSB_DESC_ENDPOINT: { + const tusb_desc_endpoint_t *p_ep = (const tusb_desc_endpoint_t *) p_desc; - if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { - tx_cable_count = p_csep->bNumEmbMIDIJack; + TU_ASSERT(tuh_edpt_open(p_midi->daddr, p_ep), NULL); + if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_IN) { + tu_edpt_stream_open(&p_midi->ep_stream.rx, p_midi->daddr, p_ep, tu_edpt_packet_size(p_ep)); + tu_edpt_stream_clear(&p_midi->ep_stream.rx); } else { - rx_cable_count = p_csep->bNumEmbMIDIJack; + tu_edpt_stream_open(&p_midi->ep_stream.tx, p_midi->daddr, p_ep, tu_edpt_packet_size(p_ep)); + tu_edpt_stream_clear(&p_midi->ep_stream.tx); + } + + p_desc = tu_desc_next(p_desc); + if (tu_desc_in_bounds(p_desc, desc_end) && tu_desc_type(p_desc) == TUSB_DESC_CS_ENDPOINT) { + const midi_desc_cs_endpoint_t *p_csep = (const midi_desc_cs_endpoint_t *) p_desc; + if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { + tx_cable_count = p_csep->bNumEmbMIDIJack; + } else { + rx_cable_count = p_csep->bNumEmbMIDIJack; + } } + break; } + + default: + break; } - p_desc = tu_desc_next(p_desc); + if (!found_new_interface) { + p_desc = tu_desc_next(p_desc); + } } p_midi->rx_cable_count_alt0 = rx_cable_count; p_midi->tx_cable_count_alt0 = tx_cable_count; + return p_desc; } -static void midih2_parse_descriptors_alt1(midih2_interface_t *p_midi, +// Parse Alt Setting 1 (MIDI 2.0 UMP) descriptors. Returns pointer past last consumed descriptor. +static const uint8_t* midih2_parse_descriptors_alt1(midih2_interface_t *p_midi, const tusb_desc_interface_t *desc_itf, const uint8_t *desc_end) { - TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass,); - TU_VERIFY(desc_itf->bAlternateSetting == 1,); + TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass, NULL); + TU_VERIFY(desc_itf->bAlternateSetting == 1, NULL); const uint8_t *p_desc = (const uint8_t *) desc_itf; p_desc = tu_desc_next(p_desc); uint8_t rx_cable_count = 0; uint8_t tx_cable_count = 0; + bool found_new_interface = false; - while (tu_desc_in_bounds(p_desc, desc_end)) { - if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) { - break; - } + while (tu_desc_in_bounds(p_desc, desc_end) && !found_new_interface) { + switch (tu_desc_type(p_desc)) { + case TUSB_DESC_INTERFACE: + found_new_interface = true; + break; - if (tu_desc_type(p_desc) == TUSB_DESC_CS_INTERFACE) { - if (tu_desc_subtype(p_desc) == MIDI_CS_INTERFACE_HEADER) { - const uint8_t *bcd_ptr = p_desc + 3; - p_midi->bcdMSC_lo = bcd_ptr[0]; - p_midi->bcdMSC_hi = bcd_ptr[1]; + case TUSB_DESC_CS_INTERFACE: + if (tu_desc_subtype(p_desc) == MIDI_CS_INTERFACE_HEADER) { + // bcdMSC at offset 3-4 in CS Interface Header + const uint8_t *bcd_ptr = p_desc + 3; + p_midi->bcdMSC_lo = bcd_ptr[0]; + p_midi->bcdMSC_hi = bcd_ptr[1]; + if (p_midi->bcdMSC_hi == 0x02) { // bcdMSC 0x0200 = USB-MIDI 2.0 + p_midi->protocol_version = 1; + } + } + break; - if (p_midi->bcdMSC_hi == 0x02) { - p_midi->protocol_version = 1; + case TUSB_DESC_ENDPOINT: { + const tusb_desc_endpoint_t *p_ep = (const tusb_desc_endpoint_t *) p_desc; + p_desc = tu_desc_next(p_desc); + + if (tu_desc_in_bounds(p_desc, desc_end) && tu_desc_type(p_desc) == TUSB_DESC_CS_ENDPOINT) { + // MIDI 2.0 CS Endpoint General 2.0: bNumGrpTrmBlk at offset 3 + if (p_desc[0] >= 4 && p_desc[2] == MIDI_CS_ENDPOINT_GENERAL_2_0) { + uint8_t num_grp_trm_blk = p_desc[3]; + if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { + tx_cable_count = num_grp_trm_blk; + } else { + rx_cable_count = num_grp_trm_blk; + } + } } + break; } + + default: + break; } - if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT) { - const tusb_desc_endpoint_t *p_ep = (const tusb_desc_endpoint_t *) p_desc; + if (!found_new_interface) { p_desc = tu_desc_next(p_desc); - - if (tu_desc_in_bounds(p_desc, desc_end) && tu_desc_type(p_desc) == TUSB_DESC_CS_ENDPOINT) { - const midi_desc_cs_endpoint_t *p_csep = (const midi_desc_cs_endpoint_t *) p_desc; - - if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { - tx_cable_count = p_csep->bNumEmbMIDIJack; - } else { - rx_cable_count = p_csep->bNumEmbMIDIJack; - } - } } - - p_desc = tu_desc_next(p_desc); } p_midi->rx_cable_count_alt1 = rx_cable_count; p_midi->tx_cable_count_alt1 = tx_cable_count; + return p_desc; } //--------------------------------------------------------------------+ @@ -327,33 +348,20 @@ uint16_t midih2_open(uint8_t rhport, uint8_t dev_addr, const tusb_desc_interface desc_itf->bInterfaceNumber, desc_itf->bAlternateSetting, dev_addr); // Dispatch to appropriate parser based on Alt Setting + const uint8_t *p_end = NULL; if (desc_itf->bAlternateSetting == 0) { - midih2_parse_descriptors_alt0(p_midi, desc_itf, desc_end); + p_end = midih2_parse_descriptors_alt0(p_midi, desc_itf, desc_end); } else if (desc_itf->bAlternateSetting == 1) { - midih2_parse_descriptors_alt1(p_midi, desc_itf, desc_end); + p_end = midih2_parse_descriptors_alt1(p_midi, desc_itf, desc_end); } - return max_len; + // Return number of bytes consumed (following midi_host.c pattern) + uint16_t const parsed_len = (p_end != NULL) ? (uint16_t)(p_end - desc_start) : 0; + return parsed_len; } -bool midih2_set_config(uint8_t dev_addr, uint8_t itf_num) { - uint8_t idx = 0; - for (idx = 0; idx < CFG_TUH_MIDI2; idx++) { - if (_midi2_host[idx].daddr == dev_addr && _midi2_host[idx].bInterfaceNumber == itf_num) { - break; - } - } - - if (idx >= CFG_TUH_MIDI2) { - // Not our interface (e.g. Audio Control) - pass through to next - usbh_driver_set_config_complete(dev_addr, itf_num); - return true; - } - - midih2_interface_t *p_midi = &_midi2_host[idx]; - - // Auto-select alt setting - midih2_auto_select_alt_setting(p_midi); +static void midih2_set_config_complete(midih2_interface_t *p_midi, uint8_t idx) { + uint8_t dev_addr = p_midi->daddr; // Invoke descriptor_cb tuh_midi2_descriptor_cb_t desc_cb = { @@ -374,7 +382,7 @@ bool midih2_set_config(uint8_t dev_addr, uint8_t itf_num) { // Invoke mount_cb tuh_midi2_mount_cb_t mount_cb = { - .daddr = p_midi->daddr, + .daddr = dev_addr, .bInterfaceNumber = p_midi->bInterfaceNumber, .protocol_version = p_midi->protocol_version, .alt_setting_active = p_midi->alt_setting_current, @@ -387,7 +395,56 @@ bool midih2_set_config(uint8_t dev_addr, uint8_t itf_num) { tu_edpt_stream_read_xfer(&p_midi->ep_stream.rx); // Signal USBH that configuration is complete - usbh_driver_set_config_complete(dev_addr, itf_num); + usbh_driver_set_config_complete(dev_addr, p_midi->bInterfaceNumber); +} + +static void midih2_set_interface_cb(tuh_xfer_t *xfer) { + uint8_t const dev_addr = xfer->daddr; + uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); + + // Find our interface + for (uint8_t idx = 0; idx < CFG_TUH_MIDI2; idx++) { + if (_midi2_host[idx].daddr == dev_addr && _midi2_host[idx].bInterfaceNumber == itf_num) { + if (xfer->result == XFER_RESULT_SUCCESS) { + midih2_set_config_complete(&_midi2_host[idx], idx); + } else { + // SET_INTERFACE failed, fall back to alt 0 + TU_LOG_DRV("MIDI2 SET_INTERFACE failed, falling back to alt 0\r\n"); + _midi2_host[idx].alt_setting_current = 0; + midih2_set_config_complete(&_midi2_host[idx], idx); + } + return; + } + } +} + +bool midih2_set_config(uint8_t dev_addr, uint8_t itf_num) { + uint8_t idx = 0; + for (idx = 0; idx < CFG_TUH_MIDI2; idx++) { + if (_midi2_host[idx].daddr == dev_addr && _midi2_host[idx].bInterfaceNumber == itf_num) { + break; + } + } + + if (idx >= CFG_TUH_MIDI2) { + // Not our interface (e.g. Audio Control) - pass through to next + usbh_driver_set_config_complete(dev_addr, itf_num); + return true; + } + + midih2_interface_t *p_midi = &_midi2_host[idx]; + + // Auto-select alt setting + midih2_auto_select_alt_setting(p_midi); + + // If MIDI 2.0 detected, issue SET_INTERFACE to activate Alt Setting 1 + if (p_midi->alt_setting_current == 1) { + TU_LOG_DRV("MIDI2 requesting SET_INTERFACE alt 1 for itf %u\r\n", itf_num); + TU_ASSERT(tuh_interface_set(dev_addr, itf_num, 1, midih2_set_interface_cb, 0)); + } else { + // MIDI 1.0 only, complete immediately + midih2_set_config_complete(p_midi, idx); + } return true; } diff --git a/src/device/usbd.h b/src/device/usbd.h index af37eff56..a9f4c5f08 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -429,18 +429,20 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ //--------------------------------------------------------------------+ // Alt Setting 1: MS Interface + MS Header (bcdMSC=0x0200) +// wTotalLength covers MS Header + all CS Endpoint descriptors +#define TUD_MIDI2_DESC_ALT1_CS_LEN(_numgtbs) (7 + (4 + (_numgtbs)) * 2) #define TUD_MIDI2_DESC_ALT1_HEAD_LEN (9 + 7) -#define TUD_MIDI2_DESC_ALT1_HEAD(_itfnum, _stridx) \ +#define TUD_MIDI2_DESC_ALT1_HEAD(_itfnum, _stridx, _numgtbs) \ /* MIDI Streaming Interface, Alt Setting 1 */\ - 9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum) + 1), 1, 2, TUSB_CLASS_AUDIO, AUDIO_SUBCLASS_MIDI_STREAMING, AUDIO_FUNC_PROTOCOL_CODE_UNDEF, _stridx,\ - /* MS Header (MIDI 2.0) */\ - 7, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_HEADER, U16_TO_U8S_LE(0x0200), U16_TO_U8S_LE(7) + 9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum) + 1), 1, 2, TUSB_CLASS_AUDIO, AUDIO_SUBCLASS_MIDI_STREAMING, AUDIO_FUNC_PROTOCOL_CODE_UNDEF, 0,\ + /* MS Header (MIDI 2.0): wTotalLength = header + 2x CS Endpoint */\ + 7, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_HEADER, U16_TO_U8S_LE(0x0200), U16_TO_U8S_LE(TUD_MIDI2_DESC_ALT1_CS_LEN(_numgtbs)) -// Alt Setting 1: Standard USB Endpoint (7 bytes) + CS Endpoint (subtype 0x02) +// Alt Setting 1: Standard USB Endpoint (7 bytes) + CS Endpoint General 2.0 #define TUD_MIDI2_DESC_ALT1_EP_LEN(_numgtbs) (7 + 4 + (_numgtbs)) -#define TUD_MIDI2_DESC_ALT1_EP(_ep, _epsize, _numgtbs) \ +#define TUD_MIDI2_DESC_ALT1_EP(_ep, _epsize, _numgtbs, ...) \ 7, TUSB_DESC_ENDPOINT, _ep, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \ - (uint8_t)(4 + (_numgtbs)), TUSB_DESC_CS_ENDPOINT, MIDI_CS_ENDPOINT_GENERAL_2_0, _numgtbs + (uint8_t)(4 + (_numgtbs)), TUSB_DESC_CS_ENDPOINT, MIDI_CS_ENDPOINT_GENERAL_2_0, _numgtbs, ## __VA_ARGS__ // Total length: Alt 0 (MIDI 1.0) + Alt 1 (UMP) #define TUD_MIDI2_DESC_LEN (TUD_MIDI_DESC_LEN + TUD_MIDI2_DESC_ALT1_HEAD_LEN + TUD_MIDI2_DESC_ALT1_EP_LEN(1) * 2) @@ -455,11 +457,9 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ TUD_MIDI_DESC_EP(_epin, _epsize, 1),\ TUD_MIDI_JACKID_OUT_EMB(1),\ /* Alt Setting 1 (UMP) */\ - TUD_MIDI2_DESC_ALT1_HEAD(_itfnum, _stridx),\ - TUD_MIDI2_DESC_ALT1_EP(_epout, _epsize, 1),\ - 1, /* bAssoGrpTrmBlkID = 1 */\ - TUD_MIDI2_DESC_ALT1_EP(_epin, _epsize, 1),\ - 1 /* bAssoGrpTrmBlkID = 1 */ + TUD_MIDI2_DESC_ALT1_HEAD(_itfnum, _stridx, 1),\ + TUD_MIDI2_DESC_ALT1_EP(_epout, _epsize, 1, 1 /* bAssoGrpTrmBlkID */),\ + TUD_MIDI2_DESC_ALT1_EP(_epin, _epsize, 1, 1 /* bAssoGrpTrmBlkID */) //--------------------------------------------------------------------+ // Audio Descriptor Templates -- cgit v1.3.1 From f8a5fc37f3472cb2e3eb584d58e9cf4588dc4329 Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Wed, 25 Mar 2026 07:29:10 -0300 Subject: fix: CI build failures on non-RP2040 platforms - Use UINT32_C(1) instead of 1u for bit shifts >= 16 in midi2_device.c to avoid shift-count-overflow on 16-bit platforms (MSP430) - Add Makefiles for midi2_device and midi2_host examples with family guard (skip if FAMILY != rp2040). These examples require Pico SDK and board-specific hardware - Restrict midi2_device CMakeLists.txt to rp2040 family (matching midi2_host) --- examples/device/midi2_device/CMakeLists.txt | 4 ++-- examples/device/midi2_device/Makefile | 27 +++++++++++++++++++++++++++ examples/host/midi2_host/Makefile | 27 +++++++++++++++++++++++++++ src/class/midi/midi2_device.c | 8 ++++---- 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 examples/device/midi2_device/Makefile create mode 100644 examples/host/midi2_host/Makefile (limited to 'examples/host') diff --git a/examples/device/midi2_device/CMakeLists.txt b/examples/device/midi2_device/CMakeLists.txt index 295af6550..3f4a876c9 100644 --- a/examples/device/midi2_device/CMakeLists.txt +++ b/examples/device/midi2_device/CMakeLists.txt @@ -7,8 +7,8 @@ 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") +# This example requires RP2040/RP2350 (USB descriptors and config are board-specific) +if(NOT FAMILY STREQUAL "rp2040") return() endif() diff --git a/examples/device/midi2_device/Makefile b/examples/device/midi2_device/Makefile new file mode 100644 index 000000000..09f069c4f --- /dev/null +++ b/examples/device/midi2_device/Makefile @@ -0,0 +1,27 @@ +# This example requires RP2040/RP2350 (USB descriptors and config are board-specific) +ifeq (,$(findstring rp2040,$(FAMILY))) +$(info Skipping midi2_device: requires FAMILY=rp2040) +all: + @: +.DEFAULT: + @: +else + +include ../../../hw/bsp/family_support.mk + +INC += \ + src \ + +# Example source +EXAMPLE_SOURCE += \ + src/main.c \ + src/usb_descriptors.c \ + +SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE)) + +# Suppress pre-existing warning in usbd.c +CFLAGS_GCC += -Wno-type-limits + +include ../../../hw/bsp/family_rules.mk + +endif diff --git a/examples/host/midi2_host/Makefile b/examples/host/midi2_host/Makefile new file mode 100644 index 000000000..2b4660516 --- /dev/null +++ b/examples/host/midi2_host/Makefile @@ -0,0 +1,27 @@ +# This example requires RP2040/RP2350 (PIO-USB, Pico SDK I2C, SSD1306 display) +ifeq (,$(findstring rp2040,$(FAMILY))) +$(info Skipping midi2_host: requires FAMILY=rp2040) +all: + @: +.DEFAULT: + @: +else + +include ../../../hw/bsp/family_support.mk + +INC += \ + src \ + +# Example source +EXAMPLE_SOURCE += \ + src/main.c \ + src/display.c \ + +SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE)) + +# Suppress pre-existing warning +CFLAGS_GCC += -Wno-type-limits + +include ../../../hw/bsp/family_rules.mk + +endif diff --git a/src/class/midi/midi2_device.c b/src/class/midi/midi2_device.c index aecbda4c5..0029737ce 100644 --- a/src/class/midi/midi2_device.c +++ b/src/class/midi/midi2_device.c @@ -158,10 +158,10 @@ static void _nego_send_endpoint_info(midi2d_interface_t* p_midi) { | ((uint32_t) STREAM_ENDPOINT_INFO << 16) | ((uint32_t) UMP_VER_MAJOR << 8) | (uint32_t) UMP_VER_MINOR; - msg[1] = (1u << 31) // Static Function Blocks flag + msg[1] = (UINT32_C(1) << 31) // Static Function Blocks flag | ((uint32_t)(CFG_TUD_MIDI2_NUM_FUNCTION_BLOCKS & 0x7F) << 24) - | (1u << 9) // MIDI 2.0 Protocol capability - | (1u << 8); // MIDI 1.0 Protocol capability + | (UINT32_C(1) << 9) // MIDI 2.0 Protocol capability + | (UINT32_C(1) << 8); // MIDI 1.0 Protocol capability _nego_send_ump(p_midi, msg, 4); } @@ -214,7 +214,7 @@ static void _nego_send_fb_info(midi2d_interface_t* p_midi, uint8_t fb_idx) { uint32_t msg[4] = {0}; msg[0] = ((uint32_t) MT_STREAM << 28) | ((uint32_t) STREAM_FB_INFO << 16) - | (1u << 15) + | (UINT32_C(1) << 15) | ((uint32_t) fb_idx << 8) | 0x02; // bDirection: bidirectional msg[1] = ((uint32_t) 0 << 24) // bFirstGroup -- cgit v1.3.1 From 13c6f6a2c7aca7165161d9f32d10087c91aa61aa Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Tue, 7 Apr 2026 22:50:29 -0300 Subject: example: add MIDI 2.0 Host example for Adafruit Feather RP2040 USB Host Board-specific variant of midi2_host targeting the Adafruit Feather RP2040 with USB Type A Host (product 5723). Hardware differences from the Waveshare RP2350-USB-A example: - PIO-USB on GP16/GP17 (vs GP12/GP13) - I2C1 via STEMMA QT on GP2/GP3 (vs I2C0 GP4/GP5) - USB Host 5V power enable on GP18 Display uses three phases: splash screen, spinner while waiting for a device, then live scrolling UMP message view with 6 visible lines. Tested board-to-board: RP2040 Pico (Device) to Feather RP2040 (Host), SSD1306 128x64 OLED showing decoded MIDI 2.0 messages in real time. --- examples/host/midi2_host_feather/CMakeLists.txt | 38 +++ examples/host/midi2_host_feather/Makefile | 1 + examples/host/midi2_host_feather/src/display.c | 276 +++++++++++++++++++++ examples/host/midi2_host_feather/src/display.h | 46 ++++ examples/host/midi2_host_feather/src/font5x7.h | 107 ++++++++ examples/host/midi2_host_feather/src/main.c | 267 ++++++++++++++++++++ examples/host/midi2_host_feather/src/tusb_config.h | 91 +++++++ 7 files changed, 826 insertions(+) create mode 100644 examples/host/midi2_host_feather/CMakeLists.txt create mode 100644 examples/host/midi2_host_feather/Makefile create mode 100644 examples/host/midi2_host_feather/src/display.c create mode 100644 examples/host/midi2_host_feather/src/display.h create mode 100644 examples/host/midi2_host_feather/src/font5x7.h create mode 100644 examples/host/midi2_host_feather/src/main.c create mode 100644 examples/host/midi2_host_feather/src/tusb_config.h (limited to 'examples/host') diff --git a/examples/host/midi2_host_feather/CMakeLists.txt b/examples/host/midi2_host_feather/CMakeLists.txt new file mode 100644 index 000000000..5d34b8170 --- /dev/null +++ b/examples/host/midi2_host_feather/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.20) + +include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) + +project(midi2_host_feather C CXX ASM) + +family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}) + +# This example requires PIO-USB and Pico SDK (I2C, SSD1306 display) +if(NOT FAMILY STREQUAL "rp2040") + return() +endif() + +add_executable(${PROJECT_NAME}) + +target_sources(${PROJECT_NAME} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c + ${CMAKE_CURRENT_SOURCE_DIR}/src/display.c +) + +target_include_directories(${PROJECT_NAME} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src +) + +family_configure_host_example(${PROJECT_NAME} noos) + +# Adafruit Feather RP2040 USB Host: PIO-USB on GP16/GP17 +target_compile_definitions(${PROJECT_NAME} PRIVATE + PIO_USB_DP_PIN_DEFAULT=16 +) +target_compile_options(${PROJECT_NAME} PRIVATE + -Wno-type-limits +) + +# SSD1306 display (I2C via STEMMA QT) +target_link_libraries(${PROJECT_NAME} PUBLIC + hardware_i2c +) diff --git a/examples/host/midi2_host_feather/Makefile b/examples/host/midi2_host_feather/Makefile new file mode 100644 index 000000000..09172c37f --- /dev/null +++ b/examples/host/midi2_host_feather/Makefile @@ -0,0 +1 @@ +include ../../../examples/build_system/make/make.mk diff --git a/examples/host/midi2_host_feather/src/display.c b/examples/host/midi2_host_feather/src/display.c new file mode 100644 index 000000000..91039fa16 --- /dev/null +++ b/examples/host/midi2_host_feather/src/display.c @@ -0,0 +1,276 @@ +/* + * 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. + */ + +// SSD1306 OLED display driver (128x64, I2C) for MIDI 2.0 Host example. +// Adafruit Feather RP2040 USB Host: I2C1 via STEMMA QT (SDA = GP2, SCL = GP3) +// +// Three display phases: +// 1. Splash : title + credits (shown during init) +// 2. Connecting : spinner animation while waiting for device +// 3. Live : header + 6 scrolling log lines + status bar + +#include "display.h" +#include +#include +#include "pico/stdlib.h" +#include "hardware/i2c.h" +#include "hardware/gpio.h" + +#define I2C_PORT i2c1 +#define I2C_SDA 2 +#define I2C_SCL 3 +#define I2C_FREQ 400000 +#define SSD1306_ADDR 0x3C + +#define SCR_W 128 +#define SCR_H 64 +#define PAGES (SCR_H / 8) +#define CHARS_PER_LINE 21 + +//--------------------------------------------------------------------+ +// Framebuffer +//--------------------------------------------------------------------+ + +static uint8_t fb[SCR_W * PAGES]; + +//--------------------------------------------------------------------+ +// Log buffer (6 lines for live view) +//--------------------------------------------------------------------+ + +#define LOG_LINES 6 +static char log_lines[LOG_LINES][CHARS_PER_LINE + 1]; +static int log_count = 0; + +//--------------------------------------------------------------------+ +// Minimal 5x7 font (ASCII 32-126) +//--------------------------------------------------------------------+ + +#include "font5x7.h" + +//--------------------------------------------------------------------+ +// SSD1306 I2C low-level +//--------------------------------------------------------------------+ + +static void ssd_cmd(uint8_t cmd) { + uint8_t buf[2] = { 0x00, cmd }; + i2c_write_blocking(I2C_PORT, SSD1306_ADDR, buf, 2, false); +} + +static void ssd_data(const uint8_t* data, size_t len) { + uint8_t buf[SCR_W + 1]; + buf[0] = 0x40; + size_t chunk = (len > SCR_W) ? SCR_W : len; + memcpy(buf + 1, data, chunk); + i2c_write_blocking(I2C_PORT, SSD1306_ADDR, buf, chunk + 1, false); +} + +static void ssd_flush(void) { + ssd_cmd(0x21); ssd_cmd(0); ssd_cmd(127); + ssd_cmd(0x22); ssd_cmd(0); ssd_cmd(7); + for (int page = 0; page < PAGES; page++) { + ssd_data(&fb[page * SCR_W], SCR_W); + } +} + +//--------------------------------------------------------------------+ +// Framebuffer drawing +//--------------------------------------------------------------------+ + +static void fb_clear(void) { + memset(fb, 0, sizeof(fb)); +} + +static void fb_char(int x, int y, char c) { + if (c < 32 || c > 126) c = '?'; + const uint8_t* glyph = font5x7 + (c - 32) * 5; + int page = y / 8; + int bit_offset = y % 8; + + if (page >= PAGES || x + 5 > SCR_W) return; + + for (int col = 0; col < 5; col++) { + uint8_t column_data = glyph[col]; + fb[(page * SCR_W) + x + col] |= (uint8_t)(column_data << bit_offset); + if (bit_offset > 0 && page + 1 < PAGES) { + fb[((page + 1) * SCR_W) + x + col] |= (uint8_t)(column_data >> (8 - bit_offset)); + } + } +} + +static void fb_string(int x, int y, const char* str) { + while (*str) { + fb_char(x, y, *str); + x += 6; + if (x + 6 > SCR_W) break; + str++; + } +} + +// Draw a horizontal line (1px) +static void fb_hline(int x0, int x1, int y) { + int page = y / 8; + uint8_t mask = (uint8_t)(1 << (y % 8)); + if (page >= PAGES) return; + for (int x = x0; x <= x1 && x < SCR_W; x++) { + fb[page * SCR_W + x] |= mask; + } +} + +//--------------------------------------------------------------------+ +// Phase 1: Splash +//--------------------------------------------------------------------+ + +void display_init(void) { + i2c_init(I2C_PORT, I2C_FREQ); + gpio_set_function(I2C_SDA, GPIO_FUNC_I2C); + gpio_set_function(I2C_SCL, GPIO_FUNC_I2C); + gpio_pull_up(I2C_SDA); + gpio_pull_up(I2C_SCL); + + sleep_ms(100); + + // SSD1306 init sequence + ssd_cmd(0xAE); + ssd_cmd(0xD5); ssd_cmd(0x80); + ssd_cmd(0xA8); ssd_cmd(0x3F); + ssd_cmd(0xD3); ssd_cmd(0x00); + ssd_cmd(0x40); + ssd_cmd(0x8D); ssd_cmd(0x14); + ssd_cmd(0x20); ssd_cmd(0x00); + ssd_cmd(0xA1); + ssd_cmd(0xC8); + ssd_cmd(0xDA); ssd_cmd(0x12); + ssd_cmd(0x81); ssd_cmd(0xCF); + ssd_cmd(0xD9); ssd_cmd(0xF1); + ssd_cmd(0xDB); ssd_cmd(0x40); + ssd_cmd(0xA4); + ssd_cmd(0xA6); + ssd_cmd(0xAF); + + // Splash screen + fb_clear(); + fb_string(4, 8, "TinyUSB MIDI 2.0"); + fb_hline(4, 123, 18); + fb_string(16, 24, "USB Host Demo"); + fb_string(4, 40, "Feather RP2040"); + fb_string(4, 52, "PIO-USB + SSD1306"); + ssd_flush(); +} + +//--------------------------------------------------------------------+ +// Phase 2: Connecting (spinner) +//--------------------------------------------------------------------+ + +static const char SPINNER[] = "|/-\\"; + +void display_connecting(uint32_t elapsed_ms) { + int idx = (int)((elapsed_ms / 200) % 4); + + // Clear bottom 2 pages for spinner area + memset(&fb[6 * SCR_W], 0, SCR_W); + memset(&fb[7 * SCR_W], 0, SCR_W); + + char line[CHARS_PER_LINE + 1]; + snprintf(line, sizeof(line), "%c Waiting device...", SPINNER[idx]); + fb_string(4, 52, line); + ssd_flush(); +} + +//--------------------------------------------------------------------+ +// Phase 3: Live view +//--------------------------------------------------------------------+ + +// Layout: +// y=0 : "TinyUSB MIDI 2.0 Host" (header, fixed) +// y=9 : separator line +// y=10 : log line 0 +// y=18 : log line 1 +// y=26 : log line 2 +// y=34 : log line 3 +// y=42 : log line 4 +// y=50 : log line 5 +// y=57 : separator line +// y=58 : status bar + +static bool live_mode = false; + +static void draw_live_frame(void) { + fb_clear(); + fb_string(0, 0, "TinyUSB MIDI 2.0 Host"); + fb_hline(0, 127, 9); + fb_hline(0, 127, 57); +} + +void display_live_begin(void) { + live_mode = true; + log_count = 0; + memset(log_lines, 0, sizeof(log_lines)); + + draw_live_frame(); + fb_string(0, 58, "Connected"); + ssd_flush(); +} + +void display_log(const char* text, uint16_t color) { + (void)color; + + if (!live_mode) { + display_live_begin(); + } + + // Scroll up if full + if (log_count >= LOG_LINES) { + for (int i = 0; i < LOG_LINES - 1; i++) { + strncpy(log_lines[i], log_lines[i + 1], CHARS_PER_LINE); + log_lines[i][CHARS_PER_LINE] = '\0'; + } + log_count = LOG_LINES - 1; + } + + strncpy(log_lines[log_count], text, CHARS_PER_LINE); + log_lines[log_count][CHARS_PER_LINE] = '\0'; + log_count++; + + // Redraw: header + log + status + draw_live_frame(); + for (int i = 0; i < log_count && i < LOG_LINES; i++) { + fb_string(0, 10 + i * 8, log_lines[i]); + } + ssd_flush(); +} + +void display_status(const char* text) { + if (!live_mode) return; + + // Clear status area (page 7) + memset(&fb[7 * SCR_W], 0, SCR_W); + // Redraw separator (might have been cleared) + fb_hline(0, 127, 57); + + char status[CHARS_PER_LINE + 1]; + strncpy(status, text, CHARS_PER_LINE); + status[CHARS_PER_LINE] = '\0'; + fb_string(0, 58, status); + ssd_flush(); +} diff --git a/examples/host/midi2_host_feather/src/display.h b/examples/host/midi2_host_feather/src/display.h new file mode 100644 index 000000000..ad8ead81d --- /dev/null +++ b/examples/host/midi2_host_feather/src/display.h @@ -0,0 +1,46 @@ +/* + * 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 DISPLAY_H_ +#define DISPLAY_H_ + +#include +#include + +// Initialize SSD1306 display and show splash screen +void display_init(void); + +// Show waiting/connecting animation (call periodically) +void display_connecting(uint32_t elapsed_ms); + +// Transition to live message view +void display_live_begin(void); + +// Add a line to the scrolling log area (6 visible lines) +void display_log(const char* text, uint16_t color); + +// Update the status bar (bottom line) +void display_status(const char* text); + +#endif diff --git a/examples/host/midi2_host_feather/src/font5x7.h b/examples/host/midi2_host_feather/src/font5x7.h new file mode 100644 index 000000000..e515faac1 --- /dev/null +++ b/examples/host/midi2_host_feather/src/font5x7.h @@ -0,0 +1,107 @@ +// Minimal 5x7 font, ASCII 32-126. Public domain. +// 5 bytes per character (5 columns, 7 rows, LSB=top row) + +#ifndef FONT5X7_H_ +#define FONT5X7_H_ + +#include + +static const uint8_t font5x7[] = { + 0x00,0x00,0x00,0x00,0x00, // 32 (space) + 0x00,0x00,0x5F,0x00,0x00, // 33 ! + 0x00,0x07,0x00,0x07,0x00, // 34 " + 0x14,0x7F,0x14,0x7F,0x14, // 35 # + 0x24,0x2A,0x7F,0x2A,0x12, // 36 $ + 0x23,0x13,0x08,0x64,0x62, // 37 % + 0x36,0x49,0x55,0x22,0x50, // 38 & + 0x00,0x05,0x03,0x00,0x00, // 39 ' + 0x00,0x1C,0x22,0x41,0x00, // 40 ( + 0x00,0x41,0x22,0x1C,0x00, // 41 ) + 0x08,0x2A,0x1C,0x2A,0x08, // 42 * + 0x08,0x08,0x3E,0x08,0x08, // 43 + + 0x00,0x50,0x30,0x00,0x00, // 44 , + 0x08,0x08,0x08,0x08,0x08, // 45 - + 0x00,0x60,0x60,0x00,0x00, // 46 . + 0x20,0x10,0x08,0x04,0x02, // 47 / + 0x3E,0x51,0x49,0x45,0x3E, // 48 0 + 0x00,0x42,0x7F,0x40,0x00, // 49 1 + 0x42,0x61,0x51,0x49,0x46, // 50 2 + 0x21,0x41,0x45,0x4B,0x31, // 51 3 + 0x18,0x14,0x12,0x7F,0x10, // 52 4 + 0x27,0x45,0x45,0x45,0x39, // 53 5 + 0x3C,0x4A,0x49,0x49,0x30, // 54 6 + 0x01,0x71,0x09,0x05,0x03, // 55 7 + 0x36,0x49,0x49,0x49,0x36, // 56 8 + 0x06,0x49,0x49,0x29,0x1E, // 57 9 + 0x00,0x36,0x36,0x00,0x00, // 58 : + 0x00,0x56,0x36,0x00,0x00, // 59 ; + 0x00,0x08,0x14,0x22,0x41, // 60 < + 0x14,0x14,0x14,0x14,0x14, // 61 = + 0x41,0x22,0x14,0x08,0x00, // 62 > + 0x02,0x01,0x51,0x09,0x06, // 63 ? + 0x32,0x49,0x79,0x41,0x3E, // 64 @ + 0x7E,0x11,0x11,0x11,0x7E, // 65 A + 0x7F,0x49,0x49,0x49,0x36, // 66 B + 0x3E,0x41,0x41,0x41,0x22, // 67 C + 0x7F,0x41,0x41,0x22,0x1C, // 68 D + 0x7F,0x49,0x49,0x49,0x41, // 69 E + 0x7F,0x09,0x09,0x01,0x01, // 70 F + 0x3E,0x41,0x41,0x51,0x32, // 71 G + 0x7F,0x08,0x08,0x08,0x7F, // 72 H + 0x00,0x41,0x7F,0x41,0x00, // 73 I + 0x20,0x40,0x41,0x3F,0x01, // 74 J + 0x7F,0x08,0x14,0x22,0x41, // 75 K + 0x7F,0x40,0x40,0x40,0x40, // 76 L + 0x7F,0x02,0x04,0x02,0x7F, // 77 M + 0x7F,0x04,0x08,0x10,0x7F, // 78 N + 0x3E,0x41,0x41,0x41,0x3E, // 79 O + 0x7F,0x09,0x09,0x09,0x06, // 80 P + 0x3E,0x41,0x51,0x21,0x5E, // 81 Q + 0x7F,0x09,0x19,0x29,0x46, // 82 R + 0x46,0x49,0x49,0x49,0x31, // 83 S + 0x01,0x01,0x7F,0x01,0x01, // 84 T + 0x3F,0x40,0x40,0x40,0x3F, // 85 U + 0x1F,0x20,0x40,0x20,0x1F, // 86 V + 0x7F,0x20,0x18,0x20,0x7F, // 87 W + 0x63,0x14,0x08,0x14,0x63, // 88 X + 0x03,0x04,0x78,0x04,0x03, // 89 Y + 0x61,0x51,0x49,0x45,0x43, // 90 Z + 0x00,0x00,0x7F,0x41,0x41, // 91 [ + 0x02,0x04,0x08,0x10,0x20, // 92 backslash + 0x41,0x41,0x7F,0x00,0x00, // 93 ] + 0x04,0x02,0x01,0x02,0x04, // 94 ^ + 0x40,0x40,0x40,0x40,0x40, // 95 _ + 0x00,0x01,0x02,0x04,0x00, // 96 ` + 0x20,0x54,0x54,0x54,0x78, // 97 a + 0x7F,0x48,0x44,0x44,0x38, // 98 b + 0x38,0x44,0x44,0x44,0x20, // 99 c + 0x38,0x44,0x44,0x48,0x7F, // 100 d + 0x38,0x54,0x54,0x54,0x18, // 101 e + 0x08,0x7E,0x09,0x01,0x02, // 102 f + 0x08,0x14,0x54,0x54,0x3C, // 103 g + 0x7F,0x08,0x04,0x04,0x78, // 104 h + 0x00,0x44,0x7D,0x40,0x00, // 105 i + 0x20,0x40,0x44,0x3D,0x00, // 106 j + 0x00,0x7F,0x10,0x28,0x44, // 107 k + 0x00,0x41,0x7F,0x40,0x00, // 108 l + 0x7C,0x04,0x18,0x04,0x78, // 109 m + 0x7C,0x08,0x04,0x04,0x78, // 110 n + 0x38,0x44,0x44,0x44,0x38, // 111 o + 0x7C,0x14,0x14,0x14,0x08, // 112 p + 0x08,0x14,0x14,0x18,0x7C, // 113 q + 0x7C,0x08,0x04,0x04,0x08, // 114 r + 0x48,0x54,0x54,0x54,0x20, // 115 s + 0x04,0x3F,0x44,0x40,0x20, // 116 t + 0x3C,0x40,0x40,0x20,0x7C, // 117 u + 0x1C,0x20,0x40,0x20,0x1C, // 118 v + 0x3C,0x40,0x30,0x40,0x3C, // 119 w + 0x44,0x28,0x10,0x28,0x44, // 120 x + 0x0C,0x50,0x50,0x50,0x3C, // 121 y + 0x44,0x64,0x54,0x4C,0x44, // 122 z + 0x00,0x08,0x36,0x41,0x00, // 123 { + 0x00,0x00,0x7F,0x00,0x00, // 124 | + 0x00,0x41,0x36,0x08,0x00, // 125 } + 0x10,0x08,0x08,0x10,0x08, // 126 ~ +}; + +#endif diff --git a/examples/host/midi2_host_feather/src/main.c b/examples/host/midi2_host_feather/src/main.c new file mode 100644 index 000000000..7c996355d --- /dev/null +++ b/examples/host/midi2_host_feather/src/main.c @@ -0,0 +1,267 @@ +/* + * 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. + */ + +// MIDI 2.0 Host Example: Adafruit Feather RP2040 USB Host +// +// Receives UMP from a MIDI 2.0 Device via PIO-USB (GP16/GP17). +// SSD1306 OLED (I2C1, GP2/GP3) shows splash, spinner, then live UMP messages. + +#include +#include +#include "bsp/board_api.h" +#include "tusb.h" +#include "hardware/gpio.h" +#include "class/midi/midi2_host.h" +#include "class/midi/midi.h" +#include "display.h" + +//--------------------------------------------------------------------+ +// State +//--------------------------------------------------------------------+ + +static uint32_t note_count = 0; +static uint8_t midi2_idx = 0xFF; +static bool device_connected = false; +static bool mounted = false; + +//--------------------------------------------------------------------+ +// Note name helper +//--------------------------------------------------------------------+ + +static const char* NOTE_NAMES[] = { + "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" +}; + +static void note_name(uint8_t pitch, char* buf, size_t len) { + int octave = (pitch / 12) - 1; + snprintf(buf, len, "%s%d", NOTE_NAMES[pitch % 12], octave); +} + +//--------------------------------------------------------------------+ +// UMP decoder +//--------------------------------------------------------------------+ + +static void decode_ump(const uint32_t* words, uint8_t word_count) { + uint8_t mt = (uint8_t)((words[0] >> 28) & 0x0F); + char line[64]; + + if (mt == 0x4 && word_count >= 2) { + uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F); + uint8_t channel = (uint8_t)((words[0] >> 16) & 0x0F); + + switch (status) { + case 0x9: { + uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); + uint16_t vel = (uint16_t)((words[1] >> 16) & 0xFFFF); + char nn[6]; + note_name(pitch, nn, sizeof(nn)); + snprintf(line, sizeof(line), "NoteOn %s ch%u v%04X", nn, channel, vel); + display_log(line, 0x07E0); + note_count++; + break; + } + case 0x8: { + uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); + char nn[6]; + note_name(pitch, nn, sizeof(nn)); + snprintf(line, sizeof(line), "NoteOff %s ch%u", nn, channel); + display_log(line, 0x8410); + break; + } + case 0xB: { + uint8_t idx = (uint8_t)((words[0] >> 8) & 0x7F); + snprintf(line, sizeof(line), "CC%-3u %08lX", idx, (unsigned long)words[1]); + display_log(line, 0x001F); + break; + } + case 0xC: { + uint8_t prog = (uint8_t)((words[1] >> 24) & 0x7F); + snprintf(line, sizeof(line), "ProgChg %u", prog); + display_log(line, 0xFFE0); + break; + } + case 0xE: { + snprintf(line, sizeof(line), "PBend %08lX", (unsigned long)words[1]); + display_log(line, 0xF81F); + break; + } + case 0xD: { + snprintf(line, sizeof(line), "CPress %08lX", (unsigned long)words[1]); + display_log(line, 0xFC10); + break; + } + case 0xA: { + uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); + char nn[6]; + note_name(pitch, nn, sizeof(nn)); + snprintf(line, sizeof(line), "PolyP %s %08lX", nn, (unsigned long)words[1]); + display_log(line, 0xFC10); + break; + } + case 0xF: { + uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); + uint8_t flags = (uint8_t)(words[0] & 0xFF); + snprintf(line, sizeof(line), "PN-Mgmt n%u f%02X", pitch, flags); + display_log(line, 0x07FF); + break; + } + default: { + snprintf(line, sizeof(line), "M2CVM s=0x%X", status); + display_log(line, 0xFFFF); + break; + } + } + } else if (mt == 0x0) { + uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F); + if (status == 0x2) { + uint16_t ts = words[0] & 0xFFFF; + snprintf(line, sizeof(line), "JR-TS %04X", ts); + display_log(line, 0x8410); + } + } else { + snprintf(line, sizeof(line), "MT=0x%X w0=%08lX", + mt, (unsigned long)words[0]); + display_log(line, 0xFFFF); + } +} + +//--------------------------------------------------------------------+ +// MIDI 2.0 Host Callbacks +//--------------------------------------------------------------------+ + +void tuh_midi2_descriptor_cb(uint8_t idx, const tuh_midi2_descriptor_cb_t* d) { + (void)idx; + char line[48]; + snprintf(line, sizeof(line), "%s RX=%u TX=%u", + d->protocol_version ? "MIDI2" : "MIDI1", + d->rx_cable_count, d->tx_cable_count); + display_log(line, 0x07E0); +} + +void tuh_midi2_mount_cb(uint8_t idx, const tuh_midi2_mount_cb_t* m) { + midi2_idx = idx; + mounted = true; + + display_live_begin(); + + if (m->protocol_version) { + display_log("MIDI 2.0 ready", 0x07E0); + } else { + display_log("MIDI 1.0 device", 0xFFE0); + } + display_status("Receiving..."); +} + +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; + decode_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; + device_connected = false; + mounted = false; + note_count = 0; + display_log("Disconnected", 0xF800); + display_status("Waiting..."); +} + +//--------------------------------------------------------------------+ +// Main +//--------------------------------------------------------------------+ + +int main(void) { + board_init(); + + // Enable 5V to USB-A port (Feather RP2040 USB Host: GP18) + gpio_init(18); + gpio_set_dir(18, GPIO_OUT); + gpio_put(18, 1); + + display_init(); + sleep_ms(1500); + + tusb_rhport_init_t host_init = { + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_FULL, + }; + tusb_init(BOARD_TUH_RHPORT, &host_init); + + uint32_t last_status_ms = 0; + + while (1) { + tuh_task(); + + uint32_t now = tusb_time_millis_api(); + + if (!mounted) { + // Spinner while waiting for device + if (now - last_status_ms > 200) { + last_status_ms = now; + display_connecting(now); + } + } else { + // Update note count every 2 seconds + if (now - last_status_ms > 2000) { + last_status_ms = now; + char line[22]; + snprintf(line, sizeof(line), "Notes: %lu", (unsigned long)note_count); + display_status(line); + } + } + } + + return 0; +} + +// Generic USB device mount/unmount +void tuh_mount_cb(uint8_t daddr) { + (void)daddr; + device_connected = true; +} + +void tuh_umount_cb(uint8_t daddr) { + (void)daddr; + device_connected = false; + mounted = false; +} diff --git a/examples/host/midi2_host_feather/src/tusb_config.h b/examples/host/midi2_host_feather/src/tusb_config.h new file mode 100644 index 000000000..362d0e2d0 --- /dev/null +++ b/examples/host/midi2_host_feather/src/tusb_config.h @@ -0,0 +1,91 @@ +/* + * 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 + +#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 +// Adafruit Feather RP2040 USB Host: PIO-USB on GP16/GP17 (USB-A Host port) +//--------------------------------------------------------------------+ + +#define CFG_TUH_ENABLED 1 + +// PIO-USB Host on rhport 1 (USB-A connector on GP16/GP17) +#define CFG_TUH_RPI_PIO_USB 1 +#define BOARD_TUH_RHPORT 1 + +#ifndef BOARD_TUH_MAX_SPEED +#define BOARD_TUH_MAX_SPEED OPT_MODE_FULL_SPEED +#endif + +#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED + +//--------------------------------------------------------------------+ +// Driver Configuration +//--------------------------------------------------------------------+ + +#define CFG_TUH_ENUMERATION_BUFSIZE 256 + +#define CFG_TUH_HUB 0 +#define CFG_TUH_DEVICE_MAX 1 + +// MIDI 2.0 Host +#define CFG_TUH_MIDI2 1 +#define CFG_TUH_MIDI2_RX_BUFSIZE 512 +#define CFG_TUH_MIDI2_TX_BUFSIZE 512 + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.3.1 From c4847006dbb7565b71055837103ec788c4cb6fee Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Tue, 7 Apr 2026 23:04:03 -0300 Subject: fix: skip midi2_host_feather build on non-RP2040 platforms The Makefile was missing the FAMILY=rp2040 guard, causing CI failures on stm32h7rs and other non-RP2040 targets. Matches the approach used by midi2_host. --- examples/host/midi2_host_feather/Makefile | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'examples/host') diff --git a/examples/host/midi2_host_feather/Makefile b/examples/host/midi2_host_feather/Makefile index 09172c37f..933d19434 100644 --- a/examples/host/midi2_host_feather/Makefile +++ b/examples/host/midi2_host_feather/Makefile @@ -1 +1,27 @@ -include ../../../examples/build_system/make/make.mk +# This example requires RP2040 (PIO-USB, Pico SDK I2C, SSD1306 display) +ifeq (,$(findstring rp2040,$(FAMILY))) +$(info Skipping midi2_host_feather: requires FAMILY=rp2040) +all: + @: +.DEFAULT: + @: +else + +include ../../../hw/bsp/family_support.mk + +INC += \ + src \ + +# Example source +EXAMPLE_SOURCE += \ + src/main.c \ + src/display.c \ + +SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE)) + +# Suppress pre-existing warning +CFLAGS_GCC += -Wno-type-limits + +include ../../../hw/bsp/family_rules.mk + +endif -- cgit v1.3.1 From b4880761bcadbee86d047e0a3b329859926caad5 Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Tue, 7 Apr 2026 23:46:10 -0300 Subject: refactor: replace midi2_host (Waveshare) with midi2_host_feather Remove the Waveshare RP2350-USB-A host example and board definition. The Feather RP2040 USB Host is the TinyUSB reference board for PIO-USB host and does not require the CFG_TUSB_DEBUG workaround needed by the Waveshare (R13 pull-up issue). One host example is sufficient for the PR. The Feather variant has an improved display with splash, spinner, and 6-line live view. --- examples/host/CMakeLists.txt | 2 +- examples/host/midi2_host/CMakeLists.txt | 38 --- examples/host/midi2_host/Makefile | 27 -- examples/host/midi2_host/src/display.c | 221 -------------- examples/host/midi2_host/src/display.h | 54 ---- examples/host/midi2_host/src/font5x7.h | 107 ------- examples/host/midi2_host/src/main.c | 326 --------------------- examples/host/midi2_host/src/tusb_config.h | 91 ------ .../boards/waveshare_rp2350_usb_a/board.cmake | 2 - .../rp2040/boards/waveshare_rp2350_usb_a/board.h | 49 ---- 10 files changed, 1 insertion(+), 916 deletions(-) delete mode 100644 examples/host/midi2_host/CMakeLists.txt delete mode 100644 examples/host/midi2_host/Makefile delete mode 100644 examples/host/midi2_host/src/display.c delete mode 100644 examples/host/midi2_host/src/display.h delete mode 100644 examples/host/midi2_host/src/font5x7.h delete mode 100644 examples/host/midi2_host/src/main.c delete mode 100644 examples/host/midi2_host/src/tusb_config.h delete mode 100644 hw/bsp/rp2040/boards/waveshare_rp2350_usb_a/board.cmake delete mode 100644 hw/bsp/rp2040/boards/waveshare_rp2350_usb_a/board.h (limited to 'examples/host') diff --git a/examples/host/CMakeLists.txt b/examples/host/CMakeLists.txt index 7c74e3c73..2cf2c10af 100644 --- a/examples/host/CMakeLists.txt +++ b/examples/host/CMakeLists.txt @@ -13,7 +13,7 @@ set(EXAMPLE_LIST device_info hid_controller midi_rx - midi2_host + midi2_host_feather msc_file_explorer msc_file_explorer_freertos ) diff --git a/examples/host/midi2_host/CMakeLists.txt b/examples/host/midi2_host/CMakeLists.txt deleted file mode 100644 index cd70d122a..000000000 --- a/examples/host/midi2_host/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -cmake_minimum_required(VERSION 3.20) - -include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) - -project(midi2_host C CXX ASM) - -family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}) - -# This example requires PIO-USB and Pico SDK (I2C, SSD1306 display) -if(NOT FAMILY STREQUAL "rp2040") - return() -endif() - -add_executable(${PROJECT_NAME}) - -target_sources(${PROJECT_NAME} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c - ${CMAKE_CURRENT_SOURCE_DIR}/src/display.c -) - -target_include_directories(${PROJECT_NAME} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src -) - -family_configure_host_example(${PROJECT_NAME} noos) - -# Waveshare RP2350-USB-A: PIO-USB on GP12/GP13 -target_compile_definitions(${PROJECT_NAME} PRIVATE - PIO_USB_DP_PIN_DEFAULT=12 -) -target_compile_options(${PROJECT_NAME} PRIVATE - -Wno-type-limits -) - -# SSD1306 display (I2C) -target_link_libraries(${PROJECT_NAME} PUBLIC - hardware_i2c -) diff --git a/examples/host/midi2_host/Makefile b/examples/host/midi2_host/Makefile deleted file mode 100644 index 2b4660516..000000000 --- a/examples/host/midi2_host/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -# This example requires RP2040/RP2350 (PIO-USB, Pico SDK I2C, SSD1306 display) -ifeq (,$(findstring rp2040,$(FAMILY))) -$(info Skipping midi2_host: requires FAMILY=rp2040) -all: - @: -.DEFAULT: - @: -else - -include ../../../hw/bsp/family_support.mk - -INC += \ - src \ - -# Example source -EXAMPLE_SOURCE += \ - src/main.c \ - src/display.c \ - -SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE)) - -# Suppress pre-existing warning -CFLAGS_GCC += -Wno-type-limits - -include ../../../hw/bsp/family_rules.mk - -endif diff --git a/examples/host/midi2_host/src/display.c b/examples/host/midi2_host/src/display.c deleted file mode 100644 index 4745c2961..000000000 --- a/examples/host/midi2_host/src/display.c +++ /dev/null @@ -1,221 +0,0 @@ -/* - * 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. - */ - -// SSD1306 OLED display driver (128x64, I2C) for MIDI 2.0 Host example. -// Minimal text-only implementation, no graphics library. -// I2C0: SDA = GP4, SCL = GP5, Address = 0x3C - -#include "display.h" -#include -#include -#include "pico/stdlib.h" -#include "hardware/i2c.h" -#include "hardware/gpio.h" - -#define I2C_PORT i2c0 -#define I2C_SDA 4 -#define I2C_SCL 5 -#define I2C_FREQ 400000 -#define SSD1306_ADDR 0x3C - -#define SCR_W 128 -#define SCR_H 64 -#define PAGES (SCR_H / 8) // 8 pages -#define CHARS_PER_LINE 21 // 128 / 6 = 21 chars - -// Framebuffer -static uint8_t fb[SCR_W * PAGES]; - -// Log buffer -#define LOG_LINES 3 -static char log_lines[LOG_LINES][CHARS_PER_LINE + 1]; -static int log_count = 0; - -// Status line -static char status_text[CHARS_PER_LINE + 1] = ""; - -//--------------------------------------------------------------------+ -// Minimal 5x7 font (ASCII 32-126) -//--------------------------------------------------------------------+ -#include "font5x7.h" - -//--------------------------------------------------------------------+ -// SSD1306 I2C commands -//--------------------------------------------------------------------+ - -static void ssd_cmd(uint8_t cmd) { - uint8_t buf[2] = { 0x00, cmd }; // Co=0, D/C=0 - i2c_write_blocking(I2C_PORT, SSD1306_ADDR, buf, 2, false); -} - -static void ssd_data(const uint8_t* data, size_t len) { - uint8_t buf[SCR_W + 1]; - buf[0] = 0x40; // Co=0, D/C=1 - size_t chunk = (len > SCR_W) ? SCR_W : len; - memcpy(buf + 1, data, chunk); - i2c_write_blocking(I2C_PORT, SSD1306_ADDR, buf, chunk + 1, false); -} - -static void ssd_flush(void) { - ssd_cmd(0x21); ssd_cmd(0); ssd_cmd(127); // Column range - ssd_cmd(0x22); ssd_cmd(0); ssd_cmd(7); // Page range - for (int page = 0; page < PAGES; page++) { - ssd_data(&fb[page * SCR_W], SCR_W); - } -} - -//--------------------------------------------------------------------+ -// Framebuffer drawing -//--------------------------------------------------------------------+ - -static void fb_clear(void) { - memset(fb, 0, sizeof(fb)); -} - -static void fb_char(int x, int y, char c) { - if (c < 32 || c > 126) c = '?'; - const uint8_t* glyph = font5x7 + (c - 32) * 5; - int page = y / 8; - int bit_offset = y % 8; - - if (page >= PAGES || x + 5 > SCR_W) return; - - for (int col = 0; col < 5; col++) { - uint8_t column_data = glyph[col]; - fb[(page * SCR_W) + x + col] |= (uint8_t)(column_data << bit_offset); - if (bit_offset > 0 && page + 1 < PAGES) { - fb[((page + 1) * SCR_W) + x + col] |= (uint8_t)(column_data >> (8 - bit_offset)); - } - } -} - -static void fb_string(int x, int y, const char* str) { - while (*str) { - fb_char(x, y, *str); - x += 6; - if (x + 6 > SCR_W) break; - str++; - } -} - -//--------------------------------------------------------------------+ -// Display API -//--------------------------------------------------------------------+ - -void display_init(void) { - i2c_init(I2C_PORT, I2C_FREQ); - gpio_set_function(I2C_SDA, GPIO_FUNC_I2C); - gpio_set_function(I2C_SCL, GPIO_FUNC_I2C); - gpio_pull_up(I2C_SDA); - gpio_pull_up(I2C_SCL); - - sleep_ms(100); - - // SSD1306 init sequence - ssd_cmd(0xAE); // Display off - ssd_cmd(0xD5); ssd_cmd(0x80); // Clock div - ssd_cmd(0xA8); ssd_cmd(0x3F); // Multiplex 64 - ssd_cmd(0xD3); ssd_cmd(0x00); // Display offset - ssd_cmd(0x40); // Start line 0 - ssd_cmd(0x8D); ssd_cmd(0x14); // Charge pump on - ssd_cmd(0x20); ssd_cmd(0x00); // Horizontal addressing - ssd_cmd(0xA1); // Segment remap - ssd_cmd(0xC8); // COM scan direction - ssd_cmd(0xDA); ssd_cmd(0x12); // COM pins - ssd_cmd(0x81); ssd_cmd(0xCF); // Contrast - ssd_cmd(0xD9); ssd_cmd(0xF1); // Pre-charge - ssd_cmd(0xDB); ssd_cmd(0x40); // VCOMH deselect - ssd_cmd(0xA4); // Display from RAM - ssd_cmd(0xA6); // Normal display - ssd_cmd(0xAF); // Display on - - fb_clear(); - fb_string(0, 0, "MIDI 2.0 Host"); - ssd_flush(); -} - -void display_checklist_update(const checklist_t* ck) { - struct { const char* label; bool ok; } items[] = { - { "PWR", ck->pwr_on }, - { "TinyUSB", ck->tusb_init }, - { "USB bus", ck->bus_active }, - { "Device", ck->device_connected }, - { "Descript", ck->descriptor_parsed }, - { "Alt1 UMP", ck->alt_setting_ok }, - { "Mount", ck->mounted }, - { "RX UMP", ck->receiving }, - }; - - // Clear checklist area (lines 1-4, two columns) - for (int p = 1; p <= 4; p++) { - memset(&fb[p * SCR_W], 0, SCR_W); - } - - for (int i = 0; i < 8; i++) { - int col = (i < 4) ? 0 : 64; - int row = (i < 4) ? i : i - 4; - int y = 9 + row * 8; - char line[12]; - snprintf(line, sizeof(line), "%s %s", items[i].ok ? "OK" : "..", items[i].label); - fb_string(col, y, line); - } - - ssd_flush(); -} - -void display_log(const char* text, uint16_t color) { - (void)color; // SSD1306 is monochrome - - if (log_count >= LOG_LINES) { - for (int i = 0; i < LOG_LINES - 1; i++) { - strncpy(log_lines[i], log_lines[i + 1], CHARS_PER_LINE); - log_lines[i][CHARS_PER_LINE] = '\0'; - } - log_count = LOG_LINES - 1; - } - - strncpy(log_lines[log_count], text, CHARS_PER_LINE); - log_lines[log_count][CHARS_PER_LINE] = '\0'; - log_count++; - - // Draw log area (pages 5-6, y=40-55) - memset(&fb[5 * SCR_W], 0, SCR_W); - memset(&fb[6 * SCR_W], 0, SCR_W); - - for (int i = 0; i < log_count && i < LOG_LINES; i++) { - fb_string(0, 41 + i * 8, log_lines[i]); - } - - ssd_flush(); -} - -void display_status(const char* text) { - strncpy(status_text, text, CHARS_PER_LINE); - status_text[CHARS_PER_LINE] = '\0'; - - // Status on last page (y=56) - memset(&fb[7 * SCR_W], 0, SCR_W); - fb_string(0, 56, status_text); - ssd_flush(); -} diff --git a/examples/host/midi2_host/src/display.h b/examples/host/midi2_host/src/display.h deleted file mode 100644 index a8b0a4b5e..000000000 --- a/examples/host/midi2_host/src/display.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 DISPLAY_H_ -#define DISPLAY_H_ - -#include -#include - -typedef struct { - bool pwr_on; - bool tusb_init; - bool bus_active; - bool device_connected; - bool descriptor_parsed; - bool alt_setting_ok; - bool mounted; - bool receiving; -} checklist_t; - -// Initialize SSD1306 display (I2C on GP4/GP5) -void display_init(void); - -// Redraw the checklist area (top portion) -void display_checklist_update(const checklist_t* ck); - -// Add a line to the scrolling log area -void display_log(const char* text, uint16_t color); - -// Update the status bar (bottom line) -void display_status(const char* text); - -#endif diff --git a/examples/host/midi2_host/src/font5x7.h b/examples/host/midi2_host/src/font5x7.h deleted file mode 100644 index e515faac1..000000000 --- a/examples/host/midi2_host/src/font5x7.h +++ /dev/null @@ -1,107 +0,0 @@ -// Minimal 5x7 font, ASCII 32-126. Public domain. -// 5 bytes per character (5 columns, 7 rows, LSB=top row) - -#ifndef FONT5X7_H_ -#define FONT5X7_H_ - -#include - -static const uint8_t font5x7[] = { - 0x00,0x00,0x00,0x00,0x00, // 32 (space) - 0x00,0x00,0x5F,0x00,0x00, // 33 ! - 0x00,0x07,0x00,0x07,0x00, // 34 " - 0x14,0x7F,0x14,0x7F,0x14, // 35 # - 0x24,0x2A,0x7F,0x2A,0x12, // 36 $ - 0x23,0x13,0x08,0x64,0x62, // 37 % - 0x36,0x49,0x55,0x22,0x50, // 38 & - 0x00,0x05,0x03,0x00,0x00, // 39 ' - 0x00,0x1C,0x22,0x41,0x00, // 40 ( - 0x00,0x41,0x22,0x1C,0x00, // 41 ) - 0x08,0x2A,0x1C,0x2A,0x08, // 42 * - 0x08,0x08,0x3E,0x08,0x08, // 43 + - 0x00,0x50,0x30,0x00,0x00, // 44 , - 0x08,0x08,0x08,0x08,0x08, // 45 - - 0x00,0x60,0x60,0x00,0x00, // 46 . - 0x20,0x10,0x08,0x04,0x02, // 47 / - 0x3E,0x51,0x49,0x45,0x3E, // 48 0 - 0x00,0x42,0x7F,0x40,0x00, // 49 1 - 0x42,0x61,0x51,0x49,0x46, // 50 2 - 0x21,0x41,0x45,0x4B,0x31, // 51 3 - 0x18,0x14,0x12,0x7F,0x10, // 52 4 - 0x27,0x45,0x45,0x45,0x39, // 53 5 - 0x3C,0x4A,0x49,0x49,0x30, // 54 6 - 0x01,0x71,0x09,0x05,0x03, // 55 7 - 0x36,0x49,0x49,0x49,0x36, // 56 8 - 0x06,0x49,0x49,0x29,0x1E, // 57 9 - 0x00,0x36,0x36,0x00,0x00, // 58 : - 0x00,0x56,0x36,0x00,0x00, // 59 ; - 0x00,0x08,0x14,0x22,0x41, // 60 < - 0x14,0x14,0x14,0x14,0x14, // 61 = - 0x41,0x22,0x14,0x08,0x00, // 62 > - 0x02,0x01,0x51,0x09,0x06, // 63 ? - 0x32,0x49,0x79,0x41,0x3E, // 64 @ - 0x7E,0x11,0x11,0x11,0x7E, // 65 A - 0x7F,0x49,0x49,0x49,0x36, // 66 B - 0x3E,0x41,0x41,0x41,0x22, // 67 C - 0x7F,0x41,0x41,0x22,0x1C, // 68 D - 0x7F,0x49,0x49,0x49,0x41, // 69 E - 0x7F,0x09,0x09,0x01,0x01, // 70 F - 0x3E,0x41,0x41,0x51,0x32, // 71 G - 0x7F,0x08,0x08,0x08,0x7F, // 72 H - 0x00,0x41,0x7F,0x41,0x00, // 73 I - 0x20,0x40,0x41,0x3F,0x01, // 74 J - 0x7F,0x08,0x14,0x22,0x41, // 75 K - 0x7F,0x40,0x40,0x40,0x40, // 76 L - 0x7F,0x02,0x04,0x02,0x7F, // 77 M - 0x7F,0x04,0x08,0x10,0x7F, // 78 N - 0x3E,0x41,0x41,0x41,0x3E, // 79 O - 0x7F,0x09,0x09,0x09,0x06, // 80 P - 0x3E,0x41,0x51,0x21,0x5E, // 81 Q - 0x7F,0x09,0x19,0x29,0x46, // 82 R - 0x46,0x49,0x49,0x49,0x31, // 83 S - 0x01,0x01,0x7F,0x01,0x01, // 84 T - 0x3F,0x40,0x40,0x40,0x3F, // 85 U - 0x1F,0x20,0x40,0x20,0x1F, // 86 V - 0x7F,0x20,0x18,0x20,0x7F, // 87 W - 0x63,0x14,0x08,0x14,0x63, // 88 X - 0x03,0x04,0x78,0x04,0x03, // 89 Y - 0x61,0x51,0x49,0x45,0x43, // 90 Z - 0x00,0x00,0x7F,0x41,0x41, // 91 [ - 0x02,0x04,0x08,0x10,0x20, // 92 backslash - 0x41,0x41,0x7F,0x00,0x00, // 93 ] - 0x04,0x02,0x01,0x02,0x04, // 94 ^ - 0x40,0x40,0x40,0x40,0x40, // 95 _ - 0x00,0x01,0x02,0x04,0x00, // 96 ` - 0x20,0x54,0x54,0x54,0x78, // 97 a - 0x7F,0x48,0x44,0x44,0x38, // 98 b - 0x38,0x44,0x44,0x44,0x20, // 99 c - 0x38,0x44,0x44,0x48,0x7F, // 100 d - 0x38,0x54,0x54,0x54,0x18, // 101 e - 0x08,0x7E,0x09,0x01,0x02, // 102 f - 0x08,0x14,0x54,0x54,0x3C, // 103 g - 0x7F,0x08,0x04,0x04,0x78, // 104 h - 0x00,0x44,0x7D,0x40,0x00, // 105 i - 0x20,0x40,0x44,0x3D,0x00, // 106 j - 0x00,0x7F,0x10,0x28,0x44, // 107 k - 0x00,0x41,0x7F,0x40,0x00, // 108 l - 0x7C,0x04,0x18,0x04,0x78, // 109 m - 0x7C,0x08,0x04,0x04,0x78, // 110 n - 0x38,0x44,0x44,0x44,0x38, // 111 o - 0x7C,0x14,0x14,0x14,0x08, // 112 p - 0x08,0x14,0x14,0x18,0x7C, // 113 q - 0x7C,0x08,0x04,0x04,0x08, // 114 r - 0x48,0x54,0x54,0x54,0x20, // 115 s - 0x04,0x3F,0x44,0x40,0x20, // 116 t - 0x3C,0x40,0x40,0x20,0x7C, // 117 u - 0x1C,0x20,0x40,0x20,0x1C, // 118 v - 0x3C,0x40,0x30,0x40,0x3C, // 119 w - 0x44,0x28,0x10,0x28,0x44, // 120 x - 0x0C,0x50,0x50,0x50,0x3C, // 121 y - 0x44,0x64,0x54,0x4C,0x44, // 122 z - 0x00,0x08,0x36,0x41,0x00, // 123 { - 0x00,0x00,0x7F,0x00,0x00, // 124 | - 0x00,0x41,0x36,0x08,0x00, // 125 } - 0x10,0x08,0x08,0x10,0x08, // 126 ~ -}; - -#endif diff --git a/examples/host/midi2_host/src/main.c b/examples/host/midi2_host/src/main.c deleted file mode 100644 index e23fbfc87..000000000 --- a/examples/host/midi2_host/src/main.c +++ /dev/null @@ -1,326 +0,0 @@ -/* - * 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. - */ - -// MIDI 2.0 Host Receiver Example -// -// Receives UMP from a MIDI 2.0 Device via PIO-USB Host. -// SSD1306 OLED (I2C, 128x64) shows boot checklist then received UMP messages. - -#include -#include -#include "bsp/board_api.h" -#include "tusb.h" -#include "class/midi/midi2_host.h" -#include "class/midi/midi.h" -#include "display.h" - -//--------------------------------------------------------------------+ -// Checklist state -//--------------------------------------------------------------------+ - -static checklist_t ck = { 0 }; -static uint32_t note_count = 0; -static uint8_t midi2_idx = 0xFF; // invalid until mount - -//--------------------------------------------------------------------+ -// Note name helper -//--------------------------------------------------------------------+ - -static const char* NOTE_NAMES[] = { - "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" -}; - -static void note_name(uint8_t pitch, char* buf, size_t len) { - int octave = (pitch / 12) - 1; - snprintf(buf, len, "%s%d", NOTE_NAMES[pitch % 12], octave); -} - -//--------------------------------------------------------------------+ -// UMP Message Type names -//--------------------------------------------------------------------+ - -static const char* mt_name(uint8_t mt) { - switch (mt) { - case 0x0: return "Utility"; - case 0x1: return "System"; - case 0x2: return "M1 CVM"; - case 0x3: return "SysEx7"; - case 0x4: return "M2 CVM"; - case 0x5: return "SysEx8"; - case 0xD: return "Flex"; - case 0xF: return "Stream"; - default: return "?"; - } -} - -//--------------------------------------------------------------------+ -// UMP decoder - extract and display MIDI 2.0 messages -//--------------------------------------------------------------------+ - -static void decode_ump(const uint32_t* words, uint8_t word_count) { - uint8_t mt = (uint8_t)((words[0] >> 28) & 0x0F); - char line[64]; - - if (mt == 0x4 && word_count >= 2) { - // MIDI 2.0 Channel Voice Message - uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F); - uint8_t channel = (uint8_t)((words[0] >> 16) & 0x0F); - - switch (status) { - case 0x9: { // Note On - uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); - uint16_t vel = (uint16_t)((words[1] >> 16) & 0xFFFF); - char nn[6]; - note_name(pitch, nn, sizeof(nn)); - snprintf(line, sizeof(line), "NoteOn %s ch%u vel=0x%04X", nn, channel, vel); - display_log(line, 0x07E0); // green - note_count++; - break; - } - case 0x8: { // Note Off - uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); - char nn[6]; - note_name(pitch, nn, sizeof(nn)); - snprintf(line, sizeof(line), "NoteOff %s ch%u", nn, channel); - display_log(line, 0x8410); // grey - break; - } - case 0xB: { // CC - uint8_t idx = (uint8_t)((words[0] >> 8) & 0x7F); - snprintf(line, sizeof(line), "CC%-3u = 0x%08lX", idx, (unsigned long)words[1]); - display_log(line, 0x001F); // blue - break; - } - case 0xC: { // Program Change - uint8_t prog = (uint8_t)((words[1] >> 24) & 0x7F); - snprintf(line, sizeof(line), "ProgChg %u", prog); - display_log(line, 0xFFE0); // yellow - break; - } - case 0xE: { // Pitch Bend - snprintf(line, sizeof(line), "PBend = 0x%08lX", (unsigned long)words[1]); - display_log(line, 0xF81F); // magenta - break; - } - case 0xD: { // Channel Pressure - snprintf(line, sizeof(line), "CPress = 0x%08lX", (unsigned long)words[1]); - display_log(line, 0xFC10); // orange - break; - } - case 0xA: { // Poly Pressure - uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); - char nn[6]; - note_name(pitch, nn, sizeof(nn)); - snprintf(line, sizeof(line), "PolyP %s = 0x%08lX", nn, (unsigned long)words[1]); - display_log(line, 0xFC10); - break; - } - case 0xF: { // Per-Note Management - uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); - uint8_t flags = (uint8_t)(words[0] & 0xFF); - snprintf(line, sizeof(line), "PN-Mgmt note=%u flags=0x%02X", pitch, flags); - display_log(line, 0x07FF); // cyan - break; - } - default: { - snprintf(line, sizeof(line), "M2CVM status=0x%X", status); - display_log(line, 0xFFFF); - break; - } - } - } else if (mt == 0x0) { - // Utility (JR Timestamp, NOOP) - uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F); - if (status == 0x2) { - uint16_t ts = words[0] & 0xFFFF; - snprintf(line, sizeof(line), "JR-TS = 0x%04X", ts); - display_log(line, 0x8410); - } - } else { - snprintf(line, sizeof(line), "MT=0x%X (%s) w0=0x%08lX", - mt, mt_name(mt), (unsigned long)words[0]); - display_log(line, 0xFFFF); - } -} - -//--------------------------------------------------------------------+ -// MIDI 2.0 Host Callbacks -//--------------------------------------------------------------------+ - -void tuh_midi2_descriptor_cb(uint8_t idx, const tuh_midi2_descriptor_cb_t* d) { - (void)idx; - ck.descriptor_parsed = true; - char line[48]; - - snprintf(line, sizeof(line), "bcdMSC=0x%02X%02X proto=%s", - d->bcdMSC_hi, d->bcdMSC_lo, - d->protocol_version ? "MIDI2" : "MIDI1"); - display_log(line, 0x07E0); - - snprintf(line, sizeof(line), "Cables: RX=%u TX=%u", - d->rx_cable_count, d->tx_cable_count); - display_log(line, 0x07E0); - - display_checklist_update(&ck); -} - -void tuh_midi2_mount_cb(uint8_t idx, const tuh_midi2_mount_cb_t* m) { - midi2_idx = idx; - ck.alt_setting_ok = true; - ck.mounted = true; - - char line[48]; - snprintf(line, sizeof(line), "Mounted addr=%u alt=%u", - m->daddr, m->alt_setting_active); - display_log(line, 0x07E0); - - if (m->protocol_version) { - display_log("MIDI 2.0 ready", 0x07E0); - } else { - display_log("MIDI 1.0 only", 0xFFE0); - } - - display_checklist_update(&ck); -} - -void tuh_midi2_rx_cb(uint8_t idx, uint32_t xferred_bytes) { - (void)xferred_bytes; - if (!ck.receiving) { - ck.receiving = true; - display_checklist_update(&ck); - } - - uint32_t words[16]; - while (1) { - uint32_t n = tuh_midi2_ump_read(idx, words, 16); - if (n == 0) break; - - // Decode complete UMP messages - 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; - decode_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; - ck.device_connected = false; - ck.descriptor_parsed = false; - ck.alt_setting_ok = false; - ck.mounted = false; - ck.receiving = false; - note_count = 0; - - display_log("Device disconnected", 0xF800); - display_checklist_update(&ck); -} - -//--------------------------------------------------------------------+ -// Main -//--------------------------------------------------------------------+ - -int main(void) { - board_init(); - - display_init(); - - ck.pwr_on = true; - display_checklist_update(&ck); - - // Init TinyUSB Host (BSP handles PIO-USB configuration via tuh_configure) - tusb_rhport_init_t host_init = { - .role = TUSB_ROLE_HOST, - .speed = TUSB_SPEED_FULL, - }; - tusb_init(BOARD_TUH_RHPORT, &host_init); - - ck.tusb_init = true; - ck.bus_active = true; - display_checklist_update(&ck); - - char dbg[40]; - snprintf(dbg, sizeof(dbg), "RHPORT=%u PIO_USB=%u", - BOARD_TUH_RHPORT, CFG_TUH_RPI_PIO_USB); - display_log(dbg, 0xFFE0); // yellow - display_status("Waiting for device..."); - - uint32_t last_status_ms = 0; - static uint32_t loop_count = 0; - - while (1) { - tuh_task(); - loop_count++; - - // Update status every 2 seconds - uint32_t now = tusb_time_millis_api(); - if (now - last_status_ms > 2000) { - last_status_ms = now; - char line[22]; - if (ck.receiving) { - snprintf(line, sizeof(line), "Notes:%lu", (unsigned long)note_count); - } else if (ck.mounted) { - snprintf(line, sizeof(line), "Mounted OK!"); - } else if (ck.device_connected) { - snprintf(line, sizeof(line), "Dev found, mounting.."); - } else { - snprintf(line, sizeof(line), "Wait.. t=%lu", (unsigned long)(now/1000)); - } - display_status(line); - } - } - - return 0; -} - -// Generic USB device mount/unmount (any class) -void tuh_mount_cb(uint8_t daddr) { - char line[32]; - uint16_t vid, pid; - tuh_vid_pid_get(daddr, &vid, &pid); - snprintf(line, sizeof(line), "USB %04X:%04X a%u", vid, pid, daddr); - display_log(line, 0x07E0); - ck.device_connected = true; - display_checklist_update(&ck); -} - -void tuh_umount_cb(uint8_t daddr) { - (void)daddr; - display_log("USB disconnected", 0xF800); - ck.device_connected = false; - ck.descriptor_parsed = false; - ck.alt_setting_ok = false; - ck.mounted = false; - ck.receiving = false; - display_checklist_update(&ck); -} diff --git a/examples/host/midi2_host/src/tusb_config.h b/examples/host/midi2_host/src/tusb_config.h deleted file mode 100644 index 650df97e1..000000000 --- a/examples/host/midi2_host/src/tusb_config.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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 - -#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 -// Waveshare RP2350-USB-A: PIO-USB on GP12/GP13 (USB-A Host port) -//-------------------------------------------------------------------- - -#define CFG_TUH_ENABLED 1 - -// PIO-USB Host on rhport 1 (USB-A connector on GP12/GP13) -#define CFG_TUH_RPI_PIO_USB 1 -#define BOARD_TUH_RHPORT 1 - -#ifndef BOARD_TUH_MAX_SPEED -#define BOARD_TUH_MAX_SPEED OPT_MODE_FULL_SPEED -#endif - -#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED - -//-------------------------------------------------------------------- -// Driver Configuration -//-------------------------------------------------------------------- - -#define CFG_TUH_ENUMERATION_BUFSIZE 256 - -#define CFG_TUH_HUB 0 -#define CFG_TUH_DEVICE_MAX 1 - -// MIDI 2.0 Host -#define CFG_TUH_MIDI2 1 -#define CFG_TUH_MIDI2_RX_BUFSIZE 512 -#define CFG_TUH_MIDI2_TX_BUFSIZE 512 - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/hw/bsp/rp2040/boards/waveshare_rp2350_usb_a/board.cmake b/hw/bsp/rp2040/boards/waveshare_rp2350_usb_a/board.cmake deleted file mode 100644 index e888c7fb3..000000000 --- a/hw/bsp/rp2040/boards/waveshare_rp2350_usb_a/board.cmake +++ /dev/null @@ -1,2 +0,0 @@ -set(PICO_PLATFORM rp2350-arm-s) -set(PICO_BOARD waveshare_rp2350_usb_a) diff --git a/hw/bsp/rp2040/boards/waveshare_rp2350_usb_a/board.h b/hw/bsp/rp2040/boards/waveshare_rp2350_usb_a/board.h deleted file mode 100644 index 0af8a695f..000000000 --- a/hw/bsp/rp2040/boards/waveshare_rp2350_usb_a/board.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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. - * - * This file is part of the TinyUSB stack. - */ - -/* metadata: - name: Waveshare RP2350-USB-A - url: https://www.waveshare.com/wiki/RP2350-USB-A -*/ - -#ifndef TUSB_BOARD_H -#define TUSB_BOARD_H - -#ifdef __cplusplus - extern "C" { -#endif - -//--------------------------------------------------------------------+ -// PIO_USB -//--------------------------------------------------------------------+ -// Waveshare RP2350-USB-A: PIO-USB on GP12 (D+) / GP13 (D-) -// PICO_DEFAULT_PIO_USB_DP_PIN already defined in SDK board header - -#ifdef __cplusplus - } -#endif - -#endif -- cgit v1.3.1 From 3efac1b28c4174b0734ef106ce8496fa39657b47 Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Sat, 16 May 2026 13:00:30 -0300 Subject: midi2: host example - simplify and rename to midi2_host Drop Feather-specific scaffolding (SSD1306 display, font, PIO-USB pin override) and rename the directory to midi2_host. The example is now a single main.c + tusb_config.h pair following the midi_rx pattern, with a printf-only UMP decoder for Channel Voice messages. Build-tested on adafruit_feather_rp2040_usb_host, daisyseed (stm32h7), mimxrt1010_evk, stm32f407disco, raspberry_pi_pico2, b_u585i_iot2a, portenta_c33, stm32h503nucleo, stlinkv3mini, feather_stm32f405. Ref #3571 --- examples/host/CMakeLists.txt | 2 +- examples/host/midi2_host/CMakeLists.txt | 29 +++ examples/host/midi2_host/CMakePresets.json | 6 + examples/host/midi2_host/Makefile | 13 + examples/host/midi2_host/src/main.c | 165 ++++++++++++ examples/host/midi2_host/src/tusb_config.h | 106 ++++++++ examples/host/midi2_host_feather/CMakeLists.txt | 38 --- examples/host/midi2_host_feather/Makefile | 27 -- examples/host/midi2_host_feather/src/display.c | 276 --------------------- examples/host/midi2_host_feather/src/display.h | 46 ---- examples/host/midi2_host_feather/src/font5x7.h | 107 -------- examples/host/midi2_host_feather/src/main.c | 267 -------------------- examples/host/midi2_host_feather/src/tusb_config.h | 91 ------- 13 files changed, 320 insertions(+), 853 deletions(-) create mode 100644 examples/host/midi2_host/CMakeLists.txt create mode 100644 examples/host/midi2_host/CMakePresets.json create mode 100644 examples/host/midi2_host/Makefile create mode 100644 examples/host/midi2_host/src/main.c create mode 100644 examples/host/midi2_host/src/tusb_config.h delete mode 100644 examples/host/midi2_host_feather/CMakeLists.txt delete mode 100644 examples/host/midi2_host_feather/Makefile delete mode 100644 examples/host/midi2_host_feather/src/display.c delete mode 100644 examples/host/midi2_host_feather/src/display.h delete mode 100644 examples/host/midi2_host_feather/src/font5x7.h delete mode 100644 examples/host/midi2_host_feather/src/main.c delete mode 100644 examples/host/midi2_host_feather/src/tusb_config.h (limited to 'examples/host') diff --git a/examples/host/CMakeLists.txt b/examples/host/CMakeLists.txt index 2cf2c10af..7c74e3c73 100644 --- a/examples/host/CMakeLists.txt +++ b/examples/host/CMakeLists.txt @@ -13,7 +13,7 @@ set(EXAMPLE_LIST device_info hid_controller midi_rx - midi2_host_feather + midi2_host msc_file_explorer msc_file_explorer_freertos ) diff --git a/examples/host/midi2_host/CMakeLists.txt b/examples/host/midi2_host/CMakeLists.txt new file mode 100644 index 000000000..0ec03bf5f --- /dev/null +++ b/examples/host/midi2_host/CMakeLists.txt @@ -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) diff --git a/examples/host/midi2_host/CMakePresets.json b/examples/host/midi2_host/CMakePresets.json new file mode 100644 index 000000000..5cd8971e9 --- /dev/null +++ b/examples/host/midi2_host/CMakePresets.json @@ -0,0 +1,6 @@ +{ + "version": 6, + "include": [ + "../../../hw/bsp/BoardPresets.json" + ] +} diff --git a/examples/host/midi2_host/Makefile b/examples/host/midi2_host/Makefile new file mode 100644 index 000000000..f8292385e --- /dev/null +++ b/examples/host/midi2_host/Makefile @@ -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 diff --git a/examples/host/midi2_host/src/main.c b/examples/host/midi2_host/src/main.c new file mode 100644 index 000000000..63b08318c --- /dev/null +++ b/examples/host/midi2_host/src/main.c @@ -0,0 +1,165 @@ +/* + * 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 +#include +#include "bsp/board_api.h" +#include "tusb.h" +#include "class/midi/midi2_host.h" + +//--------------------------------------------------------------------+ +// State +//--------------------------------------------------------------------+ + +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(); + } + + return 0; +} diff --git a/examples/host/midi2_host/src/tusb_config.h b/examples/host/midi2_host/src/tusb_config.h new file mode 100644 index 000000000..4bd6ef2ea --- /dev/null +++ b/examples/host/midi2_host/src/tusb_config.h @@ -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 diff --git a/examples/host/midi2_host_feather/CMakeLists.txt b/examples/host/midi2_host_feather/CMakeLists.txt deleted file mode 100644 index 5d34b8170..000000000 --- a/examples/host/midi2_host_feather/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -cmake_minimum_required(VERSION 3.20) - -include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) - -project(midi2_host_feather C CXX ASM) - -family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}) - -# This example requires PIO-USB and Pico SDK (I2C, SSD1306 display) -if(NOT FAMILY STREQUAL "rp2040") - return() -endif() - -add_executable(${PROJECT_NAME}) - -target_sources(${PROJECT_NAME} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c - ${CMAKE_CURRENT_SOURCE_DIR}/src/display.c -) - -target_include_directories(${PROJECT_NAME} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src -) - -family_configure_host_example(${PROJECT_NAME} noos) - -# Adafruit Feather RP2040 USB Host: PIO-USB on GP16/GP17 -target_compile_definitions(${PROJECT_NAME} PRIVATE - PIO_USB_DP_PIN_DEFAULT=16 -) -target_compile_options(${PROJECT_NAME} PRIVATE - -Wno-type-limits -) - -# SSD1306 display (I2C via STEMMA QT) -target_link_libraries(${PROJECT_NAME} PUBLIC - hardware_i2c -) diff --git a/examples/host/midi2_host_feather/Makefile b/examples/host/midi2_host_feather/Makefile deleted file mode 100644 index 933d19434..000000000 --- a/examples/host/midi2_host_feather/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -# This example requires RP2040 (PIO-USB, Pico SDK I2C, SSD1306 display) -ifeq (,$(findstring rp2040,$(FAMILY))) -$(info Skipping midi2_host_feather: requires FAMILY=rp2040) -all: - @: -.DEFAULT: - @: -else - -include ../../../hw/bsp/family_support.mk - -INC += \ - src \ - -# Example source -EXAMPLE_SOURCE += \ - src/main.c \ - src/display.c \ - -SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE)) - -# Suppress pre-existing warning -CFLAGS_GCC += -Wno-type-limits - -include ../../../hw/bsp/family_rules.mk - -endif diff --git a/examples/host/midi2_host_feather/src/display.c b/examples/host/midi2_host_feather/src/display.c deleted file mode 100644 index 91039fa16..000000000 --- a/examples/host/midi2_host_feather/src/display.c +++ /dev/null @@ -1,276 +0,0 @@ -/* - * 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. - */ - -// SSD1306 OLED display driver (128x64, I2C) for MIDI 2.0 Host example. -// Adafruit Feather RP2040 USB Host: I2C1 via STEMMA QT (SDA = GP2, SCL = GP3) -// -// Three display phases: -// 1. Splash : title + credits (shown during init) -// 2. Connecting : spinner animation while waiting for device -// 3. Live : header + 6 scrolling log lines + status bar - -#include "display.h" -#include -#include -#include "pico/stdlib.h" -#include "hardware/i2c.h" -#include "hardware/gpio.h" - -#define I2C_PORT i2c1 -#define I2C_SDA 2 -#define I2C_SCL 3 -#define I2C_FREQ 400000 -#define SSD1306_ADDR 0x3C - -#define SCR_W 128 -#define SCR_H 64 -#define PAGES (SCR_H / 8) -#define CHARS_PER_LINE 21 - -//--------------------------------------------------------------------+ -// Framebuffer -//--------------------------------------------------------------------+ - -static uint8_t fb[SCR_W * PAGES]; - -//--------------------------------------------------------------------+ -// Log buffer (6 lines for live view) -//--------------------------------------------------------------------+ - -#define LOG_LINES 6 -static char log_lines[LOG_LINES][CHARS_PER_LINE + 1]; -static int log_count = 0; - -//--------------------------------------------------------------------+ -// Minimal 5x7 font (ASCII 32-126) -//--------------------------------------------------------------------+ - -#include "font5x7.h" - -//--------------------------------------------------------------------+ -// SSD1306 I2C low-level -//--------------------------------------------------------------------+ - -static void ssd_cmd(uint8_t cmd) { - uint8_t buf[2] = { 0x00, cmd }; - i2c_write_blocking(I2C_PORT, SSD1306_ADDR, buf, 2, false); -} - -static void ssd_data(const uint8_t* data, size_t len) { - uint8_t buf[SCR_W + 1]; - buf[0] = 0x40; - size_t chunk = (len > SCR_W) ? SCR_W : len; - memcpy(buf + 1, data, chunk); - i2c_write_blocking(I2C_PORT, SSD1306_ADDR, buf, chunk + 1, false); -} - -static void ssd_flush(void) { - ssd_cmd(0x21); ssd_cmd(0); ssd_cmd(127); - ssd_cmd(0x22); ssd_cmd(0); ssd_cmd(7); - for (int page = 0; page < PAGES; page++) { - ssd_data(&fb[page * SCR_W], SCR_W); - } -} - -//--------------------------------------------------------------------+ -// Framebuffer drawing -//--------------------------------------------------------------------+ - -static void fb_clear(void) { - memset(fb, 0, sizeof(fb)); -} - -static void fb_char(int x, int y, char c) { - if (c < 32 || c > 126) c = '?'; - const uint8_t* glyph = font5x7 + (c - 32) * 5; - int page = y / 8; - int bit_offset = y % 8; - - if (page >= PAGES || x + 5 > SCR_W) return; - - for (int col = 0; col < 5; col++) { - uint8_t column_data = glyph[col]; - fb[(page * SCR_W) + x + col] |= (uint8_t)(column_data << bit_offset); - if (bit_offset > 0 && page + 1 < PAGES) { - fb[((page + 1) * SCR_W) + x + col] |= (uint8_t)(column_data >> (8 - bit_offset)); - } - } -} - -static void fb_string(int x, int y, const char* str) { - while (*str) { - fb_char(x, y, *str); - x += 6; - if (x + 6 > SCR_W) break; - str++; - } -} - -// Draw a horizontal line (1px) -static void fb_hline(int x0, int x1, int y) { - int page = y / 8; - uint8_t mask = (uint8_t)(1 << (y % 8)); - if (page >= PAGES) return; - for (int x = x0; x <= x1 && x < SCR_W; x++) { - fb[page * SCR_W + x] |= mask; - } -} - -//--------------------------------------------------------------------+ -// Phase 1: Splash -//--------------------------------------------------------------------+ - -void display_init(void) { - i2c_init(I2C_PORT, I2C_FREQ); - gpio_set_function(I2C_SDA, GPIO_FUNC_I2C); - gpio_set_function(I2C_SCL, GPIO_FUNC_I2C); - gpio_pull_up(I2C_SDA); - gpio_pull_up(I2C_SCL); - - sleep_ms(100); - - // SSD1306 init sequence - ssd_cmd(0xAE); - ssd_cmd(0xD5); ssd_cmd(0x80); - ssd_cmd(0xA8); ssd_cmd(0x3F); - ssd_cmd(0xD3); ssd_cmd(0x00); - ssd_cmd(0x40); - ssd_cmd(0x8D); ssd_cmd(0x14); - ssd_cmd(0x20); ssd_cmd(0x00); - ssd_cmd(0xA1); - ssd_cmd(0xC8); - ssd_cmd(0xDA); ssd_cmd(0x12); - ssd_cmd(0x81); ssd_cmd(0xCF); - ssd_cmd(0xD9); ssd_cmd(0xF1); - ssd_cmd(0xDB); ssd_cmd(0x40); - ssd_cmd(0xA4); - ssd_cmd(0xA6); - ssd_cmd(0xAF); - - // Splash screen - fb_clear(); - fb_string(4, 8, "TinyUSB MIDI 2.0"); - fb_hline(4, 123, 18); - fb_string(16, 24, "USB Host Demo"); - fb_string(4, 40, "Feather RP2040"); - fb_string(4, 52, "PIO-USB + SSD1306"); - ssd_flush(); -} - -//--------------------------------------------------------------------+ -// Phase 2: Connecting (spinner) -//--------------------------------------------------------------------+ - -static const char SPINNER[] = "|/-\\"; - -void display_connecting(uint32_t elapsed_ms) { - int idx = (int)((elapsed_ms / 200) % 4); - - // Clear bottom 2 pages for spinner area - memset(&fb[6 * SCR_W], 0, SCR_W); - memset(&fb[7 * SCR_W], 0, SCR_W); - - char line[CHARS_PER_LINE + 1]; - snprintf(line, sizeof(line), "%c Waiting device...", SPINNER[idx]); - fb_string(4, 52, line); - ssd_flush(); -} - -//--------------------------------------------------------------------+ -// Phase 3: Live view -//--------------------------------------------------------------------+ - -// Layout: -// y=0 : "TinyUSB MIDI 2.0 Host" (header, fixed) -// y=9 : separator line -// y=10 : log line 0 -// y=18 : log line 1 -// y=26 : log line 2 -// y=34 : log line 3 -// y=42 : log line 4 -// y=50 : log line 5 -// y=57 : separator line -// y=58 : status bar - -static bool live_mode = false; - -static void draw_live_frame(void) { - fb_clear(); - fb_string(0, 0, "TinyUSB MIDI 2.0 Host"); - fb_hline(0, 127, 9); - fb_hline(0, 127, 57); -} - -void display_live_begin(void) { - live_mode = true; - log_count = 0; - memset(log_lines, 0, sizeof(log_lines)); - - draw_live_frame(); - fb_string(0, 58, "Connected"); - ssd_flush(); -} - -void display_log(const char* text, uint16_t color) { - (void)color; - - if (!live_mode) { - display_live_begin(); - } - - // Scroll up if full - if (log_count >= LOG_LINES) { - for (int i = 0; i < LOG_LINES - 1; i++) { - strncpy(log_lines[i], log_lines[i + 1], CHARS_PER_LINE); - log_lines[i][CHARS_PER_LINE] = '\0'; - } - log_count = LOG_LINES - 1; - } - - strncpy(log_lines[log_count], text, CHARS_PER_LINE); - log_lines[log_count][CHARS_PER_LINE] = '\0'; - log_count++; - - // Redraw: header + log + status - draw_live_frame(); - for (int i = 0; i < log_count && i < LOG_LINES; i++) { - fb_string(0, 10 + i * 8, log_lines[i]); - } - ssd_flush(); -} - -void display_status(const char* text) { - if (!live_mode) return; - - // Clear status area (page 7) - memset(&fb[7 * SCR_W], 0, SCR_W); - // Redraw separator (might have been cleared) - fb_hline(0, 127, 57); - - char status[CHARS_PER_LINE + 1]; - strncpy(status, text, CHARS_PER_LINE); - status[CHARS_PER_LINE] = '\0'; - fb_string(0, 58, status); - ssd_flush(); -} diff --git a/examples/host/midi2_host_feather/src/display.h b/examples/host/midi2_host_feather/src/display.h deleted file mode 100644 index ad8ead81d..000000000 --- a/examples/host/midi2_host_feather/src/display.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 DISPLAY_H_ -#define DISPLAY_H_ - -#include -#include - -// Initialize SSD1306 display and show splash screen -void display_init(void); - -// Show waiting/connecting animation (call periodically) -void display_connecting(uint32_t elapsed_ms); - -// Transition to live message view -void display_live_begin(void); - -// Add a line to the scrolling log area (6 visible lines) -void display_log(const char* text, uint16_t color); - -// Update the status bar (bottom line) -void display_status(const char* text); - -#endif diff --git a/examples/host/midi2_host_feather/src/font5x7.h b/examples/host/midi2_host_feather/src/font5x7.h deleted file mode 100644 index e515faac1..000000000 --- a/examples/host/midi2_host_feather/src/font5x7.h +++ /dev/null @@ -1,107 +0,0 @@ -// Minimal 5x7 font, ASCII 32-126. Public domain. -// 5 bytes per character (5 columns, 7 rows, LSB=top row) - -#ifndef FONT5X7_H_ -#define FONT5X7_H_ - -#include - -static const uint8_t font5x7[] = { - 0x00,0x00,0x00,0x00,0x00, // 32 (space) - 0x00,0x00,0x5F,0x00,0x00, // 33 ! - 0x00,0x07,0x00,0x07,0x00, // 34 " - 0x14,0x7F,0x14,0x7F,0x14, // 35 # - 0x24,0x2A,0x7F,0x2A,0x12, // 36 $ - 0x23,0x13,0x08,0x64,0x62, // 37 % - 0x36,0x49,0x55,0x22,0x50, // 38 & - 0x00,0x05,0x03,0x00,0x00, // 39 ' - 0x00,0x1C,0x22,0x41,0x00, // 40 ( - 0x00,0x41,0x22,0x1C,0x00, // 41 ) - 0x08,0x2A,0x1C,0x2A,0x08, // 42 * - 0x08,0x08,0x3E,0x08,0x08, // 43 + - 0x00,0x50,0x30,0x00,0x00, // 44 , - 0x08,0x08,0x08,0x08,0x08, // 45 - - 0x00,0x60,0x60,0x00,0x00, // 46 . - 0x20,0x10,0x08,0x04,0x02, // 47 / - 0x3E,0x51,0x49,0x45,0x3E, // 48 0 - 0x00,0x42,0x7F,0x40,0x00, // 49 1 - 0x42,0x61,0x51,0x49,0x46, // 50 2 - 0x21,0x41,0x45,0x4B,0x31, // 51 3 - 0x18,0x14,0x12,0x7F,0x10, // 52 4 - 0x27,0x45,0x45,0x45,0x39, // 53 5 - 0x3C,0x4A,0x49,0x49,0x30, // 54 6 - 0x01,0x71,0x09,0x05,0x03, // 55 7 - 0x36,0x49,0x49,0x49,0x36, // 56 8 - 0x06,0x49,0x49,0x29,0x1E, // 57 9 - 0x00,0x36,0x36,0x00,0x00, // 58 : - 0x00,0x56,0x36,0x00,0x00, // 59 ; - 0x00,0x08,0x14,0x22,0x41, // 60 < - 0x14,0x14,0x14,0x14,0x14, // 61 = - 0x41,0x22,0x14,0x08,0x00, // 62 > - 0x02,0x01,0x51,0x09,0x06, // 63 ? - 0x32,0x49,0x79,0x41,0x3E, // 64 @ - 0x7E,0x11,0x11,0x11,0x7E, // 65 A - 0x7F,0x49,0x49,0x49,0x36, // 66 B - 0x3E,0x41,0x41,0x41,0x22, // 67 C - 0x7F,0x41,0x41,0x22,0x1C, // 68 D - 0x7F,0x49,0x49,0x49,0x41, // 69 E - 0x7F,0x09,0x09,0x01,0x01, // 70 F - 0x3E,0x41,0x41,0x51,0x32, // 71 G - 0x7F,0x08,0x08,0x08,0x7F, // 72 H - 0x00,0x41,0x7F,0x41,0x00, // 73 I - 0x20,0x40,0x41,0x3F,0x01, // 74 J - 0x7F,0x08,0x14,0x22,0x41, // 75 K - 0x7F,0x40,0x40,0x40,0x40, // 76 L - 0x7F,0x02,0x04,0x02,0x7F, // 77 M - 0x7F,0x04,0x08,0x10,0x7F, // 78 N - 0x3E,0x41,0x41,0x41,0x3E, // 79 O - 0x7F,0x09,0x09,0x09,0x06, // 80 P - 0x3E,0x41,0x51,0x21,0x5E, // 81 Q - 0x7F,0x09,0x19,0x29,0x46, // 82 R - 0x46,0x49,0x49,0x49,0x31, // 83 S - 0x01,0x01,0x7F,0x01,0x01, // 84 T - 0x3F,0x40,0x40,0x40,0x3F, // 85 U - 0x1F,0x20,0x40,0x20,0x1F, // 86 V - 0x7F,0x20,0x18,0x20,0x7F, // 87 W - 0x63,0x14,0x08,0x14,0x63, // 88 X - 0x03,0x04,0x78,0x04,0x03, // 89 Y - 0x61,0x51,0x49,0x45,0x43, // 90 Z - 0x00,0x00,0x7F,0x41,0x41, // 91 [ - 0x02,0x04,0x08,0x10,0x20, // 92 backslash - 0x41,0x41,0x7F,0x00,0x00, // 93 ] - 0x04,0x02,0x01,0x02,0x04, // 94 ^ - 0x40,0x40,0x40,0x40,0x40, // 95 _ - 0x00,0x01,0x02,0x04,0x00, // 96 ` - 0x20,0x54,0x54,0x54,0x78, // 97 a - 0x7F,0x48,0x44,0x44,0x38, // 98 b - 0x38,0x44,0x44,0x44,0x20, // 99 c - 0x38,0x44,0x44,0x48,0x7F, // 100 d - 0x38,0x54,0x54,0x54,0x18, // 101 e - 0x08,0x7E,0x09,0x01,0x02, // 102 f - 0x08,0x14,0x54,0x54,0x3C, // 103 g - 0x7F,0x08,0x04,0x04,0x78, // 104 h - 0x00,0x44,0x7D,0x40,0x00, // 105 i - 0x20,0x40,0x44,0x3D,0x00, // 106 j - 0x00,0x7F,0x10,0x28,0x44, // 107 k - 0x00,0x41,0x7F,0x40,0x00, // 108 l - 0x7C,0x04,0x18,0x04,0x78, // 109 m - 0x7C,0x08,0x04,0x04,0x78, // 110 n - 0x38,0x44,0x44,0x44,0x38, // 111 o - 0x7C,0x14,0x14,0x14,0x08, // 112 p - 0x08,0x14,0x14,0x18,0x7C, // 113 q - 0x7C,0x08,0x04,0x04,0x08, // 114 r - 0x48,0x54,0x54,0x54,0x20, // 115 s - 0x04,0x3F,0x44,0x40,0x20, // 116 t - 0x3C,0x40,0x40,0x20,0x7C, // 117 u - 0x1C,0x20,0x40,0x20,0x1C, // 118 v - 0x3C,0x40,0x30,0x40,0x3C, // 119 w - 0x44,0x28,0x10,0x28,0x44, // 120 x - 0x0C,0x50,0x50,0x50,0x3C, // 121 y - 0x44,0x64,0x54,0x4C,0x44, // 122 z - 0x00,0x08,0x36,0x41,0x00, // 123 { - 0x00,0x00,0x7F,0x00,0x00, // 124 | - 0x00,0x41,0x36,0x08,0x00, // 125 } - 0x10,0x08,0x08,0x10,0x08, // 126 ~ -}; - -#endif diff --git a/examples/host/midi2_host_feather/src/main.c b/examples/host/midi2_host_feather/src/main.c deleted file mode 100644 index 7c996355d..000000000 --- a/examples/host/midi2_host_feather/src/main.c +++ /dev/null @@ -1,267 +0,0 @@ -/* - * 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. - */ - -// MIDI 2.0 Host Example: Adafruit Feather RP2040 USB Host -// -// Receives UMP from a MIDI 2.0 Device via PIO-USB (GP16/GP17). -// SSD1306 OLED (I2C1, GP2/GP3) shows splash, spinner, then live UMP messages. - -#include -#include -#include "bsp/board_api.h" -#include "tusb.h" -#include "hardware/gpio.h" -#include "class/midi/midi2_host.h" -#include "class/midi/midi.h" -#include "display.h" - -//--------------------------------------------------------------------+ -// State -//--------------------------------------------------------------------+ - -static uint32_t note_count = 0; -static uint8_t midi2_idx = 0xFF; -static bool device_connected = false; -static bool mounted = false; - -//--------------------------------------------------------------------+ -// Note name helper -//--------------------------------------------------------------------+ - -static const char* NOTE_NAMES[] = { - "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" -}; - -static void note_name(uint8_t pitch, char* buf, size_t len) { - int octave = (pitch / 12) - 1; - snprintf(buf, len, "%s%d", NOTE_NAMES[pitch % 12], octave); -} - -//--------------------------------------------------------------------+ -// UMP decoder -//--------------------------------------------------------------------+ - -static void decode_ump(const uint32_t* words, uint8_t word_count) { - uint8_t mt = (uint8_t)((words[0] >> 28) & 0x0F); - char line[64]; - - if (mt == 0x4 && word_count >= 2) { - uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F); - uint8_t channel = (uint8_t)((words[0] >> 16) & 0x0F); - - switch (status) { - case 0x9: { - uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); - uint16_t vel = (uint16_t)((words[1] >> 16) & 0xFFFF); - char nn[6]; - note_name(pitch, nn, sizeof(nn)); - snprintf(line, sizeof(line), "NoteOn %s ch%u v%04X", nn, channel, vel); - display_log(line, 0x07E0); - note_count++; - break; - } - case 0x8: { - uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); - char nn[6]; - note_name(pitch, nn, sizeof(nn)); - snprintf(line, sizeof(line), "NoteOff %s ch%u", nn, channel); - display_log(line, 0x8410); - break; - } - case 0xB: { - uint8_t idx = (uint8_t)((words[0] >> 8) & 0x7F); - snprintf(line, sizeof(line), "CC%-3u %08lX", idx, (unsigned long)words[1]); - display_log(line, 0x001F); - break; - } - case 0xC: { - uint8_t prog = (uint8_t)((words[1] >> 24) & 0x7F); - snprintf(line, sizeof(line), "ProgChg %u", prog); - display_log(line, 0xFFE0); - break; - } - case 0xE: { - snprintf(line, sizeof(line), "PBend %08lX", (unsigned long)words[1]); - display_log(line, 0xF81F); - break; - } - case 0xD: { - snprintf(line, sizeof(line), "CPress %08lX", (unsigned long)words[1]); - display_log(line, 0xFC10); - break; - } - case 0xA: { - uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); - char nn[6]; - note_name(pitch, nn, sizeof(nn)); - snprintf(line, sizeof(line), "PolyP %s %08lX", nn, (unsigned long)words[1]); - display_log(line, 0xFC10); - break; - } - case 0xF: { - uint8_t pitch = (uint8_t)((words[0] >> 8) & 0x7F); - uint8_t flags = (uint8_t)(words[0] & 0xFF); - snprintf(line, sizeof(line), "PN-Mgmt n%u f%02X", pitch, flags); - display_log(line, 0x07FF); - break; - } - default: { - snprintf(line, sizeof(line), "M2CVM s=0x%X", status); - display_log(line, 0xFFFF); - break; - } - } - } else if (mt == 0x0) { - uint8_t status = (uint8_t)((words[0] >> 20) & 0x0F); - if (status == 0x2) { - uint16_t ts = words[0] & 0xFFFF; - snprintf(line, sizeof(line), "JR-TS %04X", ts); - display_log(line, 0x8410); - } - } else { - snprintf(line, sizeof(line), "MT=0x%X w0=%08lX", - mt, (unsigned long)words[0]); - display_log(line, 0xFFFF); - } -} - -//--------------------------------------------------------------------+ -// MIDI 2.0 Host Callbacks -//--------------------------------------------------------------------+ - -void tuh_midi2_descriptor_cb(uint8_t idx, const tuh_midi2_descriptor_cb_t* d) { - (void)idx; - char line[48]; - snprintf(line, sizeof(line), "%s RX=%u TX=%u", - d->protocol_version ? "MIDI2" : "MIDI1", - d->rx_cable_count, d->tx_cable_count); - display_log(line, 0x07E0); -} - -void tuh_midi2_mount_cb(uint8_t idx, const tuh_midi2_mount_cb_t* m) { - midi2_idx = idx; - mounted = true; - - display_live_begin(); - - if (m->protocol_version) { - display_log("MIDI 2.0 ready", 0x07E0); - } else { - display_log("MIDI 1.0 device", 0xFFE0); - } - display_status("Receiving..."); -} - -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; - decode_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; - device_connected = false; - mounted = false; - note_count = 0; - display_log("Disconnected", 0xF800); - display_status("Waiting..."); -} - -//--------------------------------------------------------------------+ -// Main -//--------------------------------------------------------------------+ - -int main(void) { - board_init(); - - // Enable 5V to USB-A port (Feather RP2040 USB Host: GP18) - gpio_init(18); - gpio_set_dir(18, GPIO_OUT); - gpio_put(18, 1); - - display_init(); - sleep_ms(1500); - - tusb_rhport_init_t host_init = { - .role = TUSB_ROLE_HOST, - .speed = TUSB_SPEED_FULL, - }; - tusb_init(BOARD_TUH_RHPORT, &host_init); - - uint32_t last_status_ms = 0; - - while (1) { - tuh_task(); - - uint32_t now = tusb_time_millis_api(); - - if (!mounted) { - // Spinner while waiting for device - if (now - last_status_ms > 200) { - last_status_ms = now; - display_connecting(now); - } - } else { - // Update note count every 2 seconds - if (now - last_status_ms > 2000) { - last_status_ms = now; - char line[22]; - snprintf(line, sizeof(line), "Notes: %lu", (unsigned long)note_count); - display_status(line); - } - } - } - - return 0; -} - -// Generic USB device mount/unmount -void tuh_mount_cb(uint8_t daddr) { - (void)daddr; - device_connected = true; -} - -void tuh_umount_cb(uint8_t daddr) { - (void)daddr; - device_connected = false; - mounted = false; -} diff --git a/examples/host/midi2_host_feather/src/tusb_config.h b/examples/host/midi2_host_feather/src/tusb_config.h deleted file mode 100644 index 362d0e2d0..000000000 --- a/examples/host/midi2_host_feather/src/tusb_config.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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 - -#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 -// Adafruit Feather RP2040 USB Host: PIO-USB on GP16/GP17 (USB-A Host port) -//--------------------------------------------------------------------+ - -#define CFG_TUH_ENABLED 1 - -// PIO-USB Host on rhport 1 (USB-A connector on GP16/GP17) -#define CFG_TUH_RPI_PIO_USB 1 -#define BOARD_TUH_RHPORT 1 - -#ifndef BOARD_TUH_MAX_SPEED -#define BOARD_TUH_MAX_SPEED OPT_MODE_FULL_SPEED -#endif - -#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED - -//--------------------------------------------------------------------+ -// Driver Configuration -//--------------------------------------------------------------------+ - -#define CFG_TUH_ENUMERATION_BUFSIZE 256 - -#define CFG_TUH_HUB 0 -#define CFG_TUH_DEVICE_MAX 1 - -// MIDI 2.0 Host -#define CFG_TUH_MIDI2 1 -#define CFG_TUH_MIDI2_RX_BUFSIZE 512 -#define CFG_TUH_MIDI2_TX_BUFSIZE 512 - -#ifdef __cplusplus -} -#endif - -#endif -- cgit v1.3.1 From 214de5181c7d011035f0071b3bb99c1c79206876 Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Sat, 16 May 2026 20:39:20 -0300 Subject: midi2: fix CI matrix with only.txt and skip.txt Restrict midi2_host build to MCUs with USB host support (cloned from midi_rx) and skip SAMD11 from midi2_device (ROM overflow on link). --- examples/device/midi2_device/skip.txt | 1 + examples/host/midi2_host/only.txt | 34 ++++++++++++++++++++++++++++++++++ examples/host/midi2_host/skip.txt | 1 + 3 files changed, 36 insertions(+) create mode 100644 examples/device/midi2_device/skip.txt create mode 100644 examples/host/midi2_host/only.txt create mode 100644 examples/host/midi2_host/skip.txt (limited to 'examples/host') diff --git a/examples/device/midi2_device/skip.txt b/examples/device/midi2_device/skip.txt new file mode 100644 index 000000000..eadb6e74a --- /dev/null +++ b/examples/device/midi2_device/skip.txt @@ -0,0 +1 @@ +mcu:SAMD11 diff --git a/examples/host/midi2_host/only.txt b/examples/host/midi2_host/only.txt new file mode 100644 index 000000000..c71aacd87 --- /dev/null +++ b/examples/host/midi2_host/only.txt @@ -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 diff --git a/examples/host/midi2_host/skip.txt b/examples/host/midi2_host/skip.txt new file mode 100644 index 000000000..308796869 --- /dev/null +++ b/examples/host/midi2_host/skip.txt @@ -0,0 +1 @@ +board:lpcxpresso54114 -- cgit v1.3.1 From 7c214f9d4f47cfadf18fc7e67d15cc22b1bc6f99 Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Sat, 16 May 2026 21:35:21 -0300 Subject: midi2: add sources to TINYUSB_SRC_C, skip family Make build was missing midi2_device.c/midi2_host.c from src/tinyusb.mk. Skip stm32h7s3nucleo board which exposes a pre-existing uninitialized warning in its board.h (board_init2) under Make+LTO. --- examples/device/midi2_device/skip.txt | 1 + examples/host/midi2_host/skip.txt | 1 + src/tinyusb.mk | 2 ++ 3 files changed, 4 insertions(+) (limited to 'examples/host') diff --git a/examples/device/midi2_device/skip.txt b/examples/device/midi2_device/skip.txt index eadb6e74a..3bc342bff 100644 --- a/examples/device/midi2_device/skip.txt +++ b/examples/device/midi2_device/skip.txt @@ -1 +1,2 @@ mcu:SAMD11 +board:stm32h7s3nucleo diff --git a/examples/host/midi2_host/skip.txt b/examples/host/midi2_host/skip.txt index 308796869..ee7ed0fc0 100644 --- a/examples/host/midi2_host/skip.txt +++ b/examples/host/midi2_host/skip.txt @@ -1 +1,2 @@ board:lpcxpresso54114 +board:stm32h7s3nucleo diff --git a/src/tinyusb.mk b/src/tinyusb.mk index e3ef35dcf..365043927 100644 --- a/src/tinyusb.mk +++ b/src/tinyusb.mk @@ -10,6 +10,7 @@ TINYUSB_SRC_C += \ src/class/dfu/dfu_rt_device.c \ src/class/hid/hid_device.c \ src/class/midi/midi_device.c \ + src/class/midi/midi2_device.c \ src/class/msc/msc_device.c \ src/class/mtp/mtp_device.c \ src/class/net/ecm_rndis_device.c \ @@ -23,5 +24,6 @@ TINYUSB_SRC_C += \ src/class/cdc/cdc_host.c \ src/class/hid/hid_host.c \ src/class/midi/midi_host.c \ + src/class/midi/midi2_host.c \ src/class/msc/msc_host.c \ src/class/vendor/vendor_host.c \ -- cgit v1.3.1 From 7cf579dd49ab84163497ebc4348d208794a7e635 Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Sat, 16 May 2026 22:37:45 -0300 Subject: midi2: skip 3 more boards with same tcpp0203 BSP bug stm32h573i_dk, stm32n657nucleo and stm32n6570dk have the same pre-existing uninitialized io_ctx.GetTick in their board.h that we already skip on stm32h7s3nucleo. Trips with Make+LTO; CMake passes. --- examples/device/midi2_device/skip.txt | 6 ++++++ examples/host/midi2_host/skip.txt | 6 ++++++ 2 files changed, 12 insertions(+) (limited to 'examples/host') diff --git a/examples/device/midi2_device/skip.txt b/examples/device/midi2_device/skip.txt index 3bc342bff..76d694958 100644 --- a/examples/device/midi2_device/skip.txt +++ b/examples/device/midi2_device/skip.txt @@ -1,2 +1,8 @@ mcu:SAMD11 +# Skip boards exposing a pre-existing uninitialized io_ctx.GetTick in +# their board.h (called from board_init2 before TCPP0203_RegisterBusIO). +# Trips with Make+LTO; CMake passes. board:stm32h7s3nucleo +board:stm32h573i_dk +board:stm32n657nucleo +board:stm32n6570dk diff --git a/examples/host/midi2_host/skip.txt b/examples/host/midi2_host/skip.txt index ee7ed0fc0..2232e513a 100644 --- a/examples/host/midi2_host/skip.txt +++ b/examples/host/midi2_host/skip.txt @@ -1,2 +1,8 @@ board:lpcxpresso54114 +# Skip boards exposing a pre-existing uninitialized io_ctx.GetTick in +# their board.h (called from board_init2 before TCPP0203_RegisterBusIO). +# Trips with Make+LTO; CMake passes. board:stm32h7s3nucleo +board:stm32h573i_dk +board:stm32n657nucleo +board:stm32n6570dk -- cgit v1.3.1 From 2a58e8256ebe683106ef3070b5b418c8ba88c8f0 Mon Sep 17 00:00:00 2001 From: Saulo Veríssimo Date: Sat, 16 May 2026 22:54:25 -0300 Subject: midi2: skip.txt comments --- examples/device/midi2_device/skip.txt | 3 --- examples/host/midi2_host/skip.txt | 3 --- 2 files changed, 6 deletions(-) (limited to 'examples/host') diff --git a/examples/device/midi2_device/skip.txt b/examples/device/midi2_device/skip.txt index 76d694958..4cabba2fb 100644 --- a/examples/device/midi2_device/skip.txt +++ b/examples/device/midi2_device/skip.txt @@ -1,7 +1,4 @@ mcu:SAMD11 -# Skip boards exposing a pre-existing uninitialized io_ctx.GetTick in -# their board.h (called from board_init2 before TCPP0203_RegisterBusIO). -# Trips with Make+LTO; CMake passes. board:stm32h7s3nucleo board:stm32h573i_dk board:stm32n657nucleo diff --git a/examples/host/midi2_host/skip.txt b/examples/host/midi2_host/skip.txt index 2232e513a..0f96db3e9 100644 --- a/examples/host/midi2_host/skip.txt +++ b/examples/host/midi2_host/skip.txt @@ -1,7 +1,4 @@ board:lpcxpresso54114 -# Skip boards exposing a pre-existing uninitialized io_ctx.GetTick in -# their board.h (called from board_init2 before TCPP0203_RegisterBusIO). -# Trips with Make+LTO; CMake passes. board:stm32h7s3nucleo board:stm32h573i_dk board:stm32n657nucleo -- cgit v1.3.1 From 116395a1b24f2cb1787b4110a8c3749a0435f1ee Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Sun, 17 May 2026 14:05:03 +0200 Subject: bsp: fix tcpp0203 lto uninit error Signed-off-by: HiFiPhile --- examples/device/midi2_device/skip.txt | 4 ---- examples/host/midi2_host/skip.txt | 4 ---- hw/bsp/stm32h5/boards/stm32h573i_dk/board.h | 5 +++++ hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h | 5 +++++ hw/bsp/stm32n6/boards/stm32n6570dk/board.h | 5 +++++ hw/bsp/stm32n6/boards/stm32n657nucleo/board.h | 5 +++++ 6 files changed, 20 insertions(+), 8 deletions(-) (limited to 'examples/host') diff --git a/examples/device/midi2_device/skip.txt b/examples/device/midi2_device/skip.txt index 4cabba2fb..eadb6e74a 100644 --- a/examples/device/midi2_device/skip.txt +++ b/examples/device/midi2_device/skip.txt @@ -1,5 +1 @@ mcu:SAMD11 -board:stm32h7s3nucleo -board:stm32h573i_dk -board:stm32n657nucleo -board:stm32n6570dk diff --git a/examples/host/midi2_host/skip.txt b/examples/host/midi2_host/skip.txt index 0f96db3e9..308796869 100644 --- a/examples/host/midi2_host/skip.txt +++ b/examples/host/midi2_host/skip.txt @@ -1,5 +1 @@ board:lpcxpresso54114 -board:stm32h7s3nucleo -board:stm32h573i_dk -board:stm32n657nucleo -board:stm32n6570dk diff --git a/hw/bsp/stm32h5/boards/stm32h573i_dk/board.h b/hw/bsp/stm32h5/boards/stm32h573i_dk/board.h index 5788837ab..e9c406486 100644 --- a/hw/bsp/stm32h5/boards/stm32h573i_dk/board.h +++ b/hw/bsp/stm32h5/boards/stm32h573i_dk/board.h @@ -204,6 +204,10 @@ int32_t i2c_writereg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Le return 0; } +static int32_t i2c_get_tick(void) { + return (int32_t) HAL_GetTick(); +} + static inline void board_init2(void) { TCPP0203_IO_t io_ctx; @@ -212,6 +216,7 @@ static inline void board_init2(void) { io_ctx.DeInit = board_tcpp0203_deinit; io_ctx.ReadReg = i2c_readreg; io_ctx.WriteReg = i2c_writereg; + io_ctx.GetTick = i2c_get_tick; TU_ASSERT(TCPP0203_RegisterBusIO(&tcpp0203_obj, &io_ctx) == TCPP0203_OK, ); diff --git a/hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h b/hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h index b1446414e..996eb1515 100644 --- a/hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h +++ b/hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h @@ -223,6 +223,10 @@ int32_t i2c_writereg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Le return 0; } +static int32_t i2c_get_tick(void) { + return (int32_t) HAL_GetTick(); +} + static inline void board_init2(void) { TCPP0203_IO_t io_ctx; @@ -231,6 +235,7 @@ static inline void board_init2(void) { io_ctx.DeInit = board_tcpp0203_deinit; io_ctx.ReadReg = i2c_readreg; io_ctx.WriteReg = i2c_writereg; + io_ctx.GetTick = i2c_get_tick; TU_ASSERT(TCPP0203_RegisterBusIO(&tcpp0203_obj, &io_ctx) == TCPP0203_OK, ); diff --git a/hw/bsp/stm32n6/boards/stm32n6570dk/board.h b/hw/bsp/stm32n6/boards/stm32n6570dk/board.h index 4d162bbca..0cba5e1f9 100644 --- a/hw/bsp/stm32n6/boards/stm32n6570dk/board.h +++ b/hw/bsp/stm32n6/boards/stm32n6570dk/board.h @@ -244,6 +244,10 @@ static int32_t i2c_writereg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint return 0; } +static int32_t i2c_get_tick(void) { + return (int32_t) HAL_GetTick(); +} + static inline void board_init2(void) { TCPP0203_IO_t io_ctx; @@ -252,6 +256,7 @@ static inline void board_init2(void) { io_ctx.DeInit = board_tcpp0203_deinit; io_ctx.ReadReg = i2c_readreg; io_ctx.WriteReg = i2c_writereg; + io_ctx.GetTick = i2c_get_tick; TU_ASSERT(TCPP0203_RegisterBusIO(&tcpp0203_obj, &io_ctx) == TCPP0203_OK, ); diff --git a/hw/bsp/stm32n6/boards/stm32n657nucleo/board.h b/hw/bsp/stm32n6/boards/stm32n657nucleo/board.h index c26367af6..be9ea7a31 100644 --- a/hw/bsp/stm32n6/boards/stm32n657nucleo/board.h +++ b/hw/bsp/stm32n6/boards/stm32n657nucleo/board.h @@ -239,6 +239,10 @@ int32_t i2c_writereg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Le return 0; } +static int32_t i2c_get_tick(void) { + return (int32_t) HAL_GetTick(); +} + static inline void board_init2(void) { TCPP0203_IO_t io_ctx; @@ -247,6 +251,7 @@ static inline void board_init2(void) { io_ctx.DeInit = board_tcpp0203_deinit; io_ctx.ReadReg = i2c_readreg; io_ctx.WriteReg = i2c_writereg; + io_ctx.GetTick = i2c_get_tick; TU_ASSERT(TCPP0203_RegisterBusIO(&tcpp0203_obj, &io_ctx) == TCPP0203_OK, ); -- cgit v1.3.1