SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lharoon 0:22612ae617a0 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
lharoon 0:22612ae617a0 2 *
lharoon 0:22612ae617a0 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
lharoon 0:22612ae617a0 4 * and associated documentation files (the "Software"), to deal in the Software without
lharoon 0:22612ae617a0 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
lharoon 0:22612ae617a0 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
lharoon 0:22612ae617a0 7 * Software is furnished to do so, subject to the following conditions:
lharoon 0:22612ae617a0 8 *
lharoon 0:22612ae617a0 9 * The above copyright notice and this permission notice shall be included in all copies or
lharoon 0:22612ae617a0 10 * substantial portions of the Software.
lharoon 0:22612ae617a0 11 *
lharoon 0:22612ae617a0 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
lharoon 0:22612ae617a0 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
lharoon 0:22612ae617a0 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
lharoon 0:22612ae617a0 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
lharoon 0:22612ae617a0 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
lharoon 0:22612ae617a0 17 */
lharoon 0:22612ae617a0 18
lharoon 0:22612ae617a0 19 #include "stdint.h"
lharoon 0:22612ae617a0 20 #include "USBCDC.h"
lharoon 0:22612ae617a0 21 #include "USBBusInterface.h"
lharoon 0:22612ae617a0 22
lharoon 0:22612ae617a0 23 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
lharoon 0:22612ae617a0 24
lharoon 0:22612ae617a0 25 #define DEFAULT_CONFIGURATION (1)
lharoon 0:22612ae617a0 26
lharoon 0:22612ae617a0 27 #define CDC_SET_LINE_CODING 0x20
lharoon 0:22612ae617a0 28 #define CDC_GET_LINE_CODING 0x21
lharoon 0:22612ae617a0 29 #define CDC_SET_CONTROL_LINE_STATE 0x22
lharoon 0:22612ae617a0 30
lharoon 0:22612ae617a0 31 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
lharoon 0:22612ae617a0 32
lharoon 0:22612ae617a0 33 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
lharoon 0:22612ae617a0 34 USBDevice::connect();
lharoon 0:22612ae617a0 35 }
lharoon 0:22612ae617a0 36
lharoon 0:22612ae617a0 37 bool USBCDC::USBCallback_request(void) {
lharoon 0:22612ae617a0 38 /* Called in ISR context */
lharoon 0:22612ae617a0 39
lharoon 0:22612ae617a0 40 bool success = false;
lharoon 0:22612ae617a0 41 CONTROL_TRANSFER * transfer = getTransferPtr();
lharoon 0:22612ae617a0 42
lharoon 0:22612ae617a0 43 /* Process class-specific requests */
lharoon 0:22612ae617a0 44
lharoon 0:22612ae617a0 45 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
lharoon 0:22612ae617a0 46 switch (transfer->setup.bRequest) {
lharoon 0:22612ae617a0 47 case CDC_GET_LINE_CODING:
lharoon 0:22612ae617a0 48 transfer->remaining = 7;
lharoon 0:22612ae617a0 49 transfer->ptr = cdc_line_coding;
lharoon 0:22612ae617a0 50 transfer->direction = DEVICE_TO_HOST;
lharoon 0:22612ae617a0 51 success = true;
lharoon 0:22612ae617a0 52 break;
lharoon 0:22612ae617a0 53 case CDC_SET_LINE_CODING:
lharoon 0:22612ae617a0 54 transfer->remaining = 7;
lharoon 0:22612ae617a0 55 success = true;
lharoon 0:22612ae617a0 56 break;
lharoon 0:22612ae617a0 57 case CDC_SET_CONTROL_LINE_STATE:
lharoon 0:22612ae617a0 58 success = true;
lharoon 0:22612ae617a0 59 break;
lharoon 0:22612ae617a0 60 default:
lharoon 0:22612ae617a0 61 break;
lharoon 0:22612ae617a0 62 }
lharoon 0:22612ae617a0 63 }
lharoon 0:22612ae617a0 64
lharoon 0:22612ae617a0 65 return success;
lharoon 0:22612ae617a0 66 }
lharoon 0:22612ae617a0 67
lharoon 0:22612ae617a0 68
lharoon 0:22612ae617a0 69 // Called in ISR context
lharoon 0:22612ae617a0 70 // Set configuration. Return false if the
lharoon 0:22612ae617a0 71 // configuration is not supported.
lharoon 0:22612ae617a0 72 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
lharoon 0:22612ae617a0 73 if (configuration != DEFAULT_CONFIGURATION) {
lharoon 0:22612ae617a0 74 return false;
lharoon 0:22612ae617a0 75 }
lharoon 0:22612ae617a0 76
lharoon 0:22612ae617a0 77 // Configure endpoints > 0
lharoon 0:22612ae617a0 78 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
lharoon 0:22612ae617a0 79 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
lharoon 0:22612ae617a0 80 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
lharoon 0:22612ae617a0 81
lharoon 0:22612ae617a0 82 // We activate the endpoint to be able to recceive data
lharoon 0:22612ae617a0 83 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
lharoon 0:22612ae617a0 84 return true;
lharoon 0:22612ae617a0 85 }
lharoon 0:22612ae617a0 86
lharoon 0:22612ae617a0 87 bool USBCDC::send(uint8_t * buffer, uint16_t size) {
lharoon 0:22612ae617a0 88 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
lharoon 0:22612ae617a0 89 }
lharoon 0:22612ae617a0 90
lharoon 0:22612ae617a0 91 bool USBCDC::readEP(uint8_t * buffer, uint16_t * size) {
lharoon 0:22612ae617a0 92 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
lharoon 0:22612ae617a0 93 return false;
lharoon 0:22612ae617a0 94 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
lharoon 0:22612ae617a0 95 return false;
lharoon 0:22612ae617a0 96 return true;
lharoon 0:22612ae617a0 97 }
lharoon 0:22612ae617a0 98
lharoon 0:22612ae617a0 99 bool USBCDC::readEP_NB(uint8_t * buffer, uint16_t * size) {
lharoon 0:22612ae617a0 100 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
lharoon 0:22612ae617a0 101 return false;
lharoon 0:22612ae617a0 102 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
lharoon 0:22612ae617a0 103 return false;
lharoon 0:22612ae617a0 104 return true;
lharoon 0:22612ae617a0 105 }
lharoon 0:22612ae617a0 106
lharoon 0:22612ae617a0 107
lharoon 0:22612ae617a0 108 uint8_t * USBCDC::deviceDesc() {
lharoon 0:22612ae617a0 109 static uint8_t deviceDescriptor[] = {
lharoon 0:22612ae617a0 110 18, // bLength
lharoon 0:22612ae617a0 111 1, // bDescriptorType
lharoon 0:22612ae617a0 112 0x10, 0x01, // bcdUSB
lharoon 0:22612ae617a0 113 2, // bDeviceClass
lharoon 0:22612ae617a0 114 0, // bDeviceSubClass
lharoon 0:22612ae617a0 115 0, // bDeviceProtocol
lharoon 0:22612ae617a0 116 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
lharoon 0:22612ae617a0 117 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
lharoon 0:22612ae617a0 118 LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct
lharoon 0:22612ae617a0 119 0x00, 0x01, // bcdDevice
lharoon 0:22612ae617a0 120 1, // iManufacturer
lharoon 0:22612ae617a0 121 2, // iProduct
lharoon 0:22612ae617a0 122 3, // iSerialNumber
lharoon 0:22612ae617a0 123 1 // bNumConfigurations
lharoon 0:22612ae617a0 124 };
lharoon 0:22612ae617a0 125 return deviceDescriptor;
lharoon 0:22612ae617a0 126 }
lharoon 0:22612ae617a0 127
lharoon 0:22612ae617a0 128 uint8_t * USBCDC::stringIinterfaceDesc() {
lharoon 0:22612ae617a0 129 static uint8_t stringIinterfaceDescriptor[] = {
lharoon 0:22612ae617a0 130 0x08,
lharoon 0:22612ae617a0 131 STRING_DESCRIPTOR,
lharoon 0:22612ae617a0 132 'C',0,'D',0,'C',0,
lharoon 0:22612ae617a0 133 };
lharoon 0:22612ae617a0 134 return stringIinterfaceDescriptor;
lharoon 0:22612ae617a0 135 }
lharoon 0:22612ae617a0 136
lharoon 0:22612ae617a0 137 uint8_t * USBCDC::stringIproductDesc() {
lharoon 0:22612ae617a0 138 static uint8_t stringIproductDescriptor[] = {
lharoon 0:22612ae617a0 139 0x16,
lharoon 0:22612ae617a0 140 STRING_DESCRIPTOR,
lharoon 0:22612ae617a0 141 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
lharoon 0:22612ae617a0 142 };
lharoon 0:22612ae617a0 143 return stringIproductDescriptor;
lharoon 0:22612ae617a0 144 }
lharoon 0:22612ae617a0 145
lharoon 0:22612ae617a0 146
lharoon 0:22612ae617a0 147 #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
lharoon 0:22612ae617a0 148
lharoon 0:22612ae617a0 149 uint8_t * USBCDC::configurationDesc() {
lharoon 0:22612ae617a0 150 static uint8_t configDescriptor[] = {
lharoon 0:22612ae617a0 151 9, // bLength;
lharoon 0:22612ae617a0 152 2, // bDescriptorType;
lharoon 0:22612ae617a0 153 LSB(CONFIG1_DESC_SIZE), // wTotalLength
lharoon 0:22612ae617a0 154 MSB(CONFIG1_DESC_SIZE),
lharoon 0:22612ae617a0 155 2, // bNumInterfaces
lharoon 0:22612ae617a0 156 1, // bConfigurationValue
lharoon 0:22612ae617a0 157 0, // iConfiguration
lharoon 0:22612ae617a0 158 0x80, // bmAttributes
lharoon 0:22612ae617a0 159 50, // bMaxPower
lharoon 0:22612ae617a0 160
lharoon 0:22612ae617a0 161 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
lharoon 0:22612ae617a0 162 9, // bLength
lharoon 0:22612ae617a0 163 4, // bDescriptorType
lharoon 0:22612ae617a0 164 0, // bInterfaceNumber
lharoon 0:22612ae617a0 165 0, // bAlternateSetting
lharoon 0:22612ae617a0 166 1, // bNumEndpoints
lharoon 0:22612ae617a0 167 0x02, // bInterfaceClass
lharoon 0:22612ae617a0 168 0x02, // bInterfaceSubClass
lharoon 0:22612ae617a0 169 0x01, // bInterfaceProtocol
lharoon 0:22612ae617a0 170 0, // iInterface
lharoon 0:22612ae617a0 171
lharoon 0:22612ae617a0 172 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
lharoon 0:22612ae617a0 173 5, // bFunctionLength
lharoon 0:22612ae617a0 174 0x24, // bDescriptorType
lharoon 0:22612ae617a0 175 0x00, // bDescriptorSubtype
lharoon 0:22612ae617a0 176 0x10, 0x01, // bcdCDC
lharoon 0:22612ae617a0 177
lharoon 0:22612ae617a0 178 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
lharoon 0:22612ae617a0 179 5, // bFunctionLength
lharoon 0:22612ae617a0 180 0x24, // bDescriptorType
lharoon 0:22612ae617a0 181 0x01, // bDescriptorSubtype
lharoon 0:22612ae617a0 182 0x03, // bmCapabilities
lharoon 0:22612ae617a0 183 1, // bDataInterface
lharoon 0:22612ae617a0 184
lharoon 0:22612ae617a0 185 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
lharoon 0:22612ae617a0 186 4, // bFunctionLength
lharoon 0:22612ae617a0 187 0x24, // bDescriptorType
lharoon 0:22612ae617a0 188 0x02, // bDescriptorSubtype
lharoon 0:22612ae617a0 189 0x06, // bmCapabilities
lharoon 0:22612ae617a0 190
lharoon 0:22612ae617a0 191 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
lharoon 0:22612ae617a0 192 5, // bFunctionLength
lharoon 0:22612ae617a0 193 0x24, // bDescriptorType
lharoon 0:22612ae617a0 194 0x06, // bDescriptorSubtype
lharoon 0:22612ae617a0 195 0, // bMasterInterface
lharoon 0:22612ae617a0 196 1, // bSlaveInterface0
lharoon 0:22612ae617a0 197
lharoon 0:22612ae617a0 198 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
lharoon 0:22612ae617a0 199 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
lharoon 0:22612ae617a0 200 ENDPOINT_DESCRIPTOR, // bDescriptorType
lharoon 0:22612ae617a0 201 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
lharoon 0:22612ae617a0 202 E_INTERRUPT, // bmAttributes (0x03=intr)
lharoon 0:22612ae617a0 203 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
lharoon 0:22612ae617a0 204 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
lharoon 0:22612ae617a0 205 16, // bInterval
lharoon 0:22612ae617a0 206
lharoon 0:22612ae617a0 207
lharoon 0:22612ae617a0 208
lharoon 0:22612ae617a0 209
lharoon 0:22612ae617a0 210 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
lharoon 0:22612ae617a0 211 9, // bLength
lharoon 0:22612ae617a0 212 4, // bDescriptorType
lharoon 0:22612ae617a0 213 1, // bInterfaceNumber
lharoon 0:22612ae617a0 214 0, // bAlternateSetting
lharoon 0:22612ae617a0 215 2, // bNumEndpoints
lharoon 0:22612ae617a0 216 0x0A, // bInterfaceClass
lharoon 0:22612ae617a0 217 0x00, // bInterfaceSubClass
lharoon 0:22612ae617a0 218 0x00, // bInterfaceProtocol
lharoon 0:22612ae617a0 219 0, // iInterface
lharoon 0:22612ae617a0 220
lharoon 0:22612ae617a0 221 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
lharoon 0:22612ae617a0 222 7, // bLength
lharoon 0:22612ae617a0 223 5, // bDescriptorType
lharoon 0:22612ae617a0 224 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
lharoon 0:22612ae617a0 225 0x02, // bmAttributes (0x02=bulk)
lharoon 0:22612ae617a0 226 LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)
lharoon 0:22612ae617a0 227 MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)
lharoon 0:22612ae617a0 228 0, // bInterval
lharoon 0:22612ae617a0 229
lharoon 0:22612ae617a0 230 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
lharoon 0:22612ae617a0 231 7, // bLength
lharoon 0:22612ae617a0 232 5, // bDescriptorType
lharoon 0:22612ae617a0 233 PHY_TO_DESC(EPBULK_OUT),// bEndpointAddress
lharoon 0:22612ae617a0 234 0x02, // bmAttributes (0x02=bulk)
lharoon 0:22612ae617a0 235 LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)
lharoon 0:22612ae617a0 236 MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)
lharoon 0:22612ae617a0 237 0 // bInterval
lharoon 0:22612ae617a0 238 };
lharoon 0:22612ae617a0 239 return configDescriptor;
lharoon 0:22612ae617a0 240 }