remove casting to fix build error

Dependents:   RTC_Demo

Committer:
davidanasco
Date:
Fri Jul 16 08:28:53 2021 +0000
Revision:
70:2f8e3f5f75cf
Parent:
51:deafa44182d9
removed casting to fix build error

Who changed what in which revision?

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