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