USBAudio example using a microphone

Dependencies:   USBDevice mbed

Committer:
samux
Date:
Fri Dec 16 12:31:41 2011 +0000
Revision:
0:539ec61e1fbb
works with m0 and m3 (sinus) but the code is different to have the same result...

Who changed what in which revision?

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