USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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