Opencv 3.1 project on GR-PEACH board
Fork of gr-peach-opencv-project by
USBHost_custom/USBHostMSD/USBHostMSD.cpp@170:54ff26da7eb6, 2017-07-04 (annotated)
- Committer:
- thedo
- Date:
- Tue Jul 04 06:23:13 2017 +0000
- Revision:
- 170:54ff26da7eb6
- Parent:
- 166:3a9487d57a5c
project opencv 3.1 on GR PEACH board, no use SD card.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thedo | 166:3a9487d57a5c | 1 | /* mbed USBHost Library |
thedo | 166:3a9487d57a5c | 2 | * Copyright (c) 2006-2013 ARM Limited |
thedo | 166:3a9487d57a5c | 3 | * |
thedo | 166:3a9487d57a5c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
thedo | 166:3a9487d57a5c | 5 | * you may not use this file except in compliance with the License. |
thedo | 166:3a9487d57a5c | 6 | * You may obtain a copy of the License at |
thedo | 166:3a9487d57a5c | 7 | * |
thedo | 166:3a9487d57a5c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
thedo | 166:3a9487d57a5c | 9 | * |
thedo | 166:3a9487d57a5c | 10 | * Unless required by applicable law or agreed to in writing, software |
thedo | 166:3a9487d57a5c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
thedo | 166:3a9487d57a5c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
thedo | 166:3a9487d57a5c | 13 | * See the License for the specific language governing permissions and |
thedo | 166:3a9487d57a5c | 14 | * limitations under the License. |
thedo | 166:3a9487d57a5c | 15 | */ |
thedo | 166:3a9487d57a5c | 16 | |
thedo | 166:3a9487d57a5c | 17 | #include "USBHostMSD.h" |
thedo | 166:3a9487d57a5c | 18 | |
thedo | 166:3a9487d57a5c | 19 | #if USBHOST_MSD |
thedo | 166:3a9487d57a5c | 20 | |
thedo | 166:3a9487d57a5c | 21 | #include "dbg.h" |
thedo | 166:3a9487d57a5c | 22 | |
thedo | 166:3a9487d57a5c | 23 | #define CBW_SIGNATURE 0x43425355 |
thedo | 166:3a9487d57a5c | 24 | #define CSW_SIGNATURE 0x53425355 |
thedo | 166:3a9487d57a5c | 25 | |
thedo | 166:3a9487d57a5c | 26 | #define DEVICE_TO_HOST 0x80 |
thedo | 166:3a9487d57a5c | 27 | #define HOST_TO_DEVICE 0x00 |
thedo | 166:3a9487d57a5c | 28 | |
thedo | 166:3a9487d57a5c | 29 | #define GET_MAX_LUN (0xFE) |
thedo | 166:3a9487d57a5c | 30 | #define BO_MASS_STORAGE_RESET (0xFF) |
thedo | 166:3a9487d57a5c | 31 | |
thedo | 166:3a9487d57a5c | 32 | USBHostMSD::USBHostMSD() |
thedo | 166:3a9487d57a5c | 33 | { |
thedo | 166:3a9487d57a5c | 34 | host = USBHost::getHostInst(); |
thedo | 166:3a9487d57a5c | 35 | msd_init(); |
thedo | 166:3a9487d57a5c | 36 | } |
thedo | 166:3a9487d57a5c | 37 | |
thedo | 166:3a9487d57a5c | 38 | USBHostMSD::~USBHostMSD() |
thedo | 166:3a9487d57a5c | 39 | { |
thedo | 166:3a9487d57a5c | 40 | if (_is_initialized) { |
thedo | 166:3a9487d57a5c | 41 | deinit(); |
thedo | 166:3a9487d57a5c | 42 | } |
thedo | 166:3a9487d57a5c | 43 | } |
thedo | 166:3a9487d57a5c | 44 | |
thedo | 166:3a9487d57a5c | 45 | void USBHostMSD::msd_init() { |
thedo | 166:3a9487d57a5c | 46 | dev_connected = false; |
thedo | 166:3a9487d57a5c | 47 | dev = NULL; |
thedo | 166:3a9487d57a5c | 48 | bulk_in = NULL; |
thedo | 166:3a9487d57a5c | 49 | bulk_out = NULL; |
thedo | 166:3a9487d57a5c | 50 | dev_connected = false; |
thedo | 166:3a9487d57a5c | 51 | blockSize = 0; |
thedo | 166:3a9487d57a5c | 52 | blockCount = 0; |
thedo | 166:3a9487d57a5c | 53 | msd_intf = -1; |
thedo | 166:3a9487d57a5c | 54 | msd_device_found = false; |
thedo | 166:3a9487d57a5c | 55 | _is_initialized = false; |
thedo | 166:3a9487d57a5c | 56 | dev_connected = false; |
thedo | 166:3a9487d57a5c | 57 | nb_ep = 0; |
thedo | 166:3a9487d57a5c | 58 | } |
thedo | 166:3a9487d57a5c | 59 | |
thedo | 166:3a9487d57a5c | 60 | |
thedo | 166:3a9487d57a5c | 61 | bool USBHostMSD::connected() |
thedo | 166:3a9487d57a5c | 62 | { |
thedo | 166:3a9487d57a5c | 63 | return dev_connected; |
thedo | 166:3a9487d57a5c | 64 | } |
thedo | 166:3a9487d57a5c | 65 | |
thedo | 166:3a9487d57a5c | 66 | bool USBHostMSD::connect() |
thedo | 166:3a9487d57a5c | 67 | { |
thedo | 166:3a9487d57a5c | 68 | |
thedo | 166:3a9487d57a5c | 69 | if (dev_connected) { |
thedo | 166:3a9487d57a5c | 70 | return true; |
thedo | 166:3a9487d57a5c | 71 | } |
thedo | 166:3a9487d57a5c | 72 | |
thedo | 166:3a9487d57a5c | 73 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
thedo | 166:3a9487d57a5c | 74 | if ((dev = host->getDevice(i)) != NULL) { |
thedo | 166:3a9487d57a5c | 75 | |
thedo | 166:3a9487d57a5c | 76 | USB_DBG("Trying to connect MSD device\r\n"); |
thedo | 166:3a9487d57a5c | 77 | |
thedo | 166:3a9487d57a5c | 78 | if(host->enumerate(dev, this)) |
thedo | 166:3a9487d57a5c | 79 | break; |
thedo | 166:3a9487d57a5c | 80 | |
thedo | 166:3a9487d57a5c | 81 | if (msd_device_found) { |
thedo | 166:3a9487d57a5c | 82 | bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); |
thedo | 166:3a9487d57a5c | 83 | bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); |
thedo | 166:3a9487d57a5c | 84 | |
thedo | 166:3a9487d57a5c | 85 | if (!bulk_in || !bulk_out) |
thedo | 166:3a9487d57a5c | 86 | continue; |
thedo | 166:3a9487d57a5c | 87 | |
thedo | 166:3a9487d57a5c | 88 | USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); |
thedo | 166:3a9487d57a5c | 89 | dev->setName("MSD", msd_intf); |
thedo | 166:3a9487d57a5c | 90 | host->registerDriver(dev, msd_intf, this, &USBHostMSD::msd_init); |
thedo | 166:3a9487d57a5c | 91 | |
thedo | 166:3a9487d57a5c | 92 | dev_connected = true; |
thedo | 166:3a9487d57a5c | 93 | return true; |
thedo | 166:3a9487d57a5c | 94 | } |
thedo | 166:3a9487d57a5c | 95 | } //if() |
thedo | 166:3a9487d57a5c | 96 | } //for() |
thedo | 166:3a9487d57a5c | 97 | msd_init(); |
thedo | 166:3a9487d57a5c | 98 | return false; |
thedo | 166:3a9487d57a5c | 99 | } |
thedo | 166:3a9487d57a5c | 100 | |
thedo | 166:3a9487d57a5c | 101 | /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) |
thedo | 166:3a9487d57a5c | 102 | { |
thedo | 166:3a9487d57a5c | 103 | // we don't check VID/PID for MSD driver |
thedo | 166:3a9487d57a5c | 104 | } |
thedo | 166:3a9487d57a5c | 105 | |
thedo | 166:3a9487d57a5c | 106 | /*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 |
thedo | 166:3a9487d57a5c | 107 | { |
thedo | 166:3a9487d57a5c | 108 | if ((msd_intf == -1) && |
thedo | 166:3a9487d57a5c | 109 | (intf_class == MSD_CLASS) && |
thedo | 166:3a9487d57a5c | 110 | (intf_subclass == 0x06) && |
thedo | 166:3a9487d57a5c | 111 | (intf_protocol == 0x50)) { |
thedo | 166:3a9487d57a5c | 112 | msd_intf = intf_nb; |
thedo | 166:3a9487d57a5c | 113 | return true; |
thedo | 166:3a9487d57a5c | 114 | } |
thedo | 166:3a9487d57a5c | 115 | return false; |
thedo | 166:3a9487d57a5c | 116 | } |
thedo | 166:3a9487d57a5c | 117 | |
thedo | 166:3a9487d57a5c | 118 | /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
thedo | 166:3a9487d57a5c | 119 | { |
thedo | 166:3a9487d57a5c | 120 | if (intf_nb == msd_intf) { |
thedo | 166:3a9487d57a5c | 121 | if (type == BULK_ENDPOINT) { |
thedo | 166:3a9487d57a5c | 122 | nb_ep++; |
thedo | 166:3a9487d57a5c | 123 | if (nb_ep == 2) |
thedo | 166:3a9487d57a5c | 124 | msd_device_found = true; |
thedo | 166:3a9487d57a5c | 125 | return true; |
thedo | 166:3a9487d57a5c | 126 | } |
thedo | 166:3a9487d57a5c | 127 | } |
thedo | 166:3a9487d57a5c | 128 | return false; |
thedo | 166:3a9487d57a5c | 129 | } |
thedo | 166:3a9487d57a5c | 130 | |
thedo | 166:3a9487d57a5c | 131 | |
thedo | 166:3a9487d57a5c | 132 | int USBHostMSD::testUnitReady() { |
thedo | 166:3a9487d57a5c | 133 | USB_DBG("Test unit ready"); |
thedo | 166:3a9487d57a5c | 134 | return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); |
thedo | 166:3a9487d57a5c | 135 | } |
thedo | 166:3a9487d57a5c | 136 | |
thedo | 166:3a9487d57a5c | 137 | |
thedo | 166:3a9487d57a5c | 138 | int USBHostMSD::readCapacity() { |
thedo | 166:3a9487d57a5c | 139 | USB_DBG("Read capacity"); |
thedo | 166:3a9487d57a5c | 140 | uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; |
thedo | 166:3a9487d57a5c | 141 | uint8_t result[8]; |
thedo | 166:3a9487d57a5c | 142 | int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); |
thedo | 166:3a9487d57a5c | 143 | if (status == 0) { |
thedo | 166:3a9487d57a5c | 144 | blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; |
thedo | 166:3a9487d57a5c | 145 | blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; |
thedo | 166:3a9487d57a5c | 146 | USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); |
thedo | 166:3a9487d57a5c | 147 | } |
thedo | 166:3a9487d57a5c | 148 | return status; |
thedo | 166:3a9487d57a5c | 149 | } |
thedo | 166:3a9487d57a5c | 150 | |
thedo | 166:3a9487d57a5c | 151 | |
thedo | 166:3a9487d57a5c | 152 | int USBHostMSD::SCSIRequestSense() { |
thedo | 166:3a9487d57a5c | 153 | USB_DBG("Request sense"); |
thedo | 166:3a9487d57a5c | 154 | uint8_t cmd[6] = {0x03,0,0,0,18,0}; |
thedo | 166:3a9487d57a5c | 155 | uint8_t result[18]; |
thedo | 166:3a9487d57a5c | 156 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); |
thedo | 166:3a9487d57a5c | 157 | return status; |
thedo | 166:3a9487d57a5c | 158 | } |
thedo | 166:3a9487d57a5c | 159 | |
thedo | 166:3a9487d57a5c | 160 | |
thedo | 166:3a9487d57a5c | 161 | int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) { |
thedo | 166:3a9487d57a5c | 162 | USB_DBG("Inquiry"); |
thedo | 166:3a9487d57a5c | 163 | uint8_t evpd = (page_code == 0) ? 0 : 1; |
thedo | 166:3a9487d57a5c | 164 | uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0}; |
thedo | 166:3a9487d57a5c | 165 | uint8_t result[36]; |
thedo | 166:3a9487d57a5c | 166 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); |
thedo | 166:3a9487d57a5c | 167 | if (status == 0) { |
thedo | 166:3a9487d57a5c | 168 | char vid_pid[17]; |
thedo | 166:3a9487d57a5c | 169 | memcpy(vid_pid, &result[8], 8); |
thedo | 166:3a9487d57a5c | 170 | vid_pid[8] = 0; |
thedo | 166:3a9487d57a5c | 171 | USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid); |
thedo | 166:3a9487d57a5c | 172 | |
thedo | 166:3a9487d57a5c | 173 | memcpy(vid_pid, &result[16], 16); |
thedo | 166:3a9487d57a5c | 174 | vid_pid[16] = 0; |
thedo | 166:3a9487d57a5c | 175 | USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid); |
thedo | 166:3a9487d57a5c | 176 | |
thedo | 166:3a9487d57a5c | 177 | memcpy(vid_pid, &result[32], 4); |
thedo | 166:3a9487d57a5c | 178 | vid_pid[4] = 0; |
thedo | 166:3a9487d57a5c | 179 | USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); |
thedo | 166:3a9487d57a5c | 180 | } |
thedo | 166:3a9487d57a5c | 181 | return status; |
thedo | 166:3a9487d57a5c | 182 | } |
thedo | 166:3a9487d57a5c | 183 | |
thedo | 166:3a9487d57a5c | 184 | int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) { |
thedo | 166:3a9487d57a5c | 185 | // if ep stalled: send clear feature |
thedo | 166:3a9487d57a5c | 186 | if (res == USB_TYPE_STALL_ERROR) { |
thedo | 166:3a9487d57a5c | 187 | res = host->controlWrite( dev, |
thedo | 166:3a9487d57a5c | 188 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
thedo | 166:3a9487d57a5c | 189 | CLEAR_FEATURE, |
thedo | 166:3a9487d57a5c | 190 | 0, ep->getAddress(), NULL, 0); |
thedo | 166:3a9487d57a5c | 191 | // set state to IDLE if clear feature successful |
thedo | 166:3a9487d57a5c | 192 | if (res == USB_TYPE_OK) { |
thedo | 166:3a9487d57a5c | 193 | ep->setState(USB_TYPE_IDLE); |
thedo | 166:3a9487d57a5c | 194 | } |
thedo | 166:3a9487d57a5c | 195 | } |
thedo | 166:3a9487d57a5c | 196 | |
thedo | 166:3a9487d57a5c | 197 | if (res != USB_TYPE_OK) |
thedo | 166:3a9487d57a5c | 198 | return -1; |
thedo | 166:3a9487d57a5c | 199 | |
thedo | 166:3a9487d57a5c | 200 | return 0; |
thedo | 166:3a9487d57a5c | 201 | } |
thedo | 166:3a9487d57a5c | 202 | |
thedo | 166:3a9487d57a5c | 203 | |
thedo | 166:3a9487d57a5c | 204 | int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) { |
thedo | 166:3a9487d57a5c | 205 | |
thedo | 166:3a9487d57a5c | 206 | int res = 0; |
thedo | 166:3a9487d57a5c | 207 | |
thedo | 166:3a9487d57a5c | 208 | cbw.Signature = CBW_SIGNATURE; |
thedo | 166:3a9487d57a5c | 209 | cbw.Tag = 0; |
thedo | 166:3a9487d57a5c | 210 | cbw.DataLength = transfer_len; |
thedo | 166:3a9487d57a5c | 211 | cbw.Flags = flags; |
thedo | 166:3a9487d57a5c | 212 | cbw.LUN = 0; |
thedo | 166:3a9487d57a5c | 213 | cbw.CBLength = cmd_len; |
thedo | 166:3a9487d57a5c | 214 | memset(cbw.CB,0,sizeof(cbw.CB)); |
thedo | 166:3a9487d57a5c | 215 | if (cmd) { |
thedo | 166:3a9487d57a5c | 216 | memcpy(cbw.CB,cmd,cmd_len); |
thedo | 166:3a9487d57a5c | 217 | } |
thedo | 166:3a9487d57a5c | 218 | |
thedo | 166:3a9487d57a5c | 219 | // send the cbw |
thedo | 166:3a9487d57a5c | 220 | USB_DBG("Send CBW"); |
thedo | 166:3a9487d57a5c | 221 | res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); |
thedo | 166:3a9487d57a5c | 222 | if (checkResult(res, bulk_out)) |
thedo | 166:3a9487d57a5c | 223 | return -1; |
thedo | 166:3a9487d57a5c | 224 | |
thedo | 166:3a9487d57a5c | 225 | // data stage if needed |
thedo | 166:3a9487d57a5c | 226 | if (data) { |
thedo | 166:3a9487d57a5c | 227 | USB_DBG("data stage"); |
thedo | 166:3a9487d57a5c | 228 | if (flags == HOST_TO_DEVICE) { |
thedo | 166:3a9487d57a5c | 229 | |
thedo | 166:3a9487d57a5c | 230 | res = host->bulkWrite(dev, bulk_out, data, transfer_len); |
thedo | 166:3a9487d57a5c | 231 | if (checkResult(res, bulk_out)) |
thedo | 166:3a9487d57a5c | 232 | return -1; |
thedo | 166:3a9487d57a5c | 233 | |
thedo | 166:3a9487d57a5c | 234 | } else if (flags == DEVICE_TO_HOST) { |
thedo | 166:3a9487d57a5c | 235 | |
thedo | 166:3a9487d57a5c | 236 | res = host->bulkRead(dev, bulk_in, data, transfer_len); |
thedo | 166:3a9487d57a5c | 237 | if (checkResult(res, bulk_in)) |
thedo | 166:3a9487d57a5c | 238 | return -1; |
thedo | 166:3a9487d57a5c | 239 | } |
thedo | 166:3a9487d57a5c | 240 | } |
thedo | 166:3a9487d57a5c | 241 | |
thedo | 166:3a9487d57a5c | 242 | // status stage |
thedo | 166:3a9487d57a5c | 243 | csw.Signature = 0; |
thedo | 166:3a9487d57a5c | 244 | USB_DBG("Read CSW"); |
thedo | 166:3a9487d57a5c | 245 | res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); |
thedo | 166:3a9487d57a5c | 246 | if (checkResult(res, bulk_in)) |
thedo | 166:3a9487d57a5c | 247 | return -1; |
thedo | 166:3a9487d57a5c | 248 | |
thedo | 166:3a9487d57a5c | 249 | if (csw.Signature != CSW_SIGNATURE) { |
thedo | 166:3a9487d57a5c | 250 | return -1; |
thedo | 166:3a9487d57a5c | 251 | } |
thedo | 166:3a9487d57a5c | 252 | |
thedo | 166:3a9487d57a5c | 253 | USB_DBG("recv csw: status: %d", csw.Status); |
thedo | 166:3a9487d57a5c | 254 | |
thedo | 166:3a9487d57a5c | 255 | // ModeSense? |
thedo | 166:3a9487d57a5c | 256 | if ((csw.Status == 1) && (cmd[0] != 0x03)) { |
thedo | 166:3a9487d57a5c | 257 | USB_DBG("request mode sense"); |
thedo | 166:3a9487d57a5c | 258 | return SCSIRequestSense(); |
thedo | 166:3a9487d57a5c | 259 | } |
thedo | 166:3a9487d57a5c | 260 | |
thedo | 166:3a9487d57a5c | 261 | // perform reset recovery |
thedo | 166:3a9487d57a5c | 262 | if ((csw.Status == 2) && (cmd[0] != 0x03)) { |
thedo | 166:3a9487d57a5c | 263 | |
thedo | 166:3a9487d57a5c | 264 | // send Bulk-Only Mass Storage Reset request |
thedo | 166:3a9487d57a5c | 265 | res = host->controlWrite( dev, |
thedo | 166:3a9487d57a5c | 266 | USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, |
thedo | 166:3a9487d57a5c | 267 | BO_MASS_STORAGE_RESET, |
thedo | 166:3a9487d57a5c | 268 | 0, msd_intf, NULL, 0); |
thedo | 166:3a9487d57a5c | 269 | |
thedo | 166:3a9487d57a5c | 270 | // unstall both endpoints |
thedo | 166:3a9487d57a5c | 271 | res = host->controlWrite( dev, |
thedo | 166:3a9487d57a5c | 272 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
thedo | 166:3a9487d57a5c | 273 | CLEAR_FEATURE, |
thedo | 166:3a9487d57a5c | 274 | 0, bulk_in->getAddress(), NULL, 0); |
thedo | 166:3a9487d57a5c | 275 | |
thedo | 166:3a9487d57a5c | 276 | res = host->controlWrite( dev, |
thedo | 166:3a9487d57a5c | 277 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
thedo | 166:3a9487d57a5c | 278 | CLEAR_FEATURE, |
thedo | 166:3a9487d57a5c | 279 | 0, bulk_out->getAddress(), NULL, 0); |
thedo | 166:3a9487d57a5c | 280 | |
thedo | 166:3a9487d57a5c | 281 | } |
thedo | 166:3a9487d57a5c | 282 | |
thedo | 166:3a9487d57a5c | 283 | return csw.Status; |
thedo | 166:3a9487d57a5c | 284 | } |
thedo | 166:3a9487d57a5c | 285 | |
thedo | 166:3a9487d57a5c | 286 | |
thedo | 166:3a9487d57a5c | 287 | int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) { |
thedo | 166:3a9487d57a5c | 288 | uint8_t cmd[10]; |
thedo | 166:3a9487d57a5c | 289 | memset(cmd,0,10); |
thedo | 166:3a9487d57a5c | 290 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
thedo | 166:3a9487d57a5c | 291 | |
thedo | 166:3a9487d57a5c | 292 | cmd[2] = (block >> 24) & 0xff; |
thedo | 166:3a9487d57a5c | 293 | cmd[3] = (block >> 16) & 0xff; |
thedo | 166:3a9487d57a5c | 294 | cmd[4] = (block >> 8) & 0xff; |
thedo | 166:3a9487d57a5c | 295 | cmd[5] = block & 0xff; |
thedo | 166:3a9487d57a5c | 296 | |
thedo | 166:3a9487d57a5c | 297 | cmd[7] = (nbBlock >> 8) & 0xff; |
thedo | 166:3a9487d57a5c | 298 | cmd[8] = nbBlock & 0xff; |
thedo | 166:3a9487d57a5c | 299 | |
thedo | 166:3a9487d57a5c | 300 | return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); |
thedo | 166:3a9487d57a5c | 301 | } |
thedo | 166:3a9487d57a5c | 302 | |
thedo | 166:3a9487d57a5c | 303 | int USBHostMSD::getMaxLun() { |
thedo | 166:3a9487d57a5c | 304 | uint8_t buf[1], res; |
thedo | 166:3a9487d57a5c | 305 | res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
thedo | 166:3a9487d57a5c | 306 | 0xfe, 0, msd_intf, buf, 1); |
thedo | 166:3a9487d57a5c | 307 | USB_DBG("max lun: %d", buf[0]); |
thedo | 166:3a9487d57a5c | 308 | return res; |
thedo | 166:3a9487d57a5c | 309 | } |
thedo | 166:3a9487d57a5c | 310 | |
thedo | 166:3a9487d57a5c | 311 | #define USB_BLOCK_DEVICE_ERROR_WOULD_BLOCK -5001 /*!< operation would block */ |
thedo | 166:3a9487d57a5c | 312 | #define USB_BLOCK_DEVICE_ERROR_UNSUPPORTED -5002 /*!< unsupported operation */ |
thedo | 166:3a9487d57a5c | 313 | #define USB_BLOCK_DEVICE_ERROR_PARAMETER -5003 /*!< invalid parameter */ |
thedo | 166:3a9487d57a5c | 314 | #define USB_BLOCK_DEVICE_ERROR_NO_INIT -5004 /*!< uninitialized */ |
thedo | 166:3a9487d57a5c | 315 | #define USB_BLOCK_DEVICE_ERROR_NO_DEVICE -5005 /*!< device is missing or not connected */ |
thedo | 166:3a9487d57a5c | 316 | #define USB_BLOCK_DEVICE_ERROR_WRITE_PROTECTED -5006 /*!< write protected */ |
thedo | 166:3a9487d57a5c | 317 | |
thedo | 166:3a9487d57a5c | 318 | int USBHostMSD::init() { |
thedo | 166:3a9487d57a5c | 319 | uint16_t i, timeout = 10; |
thedo | 166:3a9487d57a5c | 320 | |
thedo | 166:3a9487d57a5c | 321 | _lock.lock(); |
thedo | 166:3a9487d57a5c | 322 | getMaxLun(); |
thedo | 166:3a9487d57a5c | 323 | |
thedo | 166:3a9487d57a5c | 324 | for (i = 0; i < timeout; i++) { |
thedo | 166:3a9487d57a5c | 325 | Thread::wait(100); |
thedo | 166:3a9487d57a5c | 326 | if (!testUnitReady()) |
thedo | 166:3a9487d57a5c | 327 | break; |
thedo | 166:3a9487d57a5c | 328 | } |
thedo | 166:3a9487d57a5c | 329 | |
thedo | 166:3a9487d57a5c | 330 | if (i == timeout) { |
thedo | 166:3a9487d57a5c | 331 | return BD_ERROR_DEVICE_ERROR; |
thedo | 166:3a9487d57a5c | 332 | } |
thedo | 166:3a9487d57a5c | 333 | |
thedo | 166:3a9487d57a5c | 334 | inquiry(0, 0); |
thedo | 166:3a9487d57a5c | 335 | if (readCapacity() != 0) { |
thedo | 166:3a9487d57a5c | 336 | _lock.unlock(); |
thedo | 166:3a9487d57a5c | 337 | return BD_ERROR_DEVICE_ERROR; |
thedo | 166:3a9487d57a5c | 338 | } |
thedo | 166:3a9487d57a5c | 339 | _is_initialized = true; |
thedo | 166:3a9487d57a5c | 340 | _lock.unlock(); |
thedo | 166:3a9487d57a5c | 341 | |
thedo | 166:3a9487d57a5c | 342 | return BD_ERROR_OK; |
thedo | 166:3a9487d57a5c | 343 | } |
thedo | 166:3a9487d57a5c | 344 | |
thedo | 166:3a9487d57a5c | 345 | int USBHostMSD::deinit() { |
thedo | 166:3a9487d57a5c | 346 | return 0; |
thedo | 166:3a9487d57a5c | 347 | } |
thedo | 166:3a9487d57a5c | 348 | |
thedo | 166:3a9487d57a5c | 349 | int USBHostMSD::program(const void *b, bd_addr_t addr, bd_size_t size) |
thedo | 166:3a9487d57a5c | 350 | { |
thedo | 166:3a9487d57a5c | 351 | if (!is_valid_program(addr, size)) { |
thedo | 166:3a9487d57a5c | 352 | return USB_BLOCK_DEVICE_ERROR_PARAMETER; |
thedo | 166:3a9487d57a5c | 353 | } |
thedo | 166:3a9487d57a5c | 354 | |
thedo | 166:3a9487d57a5c | 355 | _lock.lock(); |
thedo | 166:3a9487d57a5c | 356 | if (!_is_initialized) { |
thedo | 166:3a9487d57a5c | 357 | _lock.unlock(); |
thedo | 166:3a9487d57a5c | 358 | return USB_BLOCK_DEVICE_ERROR_NO_INIT; |
thedo | 166:3a9487d57a5c | 359 | } |
thedo | 166:3a9487d57a5c | 360 | |
thedo | 166:3a9487d57a5c | 361 | const uint8_t *buffer = static_cast<const uint8_t*>(b); |
thedo | 166:3a9487d57a5c | 362 | while (size > 0) { |
thedo | 166:3a9487d57a5c | 363 | bd_addr_t block = addr / 512; |
thedo | 166:3a9487d57a5c | 364 | |
thedo | 166:3a9487d57a5c | 365 | // send the data block |
thedo | 166:3a9487d57a5c | 366 | if (dataTransfer((uint8_t*)buffer, block, 1, HOST_TO_DEVICE)) { |
thedo | 166:3a9487d57a5c | 367 | _lock.unlock(); |
thedo | 166:3a9487d57a5c | 368 | return BD_ERROR_DEVICE_ERROR; |
thedo | 166:3a9487d57a5c | 369 | } |
thedo | 166:3a9487d57a5c | 370 | |
thedo | 166:3a9487d57a5c | 371 | buffer += 512; |
thedo | 166:3a9487d57a5c | 372 | addr += 512; |
thedo | 166:3a9487d57a5c | 373 | size -= 512; |
thedo | 166:3a9487d57a5c | 374 | } |
thedo | 166:3a9487d57a5c | 375 | _lock.unlock(); |
thedo | 166:3a9487d57a5c | 376 | return 0; |
thedo | 166:3a9487d57a5c | 377 | } |
thedo | 166:3a9487d57a5c | 378 | |
thedo | 166:3a9487d57a5c | 379 | int USBHostMSD::read(void *b, bd_addr_t addr, bd_size_t size) |
thedo | 166:3a9487d57a5c | 380 | { |
thedo | 166:3a9487d57a5c | 381 | if (!is_valid_read(addr, size)) { |
thedo | 166:3a9487d57a5c | 382 | return USB_BLOCK_DEVICE_ERROR_PARAMETER; |
thedo | 166:3a9487d57a5c | 383 | } |
thedo | 166:3a9487d57a5c | 384 | |
thedo | 166:3a9487d57a5c | 385 | _lock.lock(); |
thedo | 166:3a9487d57a5c | 386 | if (!_is_initialized) { |
thedo | 166:3a9487d57a5c | 387 | _lock.unlock(); |
thedo | 166:3a9487d57a5c | 388 | return USB_BLOCK_DEVICE_ERROR_PARAMETER; |
thedo | 166:3a9487d57a5c | 389 | } |
thedo | 166:3a9487d57a5c | 390 | |
thedo | 166:3a9487d57a5c | 391 | uint8_t *buffer = static_cast<uint8_t *>(b); |
thedo | 166:3a9487d57a5c | 392 | while (size > 0) { |
thedo | 166:3a9487d57a5c | 393 | bd_addr_t block = addr / 512; |
thedo | 166:3a9487d57a5c | 394 | |
thedo | 166:3a9487d57a5c | 395 | // receive the data |
thedo | 166:3a9487d57a5c | 396 | if (dataTransfer((uint8_t*)buffer, block, 1, DEVICE_TO_HOST)) { |
thedo | 166:3a9487d57a5c | 397 | _lock.unlock(); |
thedo | 166:3a9487d57a5c | 398 | return BD_ERROR_DEVICE_ERROR; |
thedo | 166:3a9487d57a5c | 399 | } |
thedo | 166:3a9487d57a5c | 400 | buffer += 512; |
thedo | 166:3a9487d57a5c | 401 | addr += 512; |
thedo | 166:3a9487d57a5c | 402 | size -= 512; |
thedo | 166:3a9487d57a5c | 403 | } |
thedo | 166:3a9487d57a5c | 404 | _lock.unlock(); |
thedo | 166:3a9487d57a5c | 405 | return 0; |
thedo | 166:3a9487d57a5c | 406 | } |
thedo | 166:3a9487d57a5c | 407 | |
thedo | 166:3a9487d57a5c | 408 | int USBHostMSD::erase(bd_addr_t addr, bd_size_t size) |
thedo | 166:3a9487d57a5c | 409 | { |
thedo | 166:3a9487d57a5c | 410 | return 0; |
thedo | 166:3a9487d57a5c | 411 | } |
thedo | 166:3a9487d57a5c | 412 | |
thedo | 166:3a9487d57a5c | 413 | bd_size_t USBHostMSD::get_read_size() const |
thedo | 166:3a9487d57a5c | 414 | { |
thedo | 166:3a9487d57a5c | 415 | return 512; |
thedo | 166:3a9487d57a5c | 416 | } |
thedo | 166:3a9487d57a5c | 417 | |
thedo | 166:3a9487d57a5c | 418 | bd_size_t USBHostMSD::get_program_size() const |
thedo | 166:3a9487d57a5c | 419 | { |
thedo | 166:3a9487d57a5c | 420 | return 512; |
thedo | 166:3a9487d57a5c | 421 | } |
thedo | 166:3a9487d57a5c | 422 | |
thedo | 166:3a9487d57a5c | 423 | bd_size_t USBHostMSD::get_erase_size() const |
thedo | 166:3a9487d57a5c | 424 | { |
thedo | 166:3a9487d57a5c | 425 | return 512; |
thedo | 166:3a9487d57a5c | 426 | } |
thedo | 166:3a9487d57a5c | 427 | |
thedo | 166:3a9487d57a5c | 428 | bd_size_t USBHostMSD::size() const |
thedo | 166:3a9487d57a5c | 429 | { |
thedo | 166:3a9487d57a5c | 430 | bd_size_t sectors = 0; |
thedo | 166:3a9487d57a5c | 431 | if(_is_initialized) { |
thedo | 166:3a9487d57a5c | 432 | sectors = blockCount; |
thedo | 166:3a9487d57a5c | 433 | } |
thedo | 166:3a9487d57a5c | 434 | return 512*sectors; |
thedo | 166:3a9487d57a5c | 435 | } |
thedo | 166:3a9487d57a5c | 436 | |
thedo | 166:3a9487d57a5c | 437 | void USBHostMSD::debug(bool dbg) |
thedo | 166:3a9487d57a5c | 438 | { |
thedo | 166:3a9487d57a5c | 439 | // _dbg = dbg; |
thedo | 166:3a9487d57a5c | 440 | } |
thedo | 166:3a9487d57a5c | 441 | |
thedo | 166:3a9487d57a5c | 442 | #endif |
thedo | 166:3a9487d57a5c | 443 |