USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Nov 13 12:30:43 2011 +0000
Revision:
5:8afbc15d6892
Parent:
4:980e6470dcce
Child:
6:126c4d980196
rename things and introduce an ERROR state

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