USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Dec 11 15:07:59 2011 +0000
Revision:
17:364ef42e502d
OK: will write doc

Who changed what in which revision?

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