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