USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Dec 11 15:56:08 2011 +0000
Revision:
20:50bf1cd2e2fe
Parent:
19:148a0b8d23bc
Child:
21:bbe291251dde
big cleaning up

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