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