USBDevice with MAX32620HSP platform support

Fork of USBDevice by mbed official

Committer:
bogdanm
Date:
Thu Sep 12 14:45:27 2013 +0100
Revision:
14:d495202c90f4
Parent:
11:eeb3cbbaa996
Child:
18:78bdbce94509
Synchronized with git revision b9d52bda50a692c05a4587bcc8d3219997444f58

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