USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Tue Dec 06 12:07:12 2011 +0000
Revision:
14:757226626acb
protection enabled when usb cable plugged. filesystem has no access when plugged

Who changed what in which revision?

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