⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scsi.c

📁 USB驱动程序,和NANDflash的驱动, 包括文件系统,cpu是arm
💻 C
字号:
/************************************************
 * NAME    : 44BLIB.C				            *
 ************************************************/

#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\def.h"
#include "..\inc\option.h"
#include "..\inc\Hal.h"
volatile TPBLK_STRUC TPBulk_Block;
volatile extern unsigned char DBUF[BUFFER_LENGTH];
volatile extern SYS_INFO_BLOCK DeviceInfo;
#define     TPBulk_CBW			TPBulk_Block.TPBulk_CommandBlock
#define	    RBC_CDB			TPBulk_CBW.cdbRBC
#define     TPBulk_CSW			TPBulk_Block.TPBulk_CommandStatus
int hi;
unsigned char SPC_Inquiry(void)
{
	#define cdbInquirySPC RBC_CDB.SpcCdb_Inquiry
	int i;
	unsigned char *p;
	TPBulk_CBW.dCBW_Signature=CBW_SIGNATURE;
	TPBulk_CBW.dCBW_Tag=0xde24a660;
	TPBulk_CBW.dCBW_DataXferLen=0x00000024;
	TPBulk_CBW.bCBW_Flag=0x80;
	TPBulk_CBW.bCBW_LUN=0;
	TPBulk_CBW.bCBW_CDBLen=0x06;//sizeof(INQUIRY_SPC);
	
	cdbInquirySPC.OperationCode=SPC_CMD_INQUIRY;
	cdbInquirySPC.EnableVPD=0;
	cdbInquirySPC.CmdSupportData=0;
	cdbInquirySPC.PageCode=0;
	cdbInquirySPC.AllocationLen=0x24;
	cdbInquirySPC.Control=0;
	for(p=(unsigned char *)DBUF,i=0;i<0x20;i++)
	{
		if(i==15)continue;
		*p++=*((unsigned char *)&TPBulk_CBW+i);
	}
	if(!epBulkSend((unsigned char *)DBUF,sizeof(TPBulk_CBW)-1,0))
		return FALSE;
	Delay(450);
	if(!epBulkRcv((unsigned char *)DBUF,38))
		return FALSE;
	if(!epBulkRcv((unsigned char *)&TPBulk_CSW,13))
		return FALSE;
	////////////////////////////////
	#undef cdbInquirySPC
	return TRUE;	
}
unsigned char SPC_TestUnit(void)
{
#define cdbTestUnit RBC_CDB.SpcCdb_TestUnit	
	int i;
	unsigned char *p;
	//unsigned char retStatus=FALSE;
	TPBulk_CBW.dCBW_Signature=CBW_SIGNATURE;
	TPBulk_CBW.dCBW_Tag=0xde24a660;
	TPBulk_CBW.dCBW_DataXferLen=0x00000000;
	TPBulk_CBW.bCBW_Flag=0x00;
	TPBulk_CBW.bCBW_LUN=0;
	TPBulk_CBW.bCBW_CDBLen=sizeof(TEST_UNIT_SPC);
	/////////////////////////////////////
	cdbTestUnit.OperationCode=SPC_CMD_TESTUNITREADY;
	//////////////////////////////////////
	for(p=(unsigned char *)DBUF,i=0;i<0x20;i++)
	{
		if(i==15)continue;
		*p++=*((unsigned char *)&TPBulk_CBW+i);
	}
	if(!epBulkSend((unsigned char *)DBUF,sizeof(TPBulk_CBW)-1,0))
		return FALSE;
	Delay(300);
	if(!epBulkRcv((unsigned char *)&TPBulk_CSW,13))
		return FALSE;
#undef cdbTestUnit
////////////////////////////
	return TRUE;
}
unsigned char SPC_LockMedia(void)
{
#define cdbLockSPC RBC_CDB.SpcCdb_Remove	
	int i;
	unsigned char *p;
	//unsigned char retStatus=FALSE;
	TPBulk_CBW.dCBW_Signature=CBW_SIGNATURE;
	TPBulk_CBW.dCBW_Tag=0xde24a660;
	TPBulk_CBW.dCBW_DataXferLen=0x00000000;
	TPBulk_CBW.bCBW_Flag=0x00;
	TPBulk_CBW.bCBW_LUN=0;
	TPBulk_CBW.bCBW_CDBLen=sizeof(MEDIA_REMOVAL_SPC);
	///////////////////////////////////////////
	cdbLockSPC.OperationCode=SPC_CMD_PRVENTALLOWMEDIUMREMOVAL;
	cdbLockSPC.Prevent=1;
//	WordSwap(cdbLockSPC.OperationCode);
	///////////////////////////////////////////
	for(p=(unsigned char *)DBUF,i=0;i<0x20;i++)
	{
		if(i==15)continue;
		*p++=*((unsigned char *)&TPBulk_CBW+i);
	}
	if(!epBulkSend((unsigned char *)DBUF,sizeof(TPBulk_CBW)-1,0))
		return FALSE;
	Delay(250);
	
	if(!epBulkRcv((unsigned char *)&TPBulk_CSW,13))
		return FALSE;
#undef cdbLockSPC
/////////////////////////////
	return TRUE;
}
unsigned char SPC_RequestSense(void)
{
#define cdbRequestSenseSPC RBC_CDB.SpcCdb_RequestSense	
	int i;
	unsigned char *p;
	//unsigned char retStatus=FALSE;
	TPBulk_CBW.dCBW_Signature=CBW_SIGNATURE;
	TPBulk_CBW.dCBW_Tag=0xde24a660;
	TPBulk_CBW.dCBW_DataXferLen=0x0000000e;
	TPBulk_CBW.bCBW_Flag=0x80;
	TPBulk_CBW.bCBW_LUN=0;
	TPBulk_CBW.bCBW_CDBLen=sizeof(REQUEST_SENSE_SPC);
		
	/////////////////////////////////////
	cdbRequestSenseSPC.OperationCode=SPC_CMD_REQUESTSENSE;
	cdbRequestSenseSPC.AllocationLen=0x0e;
	//////////////////////////////////////
	for(p=(unsigned char *)DBUF,i=0;i<0x20;i++)
	{
		if(i==15)continue;
		*p++=*((unsigned char *)&TPBulk_CBW+i);
	}
	if(!epBulkSend((unsigned char *)DBUF,sizeof(TPBulk_CBW)-1,0))
		return FALSE;
	Delay(250);
	//len=36;
	if(!epBulkRcv((unsigned char *)DBUF,18))
		return FALSE;
	Delay(250);
	if(!epBulkRcv((unsigned char *)&TPBulk_CSW,13))
		return FALSE;
/////////////////////////////
	return TRUE;
	
#undef cdbRequestSenseSPC
}

unsigned char RBC_ReadCapacity(void)
{
#define cdbReadCap RBC_CDB.RbcCdb_ReadCapacity	
	int i;
	unsigned char *p;
	//unsigned char retStatus=FALSE;
	TPBulk_CBW.dCBW_Signature=CBW_SIGNATURE;
	TPBulk_CBW.dCBW_Tag=0xde24a660;
	TPBulk_CBW.dCBW_DataXferLen=0x00000008;
	TPBulk_CBW.bCBW_Flag=0x80;
	TPBulk_CBW.bCBW_LUN=0;	
	TPBulk_CBW.bCBW_CDBLen=10;//sizeof(READ_CAPACITY_RBC);
	/////////////////////////////////////
	cdbReadCap.OperationCode=RBC_CMD_READCAPACITY;
	/////////////////////////////////////
	for(p=(unsigned char *)DBUF,i=0;i<0x20;i++)
	{
		if(i==15)continue;
		*p++=*((unsigned char *)&TPBulk_CBW+i);
	}
	if(!epBulkSend((unsigned char *)DBUF,sizeof(TPBulk_CBW)-1,0))
		return FALSE;
	Delay(250);
	//len=36;
	if(!epBulkRcv((unsigned char *)DBUF,8))
		return FALSE;
	Delay(250);
	if(!epBulkRcv((unsigned char *)&TPBulk_CSW,13))
		return FALSE;
#undef cdbReadCap
/////////////////////////////
	return TRUE;
}
unsigned char RBC_Read(unsigned long lba,unsigned char len,unsigned char *pBuffer)
{
#define cdbRead RBC_CDB.RbcCdb_Read	
	int i;
	unsigned char *p;
	hi=0;
	TPBulk_CBW.dCBW_Signature=CBW_SIGNATURE;
	TPBulk_CBW.dCBW_Tag=0xde24a660;
	TPBulk_CBW.dCBW_DataXferLen=len*DeviceInfo.BPB_BytesPerSec;
	TPBulk_CBW.bCBW_Flag=0x80;
	TPBulk_CBW.bCBW_LUN=0;
	TPBulk_CBW.bCBW_CDBLen=10;//sizeof(READ_RBC);
	/////////////////////////////////////
	cdbRead.OperationCode=RBC_CMD_READ10;
	cdbRead.VendorSpecific=0;
	cdbRead.LBA.LBA_W32=DWordSwap(lba);
	cdbRead.XferLength=len;
	//////////////////////////////////////
	for(p=(unsigned char *)DBUF,i=0;i<0x22;i++)
	{
		if(i==15)continue;
		if(i==18)continue;
		if(i==19)continue;
		*p++=*((unsigned char *)&TPBulk_CBW+i);
	}
	if(!epBulkSend((unsigned char *)DBUF,sizeof(TPBulk_CBW)-1,0))
		return FALSE;
//	Delay(18);
	i=100;
	while(i--)
	{
		Delay(1);
		hi++;
		if(epBulkRcv(pBuffer,len*DeviceInfo.BPB_BytesPerSec))
			break;
	}
	if(i==0)return FALSE;
	Delay(5);
	if(!epBulkRcv((unsigned char *)&TPBulk_CSW,13))
		return FALSE;
#undef cdbRead
	return TRUE;
}
unsigned char RBC_Write(unsigned long lba,unsigned char len,unsigned char *pBuffer,unsigned char timewait)
{
#define cdbWrite RBC_CDB.RbcCdb_Write	
	int i,waittime,endtime;
	unsigned char *p;
	unsigned char tempBuf[64];
	static unsigned long plba=0;
	if(timewait==FALSE)
	{
		if(plba==0)
		{
	//		Delay(1300);
			waittime=90;
	//		endtime=400;
		}
		else if(plba==lba)
		{
	//		Delay(1300);
			waittime=90;
	//		endtime=400;
		}
		else
		{
	//		Delay(1000);
			waittime=800;
	//		endtime=2000;
		}
	}
	else
	{
		waittime=2000;
	}
	plba=lba+len;
	
	TPBulk_CBW.dCBW_Signature=CBW_SIGNATURE;
	TPBulk_CBW.dCBW_Tag=0xc177D9b4;
	TPBulk_CBW.dCBW_DataXferLen=len*DeviceInfo.BPB_BytesPerSec;
	TPBulk_CBW.bCBW_Flag=0x0;
	TPBulk_CBW.bCBW_LUN=0;
	TPBulk_CBW.bCBW_CDBLen=10;//sizeof(WRITE_RBC);
	/////////////////////////////////////
	cdbWrite.OperationCode=RBC_CMD_WRITE10;
	cdbWrite.VendorSpecific=0;
	cdbWrite.LBA.LBA_W32=DWordSwap(lba);
	cdbWrite.XferLength=len;
	cdbWrite.Reserved2=0;
	cdbWrite.Control=0;
	//////////////////////////////////////
	for(p=(unsigned char *)tempBuf,i=0;i<0x22;i++)
	{
		if(i==15)continue;
		if(i==18)continue;
		if(i==19)continue;
		*p++=*((unsigned char *)&TPBulk_CBW+i);
	}
	if(!epBulkSend((unsigned char *)tempBuf,sizeof(TPBulk_CBW)-1,0))
		return FALSE;
	Delay(waittime);
	if(!epBulkSend(pBuffer,len*DeviceInfo.BPB_BytesPerSec,lba))
	{
		return FALSE;
	}
	i=100;

	while(i--)
	{
		if(epBulkRcv((unsigned char *)&TPBulk_CSW,13))
			break;
	}
	if(i==0)
		return FALSE;
#undef cdbWrite

/////////////////////////////
	return TRUE;
}
int EnumMassDev(void)
{
	int i,j;
	PMBR_BLOCK pMBR;
	BYTE buf[512*128];
//	PBPB_BLOCK pBPB;
	////////////////////////////////////////////////////
	if(!SPC_Inquiry())
		return FALSE;
	if(!SPC_TestUnit())
		return FALSE;
	if(!SPC_LockMedia())
		return FALSE;
	if(!SPC_RequestSense())
		return FALSE;
	if(!SPC_TestUnit())
		return FALSE;
	if(!RBC_ReadCapacity())
		return FALSE;
	pMBR=(PMBR_BLOCK)DBUF;
	DeviceInfo.BPB_BytesPerSec=512;
	if(!SPC_RequestSense())
		return FALSE;
	if(!SPC_TestUnit())
		return FALSE;
	if(!RBC_ReadCapacity())
		return FALSE;
	i=30;
	while(i--)
	if(!SPC_TestUnit())
        return FALSE;
    if(!SPC_RequestSense())
		return FALSE;

/*	for(i=0;i<512*16;i++)
		buf[i]=i%0x100;
	RBC_Write(0x39D-128,128,(unsigned char *)buf,TRUE);
	i=0;
	for(j=0;j<300;j++)
	{
		while(!RBC_Write(0x39D+j*128,128,(unsigned char *)buf,FALSE))
		{
			i++;
			Delay(1000);
			if(i>10)
				break;
		}
		if(i>10)
			break;	
	}
	if(!RBC_Read(1,1,(unsigned char *)DBUF))
		return FALSE;
*/	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -