USBAudio example using a microphone

Dependencies:   USBDevice mbed

Committer:
samux
Date:
Fri Dec 16 12:31:41 2011 +0000
Revision:
0:539ec61e1fbb
works with m0 and m3 (sinus) but the code is different to have the same result...

Who changed what in which revision?

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