test
Dependents: Production_version1_0 Production_ver1_0 USBHost_TEST USBHost_Test5 ... more
Fork of USBHost by
USBHostMSD/USBHostMSD.cpp@37:f1e388e7b752, 2017-07-20 (annotated)
- Committer:
- Kojto
- Date:
- Thu Jul 20 10:13:56 2017 +0100
- Revision:
- 37:f1e388e7b752
- Parent:
- 33:6c4cab4d356f
Update libraries (ed9d1da)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 8:93da8ea2708b | 1 | /* mbed USBHost Library |
samux | 8:93da8ea2708b | 2 | * Copyright (c) 2006-2013 ARM Limited |
samux | 8:93da8ea2708b | 3 | * |
samux | 8:93da8ea2708b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
samux | 8:93da8ea2708b | 5 | * you may not use this file except in compliance with the License. |
samux | 8:93da8ea2708b | 6 | * You may obtain a copy of the License at |
samux | 8:93da8ea2708b | 7 | * |
samux | 8:93da8ea2708b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
samux | 8:93da8ea2708b | 9 | * |
samux | 8:93da8ea2708b | 10 | * Unless required by applicable law or agreed to in writing, software |
samux | 8:93da8ea2708b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
samux | 8:93da8ea2708b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
samux | 8:93da8ea2708b | 13 | * See the License for the specific language governing permissions and |
samux | 8:93da8ea2708b | 14 | * limitations under the License. |
samux | 8:93da8ea2708b | 15 | */ |
mbed_official | 0:a554658735bf | 16 | |
mbed_official | 0:a554658735bf | 17 | #include "USBHostMSD.h" |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #if USBHOST_MSD |
mbed_official | 0:a554658735bf | 20 | |
mbed_official | 0:a554658735bf | 21 | #include "dbg.h" |
mbed_official | 0:a554658735bf | 22 | |
mbed_official | 0:a554658735bf | 23 | #define CBW_SIGNATURE 0x43425355 |
mbed_official | 0:a554658735bf | 24 | #define CSW_SIGNATURE 0x53425355 |
mbed_official | 0:a554658735bf | 25 | |
mbed_official | 0:a554658735bf | 26 | #define DEVICE_TO_HOST 0x80 |
mbed_official | 0:a554658735bf | 27 | #define HOST_TO_DEVICE 0x00 |
mbed_official | 0:a554658735bf | 28 | |
samux | 4:b320d68e98e7 | 29 | #define GET_MAX_LUN (0xFE) |
samux | 4:b320d68e98e7 | 30 | #define BO_MASS_STORAGE_RESET (0xFF) |
mbed_official | 0:a554658735bf | 31 | |
mbed_official | 0:a554658735bf | 32 | USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir) |
mbed_official | 0:a554658735bf | 33 | { |
mbed_official | 0:a554658735bf | 34 | host = USBHost::getHostInst(); |
mbed_official | 0:a554658735bf | 35 | init(); |
mbed_official | 0:a554658735bf | 36 | } |
mbed_official | 0:a554658735bf | 37 | |
mbed_official | 0:a554658735bf | 38 | void USBHostMSD::init() { |
mbed_official | 0:a554658735bf | 39 | dev_connected = false; |
mbed_official | 0:a554658735bf | 40 | dev = NULL; |
mbed_official | 0:a554658735bf | 41 | bulk_in = NULL; |
mbed_official | 0:a554658735bf | 42 | bulk_out = NULL; |
mbed_official | 0:a554658735bf | 43 | dev_connected = false; |
mbed_official | 0:a554658735bf | 44 | blockSize = 0; |
mbed_official | 0:a554658735bf | 45 | blockCount = 0; |
mbed_official | 0:a554658735bf | 46 | msd_intf = -1; |
mbed_official | 0:a554658735bf | 47 | msd_device_found = false; |
mbed_official | 0:a554658735bf | 48 | disk_init = false; |
mbed_official | 0:a554658735bf | 49 | dev_connected = false; |
samux | 4:b320d68e98e7 | 50 | nb_ep = 0; |
mbed_official | 0:a554658735bf | 51 | } |
mbed_official | 0:a554658735bf | 52 | |
mbed_official | 0:a554658735bf | 53 | |
mbed_official | 0:a554658735bf | 54 | bool USBHostMSD::connected() |
mbed_official | 0:a554658735bf | 55 | { |
mbed_official | 0:a554658735bf | 56 | return dev_connected; |
mbed_official | 0:a554658735bf | 57 | } |
mbed_official | 0:a554658735bf | 58 | |
mbed_official | 0:a554658735bf | 59 | bool USBHostMSD::connect() |
mbed_official | 0:a554658735bf | 60 | { |
mbed_official | 0:a554658735bf | 61 | |
mbed_official | 0:a554658735bf | 62 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 63 | return true; |
mbed_official | 0:a554658735bf | 64 | } |
mbed_official | 0:a554658735bf | 65 | |
samux | 5:e48791a1ef18 | 66 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 67 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 24:868cbfe611a7 | 68 | |
samux | 4:b320d68e98e7 | 69 | USB_DBG("Trying to connect MSD device\r\n"); |
mbed_official | 24:868cbfe611a7 | 70 | |
mbed_official | 0:a554658735bf | 71 | if(host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 72 | break; |
mbed_official | 0:a554658735bf | 73 | |
mbed_official | 0:a554658735bf | 74 | if (msd_device_found) { |
Kojto | 37:f1e388e7b752 | 75 | /* As this is done in a specific thread |
Kojto | 37:f1e388e7b752 | 76 | * this lock is taken to avoid to process a disconnection in |
Kojto | 37:f1e388e7b752 | 77 | * usb process during the device registering */ |
Kojto | 37:f1e388e7b752 | 78 | USBHost::Lock Lock(host); |
Kojto | 37:f1e388e7b752 | 79 | |
mbed_official | 0:a554658735bf | 80 | bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 81 | bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); |
mbed_official | 24:868cbfe611a7 | 82 | |
mbed_official | 0:a554658735bf | 83 | if (!bulk_in || !bulk_out) |
samux | 4:b320d68e98e7 | 84 | continue; |
mbed_official | 24:868cbfe611a7 | 85 | |
samux | 4:b320d68e98e7 | 86 | USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); |
samux | 4:b320d68e98e7 | 87 | dev->setName("MSD", msd_intf); |
mbed_official | 0:a554658735bf | 88 | host->registerDriver(dev, msd_intf, this, &USBHostMSD::init); |
mbed_official | 0:a554658735bf | 89 | |
mbed_official | 0:a554658735bf | 90 | dev_connected = true; |
mbed_official | 0:a554658735bf | 91 | return true; |
mbed_official | 0:a554658735bf | 92 | } |
mbed_official | 0:a554658735bf | 93 | } //if() |
mbed_official | 0:a554658735bf | 94 | } //for() |
samux | 4:b320d68e98e7 | 95 | init(); |
mbed_official | 0:a554658735bf | 96 | return false; |
mbed_official | 0:a554658735bf | 97 | } |
mbed_official | 0:a554658735bf | 98 | |
mbed_official | 0:a554658735bf | 99 | /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 100 | { |
mbed_official | 0:a554658735bf | 101 | // we don't check VID/PID for MSD driver |
mbed_official | 0:a554658735bf | 102 | } |
mbed_official | 0:a554658735bf | 103 | |
mbed_official | 0:a554658735bf | 104 | /*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 | 105 | { |
mbed_official | 0:a554658735bf | 106 | if ((msd_intf == -1) && |
mbed_official | 0:a554658735bf | 107 | (intf_class == MSD_CLASS) && |
mbed_official | 0:a554658735bf | 108 | (intf_subclass == 0x06) && |
mbed_official | 0:a554658735bf | 109 | (intf_protocol == 0x50)) { |
mbed_official | 0:a554658735bf | 110 | msd_intf = intf_nb; |
mbed_official | 0:a554658735bf | 111 | return true; |
mbed_official | 0:a554658735bf | 112 | } |
mbed_official | 0:a554658735bf | 113 | return false; |
mbed_official | 0:a554658735bf | 114 | } |
mbed_official | 0:a554658735bf | 115 | |
mbed_official | 0:a554658735bf | 116 | /*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 | 117 | { |
mbed_official | 0:a554658735bf | 118 | if (intf_nb == msd_intf) { |
mbed_official | 0:a554658735bf | 119 | if (type == BULK_ENDPOINT) { |
samux | 4:b320d68e98e7 | 120 | nb_ep++; |
samux | 4:b320d68e98e7 | 121 | if (nb_ep == 2) |
samux | 4:b320d68e98e7 | 122 | msd_device_found = true; |
mbed_official | 0:a554658735bf | 123 | return true; |
mbed_official | 0:a554658735bf | 124 | } |
mbed_official | 0:a554658735bf | 125 | } |
mbed_official | 0:a554658735bf | 126 | return false; |
mbed_official | 0:a554658735bf | 127 | } |
mbed_official | 0:a554658735bf | 128 | |
mbed_official | 0:a554658735bf | 129 | |
Kojto | 37:f1e388e7b752 | 130 | int USBHostMSD::testUnitReady() |
Kojto | 37:f1e388e7b752 | 131 | { |
samux | 4:b320d68e98e7 | 132 | USB_DBG("Test unit ready"); |
mbed_official | 0:a554658735bf | 133 | return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); |
mbed_official | 0:a554658735bf | 134 | } |
mbed_official | 0:a554658735bf | 135 | |
samux | 4:b320d68e98e7 | 136 | |
Kojto | 37:f1e388e7b752 | 137 | int USBHostMSD::readCapacity() |
Kojto | 37:f1e388e7b752 | 138 | { |
samux | 4:b320d68e98e7 | 139 | USB_DBG("Read capacity"); |
mbed_official | 0:a554658735bf | 140 | uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; |
mbed_official | 0:a554658735bf | 141 | uint8_t result[8]; |
mbed_official | 0:a554658735bf | 142 | int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); |
mbed_official | 0:a554658735bf | 143 | if (status == 0) { |
mbed_official | 0:a554658735bf | 144 | blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; |
mbed_official | 0:a554658735bf | 145 | blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; |
samux | 4:b320d68e98e7 | 146 | USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); |
mbed_official | 0:a554658735bf | 147 | } |
mbed_official | 0:a554658735bf | 148 | return status; |
mbed_official | 0:a554658735bf | 149 | } |
mbed_official | 0:a554658735bf | 150 | |
mbed_official | 0:a554658735bf | 151 | |
Kojto | 37:f1e388e7b752 | 152 | int USBHostMSD::SCSIRequestSense() |
Kojto | 37:f1e388e7b752 | 153 | { |
samux | 4:b320d68e98e7 | 154 | USB_DBG("Request sense"); |
samux | 4:b320d68e98e7 | 155 | uint8_t cmd[6] = {0x03,0,0,0,18,0}; |
samux | 4:b320d68e98e7 | 156 | uint8_t result[18]; |
samux | 4:b320d68e98e7 | 157 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); |
mbed_official | 0:a554658735bf | 158 | return status; |
mbed_official | 0:a554658735bf | 159 | } |
mbed_official | 0:a554658735bf | 160 | |
mbed_official | 0:a554658735bf | 161 | |
Kojto | 37:f1e388e7b752 | 162 | int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) |
Kojto | 37:f1e388e7b752 | 163 | { |
samux | 4:b320d68e98e7 | 164 | USB_DBG("Inquiry"); |
mbed_official | 0:a554658735bf | 165 | uint8_t evpd = (page_code == 0) ? 0 : 1; |
mbed_official | 13:b58a2204422f | 166 | uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0}; |
mbed_official | 0:a554658735bf | 167 | uint8_t result[36]; |
mbed_official | 0:a554658735bf | 168 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); |
mbed_official | 0:a554658735bf | 169 | if (status == 0) { |
samux | 4:b320d68e98e7 | 170 | char vid_pid[17]; |
mbed_official | 0:a554658735bf | 171 | memcpy(vid_pid, &result[8], 8); |
mbed_official | 0:a554658735bf | 172 | vid_pid[8] = 0; |
mbed_official | 0:a554658735bf | 173 | USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 174 | |
samux | 4:b320d68e98e7 | 175 | memcpy(vid_pid, &result[16], 16); |
samux | 4:b320d68e98e7 | 176 | vid_pid[16] = 0; |
samux | 4:b320d68e98e7 | 177 | USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 178 | |
mbed_official | 0:a554658735bf | 179 | memcpy(vid_pid, &result[32], 4); |
mbed_official | 0:a554658735bf | 180 | vid_pid[4] = 0; |
mbed_official | 0:a554658735bf | 181 | USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 182 | } |
mbed_official | 0:a554658735bf | 183 | return status; |
mbed_official | 0:a554658735bf | 184 | } |
mbed_official | 0:a554658735bf | 185 | |
Kojto | 37:f1e388e7b752 | 186 | int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) |
Kojto | 37:f1e388e7b752 | 187 | { |
mbed_official | 0:a554658735bf | 188 | // if ep stalled: send clear feature |
mbed_official | 0:a554658735bf | 189 | if (res == USB_TYPE_STALL_ERROR) { |
mbed_official | 0:a554658735bf | 190 | res = host->controlWrite( dev, |
mbed_official | 0:a554658735bf | 191 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
mbed_official | 0:a554658735bf | 192 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 193 | 0, ep->getAddress(), NULL, 0); |
mbed_official | 0:a554658735bf | 194 | // set state to IDLE if clear feature successful |
mbed_official | 0:a554658735bf | 195 | if (res == USB_TYPE_OK) { |
mbed_official | 0:a554658735bf | 196 | ep->setState(USB_TYPE_IDLE); |
mbed_official | 0:a554658735bf | 197 | } |
mbed_official | 0:a554658735bf | 198 | } |
mbed_official | 0:a554658735bf | 199 | |
mbed_official | 0:a554658735bf | 200 | if (res != USB_TYPE_OK) |
mbed_official | 0:a554658735bf | 201 | return -1; |
mbed_official | 0:a554658735bf | 202 | |
mbed_official | 0:a554658735bf | 203 | return 0; |
mbed_official | 0:a554658735bf | 204 | } |
mbed_official | 0:a554658735bf | 205 | |
mbed_official | 0:a554658735bf | 206 | |
Kojto | 37:f1e388e7b752 | 207 | int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) |
Kojto | 37:f1e388e7b752 | 208 | { |
mbed_official | 0:a554658735bf | 209 | |
mbed_official | 0:a554658735bf | 210 | int res = 0; |
mbed_official | 0:a554658735bf | 211 | |
mbed_official | 0:a554658735bf | 212 | cbw.Signature = CBW_SIGNATURE; |
mbed_official | 0:a554658735bf | 213 | cbw.Tag = 0; |
mbed_official | 0:a554658735bf | 214 | cbw.DataLength = transfer_len; |
mbed_official | 0:a554658735bf | 215 | cbw.Flags = flags; |
mbed_official | 0:a554658735bf | 216 | cbw.LUN = 0; |
mbed_official | 0:a554658735bf | 217 | cbw.CBLength = cmd_len; |
mbed_official | 0:a554658735bf | 218 | memset(cbw.CB,0,sizeof(cbw.CB)); |
mbed_official | 0:a554658735bf | 219 | if (cmd) { |
mbed_official | 0:a554658735bf | 220 | memcpy(cbw.CB,cmd,cmd_len); |
mbed_official | 0:a554658735bf | 221 | } |
mbed_official | 0:a554658735bf | 222 | |
mbed_official | 0:a554658735bf | 223 | // send the cbw |
samux | 4:b320d68e98e7 | 224 | USB_DBG("Send CBW"); |
mbed_official | 0:a554658735bf | 225 | res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); |
mbed_official | 0:a554658735bf | 226 | if (checkResult(res, bulk_out)) |
mbed_official | 0:a554658735bf | 227 | return -1; |
mbed_official | 0:a554658735bf | 228 | |
mbed_official | 0:a554658735bf | 229 | // data stage if needed |
mbed_official | 0:a554658735bf | 230 | if (data) { |
samux | 4:b320d68e98e7 | 231 | USB_DBG("data stage"); |
mbed_official | 0:a554658735bf | 232 | if (flags == HOST_TO_DEVICE) { |
mbed_official | 24:868cbfe611a7 | 233 | |
mbed_official | 0:a554658735bf | 234 | res = host->bulkWrite(dev, bulk_out, data, transfer_len); |
mbed_official | 0:a554658735bf | 235 | if (checkResult(res, bulk_out)) |
mbed_official | 0:a554658735bf | 236 | return -1; |
mbed_official | 0:a554658735bf | 237 | |
mbed_official | 0:a554658735bf | 238 | } else if (flags == DEVICE_TO_HOST) { |
mbed_official | 0:a554658735bf | 239 | |
mbed_official | 0:a554658735bf | 240 | res = host->bulkRead(dev, bulk_in, data, transfer_len); |
mbed_official | 0:a554658735bf | 241 | if (checkResult(res, bulk_in)) |
mbed_official | 0:a554658735bf | 242 | return -1; |
mbed_official | 0:a554658735bf | 243 | } |
mbed_official | 0:a554658735bf | 244 | } |
mbed_official | 0:a554658735bf | 245 | |
mbed_official | 0:a554658735bf | 246 | // status stage |
mbed_official | 0:a554658735bf | 247 | csw.Signature = 0; |
samux | 4:b320d68e98e7 | 248 | USB_DBG("Read CSW"); |
mbed_official | 0:a554658735bf | 249 | res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); |
mbed_official | 0:a554658735bf | 250 | if (checkResult(res, bulk_in)) |
mbed_official | 0:a554658735bf | 251 | return -1; |
mbed_official | 0:a554658735bf | 252 | |
mbed_official | 0:a554658735bf | 253 | if (csw.Signature != CSW_SIGNATURE) { |
mbed_official | 0:a554658735bf | 254 | return -1; |
mbed_official | 0:a554658735bf | 255 | } |
mbed_official | 24:868cbfe611a7 | 256 | |
samux | 4:b320d68e98e7 | 257 | USB_DBG("recv csw: status: %d", csw.Status); |
mbed_official | 0:a554658735bf | 258 | |
mbed_official | 0:a554658735bf | 259 | // ModeSense? |
samux | 4:b320d68e98e7 | 260 | if ((csw.Status == 1) && (cmd[0] != 0x03)) { |
samux | 4:b320d68e98e7 | 261 | USB_DBG("request mode sense"); |
mbed_official | 0:a554658735bf | 262 | return SCSIRequestSense(); |
samux | 4:b320d68e98e7 | 263 | } |
mbed_official | 24:868cbfe611a7 | 264 | |
samux | 4:b320d68e98e7 | 265 | // perform reset recovery |
samux | 4:b320d68e98e7 | 266 | if ((csw.Status == 2) && (cmd[0] != 0x03)) { |
mbed_official | 24:868cbfe611a7 | 267 | |
samux | 4:b320d68e98e7 | 268 | // send Bulk-Only Mass Storage Reset request |
samux | 4:b320d68e98e7 | 269 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 270 | USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, |
samux | 4:b320d68e98e7 | 271 | BO_MASS_STORAGE_RESET, |
samux | 4:b320d68e98e7 | 272 | 0, msd_intf, NULL, 0); |
mbed_official | 24:868cbfe611a7 | 273 | |
samux | 4:b320d68e98e7 | 274 | // unstall both endpoints |
samux | 4:b320d68e98e7 | 275 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 276 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
samux | 4:b320d68e98e7 | 277 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 278 | 0, bulk_in->getAddress(), NULL, 0); |
mbed_official | 24:868cbfe611a7 | 279 | |
samux | 4:b320d68e98e7 | 280 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 281 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
samux | 4:b320d68e98e7 | 282 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 283 | 0, bulk_out->getAddress(), NULL, 0); |
mbed_official | 24:868cbfe611a7 | 284 | |
samux | 4:b320d68e98e7 | 285 | } |
mbed_official | 0:a554658735bf | 286 | |
mbed_official | 0:a554658735bf | 287 | return csw.Status; |
mbed_official | 0:a554658735bf | 288 | } |
mbed_official | 0:a554658735bf | 289 | |
mbed_official | 0:a554658735bf | 290 | |
Kojto | 37:f1e388e7b752 | 291 | int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) |
Kojto | 37:f1e388e7b752 | 292 | { |
mbed_official | 0:a554658735bf | 293 | uint8_t cmd[10]; |
mbed_official | 0:a554658735bf | 294 | memset(cmd,0,10); |
mbed_official | 0:a554658735bf | 295 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
mbed_official | 0:a554658735bf | 296 | |
mbed_official | 0:a554658735bf | 297 | cmd[2] = (block >> 24) & 0xff; |
mbed_official | 0:a554658735bf | 298 | cmd[3] = (block >> 16) & 0xff; |
mbed_official | 0:a554658735bf | 299 | cmd[4] = (block >> 8) & 0xff; |
mbed_official | 0:a554658735bf | 300 | cmd[5] = block & 0xff; |
mbed_official | 0:a554658735bf | 301 | |
mbed_official | 0:a554658735bf | 302 | cmd[7] = (nbBlock >> 8) & 0xff; |
mbed_official | 0:a554658735bf | 303 | cmd[8] = nbBlock & 0xff; |
mbed_official | 0:a554658735bf | 304 | |
mbed_official | 0:a554658735bf | 305 | return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); |
mbed_official | 0:a554658735bf | 306 | } |
mbed_official | 0:a554658735bf | 307 | |
Kojto | 37:f1e388e7b752 | 308 | int USBHostMSD::getMaxLun() |
Kojto | 37:f1e388e7b752 | 309 | { |
samux | 4:b320d68e98e7 | 310 | uint8_t buf[1], res; |
samux | 4:b320d68e98e7 | 311 | res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
samux | 4:b320d68e98e7 | 312 | 0xfe, 0, msd_intf, buf, 1); |
samux | 4:b320d68e98e7 | 313 | USB_DBG("max lun: %d", buf[0]); |
samux | 4:b320d68e98e7 | 314 | return res; |
samux | 4:b320d68e98e7 | 315 | } |
mbed_official | 0:a554658735bf | 316 | |
mbed_official | 0:a554658735bf | 317 | int USBHostMSD::disk_initialize() { |
mbed_official | 0:a554658735bf | 318 | USB_DBG("FILESYSTEM: init"); |
mbed_official | 27:4206883f4cb7 | 319 | uint16_t i, timeout = 10; |
mbed_official | 24:868cbfe611a7 | 320 | |
samux | 4:b320d68e98e7 | 321 | getMaxLun(); |
mbed_official | 24:868cbfe611a7 | 322 | |
mbed_official | 0:a554658735bf | 323 | for (i = 0; i < timeout; i++) { |
samux | 4:b320d68e98e7 | 324 | Thread::wait(100); |
mbed_official | 0:a554658735bf | 325 | if (!testUnitReady()) |
mbed_official | 0:a554658735bf | 326 | break; |
mbed_official | 0:a554658735bf | 327 | } |
mbed_official | 24:868cbfe611a7 | 328 | |
mbed_official | 0:a554658735bf | 329 | if (i == timeout) { |
mbed_official | 0:a554658735bf | 330 | disk_init = false; |
mbed_official | 0:a554658735bf | 331 | return -1; |
mbed_official | 0:a554658735bf | 332 | } |
mbed_official | 24:868cbfe611a7 | 333 | |
mbed_official | 0:a554658735bf | 334 | inquiry(0, 0); |
mbed_official | 0:a554658735bf | 335 | disk_init = 1; |
mbed_official | 0:a554658735bf | 336 | return readCapacity(); |
mbed_official | 0:a554658735bf | 337 | } |
mbed_official | 0:a554658735bf | 338 | |
mbed_official | 33:6c4cab4d356f | 339 | int USBHostMSD::disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count) { |
mbed_official | 26:607951c26872 | 340 | USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count); |
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 | 33:6c4cab4d356f | 346 | for (uint32_t b = block_number; b < block_number + count; b++) { |
mbed_official | 26:607951c26872 | 347 | if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE)) |
mbed_official | 26:607951c26872 | 348 | return -1; |
mbed_official | 26:607951c26872 | 349 | buffer += 512; |
mbed_official | 26:607951c26872 | 350 | } |
mbed_official | 26:607951c26872 | 351 | return 0; |
mbed_official | 0:a554658735bf | 352 | } |
mbed_official | 0:a554658735bf | 353 | |
mbed_official | 33:6c4cab4d356f | 354 | int USBHostMSD::disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count) { |
mbed_official | 26:607951c26872 | 355 | USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count); |
mbed_official | 0:a554658735bf | 356 | if (!disk_init) { |
mbed_official | 0:a554658735bf | 357 | disk_initialize(); |
mbed_official | 0:a554658735bf | 358 | } |
mbed_official | 0:a554658735bf | 359 | if (!disk_init) |
mbed_official | 0:a554658735bf | 360 | return -1; |
mbed_official | 33:6c4cab4d356f | 361 | for (uint32_t b = block_number; b < block_number + count; b++) { |
mbed_official | 26:607951c26872 | 362 | if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST)) |
mbed_official | 26:607951c26872 | 363 | return -1; |
mbed_official | 26:607951c26872 | 364 | buffer += 512; |
mbed_official | 26:607951c26872 | 365 | } |
mbed_official | 26:607951c26872 | 366 | return 0; |
mbed_official | 0:a554658735bf | 367 | } |
mbed_official | 0:a554658735bf | 368 | |
mbed_official | 33:6c4cab4d356f | 369 | uint32_t USBHostMSD::disk_sectors() { |
mbed_official | 0:a554658735bf | 370 | USB_DBG("FILESYSTEM: sectors"); |
mbed_official | 0:a554658735bf | 371 | if (!disk_init) { |
mbed_official | 0:a554658735bf | 372 | disk_initialize(); |
mbed_official | 0:a554658735bf | 373 | } |
mbed_official | 0:a554658735bf | 374 | if (!disk_init) |
mbed_official | 0:a554658735bf | 375 | return 0; |
mbed_official | 0:a554658735bf | 376 | return blockCount; |
mbed_official | 0:a554658735bf | 377 | } |
mbed_official | 0:a554658735bf | 378 | |
mbed_official | 0:a554658735bf | 379 | #endif |