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