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
USBHostMSD.cpp
00001 /* mbed USBHost Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "USBHostMSD.h" 00018 00019 #define CBW_SIGNATURE 0x43425355 00020 #define CSW_SIGNATURE 0x53425355 00021 00022 #define DEVICE_TO_HOST 0x80 00023 #define HOST_TO_DEVICE 0x00 00024 00025 #define GET_MAX_LUN (0xFE) 00026 #define BO_MASS_STORAGE_RESET (0xFF) 00027 00028 USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir) 00029 { 00030 host = USBHost::getHostInst(); 00031 init(); 00032 } 00033 00034 void USBHostMSD::init() { 00035 dev_connected = false; 00036 dev = NULL; 00037 bulk_in = NULL; 00038 bulk_out = NULL; 00039 dev_connected = false; 00040 blockSize = 0; 00041 blockCount = 0; 00042 msd_intf = -1; 00043 msd_device_found = false; 00044 disk_init = false; 00045 dev_connected = false; 00046 nb_ep = 0; 00047 } 00048 00049 bool USBHostMSD::connected() 00050 { 00051 return dev_connected; 00052 } 00053 00054 bool USBHostMSD::connect() 00055 { 00056 if (dev_connected) { 00057 return true; 00058 } 00059 00060 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00061 if ((dev = host->getDevice(i)) != NULL) { 00062 00063 //USB_DBG("Trying to connect MSD device dev=%p\n", dev); 00064 00065 if(host->enumerate(dev, this)) 00066 break; 00067 00068 if (msd_device_found) { 00069 bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); 00070 bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); 00071 00072 if (!bulk_in || !bulk_out) 00073 continue; 00074 00075 USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); 00076 dev->setName("MSD", msd_intf); 00077 host->registerDriver(dev, msd_intf, this, &USBHostMSD::init); 00078 00079 dev_connected = true; 00080 return true; 00081 } 00082 } //if() 00083 } //for() 00084 init(); 00085 return false; 00086 } 00087 00088 /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) 00089 { 00090 USB_DBG2("vid:%04x pid:%04x", vid, pid); 00091 // we don't check VID/PID for MSD driver 00092 } 00093 00094 /*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 00095 { 00096 USB_DBG2("intf: %d class: %02x %02x %02x", intf_nb, intf_class, intf_subclass, intf_protocol); 00097 00098 if ((msd_intf == -1) && 00099 (intf_class == MSD_CLASS) && 00100 (intf_subclass == 0x06) && 00101 (intf_protocol == 0x50)) { 00102 msd_intf = intf_nb; 00103 return true; 00104 } 00105 return false; 00106 } 00107 00108 /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00109 { 00110 USB_DBG2("intf_nb=%d type=%d dir=%d", intf_nb, type, dir); 00111 if (intf_nb == msd_intf) { 00112 if (type == BULK_ENDPOINT) { 00113 nb_ep++; 00114 if (nb_ep == 2) 00115 msd_device_found = true; 00116 return true; 00117 } 00118 } 00119 return false; 00120 } 00121 00122 int USBHostMSD::testUnitReady() { 00123 USB_DBG("Test unit ready"); 00124 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); 00125 } 00126 00127 00128 int USBHostMSD::readCapacity() { 00129 USB_DBG("Read capacity"); 00130 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; 00131 uint8_t result[8]; 00132 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); 00133 if (status == 0) { 00134 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; 00135 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; 00136 USB_INFO("MSD blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", blockCount, blockSize, blockCount*blockSize); 00137 } 00138 return status; 00139 } 00140 00141 00142 int USBHostMSD::SCSIRequestSense() { 00143 USB_DBG("Request sense"); 00144 uint8_t cmd[6] = {0x03,0,0,0,18,0}; 00145 uint8_t result[18]; 00146 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); 00147 return status; 00148 } 00149 00150 00151 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) { 00152 USB_DBG("Inquiry"); 00153 uint8_t evpd = (page_code == 0) ? 0 : 1; 00154 uint8_t cmd[6] = {0x12, (lun << 5) | evpd, page_code, 0, 36, 0}; 00155 uint8_t result[36]; 00156 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); 00157 if (status == 0) { 00158 char vid_pid[17]; 00159 memcpy(vid_pid, &result[8], 8); 00160 vid_pid[8] = 0; 00161 USB_INFO("MSD Vendor ID: %s", vid_pid); 00162 00163 memcpy(vid_pid, &result[16], 16); 00164 vid_pid[16] = 0; 00165 USB_INFO("MSD Product ID: %s", vid_pid); 00166 00167 memcpy(vid_pid, &result[32], 4); 00168 vid_pid[4] = 0; 00169 USB_INFO("MSD Product rev: %s", vid_pid); 00170 } 00171 return status; 00172 } 00173 00174 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) { 00175 // if ep stalled: send clear feature 00176 if (res == USB_TYPE_STALL_ERROR) { 00177 res = host->controlWrite(dev, 00178 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00179 CLEAR_FEATURE, 00180 0, ep->getAddress(), NULL, 0); 00181 // set state to IDLE if clear feature successful 00182 if (res == USB_TYPE_OK) { 00183 ep->setState(USB_TYPE_IDLE); 00184 } 00185 } 00186 00187 if (res != USB_TYPE_OK) 00188 return -1; 00189 00190 return 0; 00191 } 00192 00193 00194 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) { 00195 00196 int res = 0; 00197 00198 cbw.Signature = CBW_SIGNATURE; 00199 cbw.Tag = 0; 00200 cbw.DataLength = transfer_len; 00201 cbw.Flags = flags; 00202 cbw.LUN = 0; 00203 cbw.CBLength = cmd_len; 00204 memset(cbw.CB,0,sizeof(cbw.CB)); 00205 if (cmd) { 00206 memcpy(cbw.CB,cmd,cmd_len); 00207 } 00208 00209 // send the cbw 00210 USB_DBG("Send CBW"); 00211 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); 00212 if (checkResult(res, bulk_out)) 00213 return -1; 00214 00215 // data stage if needed 00216 if (data) { 00217 USB_DBG("data stage"); 00218 if (flags == HOST_TO_DEVICE) { 00219 00220 res = host->bulkWrite(dev, bulk_out, data, transfer_len); 00221 if (checkResult(res, bulk_out)) 00222 return -1; 00223 00224 } else if (flags == DEVICE_TO_HOST) { 00225 00226 res = host->bulkRead(dev, bulk_in, data, transfer_len); 00227 if (checkResult(res, bulk_in)) 00228 return -1; 00229 } 00230 } 00231 00232 // status stage 00233 csw.Signature = 0; 00234 USB_DBG("Read CSW"); 00235 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); 00236 if (checkResult(res, bulk_in)) 00237 return -1; 00238 00239 if (csw.Signature != CSW_SIGNATURE) { 00240 return -1; 00241 } 00242 00243 USB_DBG("recv csw: status: %d", csw.Status); 00244 00245 // ModeSense? 00246 if ((csw.Status == 1) && (cmd[0] != 0x03)) { 00247 USB_DBG("request mode sense"); 00248 return SCSIRequestSense(); 00249 } 00250 00251 // perform reset recovery 00252 if ((csw.Status == 2) && (cmd[0] != 0x03)) { 00253 00254 // send Bulk-Only Mass Storage Reset request 00255 res = host->controlWrite( dev, 00256 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, 00257 BO_MASS_STORAGE_RESET, 00258 0, msd_intf, NULL, 0); 00259 00260 // unstall both endpoints 00261 res = host->controlWrite( dev, 00262 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00263 CLEAR_FEATURE, 00264 0, bulk_in->getAddress(), NULL, 0); 00265 00266 res = host->controlWrite( dev, 00267 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00268 CLEAR_FEATURE, 00269 0, bulk_out->getAddress(), NULL, 0); 00270 00271 } 00272 00273 return csw.Status; 00274 } 00275 00276 00277 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) { 00278 uint8_t cmd[10]; 00279 memset(cmd,0,10); 00280 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; 00281 00282 cmd[2] = (block >> 24) & 0xff; 00283 cmd[3] = (block >> 16) & 0xff; 00284 cmd[4] = (block >> 8) & 0xff; 00285 cmd[5] = block & 0xff; 00286 00287 cmd[7] = (nbBlock >> 8) & 0xff; 00288 cmd[8] = nbBlock & 0xff; 00289 00290 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); 00291 } 00292 00293 int USBHostMSD::getMaxLun() { 00294 uint8_t buf[1], res; 00295 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, 00296 0xfe, 0, msd_intf, buf, 1); 00297 USB_DBG("max lun: %d", buf[0]); 00298 return res; 00299 } 00300 00301 int USBHostMSD::disk_initialize() { 00302 USB_DBG("FILESYSTEM: init"); 00303 int i, timeout = 10; 00304 00305 //getMaxLun(); 00306 00307 for (i = 0; i < timeout; i++) { 00308 wait_ms(100); 00309 if (!testUnitReady()) 00310 break; 00311 } 00312 00313 if (i == timeout) { 00314 disk_init = false; 00315 return -1; 00316 } 00317 00318 inquiry(0, 0); 00319 disk_init = 1; 00320 return readCapacity(); 00321 } 00322 00323 int USBHostMSD::disk_write(const uint8_t *buffer, uint64_t block_number) { 00324 USB_DBG("FILESYSTEM: write block: %lld", block_number); 00325 if (!disk_init) { 00326 disk_initialize(); 00327 } 00328 if (!disk_init) 00329 return -1; 00330 return dataTransfer((uint8_t *)buffer, block_number, 1, HOST_TO_DEVICE); 00331 } 00332 00333 int USBHostMSD::disk_read(uint8_t * buffer, uint64_t block_number) { 00334 USB_DBG("FILESYSTEM: read block %lld", block_number); 00335 if (!disk_init) { 00336 disk_initialize(); 00337 } 00338 if (!disk_init) 00339 return -1; 00340 return dataTransfer((uint8_t *)buffer, block_number, 1, DEVICE_TO_HOST); 00341 } 00342 00343 uint64_t USBHostMSD::disk_sectors() { 00344 USB_DBG("FILESYSTEM: sectors"); 00345 if (!disk_init) { 00346 disk_initialize(); 00347 } 00348 if (!disk_init) 00349 return 0; 00350 return blockCount; 00351 } 00352
Generated on Wed Jul 13 2022 02:45:01 by 1.7.2