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