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