Pinscape Controller version 1 fork. This is a fork to allow for ongoing bug fixes to the original controller version, from before the major changes for the expansion board project.

Dependencies:   FastIO FastPWM SimpleDMA mbed

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Mon Feb 15 23:03:55 2016 +0000
Revision:
68:edfecf67a931
Parent:
52:63f0a9b45f0c
Finalize USB library updates to fix compatibility problems introduced in USBHAL_KL25Z overhaul.

Who changed what in which revision?

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