USBDevice

Dependents:   QEI_X1_LCD_test3 macnica_test

Committer:
toucyy
Date:
Thu Apr 18 07:49:37 2013 +0000
Revision:
0:2d8d0b73e1ff
[mbed] converted /QEI_HelloWorld/USBDevice

Who changed what in which revision?

UserRevisionLine numberNew contents of line
toucyy 0:2d8d0b73e1ff 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
toucyy 0:2d8d0b73e1ff 2 *
toucyy 0:2d8d0b73e1ff 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
toucyy 0:2d8d0b73e1ff 4 * and associated documentation files (the "Software"), to deal in the Software without
toucyy 0:2d8d0b73e1ff 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
toucyy 0:2d8d0b73e1ff 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
toucyy 0:2d8d0b73e1ff 7 * Software is furnished to do so, subject to the following conditions:
toucyy 0:2d8d0b73e1ff 8 *
toucyy 0:2d8d0b73e1ff 9 * The above copyright notice and this permission notice shall be included in all copies or
toucyy 0:2d8d0b73e1ff 10 * substantial portions of the Software.
toucyy 0:2d8d0b73e1ff 11 *
toucyy 0:2d8d0b73e1ff 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
toucyy 0:2d8d0b73e1ff 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
toucyy 0:2d8d0b73e1ff 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
toucyy 0:2d8d0b73e1ff 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
toucyy 0:2d8d0b73e1ff 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
toucyy 0:2d8d0b73e1ff 17 */
toucyy 0:2d8d0b73e1ff 18
toucyy 0:2d8d0b73e1ff 19 #include "stdint.h"
toucyy 0:2d8d0b73e1ff 20 #include "USBMIDI.h"
toucyy 0:2d8d0b73e1ff 21 #include "USBBusInterface.h"
toucyy 0:2d8d0b73e1ff 22
toucyy 0:2d8d0b73e1ff 23
toucyy 0:2d8d0b73e1ff 24 USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
toucyy 0:2d8d0b73e1ff 25 midi_evt = NULL;
toucyy 0:2d8d0b73e1ff 26 USBDevice::connect();
toucyy 0:2d8d0b73e1ff 27 }
toucyy 0:2d8d0b73e1ff 28
toucyy 0:2d8d0b73e1ff 29 void USBMIDI::write(MIDIMessage m) {
toucyy 0:2d8d0b73e1ff 30 USBDevice::write(EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK);
toucyy 0:2d8d0b73e1ff 31 }
toucyy 0:2d8d0b73e1ff 32
toucyy 0:2d8d0b73e1ff 33
toucyy 0:2d8d0b73e1ff 34 void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
toucyy 0:2d8d0b73e1ff 35 midi_evt = fptr;
toucyy 0:2d8d0b73e1ff 36 }
toucyy 0:2d8d0b73e1ff 37
toucyy 0:2d8d0b73e1ff 38
toucyy 0:2d8d0b73e1ff 39 bool USBMIDI::EP2_OUT_callback() {
toucyy 0:2d8d0b73e1ff 40 uint8_t buf[64];
toucyy 0:2d8d0b73e1ff 41 uint16_t len;
toucyy 0:2d8d0b73e1ff 42 readEP(EPBULK_OUT, buf, &len, 64);
toucyy 0:2d8d0b73e1ff 43
toucyy 0:2d8d0b73e1ff 44 if (midi_evt != NULL) {
toucyy 0:2d8d0b73e1ff 45 for (int i=0; i<len; i+=4) {
toucyy 0:2d8d0b73e1ff 46 midi_evt(MIDIMessage(buf+i));
toucyy 0:2d8d0b73e1ff 47 }
toucyy 0:2d8d0b73e1ff 48 }
toucyy 0:2d8d0b73e1ff 49
toucyy 0:2d8d0b73e1ff 50 // We reactivate the endpoint to receive next characters
toucyy 0:2d8d0b73e1ff 51 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
toucyy 0:2d8d0b73e1ff 52 return true;
toucyy 0:2d8d0b73e1ff 53 }
toucyy 0:2d8d0b73e1ff 54
toucyy 0:2d8d0b73e1ff 55
toucyy 0:2d8d0b73e1ff 56
toucyy 0:2d8d0b73e1ff 57 // Called in ISR context
toucyy 0:2d8d0b73e1ff 58 // Set configuration. Return false if the
toucyy 0:2d8d0b73e1ff 59 // configuration is not supported.
toucyy 0:2d8d0b73e1ff 60 bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
toucyy 0:2d8d0b73e1ff 61 if (configuration != DEFAULT_CONFIGURATION) {
toucyy 0:2d8d0b73e1ff 62 return false;
toucyy 0:2d8d0b73e1ff 63 }
toucyy 0:2d8d0b73e1ff 64
toucyy 0:2d8d0b73e1ff 65 // Configure endpoints > 0
toucyy 0:2d8d0b73e1ff 66 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
toucyy 0:2d8d0b73e1ff 67 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
toucyy 0:2d8d0b73e1ff 68
toucyy 0:2d8d0b73e1ff 69 // We activate the endpoint to be able to receive data
toucyy 0:2d8d0b73e1ff 70 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
toucyy 0:2d8d0b73e1ff 71 return true;
toucyy 0:2d8d0b73e1ff 72 }
toucyy 0:2d8d0b73e1ff 73
toucyy 0:2d8d0b73e1ff 74
toucyy 0:2d8d0b73e1ff 75 uint8_t * USBMIDI::stringIinterfaceDesc() {
toucyy 0:2d8d0b73e1ff 76 static uint8_t stringIinterfaceDescriptor[] = {
toucyy 0:2d8d0b73e1ff 77 0x0c, //bLength
toucyy 0:2d8d0b73e1ff 78 STRING_DESCRIPTOR, //bDescriptorType 0x03
toucyy 0:2d8d0b73e1ff 79 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
toucyy 0:2d8d0b73e1ff 80 };
toucyy 0:2d8d0b73e1ff 81 return stringIinterfaceDescriptor;
toucyy 0:2d8d0b73e1ff 82 }
toucyy 0:2d8d0b73e1ff 83
toucyy 0:2d8d0b73e1ff 84 uint8_t * USBMIDI::stringIproductDesc() {
toucyy 0:2d8d0b73e1ff 85 static uint8_t stringIproductDescriptor[] = {
toucyy 0:2d8d0b73e1ff 86 0x16, //bLength
toucyy 0:2d8d0b73e1ff 87 STRING_DESCRIPTOR, //bDescriptorType 0x03
toucyy 0:2d8d0b73e1ff 88 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
toucyy 0:2d8d0b73e1ff 89 };
toucyy 0:2d8d0b73e1ff 90 return stringIproductDescriptor;
toucyy 0:2d8d0b73e1ff 91 }
toucyy 0:2d8d0b73e1ff 92
toucyy 0:2d8d0b73e1ff 93
toucyy 0:2d8d0b73e1ff 94 uint8_t * USBMIDI::configurationDesc() {
toucyy 0:2d8d0b73e1ff 95 static uint8_t configDescriptor[] = {
toucyy 0:2d8d0b73e1ff 96 // configuration descriptor
toucyy 0:2d8d0b73e1ff 97 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
toucyy 0:2d8d0b73e1ff 98
toucyy 0:2d8d0b73e1ff 99 // The Audio Interface Collection
toucyy 0:2d8d0b73e1ff 100 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
toucyy 0:2d8d0b73e1ff 101 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
toucyy 0:2d8d0b73e1ff 102 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
toucyy 0:2d8d0b73e1ff 103 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor
toucyy 0:2d8d0b73e1ff 104
toucyy 0:2d8d0b73e1ff 105 // MIDI IN JACKS
toucyy 0:2d8d0b73e1ff 106 0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
toucyy 0:2d8d0b73e1ff 107 0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
toucyy 0:2d8d0b73e1ff 108
toucyy 0:2d8d0b73e1ff 109 // MIDI OUT JACKS
toucyy 0:2d8d0b73e1ff 110 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
toucyy 0:2d8d0b73e1ff 111 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
toucyy 0:2d8d0b73e1ff 112
toucyy 0:2d8d0b73e1ff 113 // OUT endpoint descriptor
toucyy 0:2d8d0b73e1ff 114 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
toucyy 0:2d8d0b73e1ff 115 0x05, 0x25, 0x01, 0x01, 0x01,
toucyy 0:2d8d0b73e1ff 116
toucyy 0:2d8d0b73e1ff 117 // IN endpoint descriptor
toucyy 0:2d8d0b73e1ff 118 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
toucyy 0:2d8d0b73e1ff 119 0x05, 0x25, 0x01, 0x01, 0x03,
toucyy 0:2d8d0b73e1ff 120 };
toucyy 0:2d8d0b73e1ff 121 return configDescriptor;
toucyy 0:2d8d0b73e1ff 122 }