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 Norimasa Okamoto

Committer:
va009039
Date:
Sat Jan 25 12:51:44 2014 +0000
Revision:
3:a3872f7593e2
Child:
8:6463cd1964c0
fix max packet size

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 3:a3872f7593e2 1 /* mbed USBHost Library
va009039 3:a3872f7593e2 2 * Copyright (c) 2006-2013 ARM Limited
va009039 3:a3872f7593e2 3 *
va009039 3:a3872f7593e2 4 * Licensed under the Apache License, Version 2.0 (the "License");
va009039 3:a3872f7593e2 5 * you may not use this file except in compliance with the License.
va009039 3:a3872f7593e2 6 * You may obtain a copy of the License at
va009039 3:a3872f7593e2 7 *
va009039 3:a3872f7593e2 8 * http://www.apache.org/licenses/LICENSE-2.0
va009039 3:a3872f7593e2 9 *
va009039 3:a3872f7593e2 10 * Unless required by applicable law or agreed to in writing, software
va009039 3:a3872f7593e2 11 * distributed under the License is distributed on an "AS IS" BASIS,
va009039 3:a3872f7593e2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
va009039 3:a3872f7593e2 13 * See the License for the specific language governing permissions and
va009039 3:a3872f7593e2 14 * limitations under the License.
va009039 3:a3872f7593e2 15 */
va009039 3:a3872f7593e2 16
va009039 3:a3872f7593e2 17 #include "USBHostMSD.h"
va009039 3:a3872f7593e2 18
va009039 3:a3872f7593e2 19 #define CBW_SIGNATURE 0x43425355
va009039 3:a3872f7593e2 20 #define CSW_SIGNATURE 0x53425355
va009039 3:a3872f7593e2 21
va009039 3:a3872f7593e2 22 #define DEVICE_TO_HOST 0x80
va009039 3:a3872f7593e2 23 #define HOST_TO_DEVICE 0x00
va009039 3:a3872f7593e2 24
va009039 3:a3872f7593e2 25 #define GET_MAX_LUN (0xFE)
va009039 3:a3872f7593e2 26 #define BO_MASS_STORAGE_RESET (0xFF)
va009039 3:a3872f7593e2 27
va009039 3:a3872f7593e2 28 USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir)
va009039 3:a3872f7593e2 29 {
va009039 3:a3872f7593e2 30 host = USBHost::getHostInst();
va009039 3:a3872f7593e2 31 init();
va009039 3:a3872f7593e2 32 report = &host->report;
va009039 3:a3872f7593e2 33 }
va009039 3:a3872f7593e2 34
va009039 3:a3872f7593e2 35 void USBHostMSD::init() {
va009039 3:a3872f7593e2 36 dev_connected = false;
va009039 3:a3872f7593e2 37 dev = NULL;
va009039 3:a3872f7593e2 38 bulk_in = NULL;
va009039 3:a3872f7593e2 39 bulk_out = NULL;
va009039 3:a3872f7593e2 40 dev_connected = false;
va009039 3:a3872f7593e2 41 blockSize = 0;
va009039 3:a3872f7593e2 42 blockCount = 0;
va009039 3:a3872f7593e2 43 msd_intf = 1; //msd_intf = -1;
va009039 3:a3872f7593e2 44 msd_device_found = false;
va009039 3:a3872f7593e2 45 disk_init = false;
va009039 3:a3872f7593e2 46 dev_connected = false;
va009039 3:a3872f7593e2 47 nb_ep = 0;
va009039 3:a3872f7593e2 48 }
va009039 3:a3872f7593e2 49
va009039 3:a3872f7593e2 50
va009039 3:a3872f7593e2 51 bool USBHostMSD::connected()
va009039 3:a3872f7593e2 52 {
va009039 3:a3872f7593e2 53 return true;
va009039 3:a3872f7593e2 54 }
va009039 3:a3872f7593e2 55
va009039 3:a3872f7593e2 56 bool USBHostMSD::connect()
va009039 3:a3872f7593e2 57 {
va009039 3:a3872f7593e2 58 return true;
va009039 3:a3872f7593e2 59 }
va009039 3:a3872f7593e2 60
va009039 3:a3872f7593e2 61 int USBHostMSD::testUnitReady() {
va009039 3:a3872f7593e2 62 USB_DBG("Test unit ready");
va009039 3:a3872f7593e2 63 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
va009039 3:a3872f7593e2 64 }
va009039 3:a3872f7593e2 65
va009039 3:a3872f7593e2 66
va009039 3:a3872f7593e2 67 int USBHostMSD::readCapacity() {
va009039 3:a3872f7593e2 68 USB_DBG("Read capacity");
va009039 3:a3872f7593e2 69 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
va009039 3:a3872f7593e2 70 uint8_t result[8];
va009039 3:a3872f7593e2 71 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8);
va009039 3:a3872f7593e2 72 if (status == 0) {
va009039 3:a3872f7593e2 73 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3];
va009039 3:a3872f7593e2 74 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7];
va009039 3:a3872f7593e2 75 USB_INFO("MSD blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", blockCount, blockSize, blockCount*blockSize);
va009039 3:a3872f7593e2 76 }
va009039 3:a3872f7593e2 77 return status;
va009039 3:a3872f7593e2 78 }
va009039 3:a3872f7593e2 79
va009039 3:a3872f7593e2 80
va009039 3:a3872f7593e2 81 int USBHostMSD::SCSIRequestSense() {
va009039 3:a3872f7593e2 82 USB_DBG("Request sense");
va009039 3:a3872f7593e2 83 uint8_t cmd[6] = {0x03,0,0,0,18,0};
va009039 3:a3872f7593e2 84 uint8_t result[18];
va009039 3:a3872f7593e2 85 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18);
va009039 3:a3872f7593e2 86 return status;
va009039 3:a3872f7593e2 87 }
va009039 3:a3872f7593e2 88
va009039 3:a3872f7593e2 89
va009039 3:a3872f7593e2 90 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
va009039 3:a3872f7593e2 91 USB_DBG("Inquiry");
va009039 3:a3872f7593e2 92 uint8_t evpd = (page_code == 0) ? 0 : 1;
va009039 3:a3872f7593e2 93 uint8_t cmd[6] = {0x12, (lun << 5) | evpd, page_code, 0, 36, 0};
va009039 3:a3872f7593e2 94 uint8_t result[36];
va009039 3:a3872f7593e2 95 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36);
va009039 3:a3872f7593e2 96 if (status == 0) {
va009039 3:a3872f7593e2 97 char vid_pid[17];
va009039 3:a3872f7593e2 98 memcpy(vid_pid, &result[8], 8);
va009039 3:a3872f7593e2 99 vid_pid[8] = 0;
va009039 3:a3872f7593e2 100 USB_INFO("MSD Vendor ID: %s", vid_pid);
va009039 3:a3872f7593e2 101
va009039 3:a3872f7593e2 102 memcpy(vid_pid, &result[16], 16);
va009039 3:a3872f7593e2 103 vid_pid[16] = 0;
va009039 3:a3872f7593e2 104 USB_INFO("MSD Product ID: %s", vid_pid);
va009039 3:a3872f7593e2 105
va009039 3:a3872f7593e2 106 memcpy(vid_pid, &result[32], 4);
va009039 3:a3872f7593e2 107 vid_pid[4] = 0;
va009039 3:a3872f7593e2 108 USB_INFO("MSD Product rev: %s", vid_pid);
va009039 3:a3872f7593e2 109 }
va009039 3:a3872f7593e2 110 return status;
va009039 3:a3872f7593e2 111 }
va009039 3:a3872f7593e2 112
va009039 3:a3872f7593e2 113 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
va009039 3:a3872f7593e2 114 // if ep stalled: send clear feature
va009039 3:a3872f7593e2 115 if (res == USB_TYPE_STALL_ERROR) {
va009039 3:a3872f7593e2 116 res = host->controlWrite(dev,
va009039 3:a3872f7593e2 117 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
va009039 3:a3872f7593e2 118 CLEAR_FEATURE,
va009039 3:a3872f7593e2 119 0, ep->getAddress(), NULL, 0);
va009039 3:a3872f7593e2 120 // set state to IDLE if clear feature successful
va009039 3:a3872f7593e2 121 if (res == USB_TYPE_OK) {
va009039 3:a3872f7593e2 122 ep->setState(USB_TYPE_IDLE);
va009039 3:a3872f7593e2 123 }
va009039 3:a3872f7593e2 124 }
va009039 3:a3872f7593e2 125
va009039 3:a3872f7593e2 126 if (res != USB_TYPE_OK)
va009039 3:a3872f7593e2 127 return -1;
va009039 3:a3872f7593e2 128
va009039 3:a3872f7593e2 129 return 0;
va009039 3:a3872f7593e2 130 }
va009039 3:a3872f7593e2 131
va009039 3:a3872f7593e2 132
va009039 3:a3872f7593e2 133 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
va009039 3:a3872f7593e2 134
va009039 3:a3872f7593e2 135 int res = 0;
va009039 3:a3872f7593e2 136
va009039 3:a3872f7593e2 137 cbw.Signature = CBW_SIGNATURE;
va009039 3:a3872f7593e2 138 cbw.Tag = 0;
va009039 3:a3872f7593e2 139 cbw.DataLength = transfer_len;
va009039 3:a3872f7593e2 140 cbw.Flags = flags;
va009039 3:a3872f7593e2 141 cbw.LUN = 0;
va009039 3:a3872f7593e2 142 cbw.CBLength = cmd_len;
va009039 3:a3872f7593e2 143 memset(cbw.CB,0,sizeof(cbw.CB));
va009039 3:a3872f7593e2 144 if (cmd) {
va009039 3:a3872f7593e2 145 memcpy(cbw.CB,cmd,cmd_len);
va009039 3:a3872f7593e2 146 }
va009039 3:a3872f7593e2 147
va009039 3:a3872f7593e2 148 // send the cbw
va009039 3:a3872f7593e2 149 USB_DBG("Send CBW");
va009039 3:a3872f7593e2 150 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31);
va009039 3:a3872f7593e2 151 if (checkResult(res, bulk_out))
va009039 3:a3872f7593e2 152 return -1;
va009039 3:a3872f7593e2 153
va009039 3:a3872f7593e2 154 // data stage if needed
va009039 3:a3872f7593e2 155 if (data) {
va009039 3:a3872f7593e2 156 USB_DBG("data stage");
va009039 3:a3872f7593e2 157 if (flags == HOST_TO_DEVICE) {
va009039 3:a3872f7593e2 158
va009039 3:a3872f7593e2 159 res = host->bulkWrite(dev, bulk_out, data, transfer_len);
va009039 3:a3872f7593e2 160 if (checkResult(res, bulk_out))
va009039 3:a3872f7593e2 161 return -1;
va009039 3:a3872f7593e2 162
va009039 3:a3872f7593e2 163 } else if (flags == DEVICE_TO_HOST) {
va009039 3:a3872f7593e2 164
va009039 3:a3872f7593e2 165 res = host->bulkRead(dev, bulk_in, data, transfer_len);
va009039 3:a3872f7593e2 166 if (checkResult(res, bulk_in))
va009039 3:a3872f7593e2 167 return -1;
va009039 3:a3872f7593e2 168 }
va009039 3:a3872f7593e2 169 }
va009039 3:a3872f7593e2 170
va009039 3:a3872f7593e2 171 // status stage
va009039 3:a3872f7593e2 172 csw.Signature = 0;
va009039 3:a3872f7593e2 173 USB_DBG("Read CSW");
va009039 3:a3872f7593e2 174 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13);
va009039 3:a3872f7593e2 175 if (checkResult(res, bulk_in))
va009039 3:a3872f7593e2 176 return -1;
va009039 3:a3872f7593e2 177
va009039 3:a3872f7593e2 178 if (csw.Signature != CSW_SIGNATURE) {
va009039 3:a3872f7593e2 179 return -1;
va009039 3:a3872f7593e2 180 }
va009039 3:a3872f7593e2 181
va009039 3:a3872f7593e2 182 USB_DBG("recv csw: status: %d", csw.Status);
va009039 3:a3872f7593e2 183
va009039 3:a3872f7593e2 184 // ModeSense?
va009039 3:a3872f7593e2 185 if ((csw.Status == 1) && (cmd[0] != 0x03)) {
va009039 3:a3872f7593e2 186 USB_DBG("request mode sense");
va009039 3:a3872f7593e2 187 return SCSIRequestSense();
va009039 3:a3872f7593e2 188 }
va009039 3:a3872f7593e2 189
va009039 3:a3872f7593e2 190 // perform reset recovery
va009039 3:a3872f7593e2 191 if ((csw.Status == 2) && (cmd[0] != 0x03)) {
va009039 3:a3872f7593e2 192
va009039 3:a3872f7593e2 193 // send Bulk-Only Mass Storage Reset request
va009039 3:a3872f7593e2 194 res = host->controlWrite( dev,
va009039 3:a3872f7593e2 195 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
va009039 3:a3872f7593e2 196 BO_MASS_STORAGE_RESET,
va009039 3:a3872f7593e2 197 0, msd_intf, NULL, 0);
va009039 3:a3872f7593e2 198
va009039 3:a3872f7593e2 199 // unstall both endpoints
va009039 3:a3872f7593e2 200 res = host->controlWrite( dev,
va009039 3:a3872f7593e2 201 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
va009039 3:a3872f7593e2 202 CLEAR_FEATURE,
va009039 3:a3872f7593e2 203 0, bulk_in->getAddress(), NULL, 0);
va009039 3:a3872f7593e2 204
va009039 3:a3872f7593e2 205 res = host->controlWrite( dev,
va009039 3:a3872f7593e2 206 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
va009039 3:a3872f7593e2 207 CLEAR_FEATURE,
va009039 3:a3872f7593e2 208 0, bulk_out->getAddress(), NULL, 0);
va009039 3:a3872f7593e2 209
va009039 3:a3872f7593e2 210 }
va009039 3:a3872f7593e2 211
va009039 3:a3872f7593e2 212 return csw.Status;
va009039 3:a3872f7593e2 213 }
va009039 3:a3872f7593e2 214
va009039 3:a3872f7593e2 215
va009039 3:a3872f7593e2 216 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
va009039 3:a3872f7593e2 217 uint8_t cmd[10];
va009039 3:a3872f7593e2 218 memset(cmd,0,10);
va009039 3:a3872f7593e2 219 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
va009039 3:a3872f7593e2 220
va009039 3:a3872f7593e2 221 cmd[2] = (block >> 24) & 0xff;
va009039 3:a3872f7593e2 222 cmd[3] = (block >> 16) & 0xff;
va009039 3:a3872f7593e2 223 cmd[4] = (block >> 8) & 0xff;
va009039 3:a3872f7593e2 224 cmd[5] = block & 0xff;
va009039 3:a3872f7593e2 225
va009039 3:a3872f7593e2 226 cmd[7] = (nbBlock >> 8) & 0xff;
va009039 3:a3872f7593e2 227 cmd[8] = nbBlock & 0xff;
va009039 3:a3872f7593e2 228
va009039 3:a3872f7593e2 229 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
va009039 3:a3872f7593e2 230 }
va009039 3:a3872f7593e2 231
va009039 3:a3872f7593e2 232 int USBHostMSD::getMaxLun() {
va009039 3:a3872f7593e2 233 uint8_t buf[1], res;
va009039 3:a3872f7593e2 234 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
va009039 3:a3872f7593e2 235 0xfe, 0, msd_intf, buf, 1);
va009039 3:a3872f7593e2 236 USB_DBG("max lun: %d", buf[0]);
va009039 3:a3872f7593e2 237 return res;
va009039 3:a3872f7593e2 238 }
va009039 3:a3872f7593e2 239
va009039 3:a3872f7593e2 240 int USBHostMSD::disk_initialize() {
va009039 3:a3872f7593e2 241 USB_DBG("FILESYSTEM: init");
va009039 3:a3872f7593e2 242 int i, timeout = 10;
va009039 3:a3872f7593e2 243
va009039 3:a3872f7593e2 244 //getMaxLun();
va009039 3:a3872f7593e2 245
va009039 3:a3872f7593e2 246 for (i = 0; i < timeout; i++) {
va009039 3:a3872f7593e2 247 wait_ms(100);
va009039 3:a3872f7593e2 248 if (!testUnitReady())
va009039 3:a3872f7593e2 249 break;
va009039 3:a3872f7593e2 250 }
va009039 3:a3872f7593e2 251
va009039 3:a3872f7593e2 252 if (i == timeout) {
va009039 3:a3872f7593e2 253 disk_init = false;
va009039 3:a3872f7593e2 254 return -1;
va009039 3:a3872f7593e2 255 }
va009039 3:a3872f7593e2 256
va009039 3:a3872f7593e2 257 inquiry(0, 0);
va009039 3:a3872f7593e2 258 disk_init = 1;
va009039 3:a3872f7593e2 259 return readCapacity();
va009039 3:a3872f7593e2 260 }
va009039 3:a3872f7593e2 261
va009039 3:a3872f7593e2 262 int USBHostMSD::disk_write(const uint8_t *buffer, uint64_t block_number) {
va009039 3:a3872f7593e2 263 USB_DBG("FILESYSTEM: write block: %lld", block_number);
va009039 3:a3872f7593e2 264 if (!disk_init) {
va009039 3:a3872f7593e2 265 disk_initialize();
va009039 3:a3872f7593e2 266 }
va009039 3:a3872f7593e2 267 if (!disk_init)
va009039 3:a3872f7593e2 268 return -1;
va009039 3:a3872f7593e2 269 return dataTransfer((uint8_t *)buffer, block_number, 1, HOST_TO_DEVICE);
va009039 3:a3872f7593e2 270 }
va009039 3:a3872f7593e2 271
va009039 3:a3872f7593e2 272 int USBHostMSD::disk_read(uint8_t * buffer, uint64_t block_number) {
va009039 3:a3872f7593e2 273 USB_DBG("FILESYSTEM: read block %lld", block_number);
va009039 3:a3872f7593e2 274 if (!disk_init) {
va009039 3:a3872f7593e2 275 disk_initialize();
va009039 3:a3872f7593e2 276 }
va009039 3:a3872f7593e2 277 if (!disk_init)
va009039 3:a3872f7593e2 278 return -1;
va009039 3:a3872f7593e2 279 return dataTransfer((uint8_t *)buffer, block_number, 1, DEVICE_TO_HOST);
va009039 3:a3872f7593e2 280 }
va009039 3:a3872f7593e2 281
va009039 3:a3872f7593e2 282 uint64_t USBHostMSD::disk_sectors() {
va009039 3:a3872f7593e2 283 USB_DBG("FILESYSTEM: sectors");
va009039 3:a3872f7593e2 284 if (!disk_init) {
va009039 3:a3872f7593e2 285 disk_initialize();
va009039 3:a3872f7593e2 286 }
va009039 3:a3872f7593e2 287 if (!disk_init)
va009039 3:a3872f7593e2 288 return 0;
va009039 3:a3872f7593e2 289 return blockCount;
va009039 3:a3872f7593e2 290 }
va009039 3:a3872f7593e2 291