USBDevice with MAX32620HSP platform support

Fork of USBDevice by mbed official

Committer:
samux
Date:
Sun Oct 14 12:38:56 2012 +0000
Revision:
3:6d85e04fb59f
Parent:
1:80ab0d068708
Child:
5:d27e4c226965
move EnableIRQ to connect() - add MEDIA_REMOVAL handling

Who changed what in which revision?

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