max4146x_comp

Dependencies:   MAX14690

Committer:
sdivarci
Date:
Sun Oct 25 20:10:02 2020 +0000
Revision:
0:0061165683ee
sdivarci

Who changed what in which revision?

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