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