nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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