Adaptation of the official mbed USBHost repository to work with the LPC4088 Display Module
Dependents: DMSupport DMSupport DMSupport DMSupport
Fork of DM_USBHost by
Diff: USBHostMSD/USBHostMSD.cpp
- Revision:
- 27:aa2fd412f1d3
- Parent:
- 26:607951c26872
- Child:
- 29:dc8179214fd7
diff -r 607951c26872 -r aa2fd412f1d3 USBHostMSD/USBHostMSD.cpp --- a/USBHostMSD/USBHostMSD.cpp Mon Aug 18 13:45:26 2014 +0100 +++ b/USBHostMSD/USBHostMSD.cpp Tue Dec 02 15:16:39 2014 +0000 @@ -33,6 +33,21 @@ { host = USBHost::getHostInst(); init(); + + cbw = (CBW*)host->getSafeMem(sizeof(CBW)); + csw = (CSW*)host->getSafeMem(sizeof(CSW)); +} + +USBHostMSD::~USBHostMSD() +{ + if (cbw != NULL) { + host->returnSafeMem((uint8_t*)cbw); + cbw = NULL; + } + if (csw != NULL) { + host->returnSafeMem((uint8_t*)csw); + csw = NULL; + } } void USBHostMSD::init() { @@ -40,13 +55,11 @@ dev = NULL; bulk_in = NULL; bulk_out = NULL; - dev_connected = false; blockSize = 0; blockCount = 0; msd_intf = -1; msd_device_found = false; disk_init = false; - dev_connected = false; nb_ep = 0; } @@ -131,13 +144,14 @@ int USBHostMSD::readCapacity() { USB_DBG("Read capacity"); uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; - uint8_t result[8]; + uint8_t* result = host->getSafeMem(8); int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); if (status == 0) { blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); } + host->returnSafeMem(result); return status; } @@ -145,8 +159,9 @@ int USBHostMSD::SCSIRequestSense() { USB_DBG("Request sense"); uint8_t cmd[6] = {0x03,0,0,0,18,0}; - uint8_t result[18]; + uint8_t* result = host->getSafeMem(18); int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); + host->returnSafeMem(result); return status; } @@ -155,7 +170,7 @@ USB_DBG("Inquiry"); uint8_t evpd = (page_code == 0) ? 0 : 1; uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0}; - uint8_t result[36]; + uint8_t* result = host->getSafeMem(36); int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); if (status == 0) { char vid_pid[17]; @@ -171,6 +186,7 @@ vid_pid[4] = 0; USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); } + host->returnSafeMem(result); return status; } @@ -198,20 +214,20 @@ int res = 0; - cbw.Signature = CBW_SIGNATURE; - cbw.Tag = 0; - cbw.DataLength = transfer_len; - cbw.Flags = flags; - cbw.LUN = 0; - cbw.CBLength = cmd_len; - memset(cbw.CB,0,sizeof(cbw.CB)); + cbw->Signature = CBW_SIGNATURE; + cbw->Tag = 0; + cbw->DataLength = transfer_len; + cbw->Flags = flags; + cbw->LUN = 0; + cbw->CBLength = cmd_len; + memset(cbw->CB,0,sizeof(cbw->CB)); if (cmd) { - memcpy(cbw.CB,cmd,cmd_len); + memcpy(cbw->CB,cmd,cmd_len); } // send the cbw USB_DBG("Send CBW"); - res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); + res = host->bulkWrite(dev, bulk_out,(uint8_t *)cbw, 31); if (checkResult(res, bulk_out)) return -1; @@ -233,26 +249,26 @@ } // status stage - csw.Signature = 0; + csw->Signature = 0; USB_DBG("Read CSW"); - res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); + res = host->bulkRead(dev, bulk_in,(uint8_t *)csw, 13); if (checkResult(res, bulk_in)) return -1; - if (csw.Signature != CSW_SIGNATURE) { + if (csw->Signature != CSW_SIGNATURE) { return -1; } - USB_DBG("recv csw: status: %d", csw.Status); + USB_DBG("recv csw: status: %d", csw->Status); // ModeSense? - if ((csw.Status == 1) && (cmd[0] != 0x03)) { + if ((csw->Status == 1) && (cmd[0] != 0x03)) { USB_DBG("request mode sense"); return SCSIRequestSense(); } // perform reset recovery - if ((csw.Status == 2) && (cmd[0] != 0x03)) { + if ((csw->Status == 2) && (cmd[0] != 0x03)) { // send Bulk-Only Mass Storage Reset request res = host->controlWrite( dev, @@ -273,7 +289,7 @@ } - return csw.Status; + return csw->Status; } @@ -294,10 +310,12 @@ } int USBHostMSD::getMaxLun() { - uint8_t buf[1], res; + uint8_t res; + uint8_t* buf = host->getSafeMem(1); res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, 0xfe, 0, msd_intf, buf, 1); USB_DBG("max lun: %d", buf[0]); + host->returnSafeMem(buf); return res; } @@ -330,11 +348,16 @@ } if (!disk_init) return -1; + uint8_t* safe = host->getSafeMem(512); for (uint64_t b = block_number; b < block_number + count; b++) { - if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE)) + memcpy(safe, buffer, 512); + if (dataTransfer((uint8_t*)safe, b, 1, HOST_TO_DEVICE)) { + host->returnSafeMem(safe); return -1; + } buffer += 512; } + host->returnSafeMem(safe); return 0; } @@ -345,11 +368,16 @@ } if (!disk_init) return -1; + uint8_t* safe = host->getSafeMem(512); for (uint64_t b = block_number; b < block_number + count; b++) { - if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST)) + if (dataTransfer((uint8_t*)safe, b, 1, DEVICE_TO_HOST)) { + host->returnSafeMem(safe); return -1; + } + memcpy(buffer, safe, 512); buffer += 512; } + host->returnSafeMem(safe); return 0; }