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 "USBCDC.h"
samux 14:757226626acb 21 #include "USBBusInterface.h"
samux 14:757226626acb 22
samux 14:757226626acb 23 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
samux 14:757226626acb 24
samux 14:757226626acb 25 #define DEFAULT_CONFIGURATION (1)
samux 14:757226626acb 26
samux 14:757226626acb 27 #define CDC_SET_LINE_CODING 0x20
samux 14:757226626acb 28 #define CDC_GET_LINE_CODING 0x21
samux 14:757226626acb 29 #define CDC_SET_CONTROL_LINE_STATE 0x22
samux 14:757226626acb 30
samux 14:757226626acb 31 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
samux 14:757226626acb 32
samux 14:757226626acb 33 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
samux 14:757226626acb 34 USBDevice::connect();
samux 14:757226626acb 35 }
samux 14:757226626acb 36
samux 14:757226626acb 37 bool USBCDC::USBCallback_request(void) {
samux 14:757226626acb 38 /* Called in ISR context */
samux 14:757226626acb 39
samux 14:757226626acb 40 bool success = false;
samux 14:757226626acb 41 CONTROL_TRANSFER * transfer = getTransferPtr();
samux 14:757226626acb 42
samux 14:757226626acb 43 /* Process class-specific requests */
samux 14:757226626acb 44
samux 14:757226626acb 45 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
samux 14:757226626acb 46 switch (transfer->setup.bRequest) {
samux 14:757226626acb 47 case CDC_GET_LINE_CODING:
samux 14:757226626acb 48 transfer->remaining = 7;
samux 14:757226626acb 49 transfer->ptr = cdc_line_coding;
samux 14:757226626acb 50 transfer->direction = DEVICE_TO_HOST;
samux 14:757226626acb 51 success = true;
samux 14:757226626acb 52 break;
samux 14:757226626acb 53 case CDC_SET_LINE_CODING:
samux 14:757226626acb 54 transfer->remaining = 7;
samux 14:757226626acb 55 success = true;
samux 14:757226626acb 56 break;
samux 14:757226626acb 57 case CDC_SET_CONTROL_LINE_STATE:
samux 14:757226626acb 58 success = true;
samux 14:757226626acb 59 break;
samux 14:757226626acb 60 default:
samux 14:757226626acb 61 break;
samux 14:757226626acb 62 }
samux 14:757226626acb 63 }
samux 14:757226626acb 64
samux 14:757226626acb 65 return success;
samux 14:757226626acb 66 }
samux 14:757226626acb 67
samux 14:757226626acb 68
samux 14:757226626acb 69 // Called in ISR context
samux 14:757226626acb 70 // Set configuration. Return false if the
samux 14:757226626acb 71 // configuration is not supported.
samux 14:757226626acb 72 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
samux 14:757226626acb 73 if (configuration != DEFAULT_CONFIGURATION) {
samux 14:757226626acb 74 return false;
samux 14:757226626acb 75 }
samux 14:757226626acb 76
samux 14:757226626acb 77 // Configure endpoints > 0
samux 14:757226626acb 78 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
samux 14:757226626acb 79 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
samux 14:757226626acb 80 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 14:757226626acb 81
samux 14:757226626acb 82 // We activate the endpoint to be able to recceive data
samux 14:757226626acb 83 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 14:757226626acb 84 return true;
samux 14:757226626acb 85 }
samux 14:757226626acb 86
samux 14:757226626acb 87 bool USBCDC::send(uint8_t * buffer, uint16_t size) {
samux 14:757226626acb 88 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
samux 14:757226626acb 89 }
samux 14:757226626acb 90
samux 14:757226626acb 91 bool USBCDC::readEP(uint8_t * buffer, uint16_t * size) {
samux 14:757226626acb 92 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
samux 14:757226626acb 93 return false;
samux 14:757226626acb 94 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
samux 14:757226626acb 95 return false;
samux 14:757226626acb 96 return true;
samux 14:757226626acb 97 }
samux 14:757226626acb 98
samux 14:757226626acb 99 bool USBCDC::readEP_NB(uint8_t * buffer, uint16_t * size) {
samux 14:757226626acb 100 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
samux 14:757226626acb 101 return false;
samux 14:757226626acb 102 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
samux 14:757226626acb 103 return false;
samux 14:757226626acb 104 return true;
samux 14:757226626acb 105 }
samux 14:757226626acb 106
samux 14:757226626acb 107
samux 14:757226626acb 108 uint8_t * USBCDC::deviceDesc() {
samux 14:757226626acb 109 static uint8_t deviceDescriptor[] = {
samux 14:757226626acb 110 18, // bLength
samux 14:757226626acb 111 1, // bDescriptorType
samux 14:757226626acb 112 0x10, 0x01, // bcdUSB
samux 14:757226626acb 113 2, // bDeviceClass
samux 14:757226626acb 114 0, // bDeviceSubClass
samux 14:757226626acb 115 0, // bDeviceProtocol
samux 14:757226626acb 116 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
samux 14:757226626acb 117 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
samux 14:757226626acb 118 LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct
samux 14:757226626acb 119 0x00, 0x01, // bcdDevice
samux 14:757226626acb 120 1, // iManufacturer
samux 14:757226626acb 121 2, // iProduct
samux 14:757226626acb 122 3, // iSerialNumber
samux 14:757226626acb 123 1 // bNumConfigurations
samux 14:757226626acb 124 };
samux 14:757226626acb 125 return deviceDescriptor;
samux 14:757226626acb 126 }
samux 14:757226626acb 127
samux 14:757226626acb 128 uint8_t * USBCDC::stringIinterfaceDesc() {
samux 14:757226626acb 129 static uint8_t stringIinterfaceDescriptor[] = {
samux 14:757226626acb 130 0x08,
samux 14:757226626acb 131 STRING_DESCRIPTOR,
samux 14:757226626acb 132 'C',0,'D',0,'C',0,
samux 14:757226626acb 133 };
samux 14:757226626acb 134 return stringIinterfaceDescriptor;
samux 14:757226626acb 135 }
samux 14:757226626acb 136
samux 14:757226626acb 137 uint8_t * USBCDC::stringIproductDesc() {
samux 14:757226626acb 138 static uint8_t stringIproductDescriptor[] = {
samux 14:757226626acb 139 0x16,
samux 14:757226626acb 140 STRING_DESCRIPTOR,
samux 14:757226626acb 141 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
samux 14:757226626acb 142 };
samux 14:757226626acb 143 return stringIproductDescriptor;
samux 14:757226626acb 144 }
samux 14:757226626acb 145
samux 14:757226626acb 146
samux 14:757226626acb 147 #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
samux 14:757226626acb 148
samux 14:757226626acb 149 uint8_t * USBCDC::configurationDesc() {
samux 14:757226626acb 150 static uint8_t configDescriptor[] = {
samux 14:757226626acb 151 9, // bLength;
samux 14:757226626acb 152 2, // bDescriptorType;
samux 14:757226626acb 153 LSB(CONFIG1_DESC_SIZE), // wTotalLength
samux 14:757226626acb 154 MSB(CONFIG1_DESC_SIZE),
samux 14:757226626acb 155 2, // bNumInterfaces
samux 14:757226626acb 156 1, // bConfigurationValue
samux 14:757226626acb 157 0, // iConfiguration
samux 14:757226626acb 158 0x80, // bmAttributes
samux 14:757226626acb 159 50, // bMaxPower
samux 14:757226626acb 160
samux 14:757226626acb 161 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
samux 14:757226626acb 162 9, // bLength
samux 14:757226626acb 163 4, // bDescriptorType
samux 14:757226626acb 164 0, // bInterfaceNumber
samux 14:757226626acb 165 0, // bAlternateSetting
samux 14:757226626acb 166 1, // bNumEndpoints
samux 14:757226626acb 167 0x02, // bInterfaceClass
samux 14:757226626acb 168 0x02, // bInterfaceSubClass
samux 14:757226626acb 169 0x01, // bInterfaceProtocol
samux 14:757226626acb 170 0, // iInterface
samux 14:757226626acb 171
samux 14:757226626acb 172 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
samux 14:757226626acb 173 5, // bFunctionLength
samux 14:757226626acb 174 0x24, // bDescriptorType
samux 14:757226626acb 175 0x00, // bDescriptorSubtype
samux 14:757226626acb 176 0x10, 0x01, // bcdCDC
samux 14:757226626acb 177
samux 14:757226626acb 178 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
samux 14:757226626acb 179 5, // bFunctionLength
samux 14:757226626acb 180 0x24, // bDescriptorType
samux 14:757226626acb 181 0x01, // bDescriptorSubtype
samux 14:757226626acb 182 0x03, // bmCapabilities
samux 14:757226626acb 183 1, // bDataInterface
samux 14:757226626acb 184
samux 14:757226626acb 185 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
samux 14:757226626acb 186 4, // bFunctionLength
samux 14:757226626acb 187 0x24, // bDescriptorType
samux 14:757226626acb 188 0x02, // bDescriptorSubtype
samux 14:757226626acb 189 0x06, // bmCapabilities
samux 14:757226626acb 190
samux 14:757226626acb 191 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
samux 14:757226626acb 192 5, // bFunctionLength
samux 14:757226626acb 193 0x24, // bDescriptorType
samux 14:757226626acb 194 0x06, // bDescriptorSubtype
samux 14:757226626acb 195 0, // bMasterInterface
samux 14:757226626acb 196 1, // bSlaveInterface0
samux 14:757226626acb 197
samux 14:757226626acb 198 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
samux 14:757226626acb 199 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
samux 14:757226626acb 200 ENDPOINT_DESCRIPTOR, // bDescriptorType
samux 14:757226626acb 201 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
samux 14:757226626acb 202 E_INTERRUPT, // bmAttributes (0x03=intr)
samux 14:757226626acb 203 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
samux 14:757226626acb 204 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
samux 14:757226626acb 205 16, // bInterval
samux 14:757226626acb 206
samux 14:757226626acb 207
samux 14:757226626acb 208
samux 14:757226626acb 209
samux 14:757226626acb 210 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
samux 14:757226626acb 211 9, // bLength
samux 14:757226626acb 212 4, // bDescriptorType
samux 14:757226626acb 213 1, // bInterfaceNumber
samux 14:757226626acb 214 0, // bAlternateSetting
samux 14:757226626acb 215 2, // bNumEndpoints
samux 14:757226626acb 216 0x0A, // bInterfaceClass
samux 14:757226626acb 217 0x00, // bInterfaceSubClass
samux 14:757226626acb 218 0x00, // bInterfaceProtocol
samux 14:757226626acb 219 0, // iInterface
samux 14:757226626acb 220
samux 14:757226626acb 221 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
samux 14:757226626acb 222 7, // bLength
samux 14:757226626acb 223 5, // bDescriptorType
samux 14:757226626acb 224 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
samux 14:757226626acb 225 0x02, // bmAttributes (0x02=bulk)
samux 14:757226626acb 226 LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)
samux 14:757226626acb 227 MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)
samux 14:757226626acb 228 0, // bInterval
samux 14:757226626acb 229
samux 14:757226626acb 230 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
samux 14:757226626acb 231 7, // bLength
samux 14:757226626acb 232 5, // bDescriptorType
samux 14:757226626acb 233 PHY_TO_DESC(EPBULK_OUT),// bEndpointAddress
samux 14:757226626acb 234 0x02, // bmAttributes (0x02=bulk)
samux 14:757226626acb 235 LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)
samux 14:757226626acb 236 MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)
samux 14:757226626acb 237 0 // bInterval
samux 14:757226626acb 238 };
samux 14:757226626acb 239 return configDescriptor;
samux 14:757226626acb 240 }