mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
elessair 0:f269e3021894 2 *
elessair 0:f269e3021894 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
elessair 0:f269e3021894 4 * and associated documentation files (the "Software"), to deal in the Software without
elessair 0:f269e3021894 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
elessair 0:f269e3021894 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
elessair 0:f269e3021894 7 * Software is furnished to do so, subject to the following conditions:
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * The above copyright notice and this permission notice shall be included in all copies or
elessair 0:f269e3021894 10 * substantial portions of the Software.
elessair 0:f269e3021894 11 *
elessair 0:f269e3021894 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
elessair 0:f269e3021894 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
elessair 0:f269e3021894 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
elessair 0:f269e3021894 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
elessair 0:f269e3021894 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
elessair 0:f269e3021894 17 */
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #include "stdint.h"
elessair 0:f269e3021894 20 #include "USBMIDI.h"
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
elessair 0:f269e3021894 24 : USBDevice(vendor_id, product_id, product_release), cur_data(0), data_end(true)
elessair 0:f269e3021894 25 {
elessair 0:f269e3021894 26 midi_evt = NULL;
elessair 0:f269e3021894 27 USBDevice::connect();
elessair 0:f269e3021894 28 }
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 // write plain MIDIMessage that will be converted to USBMidi event packet
elessair 0:f269e3021894 31 void USBMIDI::write(MIDIMessage m) {
elessair 0:f269e3021894 32 // first byte keeped for retro-compatibility
elessair 0:f269e3021894 33 for(int p=1; p < m.length; p+=3) {
elessair 0:f269e3021894 34 uint8_t buf[4];
elessair 0:f269e3021894 35 // Midi message to USBMidi event packet
elessair 0:f269e3021894 36 buf[0]=m.data[1] >> 4;
elessair 0:f269e3021894 37 // SysEx
elessair 0:f269e3021894 38 if(buf[0] == 0xF) {
elessair 0:f269e3021894 39 if((m.length - p) > 3) {
elessair 0:f269e3021894 40 // SysEx start or continue
elessair 0:f269e3021894 41 buf[0]=0x4;
elessair 0:f269e3021894 42 } else {
elessair 0:f269e3021894 43 switch(m.length - p) {
elessair 0:f269e3021894 44 case 1:
elessair 0:f269e3021894 45 // SysEx end with one byte
elessair 0:f269e3021894 46 buf[0]=0x5;
elessair 0:f269e3021894 47 break;
elessair 0:f269e3021894 48 case 2:
elessair 0:f269e3021894 49 // SysEx end with two bytes
elessair 0:f269e3021894 50 buf[0]=0x6;
elessair 0:f269e3021894 51 break;
elessair 0:f269e3021894 52 case 3:
elessair 0:f269e3021894 53 // SysEx end with three bytes
elessair 0:f269e3021894 54 buf[0]=0x7;
elessair 0:f269e3021894 55 break;
elessair 0:f269e3021894 56 }
elessair 0:f269e3021894 57 }
elessair 0:f269e3021894 58 }
elessair 0:f269e3021894 59 buf[1]=m.data[p];
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 if(p+1 < m.length)
elessair 0:f269e3021894 62 buf[2]=m.data[p+1];
elessair 0:f269e3021894 63 else
elessair 0:f269e3021894 64 buf[2]=0;
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66 if(p+2 < m.length)
elessair 0:f269e3021894 67 buf[3]=m.data[p+2];
elessair 0:f269e3021894 68 else
elessair 0:f269e3021894 69 buf[3]=0;
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 USBDevice::write(EPBULK_IN, buf, 4, MAX_PACKET_SIZE_EPBULK);
elessair 0:f269e3021894 72 }
elessair 0:f269e3021894 73 }
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
elessair 0:f269e3021894 77 midi_evt = fptr;
elessair 0:f269e3021894 78 }
elessair 0:f269e3021894 79
elessair 0:f269e3021894 80 bool USBMIDI::EPBULK_OUT_callback() {
elessair 0:f269e3021894 81 uint8_t buf[64];
elessair 0:f269e3021894 82 uint32_t len;
elessair 0:f269e3021894 83 readEP(EPBULK_OUT, buf, &len, 64);
elessair 0:f269e3021894 84
elessair 0:f269e3021894 85 if (midi_evt != NULL) {
elessair 0:f269e3021894 86 for (uint32_t i=0; i<len; i+=4) {
elessair 0:f269e3021894 87 uint8_t data_read;
elessair 0:f269e3021894 88 data_end=true;
elessair 0:f269e3021894 89 switch(buf[i]) {
elessair 0:f269e3021894 90 case 0x2:
elessair 0:f269e3021894 91 // Two-bytes System Common Message - undefined in USBMidi 1.0
elessair 0:f269e3021894 92 data_read=2;
elessair 0:f269e3021894 93 break;
elessair 0:f269e3021894 94 case 0x4:
elessair 0:f269e3021894 95 // SysEx start or continue
elessair 0:f269e3021894 96 data_end=false;
elessair 0:f269e3021894 97 data_read=3;
elessair 0:f269e3021894 98 break;
elessair 0:f269e3021894 99 case 0x5:
elessair 0:f269e3021894 100 // Single-byte System Common Message or SysEx end with one byte
elessair 0:f269e3021894 101 data_read=1;
elessair 0:f269e3021894 102 break;
elessair 0:f269e3021894 103 case 0x6:
elessair 0:f269e3021894 104 // SysEx end with two bytes
elessair 0:f269e3021894 105 data_read=2;
elessair 0:f269e3021894 106 break;
elessair 0:f269e3021894 107 case 0xC:
elessair 0:f269e3021894 108 // Program change
elessair 0:f269e3021894 109 data_read=2;
elessair 0:f269e3021894 110 break;
elessair 0:f269e3021894 111 case 0xD:
elessair 0:f269e3021894 112 // Channel pressure
elessair 0:f269e3021894 113 data_read=2;
elessair 0:f269e3021894 114 break;
elessair 0:f269e3021894 115 case 0xF:
elessair 0:f269e3021894 116 // Single byte
elessair 0:f269e3021894 117 data_read=1;
elessair 0:f269e3021894 118 break;
elessair 0:f269e3021894 119 default:
elessair 0:f269e3021894 120 // Others three-bytes messages
elessair 0:f269e3021894 121 data_read=3;
elessair 0:f269e3021894 122 break;
elessair 0:f269e3021894 123 }
elessair 0:f269e3021894 124
elessair 0:f269e3021894 125 for(uint8_t j=1;j<data_read+1;j++) {
elessair 0:f269e3021894 126 data[cur_data]=buf[i+j];
elessair 0:f269e3021894 127 cur_data++;
elessair 0:f269e3021894 128 }
elessair 0:f269e3021894 129
elessair 0:f269e3021894 130 if(data_end) {
elessair 0:f269e3021894 131 midi_evt(MIDIMessage(data,cur_data));
elessair 0:f269e3021894 132 cur_data=0;
elessair 0:f269e3021894 133 }
elessair 0:f269e3021894 134 }
elessair 0:f269e3021894 135 }
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 // We reactivate the endpoint to receive next characters
elessair 0:f269e3021894 138 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
elessair 0:f269e3021894 139 return true;
elessair 0:f269e3021894 140 }
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 // Called in ISR context
elessair 0:f269e3021894 143 // Set configuration. Return false if the
elessair 0:f269e3021894 144 // configuration is not supported.
elessair 0:f269e3021894 145 bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
elessair 0:f269e3021894 146 if (configuration != DEFAULT_CONFIGURATION) {
elessair 0:f269e3021894 147 return false;
elessair 0:f269e3021894 148 }
elessair 0:f269e3021894 149
elessair 0:f269e3021894 150 // Configure endpoints > 0
elessair 0:f269e3021894 151 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
elessair 0:f269e3021894 152 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
elessair 0:f269e3021894 153
elessair 0:f269e3021894 154 // We activate the endpoint to be able to receive data
elessair 0:f269e3021894 155 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
elessair 0:f269e3021894 156 return true;
elessair 0:f269e3021894 157 }
elessair 0:f269e3021894 158
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160 uint8_t * USBMIDI::stringIinterfaceDesc() {
elessair 0:f269e3021894 161 static uint8_t stringIinterfaceDescriptor[] = {
elessair 0:f269e3021894 162 0x0c, //bLength
elessair 0:f269e3021894 163 STRING_DESCRIPTOR, //bDescriptorType 0x03
elessair 0:f269e3021894 164 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
elessair 0:f269e3021894 165 };
elessair 0:f269e3021894 166 return stringIinterfaceDescriptor;
elessair 0:f269e3021894 167 }
elessair 0:f269e3021894 168
elessair 0:f269e3021894 169 uint8_t * USBMIDI::stringIproductDesc() {
elessair 0:f269e3021894 170 static uint8_t stringIproductDescriptor[] = {
elessair 0:f269e3021894 171 0x16, //bLength
elessair 0:f269e3021894 172 STRING_DESCRIPTOR, //bDescriptorType 0x03
elessair 0:f269e3021894 173 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
elessair 0:f269e3021894 174 };
elessair 0:f269e3021894 175 return stringIproductDescriptor;
elessair 0:f269e3021894 176 }
elessair 0:f269e3021894 177
elessair 0:f269e3021894 178
elessair 0:f269e3021894 179 uint8_t * USBMIDI::configurationDesc() {
elessair 0:f269e3021894 180 static uint8_t configDescriptor[] = {
elessair 0:f269e3021894 181 // configuration descriptor
elessair 0:f269e3021894 182 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
elessair 0:f269e3021894 183
elessair 0:f269e3021894 184 // The Audio Interface Collection
elessair 0:f269e3021894 185 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
elessair 0:f269e3021894 186 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
elessair 0:f269e3021894 187 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
elessair 0:f269e3021894 188 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor
elessair 0:f269e3021894 189
elessair 0:f269e3021894 190 // MIDI IN JACKS
elessair 0:f269e3021894 191 0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
elessair 0:f269e3021894 192 0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
elessair 0:f269e3021894 193
elessair 0:f269e3021894 194 // MIDI OUT JACKS
elessair 0:f269e3021894 195 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
elessair 0:f269e3021894 196 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
elessair 0:f269e3021894 197
elessair 0:f269e3021894 198 // OUT endpoint descriptor
elessair 0:f269e3021894 199 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
elessair 0:f269e3021894 200 0x05, 0x25, 0x01, 0x01, 0x01,
elessair 0:f269e3021894 201
elessair 0:f269e3021894 202 // IN endpoint descriptor
elessair 0:f269e3021894 203 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
elessair 0:f269e3021894 204 0x05, 0x25, 0x01, 0x01, 0x03,
elessair 0:f269e3021894 205 };
elessair 0:f269e3021894 206 return configDescriptor;
elessair 0:f269e3021894 207 }