USB MSD using the AHBSRAM banks 0 and 1 to create a 32k RAM disk. After FAT format 12kb is writable
Dependencies: USBDevice USBMSD_RAM mbed
Fork of USB-MSD_SD_HelloWorld_Mbed-RAMVersion by
Revision 5:8afbc15d6892, committed 2011-11-13
- Comitter:
- samux
- Date:
- Sun Nov 13 12:30:43 2011 +0000
- Parent:
- 4:980e6470dcce
- Child:
- 6:126c4d980196
- Commit message:
- rename things and introduce an ERROR state
Changed in this revision
USBDevice/USBMSD/USBMSD.cpp | Show annotated file Show diff for this revision Revisions of this file |
USBDevice/USBMSD/USBMSD.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/USBDevice/USBMSD/USBMSD.cpp Sat Nov 12 17:53:04 2011 +0000 +++ b/USBDevice/USBMSD/USBMSD.cpp Sun Nov 13 12:30:43 2011 +0000 @@ -45,9 +45,9 @@ // CSW Status enum Status { - SCSI_PASSED, - SCSI_FAILED, - SCSI_PHASE_ERROR, + CSW_PASSED, + CSW_FAILED, + CSW_ERROR, }; @@ -82,7 +82,7 @@ void USBMSD::reset() { - stage = STATE_READ_CBW; + stage = READ_CBW; } uint8_t * USBMSD::getMaxLUN() @@ -97,10 +97,13 @@ uint8_t buf[MAX_PACKET_SIZE_EPBULK]; read(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK); switch (stage) { - case STATE_READ_CBW: + // the device has to decode the CBW received + case READ_CBW: CBWDecode(buf, size); break; - case STATE_PROCESS_CBW: + + // the device has to receive data from the host + case PROCESS_CBW: switch (cbw.CB[0]) { case WRITE10: case WRITE12: @@ -111,10 +114,11 @@ break; } break; - case STATE_WAIT_CSW: - break; + + // an error has occured: stall endpoint and send CSW default: - csw.Status = SCSI_PHASE_ERROR; + stallEndpoint(EPBULK_OUT); + csw.Status = CSW_ERROR; sendCSW(); break; } @@ -127,7 +131,9 @@ // Called in ISR context when a data has been transferred bool USBMSD::EP2_IN_callback() { switch (stage) { - case STATE_PROCESS_CBW: + + // the device has to send data to the host + case PROCESS_CBW: switch (cbw.CB[0]) { case READ10: case READ12: @@ -135,15 +141,21 @@ break; } break; - case STATE_SEND_CSW: + + //the device has to send a CSW + case SEND_CSW: sendCSW(); break; - case STATE_ERROR: + + // an error has occured + case ERROR: stallEndpoint(EPBULK_IN); sendCSW(); break; - case STATE_WAIT_CSW: - stage = STATE_READ_CBW; + + // the host has received the CSW -> we wait a CBW + case WAIT_CSW: + stage = READ_CBW; break; } return true; @@ -154,7 +166,7 @@ if ((addr + size) > MemorySize) { size = MemorySize - addr; - stage = STATE_WAIT_CSW; + stage = ERROR; stallEndpoint(EPBULK_OUT); } @@ -170,8 +182,8 @@ length -= size; csw.DataResidue -= size; - if ((!length) || (stage == STATE_WAIT_CSW)) { - csw.Status = SCSI_PASSED; + if ((!length) || (stage != PROCESS_CBW)) { + csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED; sendCSW(); } } @@ -181,7 +193,7 @@ if ((addr + size) > MemorySize) { size = MemorySize - addr; - stage = STATE_WAIT_CSW; + stage = ERROR; stallEndpoint(EPBULK_OUT); } @@ -201,8 +213,8 @@ length -= size; csw.DataResidue -= size; - if ( !length || (stage == STATE_WAIT_CSW)) { - csw.Status = (memOK) ? SCSI_PASSED : SCSI_FAILED; + if ( !length || (stage != PROCESS_CBW)) { + csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED; sendCSW(); } } @@ -265,14 +277,14 @@ if (size >= cbw.DataLength) { size = cbw.DataLength; } - stage = STATE_SEND_CSW; + stage = SEND_CSW; if (!writeNB(EPBULK_IN, buf, size, MAX_PACKET_SIZE_EPBULK)) { return false; } csw.DataResidue -= size; - csw.Status = SCSI_PASSED; + csw.Status = CSW_PASSED; return true; } @@ -288,7 +300,7 @@ void USBMSD::sendCSW() { csw.Signature = CSW_Signature; writeNB(EPBULK_IN, (uint8_t *)&csw, sizeof(CSW), MAX_PACKET_SIZE_EPBULK); - stage = STATE_WAIT_CSW; + stage = WAIT_CSW; } bool USBMSD::requestSense (void) { @@ -321,7 +333,7 @@ } void USBMSD::fail() { - csw.Status = SCSI_FAILED; + csw.Status = CSW_FAILED; sendCSW(); } @@ -357,11 +369,11 @@ case READ12: if (infoTransfer()) { if ((cbw.Flags & 0x80)) { - stage = STATE_PROCESS_CBW; + stage = PROCESS_CBW; memoryRead(); } else { stallEndpoint(EPBULK_OUT); - csw.Status = SCSI_PHASE_ERROR; + csw.Status = CSW_ERROR; sendCSW(); } } @@ -370,27 +382,27 @@ case WRITE12: if (infoTransfer()) { if (!(cbw.Flags & 0x80)) { - stage = STATE_PROCESS_CBW; + stage = PROCESS_CBW; } else { stallEndpoint(EPBULK_IN); - csw.Status = SCSI_PHASE_ERROR; + csw.Status = CSW_ERROR; sendCSW(); } } break; case VERIFY10: if (!(cbw.CB[1] & 0x02)) { - csw.Status = SCSI_PASSED; + csw.Status = CSW_PASSED; sendCSW(); break; } if (infoTransfer()) { if (!(cbw.Flags & 0x80)) { - stage = STATE_PROCESS_CBW; + stage = PROCESS_CBW; memOK = true; } else { stallEndpoint(EPBULK_IN); - csw.Status = SCSI_PHASE_ERROR; + csw.Status = CSW_ERROR; sendCSW(); } } @@ -414,7 +426,7 @@ } } - csw.Status = SCSI_PASSED; + csw.Status = CSW_PASSED; sendCSW(); } @@ -426,12 +438,14 @@ if ((addr + n) > MemorySize) { n = MemorySize - addr; - stage = STATE_ERROR; + stage = ERROR; } + // we read an entire block if (!(addr%BlockSize)) blockRead(page, addr/BlockSize); + // write data which are in RAM writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK); addr += n; @@ -440,11 +454,11 @@ csw.DataResidue -= n; if (!length) { - stage = STATE_SEND_CSW; + stage = SEND_CSW; } - if (stage != STATE_PROCESS_CBW) { - csw.Status = SCSI_PASSED; + if (stage != PROCESS_CBW) { + csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED; } } @@ -474,7 +488,7 @@ length = n * BlockSize; if (!cbw.DataLength) { // host requests no data - csw.Status = SCSI_FAILED; + csw.Status = CSW_FAILED; sendCSW(); return false; } @@ -486,7 +500,7 @@ stallEndpoint(EPBULK_OUT); } - csw.Status = SCSI_FAILED; + csw.Status = CSW_FAILED; sendCSW(); return false; }
--- a/USBDevice/USBMSD/USBMSD.h Sat Nov 12 17:53:04 2011 +0000 +++ b/USBDevice/USBMSD/USBMSD.h Sun Nov 13 12:30:43 2011 +0000 @@ -32,11 +32,11 @@ // MSC Bulk-only Stage enum Stage { - STATE_READ_CBW, // wait a CBW - STATE_PROCESS_CBW, // process a CBW request - STATE_SEND_CSW, // send a CSW - STATE_ERROR, // error: out of memory - STATE_WAIT_CSW, // wait that a CSW has been effectively sent + READ_CBW, // wait a CBW + PROCESS_CBW, // process a CBW request + SEND_CSW, // send a CSW + ERROR, // error + WAIT_CSW, // wait that a CSW has been effectively sent }; // Bulk-only CBW