keil_examples/USBMem/mscuser.c
/*
 *  MSC Get Command Block Wrapper Callback
 *    Parameters:      None (global variables)
 *    Return Value:    None
 */
MSC Get Command Block Wrapper Callback
Parameters:      None (global variables)
Return Value:    None
MSC Get Command Block Wrapper Callback Parameters : None ( global variables ) Return Value : None
void MSC_GetCBW (void) {
  uint32_t n;
  for (n = 0; n < BulkLen; n++) {
    *((uint8_t *)&CBW + n) = BulkBuf[n];
  }
  if ((BulkLen == sizeof(CBW)) && (CBW.dSignature == MSC_CBW_Signature)) {
    CSW.dTag = CBW.dTag;
    CSW.dDataResidue = CBW.dDataLength;
    if ((CBW.bLUN      != 0) || 
        (CBW.bCBLength <  1) || 
        (CBW.bCBLength > 16)   ) {
fail: 
      CSW.bStatus = CSW_CMD_FAILED;
      MSC_SetCSW();
    } else {
      switch (CBW.CB[0]) {
        case SCSI_TEST_UNIT_READY:
          MSC_TestUnitReady();
          break;
        case SCSI_REQUEST_SENSE:
          MSC_RequestSense();
          break;
        case SCSI_FORMAT_UNIT:
          goto fail;
        case SCSI_INQUIRY:
          MSC_Inquiry();
          break;
        case SCSI_START_STOP_UNIT:
          goto fail;
        case SCSI_MEDIA_REMOVAL:
          goto fail;
        case SCSI_MODE_SELECT6:
          goto fail;
        case SCSI_MODE_SENSE6:
          MSC_ModeSense6();
          break;
        case SCSI_MODE_SELECT10:
          goto fail;
        case SCSI_MODE_SENSE10:
          MSC_ModeSense10();
          break;
        case SCSI_READ_FORMAT_CAPACITIES:
          MSC_ReadFormatCapacity();
          break;
        case SCSI_READ_CAPACITY:
          MSC_ReadCapacity();
          break;
        case SCSI_READ10:
		case SCSI_READ12:
          if (MSC_RWSetup()) {
            if ((CBW.bmFlags & 0x80) != 0) {
              BulkStage = MSC_BS_DATA_IN;
              MSC_MemoryRead();
            } else {                       
              MSC_SetStallEP(MSC_EP_OUT);
              CSW.bStatus = CSW_PHASE_ERROR;
              MSC_SetCSW();
            }
          }
          break;
        case SCSI_WRITE10:
		case SCSI_WRITE12:
          if (MSC_RWSetup()) {
            if ((CBW.bmFlags & 0x80) == 0) {
              BulkStage = MSC_BS_DATA_OUT;
            } else {                       
              MSC_SetStallEP(MSC_EP_IN);
              CSW.bStatus = CSW_PHASE_ERROR;
              MSC_SetCSW();
            }
          }
          break;
        case SCSI_VERIFY10:
          if ((CBW.CB[1] & 0x02) == 0) {
            CSW.bStatus = CSW_CMD_PASSED;
            MSC_SetCSW();
            break;
          }
          if (MSC_RWSetup()) {
            if ((CBW.bmFlags & 0x80) == 0) {
              BulkStage = MSC_BS_DATA_OUT;
              MemOK = TRUE;
            } else {
              MSC_SetStallEP(MSC_EP_IN);
              CSW.bStatus = CSW_PHASE_ERROR;
              MSC_SetCSW();
            }
          }
          break;
        default:
          goto fail;
      }
    }
  } else {
    MSC_SetStallEP(MSC_EP_IN);
    USB_EndPointStall |=  (MSC_EP_IN  & 0x80) ? ((1 << 16) << (MSC_EP_IN  & 0x0F)) : (1 << MSC_EP_IN);
    MSC_SetStallEP(MSC_EP_OUT);
    USB_EndPointStall |=  (MSC_EP_OUT & 0x80) ? ((1 << 16) << (MSC_EP_OUT & 0x0F)) : (1 << MSC_EP_OUT);
    BulkStage = MSC_BS_ERROR;
  }
}
void MSC_GetCBW ( void ) { uint32_t n ; for ( n = 0 ; n < BulkLen ; n ++ ) { * ( ( uint8_t * ) & CBW + n ) = BulkBuf [ n ] ; } if ( ( BulkLen == sizeof ( CBW ) ) && ( CBW . dSignature == MSC_CBW_Signature ) ) { CSW . dTag = CBW . dTag ; CSW . dDataResidue = CBW . dDataLength ; if ( ( CBW . bLUN != 0 ) || ( CBW . bCBLength < 1 ) || ( CBW . bCBLength > 16 ) ) { fail : CSW . bStatus = CSW_CMD_FAILED ; MSC_SetCSW ( ) ; } else { switch ( CBW . CB [ 0 ] ) { case SCSI_TEST_UNIT_READY : MSC_TestUnitReady ( ) ; break ; case SCSI_REQUEST_SENSE : MSC_RequestSense ( ) ; break ; case SCSI_FORMAT_UNIT : goto fail ; case SCSI_INQUIRY : MSC_Inquiry ( ) ; break ; case SCSI_START_STOP_UNIT : goto fail ; case SCSI_MEDIA_REMOVAL : goto fail ; case SCSI_MODE_SELECT6 : goto fail ; case SCSI_MODE_SENSE6 : MSC_ModeSense6 ( ) ; break ; case SCSI_MODE_SELECT10 : goto fail ; case SCSI_MODE_SENSE10 : MSC_ModeSense10 ( ) ; break ; case SCSI_READ_FORMAT_CAPACITIES : MSC_ReadFormatCapacity ( ) ; break ; case SCSI_READ_CAPACITY : MSC_ReadCapacity ( ) ; break ; case SCSI_READ10 : case SCSI_READ12 : if ( MSC_RWSetup ( ) ) { if ( ( CBW . bmFlags & 0x80 ) != 0 ) { BulkStage = MSC_BS_DATA_IN ; MSC_MemoryRead ( ) ; } else { MSC_SetStallEP ( MSC_EP_OUT ) ; CSW . bStatus = CSW_PHASE_ERROR ; MSC_SetCSW ( ) ; } } break ; case SCSI_WRITE10 : case SCSI_WRITE12 : if ( MSC_RWSetup ( ) ) { if ( ( CBW . bmFlags & 0x80 ) == 0 ) { BulkStage = MSC_BS_DATA_OUT ; } else { MSC_SetStallEP ( MSC_EP_IN ) ; CSW . bStatus = CSW_PHASE_ERROR ; MSC_SetCSW ( ) ; } } break ; case SCSI_VERIFY10 : if ( ( CBW . CB [ 1 ] & 0x02 ) == 0 ) { CSW . bStatus = CSW_CMD_PASSED ; MSC_SetCSW ( ) ; break ; } if ( MSC_RWSetup ( ) ) { if ( ( CBW . bmFlags & 0x80 ) == 0 ) { BulkStage = MSC_BS_DATA_OUT ; MemOK = TRUE ; } else { MSC_SetStallEP ( MSC_EP_IN ) ; CSW . bStatus = CSW_PHASE_ERROR ; MSC_SetCSW ( ) ; } } break ; default : goto fail ; } } } else { MSC_SetStallEP ( MSC_EP_IN ) ; USB_EndPointStall |= ( MSC_EP_IN & 0x80 ) ? ( ( 1 << 16 ) << ( MSC_EP_IN & 0x0F ) ) : ( 1 << MSC_EP_IN ) ; MSC_SetStallEP ( MSC_EP_OUT ) ; USB_EndPointStall |= ( MSC_EP_OUT & 0x80 ) ? ( ( 1 << 16 ) << ( MSC_EP_OUT & 0x0F ) ) : ( 1 << MSC_EP_OUT ) ; BulkStage = MSC_BS_ERROR ; } }
MSC Get Command Block Wrapper Callback
    Parameters:      None (global variables)
    Return Value:    None
MSC Get Command Block Wrapper Callback Parameters : None ( global variables ) Return Value : None
/* Valid CBW */ /* direction mismatch */ /* direction mismatch */ // BYTCHK = 0 -> CRC Check (not implemented) /* Invalid CBW */ /* set EP to stay stalled */ /* set EP to stay stalled */