EA BaseBoard, playing wav, PC see\'s SD-card through USB port.

Dependencies:   mbed

Committer:
Lerche
Date:
Tue Nov 22 05:45:58 2011 +0000
Revision:
0:fef366d2ed20
Thanks to those who provided EA_WavPlayer and USB_MSC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lerche 0:fef366d2ed20 1 // USB_MIDI.cpp
Lerche 0:fef366d2ed20 2 // MIDI edvice example
Lerche 0:fef366d2ed20 3 // Copyright (c) 2011 ARM Limited. All rights reserved.
Lerche 0:fef366d2ed20 4
Lerche 0:fef366d2ed20 5 //#define DEBUG
Lerche 0:fef366d2ed20 6 #include "dbg.h"
Lerche 0:fef366d2ed20 7 #include "stdint.h"
Lerche 0:fef366d2ed20 8 #include "USBMSC.h"
Lerche 0:fef366d2ed20 9 #include "USBBusInterface.h"
Lerche 0:fef366d2ed20 10 #include "USBDevice.h"
Lerche 0:fef366d2ed20 11 #include "USBDevice_Types.h"
Lerche 0:fef366d2ed20 12
Lerche 0:fef366d2ed20 13 void USBMSC::setTransferData (uint8_t *buf, int len) {
Lerche 0:fef366d2ed20 14 CONTROL_TRANSFER *transfer = getTransferPtr();
Lerche 0:fef366d2ed20 15 transfer->remaining = len;
Lerche 0:fef366d2ed20 16 transfer->ptr = buf;
Lerche 0:fef366d2ed20 17 transfer->direction = DEVICE_TO_HOST;
Lerche 0:fef366d2ed20 18 }
Lerche 0:fef366d2ed20 19
Lerche 0:fef366d2ed20 20 void USBMSC::attach(void (*fptr)(uint8_t)) {
Lerche 0:fef366d2ed20 21 msc_evt = fptr;
Lerche 0:fef366d2ed20 22 }
Lerche 0:fef366d2ed20 23
Lerche 0:fef366d2ed20 24 USBMSC::USBMSC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
Lerche 0:fef366d2ed20 25 msc_evt = NULL;
Lerche 0:fef366d2ed20 26 }
Lerche 0:fef366d2ed20 27
Lerche 0:fef366d2ed20 28 bool USBMSC::EPBULK_OUT_callback() {
Lerche 0:fef366d2ed20 29
Lerche 0:fef366d2ed20 30 if (msc_evt && ! getEndpointStallState(EPBULK_OUT)) {
Lerche 0:fef366d2ed20 31 msc_evt(EPBULK_OUT);
Lerche 0:fef366d2ed20 32 }
Lerche 0:fef366d2ed20 33
Lerche 0:fef366d2ed20 34 // We reactivate the endpoint to receive next characters
Lerche 0:fef366d2ed20 35 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Lerche 0:fef366d2ed20 36 DBG("EPBULK_OUT_callback\r\n");
Lerche 0:fef366d2ed20 37 return true;
Lerche 0:fef366d2ed20 38 }
Lerche 0:fef366d2ed20 39
Lerche 0:fef366d2ed20 40 bool USBMSC::EPBULK_IN_callback() {
Lerche 0:fef366d2ed20 41 DBG("EPBULK_IN_callback\r\n");
Lerche 0:fef366d2ed20 42
Lerche 0:fef366d2ed20 43 if (msc_evt && ! getEndpointStallState(EPBULK_IN)) {
Lerche 0:fef366d2ed20 44 msc_evt(EPBULK_IN);
Lerche 0:fef366d2ed20 45 }
Lerche 0:fef366d2ed20 46
Lerche 0:fef366d2ed20 47 return false;
Lerche 0:fef366d2ed20 48 }
Lerche 0:fef366d2ed20 49
Lerche 0:fef366d2ed20 50 bool USBMSC::USBCallback_request () {
Lerche 0:fef366d2ed20 51 bool success = false;
Lerche 0:fef366d2ed20 52 CONTROL_TRANSFER *transfer = getTransferPtr();
Lerche 0:fef366d2ed20 53
Lerche 0:fef366d2ed20 54 DBG("USBCallback_request: type %x, request %x\r\n", transfer->setup.bmRequestType.Type, transfer->setup.bRequest);
Lerche 0:fef366d2ed20 55 /* Process standard requests */
Lerche 0:fef366d2ed20 56 if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
Lerche 0:fef366d2ed20 57 {
Lerche 0:fef366d2ed20 58 switch (transfer->setup.bRequest)
Lerche 0:fef366d2ed20 59 {
Lerche 0:fef366d2ed20 60 default:
Lerche 0:fef366d2ed20 61 break;
Lerche 0:fef366d2ed20 62 }
Lerche 0:fef366d2ed20 63 }
Lerche 0:fef366d2ed20 64
Lerche 0:fef366d2ed20 65 if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
Lerche 0:fef366d2ed20 66 {
Lerche 0:fef366d2ed20 67 switch (transfer->setup.bRequest)
Lerche 0:fef366d2ed20 68 {
Lerche 0:fef366d2ed20 69 case 0xFE: // MSC get max LUN
Lerche 0:fef366d2ed20 70 static uint8_t dummy[] = {0};
Lerche 0:fef366d2ed20 71 transfer->remaining = 1;
Lerche 0:fef366d2ed20 72 transfer->ptr = dummy;
Lerche 0:fef366d2ed20 73 transfer->direction = DEVICE_TO_HOST;
Lerche 0:fef366d2ed20 74 success = true;
Lerche 0:fef366d2ed20 75 break;
Lerche 0:fef366d2ed20 76 case 0xFF: // MSC reset
Lerche 0:fef366d2ed20 77 if (transfer->setup.wLength == 0) {
Lerche 0:fef366d2ed20 78 if (msc_evt) {
Lerche 0:fef366d2ed20 79 msc_evt(0xFF);
Lerche 0:fef366d2ed20 80 }
Lerche 0:fef366d2ed20 81 success = true;
Lerche 0:fef366d2ed20 82 }
Lerche 0:fef366d2ed20 83 break;
Lerche 0:fef366d2ed20 84 default:
Lerche 0:fef366d2ed20 85 break;
Lerche 0:fef366d2ed20 86 }
Lerche 0:fef366d2ed20 87 }
Lerche 0:fef366d2ed20 88
Lerche 0:fef366d2ed20 89 return success;
Lerche 0:fef366d2ed20 90 }
Lerche 0:fef366d2ed20 91
Lerche 0:fef366d2ed20 92 bool USBMSC::USBCallback_setConfiguration(uint8_t configuration) {
Lerche 0:fef366d2ed20 93 DBG("USBCallback_setConfiguration: config %x\r\n", configuration);
Lerche 0:fef366d2ed20 94 // Called in ISR context
Lerche 0:fef366d2ed20 95 // Set configuration. Return false if the
Lerche 0:fef366d2ed20 96 // configuration is not supported.
Lerche 0:fef366d2ed20 97 if (configuration != DEFAULT_CONFIGURATION) {
Lerche 0:fef366d2ed20 98 return false;
Lerche 0:fef366d2ed20 99 }
Lerche 0:fef366d2ed20 100
Lerche 0:fef366d2ed20 101 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
Lerche 0:fef366d2ed20 102
Lerche 0:fef366d2ed20 103 // enable bulk-in interrupts on NAKs
Lerche 0:fef366d2ed20 104 // these are required to get the BOT protocol going again after a STALL
Lerche 0:fef366d2ed20 105 NakIntEnable(EPBULK_IN);
Lerche 0:fef366d2ed20 106
Lerche 0:fef366d2ed20 107 // Configure endpoints > 0
Lerche 0:fef366d2ed20 108 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
Lerche 0:fef366d2ed20 109 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Lerche 0:fef366d2ed20 110
Lerche 0:fef366d2ed20 111 // We activate the endpoint to be able to receive data
Lerche 0:fef366d2ed20 112 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Lerche 0:fef366d2ed20 113
Lerche 0:fef366d2ed20 114 return true;
Lerche 0:fef366d2ed20 115 }
Lerche 0:fef366d2ed20 116
Lerche 0:fef366d2ed20 117 uint8_t * USBMSC::configurationDesc() {
Lerche 0:fef366d2ed20 118 static uint8_t configDescriptor[] = {
Lerche 0:fef366d2ed20 119 // configuration descriptor
Lerche 0:fef366d2ed20 120 0x09, 0x02, 32, 0x00, 0x01, 0x01, 0x00, 0xC0, 0x32,
Lerche 0:fef366d2ed20 121
Lerche 0:fef366d2ed20 122 // MSC
Lerche 0:fef366d2ed20 123 // control class interface
Lerche 0:fef366d2ed20 124 0x09, 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50, 0x00,
Lerche 0:fef366d2ed20 125 // EP OUT
Lerche 0:fef366d2ed20 126 0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
Lerche 0:fef366d2ed20 127 // EP IN
Lerche 0:fef366d2ed20 128 0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00,
Lerche 0:fef366d2ed20 129 };
Lerche 0:fef366d2ed20 130 DBG("configurationDesc %d\r\n", sizeof(configDescriptor));
Lerche 0:fef366d2ed20 131 return configDescriptor;
Lerche 0:fef366d2ed20 132 }
Lerche 0:fef366d2ed20 133
Lerche 0:fef366d2ed20 134 uint8_t * USBMSC::deviceDesc() {
Lerche 0:fef366d2ed20 135 static uint8_t deviceDescriptor[] = {
Lerche 0:fef366d2ed20 136 DEVICE_DESCRIPTOR_LENGTH, /* bLength */
Lerche 0:fef366d2ed20 137 DEVICE_DESCRIPTOR, /* bDescriptorType */
Lerche 0:fef366d2ed20 138 LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */
Lerche 0:fef366d2ed20 139 MSB(USB_VERSION_2_0), /* bcdUSB (MSB) */
Lerche 0:fef366d2ed20 140 0x00, /* bDeviceClass */
Lerche 0:fef366d2ed20 141 0x00, /* bDeviceSubClass */
Lerche 0:fef366d2ed20 142 0x00, /* bDeviceprotocol */
Lerche 0:fef366d2ed20 143 MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */
Lerche 0:fef366d2ed20 144 LSB(VENDOR_ID), /* idVendor (LSB) */
Lerche 0:fef366d2ed20 145 MSB(VENDOR_ID), /* idVendor (MSB) */
Lerche 0:fef366d2ed20 146 LSB(PRODUCT_ID), /* idProduct (LSB) */
Lerche 0:fef366d2ed20 147 MSB(PRODUCT_ID), /* idProduct (MSB) */
Lerche 0:fef366d2ed20 148 LSB(PRODUCT_RELEASE), /* bcdDevice (LSB) */
Lerche 0:fef366d2ed20 149 MSB(PRODUCT_RELEASE), /* bcdDevice (MSB) */
Lerche 0:fef366d2ed20 150 STRING_OFFSET_IMANUFACTURER, /* iManufacturer */
Lerche 0:fef366d2ed20 151 STRING_OFFSET_IPRODUCT, /* iProduct */
Lerche 0:fef366d2ed20 152 STRING_OFFSET_ISERIAL, /* iSerialNumber */
Lerche 0:fef366d2ed20 153 0x01 /* bNumConfigurations */
Lerche 0:fef366d2ed20 154 };
Lerche 0:fef366d2ed20 155 return deviceDescriptor;
Lerche 0:fef366d2ed20 156 }