Skip to content

Commit 02a630b

Browse files
authored
Merge pull request #1627 from atoktoto/midihost
RP2040 MIDI Host
2 parents 29ffd57 + ee234a8 commit 02a630b

File tree

30 files changed

+1416
-170
lines changed

30 files changed

+1416
-170
lines changed

examples/host/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ family_add_subdirectory(cdc_msc_hid)
1111
family_add_subdirectory(cdc_msc_hid_freertos)
1212
family_add_subdirectory(device_info)
1313
family_add_subdirectory(hid_controller)
14+
family_add_subdirectory(midi_rx)
1415
family_add_subdirectory(msc_file_explorer)

examples/host/cdc_msc_hid/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ int main(void) {
8282

8383
void tuh_mount_cb(uint8_t dev_addr) {
8484
// application set-up
85-
printf("A device with address %d is mounted\r\n", dev_addr);
85+
printf("A device with address %u is mounted\r\n", dev_addr);
8686
}
8787

8888
void tuh_umount_cb(uint8_t dev_addr) {
8989
// application tear-down
90-
printf("A device with address %d is unmounted \r\n", dev_addr);
90+
printf("A device with address %u is unmounted \r\n", dev_addr);
9191
}
9292

9393

examples/host/device_info/src/tusb_config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@
110110
// only hub class is enabled
111111
#define CFG_TUH_HUB 1
112112

113-
// max device support (excluding hub device)
114-
// 1 hub typically has 4 ports
113+
// max device support (excluding hub device): 1 hub typically has 4 ports
115114
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
116115

117116
#ifdef __cplusplus
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
4+
5+
# gets PROJECT name for the example
6+
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
7+
8+
project(${PROJECT} C CXX ASM)
9+
10+
# Checks this example is valid for the family and initializes the project
11+
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
12+
13+
# Espressif has its own cmake build system
14+
if(FAMILY STREQUAL "espressif")
15+
return()
16+
endif()
17+
18+
add_executable(${PROJECT})
19+
20+
# Example source
21+
target_sources(${PROJECT} PUBLIC
22+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
23+
)
24+
25+
# Example include
26+
target_include_directories(${PROJECT} PUBLIC
27+
${CMAKE_CURRENT_SOURCE_DIR}/src
28+
)
29+
30+
# Configure compilation flags and libraries for the example without RTOS.
31+
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
32+
family_configure_host_example(${PROJECT} noos)

examples/host/midi_rx/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include ../../build_system/make/make.mk
2+
3+
INC += \
4+
src \
5+
$(TOP)/hw \
6+
7+
# Example source
8+
EXAMPLE_SOURCE += \
9+
src/main.c
10+
11+
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
12+
13+
include ../../build_system/make/rules.mk

examples/host/midi_rx/only.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
mcu:ESP32S2
2+
mcu:ESP32S3
3+
mcu:ESP32P4
4+
mcu:KINETIS_KL
5+
mcu:LPC175X_6X
6+
mcu:LPC177X_8X
7+
mcu:LPC18XX
8+
mcu:LPC40XX
9+
mcu:LPC43XX
10+
mcu:MAX3421
11+
mcu:MIMXRT1XXX
12+
mcu:MIMXRT10XX
13+
mcu:MIMXRT11XX
14+
mcu:MSP432E4
15+
mcu:RP2040
16+
mcu:RX65X
17+
mcu:RAXXX
18+
mcu:STM32F4
19+
mcu:STM32F7
20+
mcu:STM32H7

examples/host/midi_rx/src/main.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
#include <string.h>
29+
30+
#include "bsp/board_api.h"
31+
#include "tusb.h"
32+
33+
//--------------------------------------------------------------------+
34+
// STATIC GLOBALS DECLARATION
35+
//--------------------------------------------------------------------+
36+
37+
//--------------------------------------------------------------------+
38+
// MACRO CONSTANT TYPEDEF PROTOTYPES
39+
//--------------------------------------------------------------------+
40+
void led_blinking_task(void);
41+
void midi_host_rx_task(void);
42+
43+
/*------------- MAIN -------------*/
44+
int main(void) {
45+
board_init();
46+
47+
printf("TinyUSB Host MIDI Example\r\n");
48+
49+
// init host stack on configured roothub port
50+
tusb_rhport_init_t host_init = {
51+
.role = TUSB_ROLE_HOST,
52+
.speed = TUSB_SPEED_AUTO
53+
};
54+
tusb_init(BOARD_TUH_RHPORT, &host_init);
55+
56+
while (1) {
57+
tuh_task();
58+
led_blinking_task();
59+
midi_host_rx_task();
60+
}
61+
62+
return 0;
63+
}
64+
65+
//--------------------------------------------------------------------+
66+
// Blinking Task
67+
//--------------------------------------------------------------------+
68+
void led_blinking_task(void) {
69+
const uint32_t interval_ms = 1000;
70+
static uint32_t start_ms = 0;
71+
72+
static bool led_state = false;
73+
74+
// Blink every interval ms
75+
if (board_millis() - start_ms < interval_ms) return;// not enough time
76+
start_ms += interval_ms;
77+
78+
board_led_write(led_state);
79+
led_state = 1 - led_state;// toggle
80+
}
81+
82+
//--------------------------------------------------------------------+
83+
// MIDI host receive task
84+
//--------------------------------------------------------------------+
85+
void midi_host_rx_task(void) {
86+
// nothing to do, we just print out received data in callback
87+
}
88+
89+
//--------------------------------------------------------------------+
90+
// TinyUSB Callbacks
91+
//--------------------------------------------------------------------+
92+
93+
// Invoked when device with MIDI interface is mounted.
94+
void tuh_midi_mount_cb(uint8_t idx, const tuh_midi_mount_cb_t* mount_cb_data) {
95+
printf("MIDI Interface Index = %u, Address = %u, Number of RX cables = %u, Number of TX cables = %u\r\n",
96+
idx, mount_cb_data->daddr, mount_cb_data->rx_cable_count, mount_cb_data->tx_cable_count);
97+
}
98+
99+
// Invoked when device with hid interface is un-mounted
100+
void tuh_midi_umount_cb(uint8_t idx) {
101+
printf("MIDI Interface Index = %u is unmounted\r\n", idx);
102+
}
103+
104+
void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes) {
105+
if (xferred_bytes == 0) {
106+
return;
107+
}
108+
109+
uint8_t buffer[48];
110+
uint8_t cable_num = 0;
111+
uint32_t bytes_read = tuh_midi_stream_read(idx, &cable_num, buffer, sizeof(buffer));
112+
113+
printf("Cable %u rx: ", cable_num);
114+
for (uint32_t i = 0; i < bytes_read; i++) {
115+
printf("%02X ", buffer[i]);
116+
}
117+
printf("\r\n");
118+
}
119+
120+
void tuh_midi_tx_cb(uint8_t idx, uint32_t xferred_bytes) {
121+
(void) idx;
122+
(void) xferred_bytes;
123+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef TUSB_CONFIG_H_
27+
#define TUSB_CONFIG_H_
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
//--------------------------------------------------------------------
34+
// Common Configuration
35+
//--------------------------------------------------------------------
36+
37+
// defined by compiler flags for flexibility
38+
#ifndef CFG_TUSB_MCU
39+
#error CFG_TUSB_MCU must be defined
40+
#endif
41+
42+
// Espressif IDF requires "freertos/" prefix in include path
43+
#if TUSB_MCU_VENDOR_ESPRESSIF
44+
#define CFG_TUSB_OS_INC_PATH freertos/
45+
#endif
46+
47+
#ifndef CFG_TUSB_OS
48+
#define CFG_TUSB_OS OPT_OS_NONE
49+
#endif
50+
51+
#ifndef CFG_TUSB_DEBUG
52+
#define CFG_TUSB_DEBUG 0
53+
#endif
54+
55+
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
56+
* Tinyusb use follows macros to declare transferring memory so that they can be put
57+
* into those specific section.
58+
* e.g
59+
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
60+
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
61+
*/
62+
#ifndef CFG_TUH_MEM_SECTION
63+
#define CFG_TUH_MEM_SECTION
64+
#endif
65+
66+
#ifndef CFG_TUH_MEM_ALIGN
67+
#define CFG_TUH_MEM_ALIGN __attribute__ ((aligned(4)))
68+
#endif
69+
70+
//--------------------------------------------------------------------
71+
// Host Configuration
72+
//--------------------------------------------------------------------
73+
74+
// Enable Host stack
75+
#define CFG_TUH_ENABLED 1
76+
77+
#if CFG_TUSB_MCU == OPT_MCU_RP2040
78+
// #define CFG_TUH_RPI_PIO_USB 1 // use pio-usb as host controller
79+
// #define CFG_TUH_MAX3421 1 // use max3421 as host controller
80+
81+
// host roothub port is 1 if using either pio-usb or max3421
82+
#if (defined(CFG_TUH_RPI_PIO_USB) && CFG_TUH_RPI_PIO_USB) || (defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421)
83+
#define BOARD_TUH_RHPORT 1
84+
#endif
85+
#endif
86+
87+
// Default is max speed that hardware controller could support with on-chip PHY
88+
#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED
89+
90+
//------------------------- Board Specific --------------------------
91+
92+
// RHPort number used for host can be defined by board.mk, default to port 0
93+
#ifndef BOARD_TUH_RHPORT
94+
#define BOARD_TUH_RHPORT 0
95+
#endif
96+
97+
// RHPort max operational speed can defined by board.mk
98+
#ifndef BOARD_TUH_MAX_SPEED
99+
#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED
100+
#endif
101+
102+
//--------------------------------------------------------------------
103+
// Driver Configuration
104+
//--------------------------------------------------------------------
105+
106+
// Size of buffer to hold descriptors and other data used for enumeration
107+
#define CFG_TUH_ENUMERATION_BUFSIZE 256
108+
109+
#define CFG_TUH_HUB 1
110+
// max device support (excluding hub device): 1 hub typically has 4 ports
111+
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
112+
#define CFG_TUH_MIDI CFG_TUH_DEVICE_MAX
113+
114+
#ifdef __cplusplus
115+
}
116+
#endif
117+
118+
#endif

hw/bsp/rp2040/family.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ int board_getchar(void) {
294294
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
295295

296296
void max3421_int_handler(uint gpio, uint32_t event_mask) {
297-
if (!(gpio == MAX3421_INTR_PIN && event_mask & GPIO_IRQ_EDGE_FALL)) return;
297+
if (!(gpio == MAX3421_INTR_PIN && event_mask & GPIO_IRQ_EDGE_FALL)) {
298+
return;
299+
}
298300
tuh_int_handler(BOARD_TUH_RHPORT, true);
299301
}
300302

hw/bsp/rp2040/family.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ target_sources(tinyusb_host_base INTERFACE
113113
${TOP}/src/host/hub.c
114114
${TOP}/src/class/cdc/cdc_host.c
115115
${TOP}/src/class/hid/hid_host.c
116+
${TOP}/src/class/midi/midi_host.c
116117
${TOP}/src/class/msc/msc_host.c
117118
${TOP}/src/class/vendor/vendor_host.c
118119
)

0 commit comments

Comments
 (0)