USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Mon Nov 14 10:00:07 2011 +0000
Revision:
6:126c4d980196
Parent:
5:8afbc15d6892
now we have a connect inside USBMSD to get all capacities from storage chip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 2:27a7e7f8d399 1 // USBMSD.cpp
samux 2:27a7e7f8d399 2 // USB mass storage device example
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 "USBMSD.h"
samux 2:27a7e7f8d399 7 #include "USBBusInterface.h"
samux 2:27a7e7f8d399 8
samux 2:27a7e7f8d399 9 #define CBW_Signature 0x43425355
samux 2:27a7e7f8d399 10 #define CSW_Signature 0x53425355
samux 2:27a7e7f8d399 11
samux 2:27a7e7f8d399 12 // SCSI Commands
samux 2:27a7e7f8d399 13 #define TEST_UNIT_READY 0x00
samux 2:27a7e7f8d399 14 #define REQUEST_SENSE 0x03
samux 2:27a7e7f8d399 15 #define FORMAT_UNIT 0x04
samux 2:27a7e7f8d399 16 #define INQUIRY 0x12
samux 2:27a7e7f8d399 17 #define MODE_SELECT6 0x15
samux 2:27a7e7f8d399 18 #define MODE_SENSE6 0x1A
samux 2:27a7e7f8d399 19 #define START_STOP_UNIT 0x1B
samux 2:27a7e7f8d399 20 #define MEDIA_REMOVAL 0x1E
samux 2:27a7e7f8d399 21 #define READ_FORMAT_CAPACITIES 0x23
samux 2:27a7e7f8d399 22 #define READ_CAPACITY 0x25
samux 2:27a7e7f8d399 23 #define READ10 0x28
samux 2:27a7e7f8d399 24 #define WRITE10 0x2A
samux 2:27a7e7f8d399 25 #define VERIFY10 0x2F
samux 2:27a7e7f8d399 26 #define READ12 0xA8
samux 2:27a7e7f8d399 27 #define WRITE12 0xAA
samux 2:27a7e7f8d399 28 #define MODE_SELECT10 0x55
samux 2:27a7e7f8d399 29 #define MODE_SENSE10 0x5A
samux 2:27a7e7f8d399 30
samux 2:27a7e7f8d399 31 // MSC class specific requests
samux 2:27a7e7f8d399 32 #define MSC_REQUEST_RESET 0xFF
samux 2:27a7e7f8d399 33 #define MSC_REQUEST_GET_MAX_LUN 0xFE
samux 2:27a7e7f8d399 34
samux 2:27a7e7f8d399 35 #define DEFAULT_CONFIGURATION (1)
samux 2:27a7e7f8d399 36
samux 2:27a7e7f8d399 37 // Max In/Out Packet Size on the bulk endpoint */
samux 2:27a7e7f8d399 38 #define MAX_PACKET MAX_PACKET_SIZE_EPBULK
samux 2:27a7e7f8d399 39
samux 2:27a7e7f8d399 40 // CSW Status
samux 2:27a7e7f8d399 41 enum Status {
samux 5:8afbc15d6892 42 CSW_PASSED,
samux 5:8afbc15d6892 43 CSW_FAILED,
samux 5:8afbc15d6892 44 CSW_ERROR,
samux 2:27a7e7f8d399 45 };
samux 2:27a7e7f8d399 46
samux 2:27a7e7f8d399 47
samux 3:0ffb2eee9e06 48 USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
samux 2:27a7e7f8d399 49 }
samux 2:27a7e7f8d399 50
samux 6:126c4d980196 51
samux 6:126c4d980196 52 DigitalOut l2(LED2);
samux 2:27a7e7f8d399 53 // Called in ISR context to process a class specific request
samux 2:27a7e7f8d399 54 bool USBMSD::USBCallback_request(void) {
samux 2:27a7e7f8d399 55
samux 2:27a7e7f8d399 56 bool success = false;
samux 2:27a7e7f8d399 57 CONTROL_TRANSFER * transfer = getTransferPtr();
samux 2:27a7e7f8d399 58
samux 2:27a7e7f8d399 59 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
samux 2:27a7e7f8d399 60 switch (transfer->setup.bRequest) {
samux 2:27a7e7f8d399 61 case MSC_REQUEST_RESET:
samux 2:27a7e7f8d399 62 reset();
samux 2:27a7e7f8d399 63 success = true;
samux 2:27a7e7f8d399 64 break;
samux 2:27a7e7f8d399 65 case MSC_REQUEST_GET_MAX_LUN:
samux 2:27a7e7f8d399 66 transfer->remaining = 1;
samux 2:27a7e7f8d399 67 transfer->ptr = getMaxLUN();
samux 2:27a7e7f8d399 68 transfer->direction = DEVICE_TO_HOST;
samux 2:27a7e7f8d399 69 success = true;
samux 2:27a7e7f8d399 70 break;
samux 2:27a7e7f8d399 71 default:
samux 2:27a7e7f8d399 72 break;
samux 2:27a7e7f8d399 73 }
samux 2:27a7e7f8d399 74 }
samux 2:27a7e7f8d399 75
samux 2:27a7e7f8d399 76 return success;
samux 2:27a7e7f8d399 77 }
samux 2:27a7e7f8d399 78
samux 6:126c4d980196 79
samux 6:126c4d980196 80 bool USBMSD::connect()
samux 6:126c4d980196 81 {
samux 6:126c4d980196 82
samux 6:126c4d980196 83 //disk initialization
samux 6:126c4d980196 84 diskInit();
samux 6:126c4d980196 85
samux 6:126c4d980196 86 // get block size
samux 6:126c4d980196 87 BlockSize = blockSize();
samux 6:126c4d980196 88 if(BlockSize != 0) {
samux 6:126c4d980196 89 page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
samux 6:126c4d980196 90 if(page == NULL)
samux 6:126c4d980196 91 return false;
samux 6:126c4d980196 92 }
samux 6:126c4d980196 93
samux 6:126c4d980196 94 //get memory size
samux 6:126c4d980196 95 MemorySize = memorySize();
samux 6:126c4d980196 96 if(!MemorySize) {
samux 6:126c4d980196 97 return false;
samux 6:126c4d980196 98 }
samux 6:126c4d980196 99 BlockCount = MemorySize/BlockSize;
samux 6:126c4d980196 100
samux 6:126c4d980196 101 //connect the device
samux 6:126c4d980196 102 USBDevice::connect();
samux 6:126c4d980196 103 return true;
samux 6:126c4d980196 104 }
samux 6:126c4d980196 105
samux 6:126c4d980196 106
samux 2:27a7e7f8d399 107 void USBMSD::reset()
samux 2:27a7e7f8d399 108 {
samux 5:8afbc15d6892 109 stage = READ_CBW;
samux 2:27a7e7f8d399 110 }
samux 2:27a7e7f8d399 111
samux 2:27a7e7f8d399 112 uint8_t * USBMSD::getMaxLUN()
samux 2:27a7e7f8d399 113 {
samux 2:27a7e7f8d399 114 static uint8_t LUN[] = {0};
samux 2:27a7e7f8d399 115 return LUN;
samux 2:27a7e7f8d399 116 }
samux 2:27a7e7f8d399 117
samux 2:27a7e7f8d399 118 // Called in ISR context called when a data is received
samux 2:27a7e7f8d399 119 bool USBMSD::EP2_OUT_callback() {
samux 2:27a7e7f8d399 120 uint16_t size = 0;
samux 2:27a7e7f8d399 121 uint8_t buf[MAX_PACKET_SIZE_EPBULK];
samux 2:27a7e7f8d399 122 read(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK);
samux 2:27a7e7f8d399 123 switch (stage) {
samux 5:8afbc15d6892 124 // the device has to decode the CBW received
samux 5:8afbc15d6892 125 case READ_CBW:
samux 2:27a7e7f8d399 126 CBWDecode(buf, size);
samux 2:27a7e7f8d399 127 break;
samux 5:8afbc15d6892 128
samux 5:8afbc15d6892 129 // the device has to receive data from the host
samux 5:8afbc15d6892 130 case PROCESS_CBW:
samux 2:27a7e7f8d399 131 switch (cbw.CB[0]) {
samux 2:27a7e7f8d399 132 case WRITE10:
samux 2:27a7e7f8d399 133 case WRITE12:
samux 2:27a7e7f8d399 134 memoryWrite(buf, size);
samux 2:27a7e7f8d399 135 break;
samux 2:27a7e7f8d399 136 case VERIFY10:
samux 2:27a7e7f8d399 137 memoryVerify(buf, size);
samux 2:27a7e7f8d399 138 break;
samux 2:27a7e7f8d399 139 }
samux 2:27a7e7f8d399 140 break;
samux 5:8afbc15d6892 141
samux 5:8afbc15d6892 142 // an error has occured: stall endpoint and send CSW
samux 2:27a7e7f8d399 143 default:
samux 5:8afbc15d6892 144 stallEndpoint(EPBULK_OUT);
samux 5:8afbc15d6892 145 csw.Status = CSW_ERROR;
samux 2:27a7e7f8d399 146 sendCSW();
samux 2:27a7e7f8d399 147 break;
samux 2:27a7e7f8d399 148 }
samux 2:27a7e7f8d399 149
samux 2:27a7e7f8d399 150 //reactivate readings on the OUT bulk endpoint
samux 2:27a7e7f8d399 151 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 2:27a7e7f8d399 152 return true;
samux 2:27a7e7f8d399 153 }
samux 2:27a7e7f8d399 154
samux 2:27a7e7f8d399 155 // Called in ISR context when a data has been transferred
samux 2:27a7e7f8d399 156 bool USBMSD::EP2_IN_callback() {
samux 2:27a7e7f8d399 157 switch (stage) {
samux 5:8afbc15d6892 158
samux 5:8afbc15d6892 159 // the device has to send data to the host
samux 5:8afbc15d6892 160 case PROCESS_CBW:
samux 2:27a7e7f8d399 161 switch (cbw.CB[0]) {
samux 2:27a7e7f8d399 162 case READ10:
samux 2:27a7e7f8d399 163 case READ12:
samux 2:27a7e7f8d399 164 memoryRead();
samux 2:27a7e7f8d399 165 break;
samux 2:27a7e7f8d399 166 }
samux 2:27a7e7f8d399 167 break;
samux 5:8afbc15d6892 168
samux 5:8afbc15d6892 169 //the device has to send a CSW
samux 5:8afbc15d6892 170 case SEND_CSW:
samux 2:27a7e7f8d399 171 sendCSW();
samux 2:27a7e7f8d399 172 break;
samux 5:8afbc15d6892 173
samux 5:8afbc15d6892 174 // an error has occured
samux 5:8afbc15d6892 175 case ERROR:
samux 2:27a7e7f8d399 176 stallEndpoint(EPBULK_IN);
samux 2:27a7e7f8d399 177 sendCSW();
samux 2:27a7e7f8d399 178 break;
samux 5:8afbc15d6892 179
samux 5:8afbc15d6892 180 // the host has received the CSW -> we wait a CBW
samux 5:8afbc15d6892 181 case WAIT_CSW:
samux 5:8afbc15d6892 182 stage = READ_CBW;
samux 2:27a7e7f8d399 183 break;
samux 2:27a7e7f8d399 184 }
samux 2:27a7e7f8d399 185 return true;
samux 2:27a7e7f8d399 186 }
samux 2:27a7e7f8d399 187
samux 2:27a7e7f8d399 188
samux 2:27a7e7f8d399 189 void USBMSD::memoryWrite (uint8_t * buf, uint16_t size) {
samux 2:27a7e7f8d399 190
samux 2:27a7e7f8d399 191 if ((addr + size) > MemorySize) {
samux 2:27a7e7f8d399 192 size = MemorySize - addr;
samux 5:8afbc15d6892 193 stage = ERROR;
samux 2:27a7e7f8d399 194 stallEndpoint(EPBULK_OUT);
samux 2:27a7e7f8d399 195 }
samux 2:27a7e7f8d399 196
samux 2:27a7e7f8d399 197 // we fill an array in RAM of 1 block before writing it in memory
samux 2:27a7e7f8d399 198 for (int i = 0; i < size; i++)
samux 2:27a7e7f8d399 199 page[addr%BlockSize + i] = buf[i];
samux 2:27a7e7f8d399 200
samux 2:27a7e7f8d399 201 // if the array is filled, write it in memory
samux 2:27a7e7f8d399 202 if (!((addr + size)%BlockSize))
samux 3:0ffb2eee9e06 203 blockWrite(page, addr/BlockSize);
samux 2:27a7e7f8d399 204
samux 2:27a7e7f8d399 205 addr += size;
samux 2:27a7e7f8d399 206 length -= size;
samux 2:27a7e7f8d399 207 csw.DataResidue -= size;
samux 2:27a7e7f8d399 208
samux 5:8afbc15d6892 209 if ((!length) || (stage != PROCESS_CBW)) {
samux 5:8afbc15d6892 210 csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED;
samux 2:27a7e7f8d399 211 sendCSW();
samux 2:27a7e7f8d399 212 }
samux 2:27a7e7f8d399 213 }
samux 2:27a7e7f8d399 214
samux 2:27a7e7f8d399 215 void USBMSD::memoryVerify (uint8_t * buf, uint16_t size) {
samux 2:27a7e7f8d399 216 uint32_t n;
samux 2:27a7e7f8d399 217
samux 2:27a7e7f8d399 218 if ((addr + size) > MemorySize) {
samux 2:27a7e7f8d399 219 size = MemorySize - addr;
samux 5:8afbc15d6892 220 stage = ERROR;
samux 2:27a7e7f8d399 221 stallEndpoint(EPBULK_OUT);
samux 2:27a7e7f8d399 222 }
samux 2:27a7e7f8d399 223
samux 2:27a7e7f8d399 224 // beginning of a new block -> load a whole block in RAM
samux 2:27a7e7f8d399 225 if (!(addr%BlockSize))
samux 3:0ffb2eee9e06 226 blockRead(page, addr/BlockSize);
samux 2:27a7e7f8d399 227
samux 2:27a7e7f8d399 228 // info are in RAM -> no need to re-read memory
samux 2:27a7e7f8d399 229 for (n = 0; n < size; n++) {
samux 2:27a7e7f8d399 230 if (page[addr%BlockSize + n] != buf[n]) {
samux 2:27a7e7f8d399 231 memOK = false;
samux 2:27a7e7f8d399 232 break;
samux 2:27a7e7f8d399 233 }
samux 2:27a7e7f8d399 234 }
samux 2:27a7e7f8d399 235
samux 2:27a7e7f8d399 236 addr += size;
samux 2:27a7e7f8d399 237 length -= size;
samux 2:27a7e7f8d399 238 csw.DataResidue -= size;
samux 2:27a7e7f8d399 239
samux 5:8afbc15d6892 240 if ( !length || (stage != PROCESS_CBW)) {
samux 5:8afbc15d6892 241 csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED;
samux 2:27a7e7f8d399 242 sendCSW();
samux 2:27a7e7f8d399 243 }
samux 2:27a7e7f8d399 244 }
samux 2:27a7e7f8d399 245
samux 2:27a7e7f8d399 246
samux 2:27a7e7f8d399 247 bool USBMSD::inquiryRequest (void) {
samux 2:27a7e7f8d399 248 uint8_t inquiry[] = { 0x00, 0x80, 0x00, 0x01,
samux 2:27a7e7f8d399 249 36 - 4, 0x80, 0x00, 0x00,
samux 2:27a7e7f8d399 250 'M', 'B', 'E', 'D', '.', 'O', 'R', 'G',
samux 2:27a7e7f8d399 251 'M', 'B', 'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 'K', ' ', ' ', ' ',
samux 2:27a7e7f8d399 252 '1', '.', '0', ' ',
samux 2:27a7e7f8d399 253 };
samux 2:27a7e7f8d399 254 if (!write(inquiry, sizeof(inquiry))) {
samux 2:27a7e7f8d399 255 return false;
samux 2:27a7e7f8d399 256 }
samux 2:27a7e7f8d399 257 return true;
samux 2:27a7e7f8d399 258 }
samux 2:27a7e7f8d399 259
samux 2:27a7e7f8d399 260
samux 2:27a7e7f8d399 261 bool USBMSD::readFormatCapacity() {
samux 2:27a7e7f8d399 262 uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08,
samux 2:27a7e7f8d399 263 (BlockCount >> 24) & 0xff,
samux 2:27a7e7f8d399 264 (BlockCount >> 16) & 0xff,
samux 2:27a7e7f8d399 265 (BlockCount >> 8) & 0xff,
samux 2:27a7e7f8d399 266 (BlockCount >> 0) & 0xff,
samux 2:27a7e7f8d399 267
samux 2:27a7e7f8d399 268 0x02,
samux 2:27a7e7f8d399 269 (BlockSize >> 16) & 0xff,
samux 2:27a7e7f8d399 270 (BlockSize >> 8) & 0xff,
samux 2:27a7e7f8d399 271 (BlockSize >> 0) & 0xff,
samux 2:27a7e7f8d399 272 };
samux 2:27a7e7f8d399 273 if (!write(capacity, sizeof(capacity))) {
samux 2:27a7e7f8d399 274 return false;
samux 2:27a7e7f8d399 275 }
samux 2:27a7e7f8d399 276 return true;
samux 2:27a7e7f8d399 277 }
samux 2:27a7e7f8d399 278
samux 2:27a7e7f8d399 279
samux 2:27a7e7f8d399 280 bool USBMSD::readCapacity (void) {
samux 2:27a7e7f8d399 281 uint8_t capacity[] = {
samux 2:27a7e7f8d399 282 ((BlockCount - 1) >> 24) & 0xff,
samux 2:27a7e7f8d399 283 ((BlockCount - 1) >> 16) & 0xff,
samux 2:27a7e7f8d399 284 ((BlockCount - 1) >> 8) & 0xff,
samux 2:27a7e7f8d399 285 ((BlockCount - 1) >> 0) & 0xff,
samux 2:27a7e7f8d399 286
samux 2:27a7e7f8d399 287 (BlockSize >> 24) & 0xff,
samux 2:27a7e7f8d399 288 (BlockSize >> 16) & 0xff,
samux 2:27a7e7f8d399 289 (BlockSize >> 8) & 0xff,
samux 2:27a7e7f8d399 290 (BlockSize >> 0) & 0xff,
samux 2:27a7e7f8d399 291 };
samux 2:27a7e7f8d399 292 if (!write(capacity, sizeof(capacity))) {
samux 2:27a7e7f8d399 293 return false;
samux 2:27a7e7f8d399 294 }
samux 2:27a7e7f8d399 295 return true;
samux 2:27a7e7f8d399 296 }
samux 2:27a7e7f8d399 297
samux 2:27a7e7f8d399 298 bool USBMSD::write (uint8_t * buf, uint16_t size) {
samux 2:27a7e7f8d399 299
samux 2:27a7e7f8d399 300 if (size >= cbw.DataLength) {
samux 2:27a7e7f8d399 301 size = cbw.DataLength;
samux 2:27a7e7f8d399 302 }
samux 5:8afbc15d6892 303 stage = SEND_CSW;
samux 2:27a7e7f8d399 304
samux 2:27a7e7f8d399 305 if (!writeNB(EPBULK_IN, buf, size, MAX_PACKET_SIZE_EPBULK)) {
samux 2:27a7e7f8d399 306 return false;
samux 2:27a7e7f8d399 307 }
samux 2:27a7e7f8d399 308
samux 2:27a7e7f8d399 309 csw.DataResidue -= size;
samux 5:8afbc15d6892 310 csw.Status = CSW_PASSED;
samux 2:27a7e7f8d399 311 return true;
samux 2:27a7e7f8d399 312 }
samux 2:27a7e7f8d399 313
samux 2:27a7e7f8d399 314
samux 2:27a7e7f8d399 315 bool USBMSD::modeSense6 (void) {
samux 2:27a7e7f8d399 316 uint8_t sense6[] = { 0x03, 0x00, 0x00, 0x00 };
samux 2:27a7e7f8d399 317 if (!write(sense6, sizeof(sense6))) {
samux 2:27a7e7f8d399 318 return false;
samux 2:27a7e7f8d399 319 }
samux 2:27a7e7f8d399 320 return true;
samux 2:27a7e7f8d399 321 }
samux 2:27a7e7f8d399 322
samux 2:27a7e7f8d399 323 void USBMSD::sendCSW() {
samux 2:27a7e7f8d399 324 csw.Signature = CSW_Signature;
samux 2:27a7e7f8d399 325 writeNB(EPBULK_IN, (uint8_t *)&csw, sizeof(CSW), MAX_PACKET_SIZE_EPBULK);
samux 5:8afbc15d6892 326 stage = WAIT_CSW;
samux 2:27a7e7f8d399 327 }
samux 2:27a7e7f8d399 328
samux 2:27a7e7f8d399 329 bool USBMSD::requestSense (void) {
samux 2:27a7e7f8d399 330 uint8_t request_sense[] = {
samux 2:27a7e7f8d399 331 0x70, // Response Code
samux 2:27a7e7f8d399 332 0x00,
samux 2:27a7e7f8d399 333 0x05, // Sense Key: illegal request
samux 2:27a7e7f8d399 334 0x00,
samux 2:27a7e7f8d399 335 0x00,
samux 2:27a7e7f8d399 336 0x00,
samux 2:27a7e7f8d399 337 0x00,
samux 2:27a7e7f8d399 338 0x0A, // Additional Length
samux 2:27a7e7f8d399 339 0x00,
samux 2:27a7e7f8d399 340 0x00,
samux 2:27a7e7f8d399 341 0x00,
samux 2:27a7e7f8d399 342 0x00,
samux 2:27a7e7f8d399 343 0x30, // ASC
samux 2:27a7e7f8d399 344 0x01, // ASCQ
samux 2:27a7e7f8d399 345 0x00,
samux 2:27a7e7f8d399 346 0x00,
samux 2:27a7e7f8d399 347 0x00,
samux 2:27a7e7f8d399 348 0x00,
samux 2:27a7e7f8d399 349 };
samux 2:27a7e7f8d399 350
samux 2:27a7e7f8d399 351 if (!write(request_sense, sizeof(request_sense))) {
samux 2:27a7e7f8d399 352 return false;
samux 2:27a7e7f8d399 353 }
samux 2:27a7e7f8d399 354
samux 2:27a7e7f8d399 355 return true;
samux 2:27a7e7f8d399 356 }
samux 2:27a7e7f8d399 357
samux 2:27a7e7f8d399 358 void USBMSD::fail() {
samux 5:8afbc15d6892 359 csw.Status = CSW_FAILED;
samux 2:27a7e7f8d399 360 sendCSW();
samux 2:27a7e7f8d399 361 }
samux 2:27a7e7f8d399 362
samux 6:126c4d980196 363 DigitalOut l1(LED1);
samux 2:27a7e7f8d399 364 void USBMSD::CBWDecode(uint8_t * buf, uint16_t size) {
samux 2:27a7e7f8d399 365 if (size == sizeof(cbw)) {
samux 2:27a7e7f8d399 366 memcpy((uint8_t *)&cbw, buf, size);
samux 2:27a7e7f8d399 367 if (cbw.Signature == CBW_Signature) {
samux 2:27a7e7f8d399 368 csw.Tag = cbw.Tag;
samux 2:27a7e7f8d399 369 csw.DataResidue = cbw.DataLength;
samux 2:27a7e7f8d399 370 if ((cbw.CBLength < 1) || (cbw.CBLength > 16) ) {
samux 2:27a7e7f8d399 371 fail();
samux 2:27a7e7f8d399 372 } else {
samux 2:27a7e7f8d399 373 switch (cbw.CB[0]) {
samux 2:27a7e7f8d399 374 case TEST_UNIT_READY:
samux 2:27a7e7f8d399 375 testUnitReady();
samux 2:27a7e7f8d399 376 break;
samux 2:27a7e7f8d399 377 case REQUEST_SENSE:
samux 2:27a7e7f8d399 378 requestSense();
samux 2:27a7e7f8d399 379 break;
samux 2:27a7e7f8d399 380 case INQUIRY:
samux 2:27a7e7f8d399 381 inquiryRequest();
samux 2:27a7e7f8d399 382 break;
samux 2:27a7e7f8d399 383 case MODE_SENSE6:
samux 2:27a7e7f8d399 384 modeSense6();
samux 2:27a7e7f8d399 385 break;
samux 2:27a7e7f8d399 386 case READ_FORMAT_CAPACITIES:
samux 2:27a7e7f8d399 387 readFormatCapacity();
samux 2:27a7e7f8d399 388 break;
samux 2:27a7e7f8d399 389 case READ_CAPACITY:
samux 2:27a7e7f8d399 390 readCapacity();
samux 2:27a7e7f8d399 391 break;
samux 2:27a7e7f8d399 392 case READ10:
samux 2:27a7e7f8d399 393 case READ12:
samux 2:27a7e7f8d399 394 if (infoTransfer()) {
samux 2:27a7e7f8d399 395 if ((cbw.Flags & 0x80)) {
samux 5:8afbc15d6892 396 stage = PROCESS_CBW;
samux 2:27a7e7f8d399 397 memoryRead();
samux 2:27a7e7f8d399 398 } else {
samux 2:27a7e7f8d399 399 stallEndpoint(EPBULK_OUT);
samux 5:8afbc15d6892 400 csw.Status = CSW_ERROR;
samux 2:27a7e7f8d399 401 sendCSW();
samux 2:27a7e7f8d399 402 }
samux 2:27a7e7f8d399 403 }
samux 2:27a7e7f8d399 404 break;
samux 2:27a7e7f8d399 405 case WRITE10:
samux 2:27a7e7f8d399 406 case WRITE12:
samux 2:27a7e7f8d399 407 if (infoTransfer()) {
samux 2:27a7e7f8d399 408 if (!(cbw.Flags & 0x80)) {
samux 5:8afbc15d6892 409 stage = PROCESS_CBW;
samux 2:27a7e7f8d399 410 } else {
samux 2:27a7e7f8d399 411 stallEndpoint(EPBULK_IN);
samux 5:8afbc15d6892 412 csw.Status = CSW_ERROR;
samux 2:27a7e7f8d399 413 sendCSW();
samux 2:27a7e7f8d399 414 }
samux 2:27a7e7f8d399 415 }
samux 2:27a7e7f8d399 416 break;
samux 2:27a7e7f8d399 417 case VERIFY10:
samux 2:27a7e7f8d399 418 if (!(cbw.CB[1] & 0x02)) {
samux 5:8afbc15d6892 419 csw.Status = CSW_PASSED;
samux 2:27a7e7f8d399 420 sendCSW();
samux 2:27a7e7f8d399 421 break;
samux 2:27a7e7f8d399 422 }
samux 2:27a7e7f8d399 423 if (infoTransfer()) {
samux 2:27a7e7f8d399 424 if (!(cbw.Flags & 0x80)) {
samux 5:8afbc15d6892 425 stage = PROCESS_CBW;
samux 2:27a7e7f8d399 426 memOK = true;
samux 2:27a7e7f8d399 427 } else {
samux 2:27a7e7f8d399 428 stallEndpoint(EPBULK_IN);
samux 5:8afbc15d6892 429 csw.Status = CSW_ERROR;
samux 2:27a7e7f8d399 430 sendCSW();
samux 2:27a7e7f8d399 431 }
samux 2:27a7e7f8d399 432 }
samux 2:27a7e7f8d399 433 break;
samux 2:27a7e7f8d399 434 default:
samux 2:27a7e7f8d399 435 fail();
samux 2:27a7e7f8d399 436 break;
samux 2:27a7e7f8d399 437 }
samux 2:27a7e7f8d399 438 }
samux 2:27a7e7f8d399 439 }
samux 2:27a7e7f8d399 440 }
samux 2:27a7e7f8d399 441 }
samux 2:27a7e7f8d399 442
samux 2:27a7e7f8d399 443 void USBMSD::testUnitReady (void) {
samux 2:27a7e7f8d399 444
samux 2:27a7e7f8d399 445 if (cbw.DataLength != 0) {
samux 2:27a7e7f8d399 446 if ((cbw.Flags & 0x80) != 0) {
samux 2:27a7e7f8d399 447 stallEndpoint(EPBULK_IN);
samux 2:27a7e7f8d399 448 } else {
samux 2:27a7e7f8d399 449 stallEndpoint(EPBULK_OUT);
samux 2:27a7e7f8d399 450 }
samux 2:27a7e7f8d399 451 }
samux 2:27a7e7f8d399 452
samux 5:8afbc15d6892 453 csw.Status = CSW_PASSED;
samux 2:27a7e7f8d399 454 sendCSW();
samux 2:27a7e7f8d399 455 }
samux 2:27a7e7f8d399 456
samux 2:27a7e7f8d399 457
samux 2:27a7e7f8d399 458 void USBMSD::memoryRead (void) {
samux 2:27a7e7f8d399 459 uint32_t n;
samux 2:27a7e7f8d399 460
samux 2:27a7e7f8d399 461 n = (length > MAX_PACKET) ? MAX_PACKET : length;
samux 2:27a7e7f8d399 462
samux 2:27a7e7f8d399 463 if ((addr + n) > MemorySize) {
samux 2:27a7e7f8d399 464 n = MemorySize - addr;
samux 5:8afbc15d6892 465 stage = ERROR;
samux 2:27a7e7f8d399 466 }
samux 2:27a7e7f8d399 467
samux 5:8afbc15d6892 468 // we read an entire block
samux 2:27a7e7f8d399 469 if (!(addr%BlockSize))
samux 3:0ffb2eee9e06 470 blockRead(page, addr/BlockSize);
samux 2:27a7e7f8d399 471
samux 5:8afbc15d6892 472 // write data which are in RAM
samux 2:27a7e7f8d399 473 writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK);
samux 2:27a7e7f8d399 474
samux 2:27a7e7f8d399 475 addr += n;
samux 2:27a7e7f8d399 476 length -= n;
samux 2:27a7e7f8d399 477
samux 2:27a7e7f8d399 478 csw.DataResidue -= n;
samux 2:27a7e7f8d399 479
samux 6:126c4d980196 480 if ( !length || (stage != PROCESS_CBW)) {
samux 6:126c4d980196 481 csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;
samux 6:126c4d980196 482 sendCSW();
samux 2:27a7e7f8d399 483 }
samux 2:27a7e7f8d399 484 }
samux 2:27a7e7f8d399 485
samux 2:27a7e7f8d399 486
samux 2:27a7e7f8d399 487 bool USBMSD::infoTransfer (void) {
samux 2:27a7e7f8d399 488 uint32_t n;
samux 2:27a7e7f8d399 489
samux 2:27a7e7f8d399 490 // Logical Block Address of First Block
samux 2:27a7e7f8d399 491 n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) | (cbw.CB[5] << 0);
samux 2:27a7e7f8d399 492
samux 2:27a7e7f8d399 493 addr = n * BlockSize;
samux 2:27a7e7f8d399 494
samux 2:27a7e7f8d399 495 // Number of Blocks to transfer
samux 2:27a7e7f8d399 496 switch (cbw.CB[0]) {
samux 2:27a7e7f8d399 497 case READ10:
samux 2:27a7e7f8d399 498 case WRITE10:
samux 2:27a7e7f8d399 499 case VERIFY10:
samux 2:27a7e7f8d399 500 n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0);
samux 2:27a7e7f8d399 501 break;
samux 2:27a7e7f8d399 502
samux 2:27a7e7f8d399 503 case READ12:
samux 2:27a7e7f8d399 504 case WRITE12:
samux 2:27a7e7f8d399 505 n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) | (cbw.CB[8] << 8) | (cbw.CB[9] << 0);
samux 2:27a7e7f8d399 506 break;
samux 2:27a7e7f8d399 507 }
samux 2:27a7e7f8d399 508
samux 2:27a7e7f8d399 509 length = n * BlockSize;
samux 2:27a7e7f8d399 510
samux 2:27a7e7f8d399 511 if (!cbw.DataLength) { // host requests no data
samux 5:8afbc15d6892 512 csw.Status = CSW_FAILED;
samux 2:27a7e7f8d399 513 sendCSW();
samux 2:27a7e7f8d399 514 return false;
samux 2:27a7e7f8d399 515 }
samux 2:27a7e7f8d399 516
samux 2:27a7e7f8d399 517 if (cbw.DataLength != length) {
samux 2:27a7e7f8d399 518 if ((cbw.Flags & 0x80) != 0) {
samux 2:27a7e7f8d399 519 stallEndpoint(EPBULK_IN);
samux 2:27a7e7f8d399 520 } else {
samux 2:27a7e7f8d399 521 stallEndpoint(EPBULK_OUT);
samux 2:27a7e7f8d399 522 }
samux 2:27a7e7f8d399 523
samux 5:8afbc15d6892 524 csw.Status = CSW_FAILED;
samux 2:27a7e7f8d399 525 sendCSW();
samux 2:27a7e7f8d399 526 return false;
samux 2:27a7e7f8d399 527 }
samux 2:27a7e7f8d399 528
samux 2:27a7e7f8d399 529 return true;
samux 2:27a7e7f8d399 530 }
samux 2:27a7e7f8d399 531
samux 2:27a7e7f8d399 532
samux 2:27a7e7f8d399 533
samux 2:27a7e7f8d399 534
samux 2:27a7e7f8d399 535
samux 2:27a7e7f8d399 536 // Called in ISR context
samux 2:27a7e7f8d399 537 // Set configuration. Return false if the
samux 2:27a7e7f8d399 538 // configuration is not supported.
samux 2:27a7e7f8d399 539 bool USBMSD::USBCallback_setConfiguration(uint8_t configuration) {
samux 2:27a7e7f8d399 540 if (configuration != DEFAULT_CONFIGURATION) {
samux 2:27a7e7f8d399 541 return false;
samux 2:27a7e7f8d399 542 }
samux 2:27a7e7f8d399 543
samux 2:27a7e7f8d399 544 // Configure endpoints > 0
samux 2:27a7e7f8d399 545 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
samux 2:27a7e7f8d399 546 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 2:27a7e7f8d399 547
samux 2:27a7e7f8d399 548 //activate readings
samux 2:27a7e7f8d399 549 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 2:27a7e7f8d399 550 return true;
samux 2:27a7e7f8d399 551 }
samux 2:27a7e7f8d399 552
samux 2:27a7e7f8d399 553
samux 2:27a7e7f8d399 554 uint8_t * USBMSD::stringIinterfaceDesc() {
samux 2:27a7e7f8d399 555 static uint8_t stringIinterfaceDescriptor[] = {
samux 2:27a7e7f8d399 556 0x08, //bLength
samux 2:27a7e7f8d399 557 STRING_DESCRIPTOR, //bDescriptorType 0x03
samux 2:27a7e7f8d399 558 'M',0,'S',0,'D',0 //bString iInterface - MSD
samux 2:27a7e7f8d399 559 };
samux 2:27a7e7f8d399 560 return stringIinterfaceDescriptor;
samux 2:27a7e7f8d399 561 }
samux 2:27a7e7f8d399 562
samux 2:27a7e7f8d399 563 uint8_t * USBMSD::stringIproductDesc() {
samux 2:27a7e7f8d399 564 static uint8_t stringIproductDescriptor[] = {
samux 6:126c4d980196 565 0x12, //bLength
samux 6:126c4d980196 566 STRING_DESCRIPTOR, //bDescriptorType 0x03
samux 2:27a7e7f8d399 567 'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio
samux 2:27a7e7f8d399 568 };
samux 2:27a7e7f8d399 569 return stringIproductDescriptor;
samux 2:27a7e7f8d399 570 }
samux 2:27a7e7f8d399 571
samux 2:27a7e7f8d399 572
samux 2:27a7e7f8d399 573 uint8_t * USBMSD::configurationDesc() {
samux 2:27a7e7f8d399 574 static uint8_t configDescriptor[] = {
samux 2:27a7e7f8d399 575
samux 2:27a7e7f8d399 576 // Configuration 1
samux 2:27a7e7f8d399 577 9, // bLength
samux 2:27a7e7f8d399 578 2, // bDescriptorType
samux 2:27a7e7f8d399 579 LSB(9 + 9 + 7 + 7), // wTotalLength
samux 2:27a7e7f8d399 580 MSB(9 + 9 + 7 + 7),
samux 2:27a7e7f8d399 581 0x01, // bNumInterfaces
samux 2:27a7e7f8d399 582 0x01, // bConfigurationValue: 0x01 is used to select this configuration
samux 2:27a7e7f8d399 583 0x00, // iConfiguration: no string to describe this configuration
samux 2:27a7e7f8d399 584 0xC0, // bmAttributes
samux 2:27a7e7f8d399 585 100, // bMaxPower, device power consumption is 100 mA
samux 2:27a7e7f8d399 586
samux 2:27a7e7f8d399 587 // Interface 0, Alternate Setting 0, MSC Class
samux 2:27a7e7f8d399 588 9, // bLength
samux 2:27a7e7f8d399 589 4, // bDescriptorType
samux 2:27a7e7f8d399 590 0x00, // bInterfaceNumber
samux 2:27a7e7f8d399 591 0x00, // bAlternateSetting
samux 2:27a7e7f8d399 592 0x02, // bNumEndpoints
samux 2:27a7e7f8d399 593 0x08, // bInterfaceClass
samux 2:27a7e7f8d399 594 0x06, // bInterfaceSubClass
samux 2:27a7e7f8d399 595 0x50, // bInterfaceProtocol
samux 2:27a7e7f8d399 596 0x04, // iInterface
samux 2:27a7e7f8d399 597
samux 2:27a7e7f8d399 598 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
samux 2:27a7e7f8d399 599 7, // bLength
samux 2:27a7e7f8d399 600 5, // bDescriptorType
samux 2:27a7e7f8d399 601 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
samux 2:27a7e7f8d399 602 0x02, // bmAttributes (0x02=bulk)
samux 2:27a7e7f8d399 603 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
samux 2:27a7e7f8d399 604 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
samux 2:27a7e7f8d399 605 0, // bInterval
samux 2:27a7e7f8d399 606
samux 2:27a7e7f8d399 607 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
samux 2:27a7e7f8d399 608 7, // bLength
samux 2:27a7e7f8d399 609 5, // bDescriptorType
samux 2:27a7e7f8d399 610 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
samux 2:27a7e7f8d399 611 0x02, // bmAttributes (0x02=bulk)
samux 2:27a7e7f8d399 612 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
samux 2:27a7e7f8d399 613 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
samux 2:27a7e7f8d399 614 0 // bInterval
samux 2:27a7e7f8d399 615 };
samux 2:27a7e7f8d399 616 return configDescriptor;
samux 2:27a7e7f8d399 617 }