Adaptation of the official mbed USBHost repository to work with the LPC4088 Display Module
Dependents: DMSupport DMSupport DMSupport DMSupport
Fork of DM_USBHost by
USBHostMSD/USBHostMSD.cpp@32:f2d129436056, 2019-10-23 (annotated)
- Committer:
- embeddedartists
- Date:
- Wed Oct 23 06:58:55 2019 +0000
- Revision:
- 32:f2d129436056
- Parent:
- 29:dc8179214fd7
- Child:
- 33:819bbf04163b
Updates related to mbed OS 5
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" |
embeddedartists | 29:dc8179214fd7 | 18 | #include "diskio.h" |
mbed_official | 0:a554658735bf | 19 | |
mbed_official | 0:a554658735bf | 20 | #if USBHOST_MSD |
mbed_official | 0:a554658735bf | 21 | |
mbed_official | 0:a554658735bf | 22 | #include "dbg.h" |
mbed_official | 0:a554658735bf | 23 | |
mbed_official | 0:a554658735bf | 24 | #define CBW_SIGNATURE 0x43425355 |
mbed_official | 0:a554658735bf | 25 | #define CSW_SIGNATURE 0x53425355 |
mbed_official | 0:a554658735bf | 26 | |
mbed_official | 0:a554658735bf | 27 | #define DEVICE_TO_HOST 0x80 |
mbed_official | 0:a554658735bf | 28 | #define HOST_TO_DEVICE 0x00 |
mbed_official | 0:a554658735bf | 29 | |
samux | 4:b320d68e98e7 | 30 | #define GET_MAX_LUN (0xFE) |
samux | 4:b320d68e98e7 | 31 | #define BO_MASS_STORAGE_RESET (0xFF) |
mbed_official | 0:a554658735bf | 32 | |
mbed_official | 0:a554658735bf | 33 | USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir) |
mbed_official | 0:a554658735bf | 34 | { |
mbed_official | 0:a554658735bf | 35 | host = USBHost::getHostInst(); |
mbed_official | 0:a554658735bf | 36 | init(); |
embeddedartists | 27:aa2fd412f1d3 | 37 | |
embeddedartists | 27:aa2fd412f1d3 | 38 | cbw = (CBW*)host->getSafeMem(sizeof(CBW)); |
embeddedartists | 27:aa2fd412f1d3 | 39 | csw = (CSW*)host->getSafeMem(sizeof(CSW)); |
embeddedartists | 27:aa2fd412f1d3 | 40 | } |
embeddedartists | 27:aa2fd412f1d3 | 41 | |
embeddedartists | 27:aa2fd412f1d3 | 42 | USBHostMSD::~USBHostMSD() |
embeddedartists | 27:aa2fd412f1d3 | 43 | { |
embeddedartists | 27:aa2fd412f1d3 | 44 | if (cbw != NULL) { |
embeddedartists | 27:aa2fd412f1d3 | 45 | host->returnSafeMem((uint8_t*)cbw); |
embeddedartists | 27:aa2fd412f1d3 | 46 | cbw = NULL; |
embeddedartists | 27:aa2fd412f1d3 | 47 | } |
embeddedartists | 27:aa2fd412f1d3 | 48 | if (csw != NULL) { |
embeddedartists | 27:aa2fd412f1d3 | 49 | host->returnSafeMem((uint8_t*)csw); |
embeddedartists | 27:aa2fd412f1d3 | 50 | csw = NULL; |
embeddedartists | 27:aa2fd412f1d3 | 51 | } |
mbed_official | 0:a554658735bf | 52 | } |
mbed_official | 0:a554658735bf | 53 | |
mbed_official | 0:a554658735bf | 54 | void USBHostMSD::init() { |
mbed_official | 0:a554658735bf | 55 | dev_connected = false; |
mbed_official | 0:a554658735bf | 56 | dev = NULL; |
mbed_official | 0:a554658735bf | 57 | bulk_in = NULL; |
mbed_official | 0:a554658735bf | 58 | bulk_out = NULL; |
mbed_official | 0:a554658735bf | 59 | blockSize = 0; |
mbed_official | 0:a554658735bf | 60 | blockCount = 0; |
mbed_official | 0:a554658735bf | 61 | msd_intf = -1; |
mbed_official | 0:a554658735bf | 62 | msd_device_found = false; |
mbed_official | 0:a554658735bf | 63 | disk_init = false; |
samux | 4:b320d68e98e7 | 64 | nb_ep = 0; |
mbed_official | 0:a554658735bf | 65 | } |
mbed_official | 0:a554658735bf | 66 | |
mbed_official | 0:a554658735bf | 67 | |
mbed_official | 0:a554658735bf | 68 | bool USBHostMSD::connected() |
mbed_official | 0:a554658735bf | 69 | { |
mbed_official | 0:a554658735bf | 70 | return dev_connected; |
mbed_official | 0:a554658735bf | 71 | } |
mbed_official | 0:a554658735bf | 72 | |
mbed_official | 0:a554658735bf | 73 | bool USBHostMSD::connect() |
mbed_official | 0:a554658735bf | 74 | { |
mbed_official | 0:a554658735bf | 75 | |
mbed_official | 0:a554658735bf | 76 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 77 | return true; |
mbed_official | 0:a554658735bf | 78 | } |
mbed_official | 0:a554658735bf | 79 | |
samux | 5:e48791a1ef18 | 80 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 81 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 24:868cbfe611a7 | 82 | |
samux | 4:b320d68e98e7 | 83 | USB_DBG("Trying to connect MSD device\r\n"); |
mbed_official | 24:868cbfe611a7 | 84 | |
mbed_official | 0:a554658735bf | 85 | if(host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 86 | break; |
mbed_official | 0:a554658735bf | 87 | |
mbed_official | 0:a554658735bf | 88 | if (msd_device_found) { |
mbed_official | 0:a554658735bf | 89 | bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 90 | bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); |
mbed_official | 24:868cbfe611a7 | 91 | |
mbed_official | 0:a554658735bf | 92 | if (!bulk_in || !bulk_out) |
samux | 4:b320d68e98e7 | 93 | continue; |
mbed_official | 24:868cbfe611a7 | 94 | |
samux | 4:b320d68e98e7 | 95 | USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); |
samux | 4:b320d68e98e7 | 96 | dev->setName("MSD", msd_intf); |
mbed_official | 0:a554658735bf | 97 | host->registerDriver(dev, msd_intf, this, &USBHostMSD::init); |
mbed_official | 0:a554658735bf | 98 | |
mbed_official | 0:a554658735bf | 99 | dev_connected = true; |
mbed_official | 0:a554658735bf | 100 | return true; |
mbed_official | 0:a554658735bf | 101 | } |
mbed_official | 0:a554658735bf | 102 | } //if() |
mbed_official | 0:a554658735bf | 103 | } //for() |
samux | 4:b320d68e98e7 | 104 | init(); |
mbed_official | 0:a554658735bf | 105 | return false; |
mbed_official | 0:a554658735bf | 106 | } |
mbed_official | 0:a554658735bf | 107 | |
mbed_official | 0:a554658735bf | 108 | /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 109 | { |
mbed_official | 0:a554658735bf | 110 | // we don't check VID/PID for MSD driver |
mbed_official | 0:a554658735bf | 111 | } |
mbed_official | 0:a554658735bf | 112 | |
mbed_official | 0:a554658735bf | 113 | /*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 | 114 | { |
mbed_official | 0:a554658735bf | 115 | if ((msd_intf == -1) && |
mbed_official | 0:a554658735bf | 116 | (intf_class == MSD_CLASS) && |
mbed_official | 0:a554658735bf | 117 | (intf_subclass == 0x06) && |
mbed_official | 0:a554658735bf | 118 | (intf_protocol == 0x50)) { |
mbed_official | 0:a554658735bf | 119 | msd_intf = intf_nb; |
mbed_official | 0:a554658735bf | 120 | return true; |
mbed_official | 0:a554658735bf | 121 | } |
mbed_official | 0:a554658735bf | 122 | return false; |
mbed_official | 0:a554658735bf | 123 | } |
mbed_official | 0:a554658735bf | 124 | |
mbed_official | 0:a554658735bf | 125 | /*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 | 126 | { |
mbed_official | 0:a554658735bf | 127 | if (intf_nb == msd_intf) { |
mbed_official | 0:a554658735bf | 128 | if (type == BULK_ENDPOINT) { |
samux | 4:b320d68e98e7 | 129 | nb_ep++; |
samux | 4:b320d68e98e7 | 130 | if (nb_ep == 2) |
samux | 4:b320d68e98e7 | 131 | msd_device_found = true; |
mbed_official | 0:a554658735bf | 132 | return true; |
mbed_official | 0:a554658735bf | 133 | } |
mbed_official | 0:a554658735bf | 134 | } |
mbed_official | 0:a554658735bf | 135 | return false; |
mbed_official | 0:a554658735bf | 136 | } |
mbed_official | 0:a554658735bf | 137 | |
mbed_official | 0:a554658735bf | 138 | |
mbed_official | 0:a554658735bf | 139 | int USBHostMSD::testUnitReady() { |
samux | 4:b320d68e98e7 | 140 | USB_DBG("Test unit ready"); |
mbed_official | 0:a554658735bf | 141 | return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); |
mbed_official | 0:a554658735bf | 142 | } |
mbed_official | 0:a554658735bf | 143 | |
samux | 4:b320d68e98e7 | 144 | |
mbed_official | 0:a554658735bf | 145 | int USBHostMSD::readCapacity() { |
samux | 4:b320d68e98e7 | 146 | USB_DBG("Read capacity"); |
mbed_official | 0:a554658735bf | 147 | uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; |
embeddedartists | 27:aa2fd412f1d3 | 148 | uint8_t* result = host->getSafeMem(8); |
mbed_official | 0:a554658735bf | 149 | int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); |
mbed_official | 0:a554658735bf | 150 | if (status == 0) { |
mbed_official | 0:a554658735bf | 151 | blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; |
mbed_official | 0:a554658735bf | 152 | blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; |
samux | 4:b320d68e98e7 | 153 | USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); |
mbed_official | 0:a554658735bf | 154 | } |
embeddedartists | 27:aa2fd412f1d3 | 155 | host->returnSafeMem(result); |
mbed_official | 0:a554658735bf | 156 | return status; |
mbed_official | 0:a554658735bf | 157 | } |
mbed_official | 0:a554658735bf | 158 | |
mbed_official | 0:a554658735bf | 159 | |
mbed_official | 0:a554658735bf | 160 | int USBHostMSD::SCSIRequestSense() { |
samux | 4:b320d68e98e7 | 161 | USB_DBG("Request sense"); |
samux | 4:b320d68e98e7 | 162 | uint8_t cmd[6] = {0x03,0,0,0,18,0}; |
embeddedartists | 27:aa2fd412f1d3 | 163 | uint8_t* result = host->getSafeMem(18); |
samux | 4:b320d68e98e7 | 164 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); |
embeddedartists | 27:aa2fd412f1d3 | 165 | host->returnSafeMem(result); |
mbed_official | 0:a554658735bf | 166 | return status; |
mbed_official | 0:a554658735bf | 167 | } |
mbed_official | 0:a554658735bf | 168 | |
mbed_official | 0:a554658735bf | 169 | |
mbed_official | 0:a554658735bf | 170 | int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) { |
samux | 4:b320d68e98e7 | 171 | USB_DBG("Inquiry"); |
mbed_official | 0:a554658735bf | 172 | uint8_t evpd = (page_code == 0) ? 0 : 1; |
mbed_official | 13:b58a2204422f | 173 | uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0}; |
embeddedartists | 27:aa2fd412f1d3 | 174 | uint8_t* result = host->getSafeMem(36); |
mbed_official | 0:a554658735bf | 175 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); |
mbed_official | 0:a554658735bf | 176 | if (status == 0) { |
samux | 4:b320d68e98e7 | 177 | char vid_pid[17]; |
mbed_official | 0:a554658735bf | 178 | memcpy(vid_pid, &result[8], 8); |
mbed_official | 0:a554658735bf | 179 | vid_pid[8] = 0; |
mbed_official | 0:a554658735bf | 180 | USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 181 | |
samux | 4:b320d68e98e7 | 182 | memcpy(vid_pid, &result[16], 16); |
samux | 4:b320d68e98e7 | 183 | vid_pid[16] = 0; |
samux | 4:b320d68e98e7 | 184 | USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 185 | |
mbed_official | 0:a554658735bf | 186 | memcpy(vid_pid, &result[32], 4); |
mbed_official | 0:a554658735bf | 187 | vid_pid[4] = 0; |
mbed_official | 0:a554658735bf | 188 | USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 189 | } |
embeddedartists | 27:aa2fd412f1d3 | 190 | host->returnSafeMem(result); |
mbed_official | 0:a554658735bf | 191 | return status; |
mbed_official | 0:a554658735bf | 192 | } |
mbed_official | 0:a554658735bf | 193 | |
mbed_official | 0:a554658735bf | 194 | int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) { |
mbed_official | 0:a554658735bf | 195 | // if ep stalled: send clear feature |
mbed_official | 0:a554658735bf | 196 | if (res == USB_TYPE_STALL_ERROR) { |
mbed_official | 0:a554658735bf | 197 | res = host->controlWrite( dev, |
mbed_official | 0:a554658735bf | 198 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
mbed_official | 0:a554658735bf | 199 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 200 | 0, ep->getAddress(), NULL, 0); |
mbed_official | 0:a554658735bf | 201 | // set state to IDLE if clear feature successful |
mbed_official | 0:a554658735bf | 202 | if (res == USB_TYPE_OK) { |
mbed_official | 0:a554658735bf | 203 | ep->setState(USB_TYPE_IDLE); |
mbed_official | 0:a554658735bf | 204 | } |
mbed_official | 0:a554658735bf | 205 | } |
mbed_official | 0:a554658735bf | 206 | |
mbed_official | 0:a554658735bf | 207 | if (res != USB_TYPE_OK) |
mbed_official | 0:a554658735bf | 208 | return -1; |
mbed_official | 0:a554658735bf | 209 | |
mbed_official | 0:a554658735bf | 210 | return 0; |
mbed_official | 0:a554658735bf | 211 | } |
mbed_official | 0:a554658735bf | 212 | |
mbed_official | 0:a554658735bf | 213 | |
mbed_official | 0:a554658735bf | 214 | int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) { |
mbed_official | 0:a554658735bf | 215 | |
mbed_official | 0:a554658735bf | 216 | int res = 0; |
mbed_official | 0:a554658735bf | 217 | |
embeddedartists | 27:aa2fd412f1d3 | 218 | cbw->Signature = CBW_SIGNATURE; |
embeddedartists | 27:aa2fd412f1d3 | 219 | cbw->Tag = 0; |
embeddedartists | 27:aa2fd412f1d3 | 220 | cbw->DataLength = transfer_len; |
embeddedartists | 27:aa2fd412f1d3 | 221 | cbw->Flags = flags; |
embeddedartists | 27:aa2fd412f1d3 | 222 | cbw->LUN = 0; |
embeddedartists | 27:aa2fd412f1d3 | 223 | cbw->CBLength = cmd_len; |
embeddedartists | 27:aa2fd412f1d3 | 224 | memset(cbw->CB,0,sizeof(cbw->CB)); |
mbed_official | 0:a554658735bf | 225 | if (cmd) { |
embeddedartists | 27:aa2fd412f1d3 | 226 | memcpy(cbw->CB,cmd,cmd_len); |
mbed_official | 0:a554658735bf | 227 | } |
mbed_official | 0:a554658735bf | 228 | |
mbed_official | 0:a554658735bf | 229 | // send the cbw |
samux | 4:b320d68e98e7 | 230 | USB_DBG("Send CBW"); |
embeddedartists | 27:aa2fd412f1d3 | 231 | res = host->bulkWrite(dev, bulk_out,(uint8_t *)cbw, 31); |
mbed_official | 0:a554658735bf | 232 | if (checkResult(res, bulk_out)) |
mbed_official | 0:a554658735bf | 233 | return -1; |
mbed_official | 0:a554658735bf | 234 | |
mbed_official | 0:a554658735bf | 235 | // data stage if needed |
mbed_official | 0:a554658735bf | 236 | if (data) { |
samux | 4:b320d68e98e7 | 237 | USB_DBG("data stage"); |
mbed_official | 0:a554658735bf | 238 | if (flags == HOST_TO_DEVICE) { |
mbed_official | 24:868cbfe611a7 | 239 | |
mbed_official | 0:a554658735bf | 240 | res = host->bulkWrite(dev, bulk_out, data, transfer_len); |
mbed_official | 0:a554658735bf | 241 | if (checkResult(res, bulk_out)) |
mbed_official | 0:a554658735bf | 242 | return -1; |
mbed_official | 0:a554658735bf | 243 | |
mbed_official | 0:a554658735bf | 244 | } else if (flags == DEVICE_TO_HOST) { |
mbed_official | 0:a554658735bf | 245 | |
mbed_official | 0:a554658735bf | 246 | res = host->bulkRead(dev, bulk_in, data, transfer_len); |
mbed_official | 0:a554658735bf | 247 | if (checkResult(res, bulk_in)) |
mbed_official | 0:a554658735bf | 248 | return -1; |
mbed_official | 0:a554658735bf | 249 | } |
mbed_official | 0:a554658735bf | 250 | } |
mbed_official | 0:a554658735bf | 251 | |
mbed_official | 0:a554658735bf | 252 | // status stage |
embeddedartists | 27:aa2fd412f1d3 | 253 | csw->Signature = 0; |
samux | 4:b320d68e98e7 | 254 | USB_DBG("Read CSW"); |
embeddedartists | 27:aa2fd412f1d3 | 255 | res = host->bulkRead(dev, bulk_in,(uint8_t *)csw, 13); |
mbed_official | 0:a554658735bf | 256 | if (checkResult(res, bulk_in)) |
mbed_official | 0:a554658735bf | 257 | return -1; |
mbed_official | 0:a554658735bf | 258 | |
embeddedartists | 27:aa2fd412f1d3 | 259 | if (csw->Signature != CSW_SIGNATURE) { |
mbed_official | 0:a554658735bf | 260 | return -1; |
mbed_official | 0:a554658735bf | 261 | } |
mbed_official | 24:868cbfe611a7 | 262 | |
embeddedartists | 27:aa2fd412f1d3 | 263 | USB_DBG("recv csw: status: %d", csw->Status); |
mbed_official | 0:a554658735bf | 264 | |
mbed_official | 0:a554658735bf | 265 | // ModeSense? |
embeddedartists | 27:aa2fd412f1d3 | 266 | if ((csw->Status == 1) && (cmd[0] != 0x03)) { |
samux | 4:b320d68e98e7 | 267 | USB_DBG("request mode sense"); |
mbed_official | 0:a554658735bf | 268 | return SCSIRequestSense(); |
samux | 4:b320d68e98e7 | 269 | } |
mbed_official | 24:868cbfe611a7 | 270 | |
samux | 4:b320d68e98e7 | 271 | // perform reset recovery |
embeddedartists | 27:aa2fd412f1d3 | 272 | if ((csw->Status == 2) && (cmd[0] != 0x03)) { |
mbed_official | 24:868cbfe611a7 | 273 | |
samux | 4:b320d68e98e7 | 274 | // send Bulk-Only Mass Storage Reset request |
samux | 4:b320d68e98e7 | 275 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 276 | USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, |
samux | 4:b320d68e98e7 | 277 | BO_MASS_STORAGE_RESET, |
samux | 4:b320d68e98e7 | 278 | 0, msd_intf, NULL, 0); |
mbed_official | 24:868cbfe611a7 | 279 | |
samux | 4:b320d68e98e7 | 280 | // unstall both endpoints |
samux | 4:b320d68e98e7 | 281 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 282 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
samux | 4:b320d68e98e7 | 283 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 284 | 0, bulk_in->getAddress(), NULL, 0); |
mbed_official | 24:868cbfe611a7 | 285 | |
samux | 4:b320d68e98e7 | 286 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 287 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
samux | 4:b320d68e98e7 | 288 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 289 | 0, bulk_out->getAddress(), NULL, 0); |
mbed_official | 24:868cbfe611a7 | 290 | |
samux | 4:b320d68e98e7 | 291 | } |
mbed_official | 0:a554658735bf | 292 | |
embeddedartists | 27:aa2fd412f1d3 | 293 | return csw->Status; |
mbed_official | 0:a554658735bf | 294 | } |
mbed_official | 0:a554658735bf | 295 | |
mbed_official | 0:a554658735bf | 296 | |
mbed_official | 0:a554658735bf | 297 | int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) { |
mbed_official | 0:a554658735bf | 298 | uint8_t cmd[10]; |
mbed_official | 0:a554658735bf | 299 | memset(cmd,0,10); |
mbed_official | 0:a554658735bf | 300 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
mbed_official | 0:a554658735bf | 301 | |
mbed_official | 0:a554658735bf | 302 | cmd[2] = (block >> 24) & 0xff; |
mbed_official | 0:a554658735bf | 303 | cmd[3] = (block >> 16) & 0xff; |
mbed_official | 0:a554658735bf | 304 | cmd[4] = (block >> 8) & 0xff; |
mbed_official | 0:a554658735bf | 305 | cmd[5] = block & 0xff; |
mbed_official | 0:a554658735bf | 306 | |
mbed_official | 0:a554658735bf | 307 | cmd[7] = (nbBlock >> 8) & 0xff; |
mbed_official | 0:a554658735bf | 308 | cmd[8] = nbBlock & 0xff; |
mbed_official | 0:a554658735bf | 309 | |
mbed_official | 0:a554658735bf | 310 | return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); |
mbed_official | 0:a554658735bf | 311 | } |
mbed_official | 0:a554658735bf | 312 | |
samux | 4:b320d68e98e7 | 313 | int USBHostMSD::getMaxLun() { |
embeddedartists | 27:aa2fd412f1d3 | 314 | uint8_t res; |
embeddedartists | 27:aa2fd412f1d3 | 315 | uint8_t* buf = host->getSafeMem(1); |
samux | 4:b320d68e98e7 | 316 | res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
samux | 4:b320d68e98e7 | 317 | 0xfe, 0, msd_intf, buf, 1); |
samux | 4:b320d68e98e7 | 318 | USB_DBG("max lun: %d", buf[0]); |
embeddedartists | 27:aa2fd412f1d3 | 319 | host->returnSafeMem(buf); |
samux | 4:b320d68e98e7 | 320 | return res; |
samux | 4:b320d68e98e7 | 321 | } |
mbed_official | 0:a554658735bf | 322 | |
mbed_official | 0:a554658735bf | 323 | int USBHostMSD::disk_initialize() { |
mbed_official | 0:a554658735bf | 324 | USB_DBG("FILESYSTEM: init"); |
embeddedartists | 32:f2d129436056 | 325 | uint16_t i, timeout = 10; |
embeddedartists | 29:dc8179214fd7 | 326 | |
embeddedartists | 29:dc8179214fd7 | 327 | if (!connected()) { |
embeddedartists | 29:dc8179214fd7 | 328 | // not connected |
embeddedartists | 29:dc8179214fd7 | 329 | disk_init = 0; |
embeddedartists | 29:dc8179214fd7 | 330 | return STA_NODISK | STA_NOINIT; |
embeddedartists | 29:dc8179214fd7 | 331 | } |
mbed_official | 24:868cbfe611a7 | 332 | |
embeddedartists | 29:dc8179214fd7 | 333 | if (disk_init) { |
embeddedartists | 29:dc8179214fd7 | 334 | // already initialized |
embeddedartists | 29:dc8179214fd7 | 335 | return 0; |
embeddedartists | 29:dc8179214fd7 | 336 | } |
embeddedartists | 29:dc8179214fd7 | 337 | |
samux | 4:b320d68e98e7 | 338 | getMaxLun(); |
mbed_official | 24:868cbfe611a7 | 339 | |
mbed_official | 0:a554658735bf | 340 | for (i = 0; i < timeout; i++) { |
embeddedartists | 32:f2d129436056 | 341 | ThisThread::sleep_for(100); |
mbed_official | 0:a554658735bf | 342 | if (!testUnitReady()) |
mbed_official | 0:a554658735bf | 343 | break; |
mbed_official | 0:a554658735bf | 344 | } |
mbed_official | 24:868cbfe611a7 | 345 | |
mbed_official | 0:a554658735bf | 346 | if (i == timeout) { |
mbed_official | 0:a554658735bf | 347 | disk_init = false; |
mbed_official | 0:a554658735bf | 348 | return -1; |
mbed_official | 0:a554658735bf | 349 | } |
mbed_official | 24:868cbfe611a7 | 350 | |
mbed_official | 0:a554658735bf | 351 | inquiry(0, 0); |
mbed_official | 0:a554658735bf | 352 | disk_init = 1; |
mbed_official | 0:a554658735bf | 353 | return readCapacity(); |
mbed_official | 0:a554658735bf | 354 | } |
mbed_official | 0:a554658735bf | 355 | |
mbed_official | 26:607951c26872 | 356 | int USBHostMSD::disk_write(const uint8_t* buffer, uint64_t block_number, uint8_t count) { |
mbed_official | 26:607951c26872 | 357 | USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count); |
mbed_official | 0:a554658735bf | 358 | if (!disk_init) { |
mbed_official | 0:a554658735bf | 359 | disk_initialize(); |
mbed_official | 0:a554658735bf | 360 | } |
mbed_official | 0:a554658735bf | 361 | if (!disk_init) |
mbed_official | 0:a554658735bf | 362 | return -1; |
embeddedartists | 27:aa2fd412f1d3 | 363 | uint8_t* safe = host->getSafeMem(512); |
mbed_official | 26:607951c26872 | 364 | for (uint64_t b = block_number; b < block_number + count; b++) { |
embeddedartists | 27:aa2fd412f1d3 | 365 | memcpy(safe, buffer, 512); |
embeddedartists | 27:aa2fd412f1d3 | 366 | if (dataTransfer((uint8_t*)safe, b, 1, HOST_TO_DEVICE)) { |
embeddedartists | 27:aa2fd412f1d3 | 367 | host->returnSafeMem(safe); |
mbed_official | 26:607951c26872 | 368 | return -1; |
embeddedartists | 27:aa2fd412f1d3 | 369 | } |
mbed_official | 26:607951c26872 | 370 | buffer += 512; |
mbed_official | 26:607951c26872 | 371 | } |
embeddedartists | 27:aa2fd412f1d3 | 372 | host->returnSafeMem(safe); |
mbed_official | 26:607951c26872 | 373 | return 0; |
mbed_official | 0:a554658735bf | 374 | } |
mbed_official | 0:a554658735bf | 375 | |
mbed_official | 26:607951c26872 | 376 | int USBHostMSD::disk_read(uint8_t* buffer, uint64_t block_number, uint8_t count) { |
mbed_official | 26:607951c26872 | 377 | USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count); |
mbed_official | 0:a554658735bf | 378 | if (!disk_init) { |
mbed_official | 0:a554658735bf | 379 | disk_initialize(); |
mbed_official | 0:a554658735bf | 380 | } |
mbed_official | 0:a554658735bf | 381 | if (!disk_init) |
mbed_official | 0:a554658735bf | 382 | return -1; |
embeddedartists | 27:aa2fd412f1d3 | 383 | uint8_t* safe = host->getSafeMem(512); |
mbed_official | 26:607951c26872 | 384 | for (uint64_t b = block_number; b < block_number + count; b++) { |
embeddedartists | 27:aa2fd412f1d3 | 385 | if (dataTransfer((uint8_t*)safe, b, 1, DEVICE_TO_HOST)) { |
embeddedartists | 27:aa2fd412f1d3 | 386 | host->returnSafeMem(safe); |
mbed_official | 26:607951c26872 | 387 | return -1; |
embeddedartists | 27:aa2fd412f1d3 | 388 | } |
embeddedartists | 27:aa2fd412f1d3 | 389 | memcpy(buffer, safe, 512); |
mbed_official | 26:607951c26872 | 390 | buffer += 512; |
mbed_official | 26:607951c26872 | 391 | } |
embeddedartists | 27:aa2fd412f1d3 | 392 | host->returnSafeMem(safe); |
mbed_official | 26:607951c26872 | 393 | return 0; |
mbed_official | 0:a554658735bf | 394 | } |
mbed_official | 0:a554658735bf | 395 | |
mbed_official | 0:a554658735bf | 396 | uint64_t USBHostMSD::disk_sectors() { |
mbed_official | 0:a554658735bf | 397 | USB_DBG("FILESYSTEM: sectors"); |
mbed_official | 0:a554658735bf | 398 | if (!disk_init) { |
mbed_official | 0:a554658735bf | 399 | disk_initialize(); |
mbed_official | 0:a554658735bf | 400 | } |
mbed_official | 0:a554658735bf | 401 | if (!disk_init) |
mbed_official | 0:a554658735bf | 402 | return 0; |
mbed_official | 0:a554658735bf | 403 | return blockCount; |
mbed_official | 0:a554658735bf | 404 | } |
mbed_official | 0:a554658735bf | 405 | |
mbed_official | 0:a554658735bf | 406 | #endif |