|
38 | 38 |
|
39 | 39 | supervisor_allocation *usb_midi_allocation; |
40 | 40 |
|
| 41 | +static uint8_t[] usb_midi_descriptor[] = { |
| 42 | + 0x09, // 0 bLength |
| 43 | + 0x04, // 1 bDescriptorType (Interface) |
| 44 | + 0xFF, // 2 bInterfaceNumber [SET AT RUNTIME] |
| 45 | + 0x00, // 3 bAlternateSetting |
| 46 | + 0x00, // 4 bNumEndpoints 0 |
| 47 | + 0x01, // 5 bInterfaceClass (Audio) |
| 48 | + 0x01, // 6 bInterfaceSubClass (Audio Control) |
| 49 | + 0x00, // 7 bInterfaceProtocol |
| 50 | + 0x00, // 8 iInterface (String Index) [SET AT RUNTIME] |
| 51 | + |
| 52 | + 0x09, // 9 bLength |
| 53 | + 0x24, // 10 bDescriptorType (See Next Line) |
| 54 | + 0x01, // 11 bDescriptorSubtype (CS_INTERFACE -> HEADER) |
| 55 | + 0x00, 0x01, // 12,13 bcdADC 1.00 |
| 56 | + 0x09, 0x00, // 14,15 wTotalLength 9 |
| 57 | + 0x01, // 16 binCollection 0x01 |
| 58 | + 0xFF, // 17 baInterfaceNr [SET AT RUNTIME: one-element list: same as 20] |
| 59 | + |
| 60 | + 0x09, // 18 bLength |
| 61 | + 0x04, // 19 bDescriptorType (Interface) |
| 62 | + 0xFF, // 20 bInterfaceNumber [SET AT RUNTIME] |
| 63 | + 0x00, // 21 bAlternateSetting |
| 64 | + 0x02, // 22 bNumEndpoints 2 |
| 65 | + 0x01, // 23 bInterfaceClass (Audio) |
| 66 | + 0x03, // 24 bInterfaceSubClass (MIDI Streaming) |
| 67 | + 0x00, // 25 bInterfaceProtocol |
| 68 | + 0x0A, // 26 iInterface (String Index) [SET AT RUNTIME] |
| 69 | + |
| 70 | + 0x07, // 27 bLength |
| 71 | + 0x24, // 28 bDescriptorType (See Next Line) |
| 72 | + 0x01, 0x00, 0x01, 0x25, 0x00, // 29,30,31,32,33 |
| 73 | + 0x06, // 34 bLength |
| 74 | + 0x24, // 35 bDescriptorType (See Next Line) |
| 75 | + 0x02, 0x01, 0x01, 0x08, // 36,37,38,39 |
| 76 | + 0x06, // 40 bLength |
| 77 | + 0x24, // 41 bDescriptorType (See Next Line) |
| 78 | + 0x02, 0x02, 0x02, 0x00, // 42,43,44,45 |
| 79 | + 0x09, // 46 bLength |
| 80 | + 0x24, // 47 bDescriptorType (See Next Line) |
| 81 | + 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x09, // 48,49,50,51,52,53,54 |
| 82 | + 0x09, // 56 bLength |
| 83 | + 0x24, // 57 bDescriptorType (See Next Line) |
| 84 | + 0x03, 0x02, 0x04, 0x01, 0x01, 0x01, 0x00, // 58,59,60,61,62,63,64 |
| 85 | + 0x07, // 65 bLength |
| 86 | + 0x05, // 66 bDescriptorType (See Next Line) |
| 87 | + 0xFF, // 67 bEndpointAddress (OUT/H2D) [SET AT RUNTIME] |
| 88 | + 0x02, // 68 bmAttributes (Bulk) |
| 89 | + 0x40, 0x00, // 69,70 wMaxPacketSize 64 |
| 90 | + 0x00, // 71 bInterval 0 (unit depends on device speed) |
| 91 | + |
| 92 | + 0x05, // 72 bLength |
| 93 | + 0x25, // 73 bDescriptorType (See Next Line) |
| 94 | + 0x01, 0x01, 0x01, // 74,75,76 |
| 95 | + 0x07, // 77 bLength |
| 96 | + 0x05, // 78 bDescriptorType (See Next Line) |
| 97 | + 0xFF, // 79 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x8 | number] |
| 98 | + 0x02, // 80 bmAttributes (Bulk) |
| 99 | + 0x40, 0x00, // 81,82 wMaxPacketSize 64 |
| 100 | + 0x00, // 83 bInterval 0 (unit depends on device speed) |
| 101 | + |
| 102 | + 0x05, // 84 bLength |
| 103 | + 0x25, // 85 bDescriptorType (See Next Line) |
| 104 | + 0x01, 0x01, 0x03, // 86,87,88 |
| 105 | +}; |
| 106 | + |
41 | 107 | // Is the USB MIDI device enabled? |
42 | | -static bool usb_midi_enabled; |
| 108 | +bool usb_midi_enabled; |
| 109 | + |
| 110 | +// Indices into usb_midi_descriptor for values that must be set at runtime. |
| 111 | + |
| 112 | +#define MIDI_AUDIO_CONTROL_INTERFACE_NUMBER_INDEX 2 |
| 113 | +#define MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX 8 |
| 114 | + |
| 115 | +// These two get the same value. |
| 116 | +#define MIDI_MIDI_STREAMING_INTERFACE_NUMBER_INDEX 20 |
| 117 | +#define MIDI_MIDI_STREAMING_INTERFACE_NUMBER_XREF_INDEX 17 |
| 118 | + |
| 119 | +#define MIDI_MIDI_STREAMING_INTERFACE_STRING_INDEX 26 |
| 120 | + |
| 121 | +#define MIDI_MIDI_STREAMING_OUT_ENDPOINT_INDEX 67 |
| 122 | +#define MIDI_MIDI_STREAMING_IN_ENDPOINT_INDEX 79 |
| 123 | + |
| 124 | +size_t usb_midi_desc_length(void) { |
| 125 | + return sizeof(usb_midi_descriptor); |
| 126 | +} |
| 127 | + |
| 128 | +size_t usb_midi_add_desc(uint8_t *desc_buf, |
| 129 | + uint8_t audio_control_interface_number, |
| 130 | + uint8_t midi_streaming_interface_number, |
| 131 | + uint8_t midi_streaming_in_endpoint_address, |
| 132 | + uint8_t midi_streaming_out_endpoint_address, |
| 133 | + uint8_t audio_control_interface_string, |
| 134 | + uint8_t midi_streaming_interface_string) { |
| 135 | + memcpy(descriptor_buf, usb_midi_descriptor, sizeof(usb_midi_descriptor)); |
| 136 | + desc_buf[MIDI_AUDIO_CONTROL_INTERFACE_NUMBER_INDEX] = audio_control_interface_number; |
| 137 | + desc_buf[MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX] = audio_control_interface_string; |
| 138 | + |
| 139 | + desc_buf[MIDI_MIDI_STREAMING_INTERFACE_NUMBER_INDEX] = midi_streaming_interface_number; |
| 140 | + desc_buf[MIDI_MIDI_STREAMING_INTERFACE_NUMBER_XREF_INDEX] = midi_streaming_interface_number; |
| 141 | + desc_buf[MIDI_MIDI_STREAMING_INTERFACE_STRING_INDEX] = midi_streaming_interface_string; |
| 142 | + |
| 143 | + desc_buf[MSC_IN_ENDPOINT_INDEX] = midi_streaming_in_endpoint_address; |
| 144 | + desc_buf[MSC_OUT_ENDPOINT_INDEX] = 0x80 | midi_streaming_out_endpoint_address; |
| 145 | + return sizeof(usb_midi_descriptor); |
| 146 | +} |
| 147 | + |
43 | 148 |
|
44 | 149 | void usb_midi_init(void) { |
45 | 150 | usb_midi_enabled = true; |
|
0 commit comments