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