USB device stack, fixes for USBSerial & KL25Z

Fork of USBDevice by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMIDI.cpp Source File

USBMIDI.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "stdint.h"
00020 #include "USBMIDI.h"
00021 
00022 
00023 USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
00024     midi_evt = NULL;
00025     USBDevice::connect();
00026 }
00027 
00028 void USBMIDI::write(MIDIMessage m) {
00029     USBDevice::write(EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK);
00030 }
00031 
00032 
00033 void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
00034     midi_evt = fptr;
00035 }
00036 
00037 
00038 bool USBMIDI::EP2_OUT_callback() {
00039     uint8_t buf[64];
00040     uint32_t len;
00041     readEP(EPBULK_OUT, buf, &len, 64);
00042 
00043     if (midi_evt != NULL) {
00044         for (uint32_t i=0; i<len; i+=4) {
00045             midi_evt(MIDIMessage(buf+i));
00046         }
00047     }
00048 
00049     // We reactivate the endpoint to receive next characters
00050     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00051     return true;
00052 }
00053 
00054 
00055 
00056 // Called in ISR context
00057 // Set configuration. Return false if the
00058 // configuration is not supported.
00059 bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
00060     if (configuration != DEFAULT_CONFIGURATION) {
00061         return false;
00062     }
00063 
00064     // Configure endpoints > 0
00065     addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
00066     addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00067 
00068     // We activate the endpoint to be able to receive data
00069     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00070     return true;
00071 }
00072 
00073 
00074 uint8_t * USBMIDI::stringIinterfaceDesc() {
00075     static uint8_t stringIinterfaceDescriptor[] = {
00076         0x0c,                           //bLength
00077         STRING_DESCRIPTOR,              //bDescriptorType 0x03
00078         'A',0,'u',0,'d',0,'i',0,'o',0   //bString iInterface - Audio
00079     };
00080     return stringIinterfaceDescriptor;
00081 }
00082 
00083 uint8_t * USBMIDI::stringIproductDesc() {
00084     static uint8_t stringIproductDescriptor[] = {
00085         0x16,                                                       //bLength
00086         STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
00087         'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
00088     };
00089     return stringIproductDescriptor;
00090 }
00091 
00092 
00093 uint8_t * USBMIDI::configurationDesc() {
00094     static uint8_t configDescriptor[] = {
00095         // configuration descriptor
00096         0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
00097 
00098         // The Audio Interface Collection
00099         0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
00100         0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
00101         0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
00102         0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00,             // Class-Specific MS Interface Header Descriptor
00103 
00104         // MIDI IN JACKS
00105         0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
00106         0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
00107 
00108         // MIDI OUT JACKS
00109         0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
00110         0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
00111 
00112         // OUT endpoint descriptor
00113         0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
00114         0x05, 0x25, 0x01, 0x01, 0x01,
00115 
00116         // IN endpoint descriptor
00117         0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
00118         0x05, 0x25, 0x01, 0x01, 0x03,
00119     };
00120     return configDescriptor;
00121 }