max32630fthr quad spi , unexpected spi behavior

Committer:
boonshen
Date:
Tue Mar 13 21:12:00 2018 +0000
Revision:
0:a35c40f49345
MAX32630FTHR QuadSPI test

Who changed what in which revision?

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