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