USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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