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