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