USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Fri Nov 11 15:22:53 2011 +0000
Revision:
2:27a7e7f8d399
we have 2MB with the sdcard!!

Who changed what in which revision?

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