actualizacion no funciona
Dependencies: FATFileSystem mbed-rtos
Dependents: Proyect_Patric_electronic_door_MSC_Ok_ESP
Fork of USBHost by
USBHostMSD/USBHostMSD.cpp@4:b320d68e98e7, 2013-03-12 (annotated)
- Committer:
- samux
- Date:
- Tue Mar 12 17:23:37 2013 +0000
- Revision:
- 4:b320d68e98e7
- Parent:
- 0:a554658735bf
- Child:
- 5:e48791a1ef18
bug fixes - support composite device
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:a554658735bf | 1 | /* Copyright (c) 2010-2012 mbed.org, MIT License |
mbed_official | 0:a554658735bf | 2 | * |
mbed_official | 0:a554658735bf | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 0:a554658735bf | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mbed_official | 0:a554658735bf | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mbed_official | 0:a554658735bf | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mbed_official | 0:a554658735bf | 7 | * Software is furnished to do so, subject to the following conditions: |
mbed_official | 0:a554658735bf | 8 | * |
mbed_official | 0:a554658735bf | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 0:a554658735bf | 10 | * substantial portions of the Software. |
mbed_official | 0:a554658735bf | 11 | * |
mbed_official | 0:a554658735bf | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 0:a554658735bf | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 0:a554658735bf | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 0:a554658735bf | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:a554658735bf | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 0:a554658735bf | 17 | */ |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #include "USBHostMSD.h" |
mbed_official | 0:a554658735bf | 20 | |
mbed_official | 0:a554658735bf | 21 | #if USBHOST_MSD |
mbed_official | 0:a554658735bf | 22 | |
mbed_official | 0:a554658735bf | 23 | #include "dbg.h" |
mbed_official | 0:a554658735bf | 24 | |
mbed_official | 0:a554658735bf | 25 | #define CBW_SIGNATURE 0x43425355 |
mbed_official | 0:a554658735bf | 26 | #define CSW_SIGNATURE 0x53425355 |
mbed_official | 0:a554658735bf | 27 | |
mbed_official | 0:a554658735bf | 28 | #define DEVICE_TO_HOST 0x80 |
mbed_official | 0:a554658735bf | 29 | #define HOST_TO_DEVICE 0x00 |
mbed_official | 0:a554658735bf | 30 | |
samux | 4:b320d68e98e7 | 31 | #define GET_MAX_LUN (0xFE) |
samux | 4:b320d68e98e7 | 32 | #define BO_MASS_STORAGE_RESET (0xFF) |
mbed_official | 0:a554658735bf | 33 | |
mbed_official | 0:a554658735bf | 34 | USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir) |
mbed_official | 0:a554658735bf | 35 | { |
mbed_official | 0:a554658735bf | 36 | host = USBHost::getHostInst(); |
mbed_official | 0:a554658735bf | 37 | init(); |
mbed_official | 0:a554658735bf | 38 | } |
mbed_official | 0:a554658735bf | 39 | |
mbed_official | 0:a554658735bf | 40 | void USBHostMSD::init() { |
mbed_official | 0:a554658735bf | 41 | dev_connected = false; |
mbed_official | 0:a554658735bf | 42 | dev = NULL; |
mbed_official | 0:a554658735bf | 43 | bulk_in = NULL; |
mbed_official | 0:a554658735bf | 44 | bulk_out = NULL; |
mbed_official | 0:a554658735bf | 45 | dev_connected = false; |
mbed_official | 0:a554658735bf | 46 | blockSize = 0; |
mbed_official | 0:a554658735bf | 47 | blockCount = 0; |
mbed_official | 0:a554658735bf | 48 | msd_intf = -1; |
mbed_official | 0:a554658735bf | 49 | msd_device_found = false; |
mbed_official | 0:a554658735bf | 50 | disk_init = false; |
mbed_official | 0:a554658735bf | 51 | dev_connected = false; |
samux | 4:b320d68e98e7 | 52 | nb_ep = 0; |
mbed_official | 0:a554658735bf | 53 | } |
mbed_official | 0:a554658735bf | 54 | |
mbed_official | 0:a554658735bf | 55 | |
mbed_official | 0:a554658735bf | 56 | bool USBHostMSD::connected() |
mbed_official | 0:a554658735bf | 57 | { |
mbed_official | 0:a554658735bf | 58 | return dev_connected; |
mbed_official | 0:a554658735bf | 59 | } |
mbed_official | 0:a554658735bf | 60 | |
mbed_official | 0:a554658735bf | 61 | bool USBHostMSD::connect() |
mbed_official | 0:a554658735bf | 62 | { |
mbed_official | 0:a554658735bf | 63 | U8 i; |
mbed_official | 0:a554658735bf | 64 | |
mbed_official | 0:a554658735bf | 65 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 66 | return true; |
mbed_official | 0:a554658735bf | 67 | } |
mbed_official | 0:a554658735bf | 68 | |
mbed_official | 0:a554658735bf | 69 | for (i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 70 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 0:a554658735bf | 71 | |
samux | 4:b320d68e98e7 | 72 | USB_DBG("Trying to connect MSD device\r\n"); |
samux | 4:b320d68e98e7 | 73 | |
mbed_official | 0:a554658735bf | 74 | if(host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 75 | break; |
mbed_official | 0:a554658735bf | 76 | |
mbed_official | 0:a554658735bf | 77 | if (msd_device_found) { |
mbed_official | 0:a554658735bf | 78 | bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 79 | bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); |
mbed_official | 0:a554658735bf | 80 | |
mbed_official | 0:a554658735bf | 81 | if (!bulk_in || !bulk_out) |
samux | 4:b320d68e98e7 | 82 | continue; |
mbed_official | 0:a554658735bf | 83 | |
samux | 4:b320d68e98e7 | 84 | USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); |
samux | 4:b320d68e98e7 | 85 | dev->setName("MSD", msd_intf); |
mbed_official | 0:a554658735bf | 86 | host->registerDriver(dev, msd_intf, this, &USBHostMSD::init); |
mbed_official | 0:a554658735bf | 87 | |
mbed_official | 0:a554658735bf | 88 | dev_connected = true; |
mbed_official | 0:a554658735bf | 89 | return true; |
mbed_official | 0:a554658735bf | 90 | } |
mbed_official | 0:a554658735bf | 91 | } //if() |
mbed_official | 0:a554658735bf | 92 | } //for() |
samux | 4:b320d68e98e7 | 93 | init(); |
mbed_official | 0:a554658735bf | 94 | return false; |
mbed_official | 0:a554658735bf | 95 | } |
mbed_official | 0:a554658735bf | 96 | |
mbed_official | 0:a554658735bf | 97 | /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 98 | { |
mbed_official | 0:a554658735bf | 99 | // we don't check VID/PID for MSD driver |
mbed_official | 0:a554658735bf | 100 | } |
mbed_official | 0:a554658735bf | 101 | |
mbed_official | 0:a554658735bf | 102 | /*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed |
mbed_official | 0:a554658735bf | 103 | { |
mbed_official | 0:a554658735bf | 104 | if ((msd_intf == -1) && |
mbed_official | 0:a554658735bf | 105 | (intf_class == MSD_CLASS) && |
mbed_official | 0:a554658735bf | 106 | (intf_subclass == 0x06) && |
mbed_official | 0:a554658735bf | 107 | (intf_protocol == 0x50)) { |
mbed_official | 0:a554658735bf | 108 | msd_intf = intf_nb; |
mbed_official | 0:a554658735bf | 109 | return true; |
mbed_official | 0:a554658735bf | 110 | } |
mbed_official | 0:a554658735bf | 111 | return false; |
mbed_official | 0:a554658735bf | 112 | } |
mbed_official | 0:a554658735bf | 113 | |
mbed_official | 0:a554658735bf | 114 | /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
mbed_official | 0:a554658735bf | 115 | { |
mbed_official | 0:a554658735bf | 116 | if (intf_nb == msd_intf) { |
mbed_official | 0:a554658735bf | 117 | if (type == BULK_ENDPOINT) { |
samux | 4:b320d68e98e7 | 118 | nb_ep++; |
samux | 4:b320d68e98e7 | 119 | if (nb_ep == 2) |
samux | 4:b320d68e98e7 | 120 | msd_device_found = true; |
mbed_official | 0:a554658735bf | 121 | return true; |
mbed_official | 0:a554658735bf | 122 | } |
mbed_official | 0:a554658735bf | 123 | } |
mbed_official | 0:a554658735bf | 124 | return false; |
mbed_official | 0:a554658735bf | 125 | } |
mbed_official | 0:a554658735bf | 126 | |
mbed_official | 0:a554658735bf | 127 | |
mbed_official | 0:a554658735bf | 128 | int USBHostMSD::testUnitReady() { |
samux | 4:b320d68e98e7 | 129 | USB_DBG("Test unit ready"); |
mbed_official | 0:a554658735bf | 130 | return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); |
mbed_official | 0:a554658735bf | 131 | } |
mbed_official | 0:a554658735bf | 132 | |
samux | 4:b320d68e98e7 | 133 | |
mbed_official | 0:a554658735bf | 134 | int USBHostMSD::readCapacity() { |
samux | 4:b320d68e98e7 | 135 | USB_DBG("Read capacity"); |
mbed_official | 0:a554658735bf | 136 | uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; |
mbed_official | 0:a554658735bf | 137 | uint8_t result[8]; |
mbed_official | 0:a554658735bf | 138 | int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); |
mbed_official | 0:a554658735bf | 139 | if (status == 0) { |
mbed_official | 0:a554658735bf | 140 | blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; |
mbed_official | 0:a554658735bf | 141 | blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; |
samux | 4:b320d68e98e7 | 142 | USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); |
mbed_official | 0:a554658735bf | 143 | } |
mbed_official | 0:a554658735bf | 144 | return status; |
mbed_official | 0:a554658735bf | 145 | } |
mbed_official | 0:a554658735bf | 146 | |
mbed_official | 0:a554658735bf | 147 | |
mbed_official | 0:a554658735bf | 148 | int USBHostMSD::SCSIRequestSense() { |
samux | 4:b320d68e98e7 | 149 | USB_DBG("Request sense"); |
samux | 4:b320d68e98e7 | 150 | uint8_t cmd[6] = {0x03,0,0,0,18,0}; |
samux | 4:b320d68e98e7 | 151 | uint8_t result[18]; |
samux | 4:b320d68e98e7 | 152 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); |
mbed_official | 0:a554658735bf | 153 | return status; |
mbed_official | 0:a554658735bf | 154 | } |
mbed_official | 0:a554658735bf | 155 | |
mbed_official | 0:a554658735bf | 156 | |
mbed_official | 0:a554658735bf | 157 | int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) { |
samux | 4:b320d68e98e7 | 158 | USB_DBG("Inquiry"); |
mbed_official | 0:a554658735bf | 159 | uint8_t evpd = (page_code == 0) ? 0 : 1; |
mbed_official | 0:a554658735bf | 160 | uint8_t cmd[6] = {0x12, (lun << 5) | evpd, page_code, 0, 36, 0}; |
mbed_official | 0:a554658735bf | 161 | uint8_t result[36]; |
mbed_official | 0:a554658735bf | 162 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); |
mbed_official | 0:a554658735bf | 163 | if (status == 0) { |
samux | 4:b320d68e98e7 | 164 | char vid_pid[17]; |
mbed_official | 0:a554658735bf | 165 | memcpy(vid_pid, &result[8], 8); |
mbed_official | 0:a554658735bf | 166 | vid_pid[8] = 0; |
mbed_official | 0:a554658735bf | 167 | USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 168 | |
samux | 4:b320d68e98e7 | 169 | memcpy(vid_pid, &result[16], 16); |
samux | 4:b320d68e98e7 | 170 | vid_pid[16] = 0; |
samux | 4:b320d68e98e7 | 171 | USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 172 | |
mbed_official | 0:a554658735bf | 173 | memcpy(vid_pid, &result[32], 4); |
mbed_official | 0:a554658735bf | 174 | vid_pid[4] = 0; |
mbed_official | 0:a554658735bf | 175 | USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 176 | } |
mbed_official | 0:a554658735bf | 177 | return status; |
mbed_official | 0:a554658735bf | 178 | } |
mbed_official | 0:a554658735bf | 179 | |
mbed_official | 0:a554658735bf | 180 | int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) { |
mbed_official | 0:a554658735bf | 181 | // if ep stalled: send clear feature |
mbed_official | 0:a554658735bf | 182 | if (res == USB_TYPE_STALL_ERROR) { |
mbed_official | 0:a554658735bf | 183 | res = host->controlWrite( dev, |
mbed_official | 0:a554658735bf | 184 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
mbed_official | 0:a554658735bf | 185 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 186 | 0, ep->getAddress(), NULL, 0); |
mbed_official | 0:a554658735bf | 187 | // set state to IDLE if clear feature successful |
mbed_official | 0:a554658735bf | 188 | if (res == USB_TYPE_OK) { |
mbed_official | 0:a554658735bf | 189 | ep->setState(USB_TYPE_IDLE); |
mbed_official | 0:a554658735bf | 190 | } |
mbed_official | 0:a554658735bf | 191 | } |
mbed_official | 0:a554658735bf | 192 | |
mbed_official | 0:a554658735bf | 193 | if (res != USB_TYPE_OK) |
mbed_official | 0:a554658735bf | 194 | return -1; |
mbed_official | 0:a554658735bf | 195 | |
mbed_official | 0:a554658735bf | 196 | return 0; |
mbed_official | 0:a554658735bf | 197 | } |
mbed_official | 0:a554658735bf | 198 | |
mbed_official | 0:a554658735bf | 199 | |
mbed_official | 0:a554658735bf | 200 | int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) { |
mbed_official | 0:a554658735bf | 201 | |
mbed_official | 0:a554658735bf | 202 | int res = 0; |
mbed_official | 0:a554658735bf | 203 | |
mbed_official | 0:a554658735bf | 204 | cbw.Signature = CBW_SIGNATURE; |
mbed_official | 0:a554658735bf | 205 | cbw.Tag = 0; |
mbed_official | 0:a554658735bf | 206 | cbw.DataLength = transfer_len; |
mbed_official | 0:a554658735bf | 207 | cbw.Flags = flags; |
mbed_official | 0:a554658735bf | 208 | cbw.LUN = 0; |
mbed_official | 0:a554658735bf | 209 | cbw.CBLength = cmd_len; |
mbed_official | 0:a554658735bf | 210 | memset(cbw.CB,0,sizeof(cbw.CB)); |
mbed_official | 0:a554658735bf | 211 | if (cmd) { |
mbed_official | 0:a554658735bf | 212 | memcpy(cbw.CB,cmd,cmd_len); |
mbed_official | 0:a554658735bf | 213 | } |
mbed_official | 0:a554658735bf | 214 | |
mbed_official | 0:a554658735bf | 215 | // send the cbw |
samux | 4:b320d68e98e7 | 216 | USB_DBG("Send CBW"); |
mbed_official | 0:a554658735bf | 217 | res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); |
mbed_official | 0:a554658735bf | 218 | if (checkResult(res, bulk_out)) |
mbed_official | 0:a554658735bf | 219 | return -1; |
mbed_official | 0:a554658735bf | 220 | |
mbed_official | 0:a554658735bf | 221 | // data stage if needed |
mbed_official | 0:a554658735bf | 222 | if (data) { |
samux | 4:b320d68e98e7 | 223 | USB_DBG("data stage"); |
mbed_official | 0:a554658735bf | 224 | if (flags == HOST_TO_DEVICE) { |
samux | 4:b320d68e98e7 | 225 | |
mbed_official | 0:a554658735bf | 226 | res = host->bulkWrite(dev, bulk_out, data, transfer_len); |
mbed_official | 0:a554658735bf | 227 | if (checkResult(res, bulk_out)) |
mbed_official | 0:a554658735bf | 228 | return -1; |
mbed_official | 0:a554658735bf | 229 | |
mbed_official | 0:a554658735bf | 230 | } else if (flags == DEVICE_TO_HOST) { |
mbed_official | 0:a554658735bf | 231 | |
mbed_official | 0:a554658735bf | 232 | res = host->bulkRead(dev, bulk_in, data, transfer_len); |
mbed_official | 0:a554658735bf | 233 | if (checkResult(res, bulk_in)) |
mbed_official | 0:a554658735bf | 234 | return -1; |
mbed_official | 0:a554658735bf | 235 | } |
mbed_official | 0:a554658735bf | 236 | } |
mbed_official | 0:a554658735bf | 237 | |
mbed_official | 0:a554658735bf | 238 | // status stage |
mbed_official | 0:a554658735bf | 239 | csw.Signature = 0; |
samux | 4:b320d68e98e7 | 240 | USB_DBG("Read CSW"); |
mbed_official | 0:a554658735bf | 241 | res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); |
mbed_official | 0:a554658735bf | 242 | if (checkResult(res, bulk_in)) |
mbed_official | 0:a554658735bf | 243 | return -1; |
mbed_official | 0:a554658735bf | 244 | |
mbed_official | 0:a554658735bf | 245 | if (csw.Signature != CSW_SIGNATURE) { |
mbed_official | 0:a554658735bf | 246 | return -1; |
mbed_official | 0:a554658735bf | 247 | } |
samux | 4:b320d68e98e7 | 248 | |
samux | 4:b320d68e98e7 | 249 | USB_DBG("recv csw: status: %d", csw.Status); |
mbed_official | 0:a554658735bf | 250 | |
mbed_official | 0:a554658735bf | 251 | // ModeSense? |
samux | 4:b320d68e98e7 | 252 | if ((csw.Status == 1) && (cmd[0] != 0x03)) { |
samux | 4:b320d68e98e7 | 253 | USB_DBG("request mode sense"); |
mbed_official | 0:a554658735bf | 254 | return SCSIRequestSense(); |
samux | 4:b320d68e98e7 | 255 | } |
samux | 4:b320d68e98e7 | 256 | |
samux | 4:b320d68e98e7 | 257 | // perform reset recovery |
samux | 4:b320d68e98e7 | 258 | if ((csw.Status == 2) && (cmd[0] != 0x03)) { |
samux | 4:b320d68e98e7 | 259 | |
samux | 4:b320d68e98e7 | 260 | // send Bulk-Only Mass Storage Reset request |
samux | 4:b320d68e98e7 | 261 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 262 | USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, |
samux | 4:b320d68e98e7 | 263 | BO_MASS_STORAGE_RESET, |
samux | 4:b320d68e98e7 | 264 | 0, msd_intf, NULL, 0); |
samux | 4:b320d68e98e7 | 265 | |
samux | 4:b320d68e98e7 | 266 | // unstall both endpoints |
samux | 4:b320d68e98e7 | 267 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 268 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
samux | 4:b320d68e98e7 | 269 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 270 | 0, bulk_in->getAddress(), NULL, 0); |
samux | 4:b320d68e98e7 | 271 | |
samux | 4:b320d68e98e7 | 272 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 273 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
samux | 4:b320d68e98e7 | 274 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 275 | 0, bulk_out->getAddress(), NULL, 0); |
samux | 4:b320d68e98e7 | 276 | |
samux | 4:b320d68e98e7 | 277 | } |
mbed_official | 0:a554658735bf | 278 | |
mbed_official | 0:a554658735bf | 279 | return csw.Status; |
mbed_official | 0:a554658735bf | 280 | } |
mbed_official | 0:a554658735bf | 281 | |
mbed_official | 0:a554658735bf | 282 | |
mbed_official | 0:a554658735bf | 283 | int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) { |
mbed_official | 0:a554658735bf | 284 | uint8_t cmd[10]; |
mbed_official | 0:a554658735bf | 285 | memset(cmd,0,10); |
mbed_official | 0:a554658735bf | 286 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
mbed_official | 0:a554658735bf | 287 | |
mbed_official | 0:a554658735bf | 288 | cmd[2] = (block >> 24) & 0xff; |
mbed_official | 0:a554658735bf | 289 | cmd[3] = (block >> 16) & 0xff; |
mbed_official | 0:a554658735bf | 290 | cmd[4] = (block >> 8) & 0xff; |
mbed_official | 0:a554658735bf | 291 | cmd[5] = block & 0xff; |
mbed_official | 0:a554658735bf | 292 | |
mbed_official | 0:a554658735bf | 293 | cmd[7] = (nbBlock >> 8) & 0xff; |
mbed_official | 0:a554658735bf | 294 | cmd[8] = nbBlock & 0xff; |
mbed_official | 0:a554658735bf | 295 | |
mbed_official | 0:a554658735bf | 296 | return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); |
mbed_official | 0:a554658735bf | 297 | } |
mbed_official | 0:a554658735bf | 298 | |
samux | 4:b320d68e98e7 | 299 | int USBHostMSD::getMaxLun() { |
samux | 4:b320d68e98e7 | 300 | uint8_t buf[1], res; |
samux | 4:b320d68e98e7 | 301 | res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
samux | 4:b320d68e98e7 | 302 | 0xfe, 0, msd_intf, buf, 1); |
samux | 4:b320d68e98e7 | 303 | USB_DBG("max lun: %d", buf[0]); |
samux | 4:b320d68e98e7 | 304 | return res; |
samux | 4:b320d68e98e7 | 305 | } |
mbed_official | 0:a554658735bf | 306 | |
mbed_official | 0:a554658735bf | 307 | int USBHostMSD::disk_initialize() { |
mbed_official | 0:a554658735bf | 308 | USB_DBG("FILESYSTEM: init"); |
samux | 4:b320d68e98e7 | 309 | U16 i, timeout = 10; |
samux | 4:b320d68e98e7 | 310 | |
samux | 4:b320d68e98e7 | 311 | getMaxLun(); |
mbed_official | 0:a554658735bf | 312 | |
mbed_official | 0:a554658735bf | 313 | for (i = 0; i < timeout; i++) { |
samux | 4:b320d68e98e7 | 314 | Thread::wait(100); |
mbed_official | 0:a554658735bf | 315 | if (!testUnitReady()) |
mbed_official | 0:a554658735bf | 316 | break; |
mbed_official | 0:a554658735bf | 317 | } |
mbed_official | 0:a554658735bf | 318 | |
mbed_official | 0:a554658735bf | 319 | if (i == timeout) { |
mbed_official | 0:a554658735bf | 320 | disk_init = false; |
mbed_official | 0:a554658735bf | 321 | return -1; |
mbed_official | 0:a554658735bf | 322 | } |
samux | 4:b320d68e98e7 | 323 | |
mbed_official | 0:a554658735bf | 324 | inquiry(0, 0); |
mbed_official | 0:a554658735bf | 325 | disk_init = 1; |
mbed_official | 0:a554658735bf | 326 | return readCapacity(); |
mbed_official | 0:a554658735bf | 327 | } |
mbed_official | 0:a554658735bf | 328 | |
mbed_official | 0:a554658735bf | 329 | int USBHostMSD::disk_write(const uint8_t *buffer, uint64_t block_number) { |
mbed_official | 0:a554658735bf | 330 | USB_DBG("FILESYSTEM: write block: %lld", block_number); |
mbed_official | 0:a554658735bf | 331 | if (!disk_init) { |
mbed_official | 0:a554658735bf | 332 | disk_initialize(); |
mbed_official | 0:a554658735bf | 333 | } |
mbed_official | 0:a554658735bf | 334 | if (!disk_init) |
mbed_official | 0:a554658735bf | 335 | return -1; |
mbed_official | 0:a554658735bf | 336 | return dataTransfer((uint8_t *)buffer, block_number, 1, HOST_TO_DEVICE); |
mbed_official | 0:a554658735bf | 337 | } |
mbed_official | 0:a554658735bf | 338 | |
mbed_official | 0:a554658735bf | 339 | int USBHostMSD::disk_read(uint8_t * buffer, uint64_t block_number) { |
mbed_official | 0:a554658735bf | 340 | USB_DBG("FILESYSTEM: read block %lld", block_number); |
mbed_official | 0:a554658735bf | 341 | if (!disk_init) { |
mbed_official | 0:a554658735bf | 342 | disk_initialize(); |
mbed_official | 0:a554658735bf | 343 | } |
mbed_official | 0:a554658735bf | 344 | if (!disk_init) |
mbed_official | 0:a554658735bf | 345 | return -1; |
mbed_official | 0:a554658735bf | 346 | return dataTransfer((uint8_t *)buffer, block_number, 1, DEVICE_TO_HOST); |
mbed_official | 0:a554658735bf | 347 | } |
mbed_official | 0:a554658735bf | 348 | |
mbed_official | 0:a554658735bf | 349 | uint64_t USBHostMSD::disk_sectors() { |
mbed_official | 0:a554658735bf | 350 | USB_DBG("FILESYSTEM: sectors"); |
mbed_official | 0:a554658735bf | 351 | if (!disk_init) { |
mbed_official | 0:a554658735bf | 352 | disk_initialize(); |
mbed_official | 0:a554658735bf | 353 | } |
mbed_official | 0:a554658735bf | 354 | if (!disk_init) |
mbed_official | 0:a554658735bf | 355 | return 0; |
mbed_official | 0:a554658735bf | 356 | return blockCount; |
mbed_official | 0:a554658735bf | 357 | } |
mbed_official | 0:a554658735bf | 358 | |
mbed_official | 0:a554658735bf | 359 | #endif |