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