Skip to content

Commit f4af6f4

Browse files
committed
Add MIDI device implementation
1 parent 405c8e6 commit f4af6f4

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

include/midi.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef MIDI_DEFINED_H_
2+
#define MIDI_DEFINED_H_
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#ifdef __cplusplus
9+
}
10+
#endif
11+
12+
#endif /* MIDI_DEFINED_H_ */

src/midi.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <mrubyc.h>
2+
3+
#include "../include/midi.h"
4+
#include "../include/usb_descriptors.h"
5+
#include "picoruby-prk-midi/include/prk-midi.h"
6+
7+
uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint
8+
uint8_t packet[4];
9+
10+
void Midi_init(void)
11+
{
12+
while(tud_midi_available()) tud_midi_packet_read(packet);
13+
}
14+
15+
void Midi_stream_write(uint8_t* packet, uint8_t len)
16+
{
17+
tud_midi_stream_write(cable_num, packet, len);
18+
}

src/usb_descriptors.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tusb_desc_device_t desc_device =
3333
.bNumConfigurations = 0x01
3434
};
3535

36-
#define STRING_DESC_ARR_SIZE 6
36+
#define STRING_DESC_ARR_SIZE 7
3737

3838
#define PRK_CONF_SIZE 64
3939

@@ -47,6 +47,7 @@ static char const *string_desc_arr[STRING_DESC_ARR_SIZE] =
4747
PRK_SERIAL, // 3: Serial
4848
"PRK CDC", // 4: CDC Interface
4949
"PRK MSC", // 5: MSC Interface
50+
"PRK MIDI", // 6: MIDI Interface
5051
};
5152

5253
// Invoked when received GET DEVICE DESCRIPTOR
@@ -61,9 +62,9 @@ tud_descriptor_device_cb(void)
6162
//--------------------------------------------------------------------+
6263

6364
#ifdef PICORUBY_NO_MSC
64-
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN)
65+
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN + TUD_MIDI_DESC_LEN)
6566
#else
66-
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN)
67+
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN + TUD_MIDI_DESC_LEN)
6768
#endif
6869

6970
#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX
@@ -112,6 +113,9 @@ tud_descriptor_device_cb(void)
112113
#define EPNUM_HID_IN 0x84
113114
#define EPNUM_JOYSTICK_IN 0x85
114115

116+
#define EPNUM_MIDI_OUT 0x06
117+
#define EPNUM_MIDI_IN 0x86
118+
115119
uint8_t const desc_hid_report[] =
116120
{
117121
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD )),
@@ -131,6 +135,8 @@ enum
131135
ITF_NUM_CDC_DATA,
132136
ITF_NUM_HID,
133137
ITF_NUM_JOYSTICK,
138+
ITF_NUM_MIDI,
139+
ITF_NUM_MIDI_STREAMING,
134140
#ifndef PICORUBY_NO_MSC
135141
ITF_NUM_MSC,
136142
#endif
@@ -145,6 +151,9 @@ uint8_t const desc_fs_configuration[] =
145151
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
146152
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
147153

154+
// Interface number, string index, EP Out & EP In address, EP size
155+
TUD_MIDI_DESCRIPTOR(ITF_NUM_MIDI, 6, EPNUM_MIDI_OUT, EPNUM_MIDI_IN, 64),
156+
148157
#ifndef PICORUBY_NO_MSC
149158
// Interface number, string index, EP Out & EP In address, EP size
150159
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),

test/models/midi.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../lib/picoruby/mrbgems/picoruby-prk-midi/mrblib/midi.rb

tinyusb/tusb_config.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
#define CFG_TUD_CDC 1
9898
#define CFG_TUD_MSC 1
9999
#define CFG_TUD_HID 2
100-
#define CFG_TUD_MIDI 0
100+
#define CFG_TUD_MIDI 1
101101
#define CFG_TUD_VENDOR 0
102102

103103
// HID buffer size Should be sufficient to hold ID (if any) + Data
@@ -118,6 +118,10 @@
118118
// CDC Endpoint transfer buffer size, more is faster
119119
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
120120

121+
// MIDI FIFO size of TX and RX
122+
#define CFG_TUD_MIDI_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
123+
#define CFG_TUD_MIDI_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
124+
121125
#ifdef __cplusplus
122126
}
123127
#endif

0 commit comments

Comments
 (0)