Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

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