Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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