1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 USBMIDI::USBMIDI(uint16_t vendor_id
, uint16_t product_id
, uint16_t product_release
)
24 : USBDevice(vendor_id
, product_id
, product_release
), cur_data(0), data_end(true)
30 // write plain MIDIMessage that will be converted to USBMidi event packet
31 void USBMIDI::write(MIDIMessage m
) {
32 // first byte keeped for retro-compatibility
33 for(int p
=1; p
< m
.length
; p
+=3) {
35 // Midi message to USBMidi event packet
36 buf
[0]=m
.data
[1] >> 4;
39 if((m
.length
- p
) > 3) {
40 // SysEx start or continue
43 switch(m
.length
- p
) {
45 // SysEx end with one byte
49 // SysEx end with two bytes
53 // SysEx end with three bytes
71 USBDevice::write(EPBULK_IN
, buf
, 4, MAX_PACKET_SIZE_EPBULK
);
76 void USBMIDI::attach(void (*fptr
)(MIDIMessage
)) {
80 bool USBMIDI::EPBULK_OUT_callback() {
83 readEP(EPBULK_OUT
, buf
, &len
, 64);
85 if (midi_evt
!= NULL
) {
86 for (uint32_t i
=0; i
<len
; i
+=4) {
91 // Two-bytes System Common Message - undefined in USBMidi 1.0
95 // SysEx start or continue
100 // Single-byte System Common Message or SysEx end with one byte
104 // SysEx end with two bytes
120 // Others three-bytes messages
125 for(uint8_t j
=1;j
<data_read
+1;j
++) {
126 data
[cur_data
]=buf
[i
+j
];
131 midi_evt(MIDIMessage(data
,cur_data
));
137 // We reactivate the endpoint to receive next characters
138 readStart(EPBULK_OUT
, MAX_PACKET_SIZE_EPBULK
);
142 // Called in ISR context
143 // Set configuration. Return false if the
144 // configuration is not supported.
145 bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration
) {
146 if (configuration
!= DEFAULT_CONFIGURATION
) {
150 // Configure endpoints > 0
151 addEndpoint(EPBULK_IN
, MAX_PACKET_SIZE_EPBULK
);
152 addEndpoint(EPBULK_OUT
, MAX_PACKET_SIZE_EPBULK
);
154 // We activate the endpoint to be able to receive data
155 readStart(EPBULK_OUT
, MAX_PACKET_SIZE_EPBULK
);
160 uint8_t * USBMIDI::stringIinterfaceDesc() {
161 static uint8_t stringIinterfaceDescriptor
[] = {
163 STRING_DESCRIPTOR
, //bDescriptorType 0x03
164 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
166 return stringIinterfaceDescriptor
;
169 uint8_t * USBMIDI::stringIproductDesc() {
170 static uint8_t stringIproductDescriptor
[] = {
172 STRING_DESCRIPTOR
, //bDescriptorType 0x03
173 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
175 return stringIproductDescriptor
;
179 uint8_t * USBMIDI::configurationDesc() {
180 static uint8_t configDescriptor
[] = {
181 // configuration descriptor
182 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
184 // The Audio Interface Collection
185 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
186 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
187 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
188 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor
191 0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
192 0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
195 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
196 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
198 // OUT endpoint descriptor
199 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
200 0x05, 0x25, 0x01, 0x01, 0x01,
202 // IN endpoint descriptor
203 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
204 0x05, 0x25, 0x01, 0x01, 0x03,
206 return configDescriptor
;