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

📄 usbout.c

📁 基于arm9的,usb设备(u盘)的读写操作实现.
💻 C
字号:
/****************************************************************
 NAME: usbout.c
 DESC: USB bulk-OUT operation related functions
 HISTORY:
 Mar.25.2002:purnnamu: ported for S3C2410X.
 Mar.27.2002:purnnamu: DMA is enabled.
 ****************************************************************/
 
#include <string.h>
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"

#include "2410usb.h"
#include "usbmain.h"
#include "usb.h"
#include "usblib.h"
#include "usbsetup.h"
#include "usbout.h"

#include "Globe.h"


//static void PrintEpoPkt(U8 *pt,int cnt);
static void RdPktEp3_CheckSum(U8 *buf,int num);



// ===================================================================
// All following commands will operate in case 
// - out_csr3 is valid.
// ===================================================================

 

#define CLR_EP3_OUT_PKT_READY() rOUT_CSR1_REG= ( out_csr3 &(~ EPO_WR_BITS)\
					&(~EPO_OUT_PKT_READY) ) 
#define SET_EP3_SEND_STALL()	rOUT_CSR1_REG= ( out_csr3 & (~EPO_WR_BITS)\
					| EPO_SEND_STALL) )
#define CLR_EP3_SENT_STALL()	rOUT_CSR1_REG= ( out_csr3 & (~EPO_WR_BITS)\
					&(~EPO_SENT_STALL) )
#define FLUSH_EP3_FIFO() 	rOUT_CSR1_REG= ( out_csr3 & (~EPO_WR_BITS)\
					|EPO_FIFO_FLUSH) )

// ***************************
// *** VERY IMPORTANT NOTE ***
// ***************************
// Prepare for the packit size constraint!!!

// EP3 = OUT end point. 

U8 ep3Buf[EP3_PKT_SIZE];
static U8 tempBuf[64+1];
static U8 TempMem[16];//Save address from PC

#ifdef _FOR_UDISK_
#include "f_type.h"
#include "F_driver.h"
#include "MASS_STORAGE.H"
u8 CBWTable[31] = {0};
u8 CBW_flg = 0;//0:none; 1:CBW; 2:PC out data; 3:PC in data
u8 Data_flg = 0;//0:non; 1:Data; 2:CSW; 3:write10 CSW
u8 Data_loop = 0;
u8 tempbuf[64] = {0};
u8 CSWbuf[13] = {0};
u8 *buffer;
int bufLen;

u8 DiskIndex;
u32 LBA_START,dCBWDaTrLen;
u16 LBA_Length;
u8 SCSICommand;
u8 buffer1[512*8];//it is very importent
extern void Mass_acces(void);
void Mass_acces(void)
{
	u32 i;
	u32 j;
	
	if(CBW_flg == 1)
	{
		CBW_flg = 0;
		GetCBWTable(CBWTable, &DiskIndex,&LBA_START, &LBA_Length, &SCSICommand);
		dCBWDaTrLen=(u32)(((u32)(CBWTable[11]<<8)+(CBWTable[10]))<<15);
       	dCBWDaTrLen=(dCBWDaTrLen<<1)+(u32)((u32)(CBWTable[9]<<8)+(CBWTable[8])) ; // copmpute the length
       	
       	//Uart_Printf("SCSICommand: 0x%x\n",SCSICommand);
		switch(SCSICommand)
		{
			case Format_Unit://0x04
				Uart_Printf("SCSICommand: 0x%x\n",SCSICommand);
				break;
			case Inquiry://0x12
				//Uart_Printf("dCBWDaTrLen:%d\n",dCBWDaTrLen);
				FillInquiryTable(tempbuf);
				FillCSWTable(CSWbuf);
				Data_flg = 2;
				PrepareEp1Fifo_1(tempbuf,36);
				//PrepareEp1Fifo_1(DISK_INF,36);
				break;
			case Read_10://0x28
				//Uart_Printf("1.LBA_START: %d\tLBA_Length: %d\n",LBA_START,LBA_Length);
				F_ReadSector(hFile,LBA_START,1,buffer1);
				bufLen = LBA_Length*512-EP1_PKT_SIZE;
				for(j=0;j<EP1_PKT_SIZE;j++)
				{
					tempbuf[j] = buffer1[j];
				}
				buffer = buffer1;
				buffer += EP1_PKT_SIZE;
				FillCSWTable(CSWbuf);
				Data_flg = 1;
				Data_loop = 0;
				PrepareEp1Fifo_1(tempbuf,EP1_PKT_SIZE);
				break;
			case Write_10://0x2a
				//Uart_Printf("2.LBA_START: %d\tLBA_Length: %d\tdCBWDaTrLen:%d\n",LBA_START,LBA_Length,dCBWDaTrLen);
				buffer = buffer1;
				bufLen = LBA_Length*512;
				FillCSWTable(CSWbuf);
				Data_flg = 1;
				Data_loop = 0;
				break;
			case Read_Format_capacity://0x23
				FillCSWTable(CSWbuf);
				CSWbuf[8]=(u32)(dCBWDaTrLen-0x04);
				Data_flg = 2;
				PrepareEp1Fifo_1(REQUEST_23,0x14/*12*/);
				break;
			case Read_Capacity://0x25
				//Uart_Printf("CBWDaTrLen:%d\n",dCBWDaTrLen);
				if(FillCapacityTable(tempbuf))
				{
					Uart_Printf("FillCapacityTable error!\n");
				}
				FillCSWTable(CSWbuf);
				Data_flg = 2;
				PrepareEp1Fifo_1(tempbuf,0x08);
				break;
			case Request_Sense://0x03
				FillCSWTable(CSWbuf);
				Data_flg = 2;
				PrepareEp1Fifo_1(SENSE,0x12);
				break;
			case Test_Unit_Ready://0x00
			//case Verify://0x2f
			case Medium_Removal://0x1e
				FillCSWTable(CSWbuf);
				for(j=4;j<8;j++)//TAG
				{
					CSWbuf[j] = CBWTable[j];
				}
				Data_flg = 0;
				PrepareEp1Fifo_1(CSWbuf,13);
				break;
				///*
			case Mode_Select://0x55
			//case Mode_Sense:
				FillCSWTable(CSWbuf);
				for(j=4;j<8;j++)//TAG
				{
					CSWbuf[j] = CBWTable[j];
				}
				Data_flg = 2;
				PrepareEp1Fifo_1(tempbuf,0);
				break;
			case Mode_Sence_06://0x1a
				FillCSWTable(CSWbuf);
				CSWbuf[8]=(u32)(dCBWDaTrLen-0x04); 
				Data_flg = 2;
				PrepareEp1Fifo_1(REQUEST_1A,0x04);
				break;
			default:
				break;
		}
	}
	else if(CBW_flg == 2)
	{
		CBW_flg = 0;
		//Uart_Printf("write10\n");
		if(Data_flg==1)
	    	{
			if(bufLen>EP3_PKT_SIZE)
	    		{
	    			for(i=0;i<EP3_PKT_SIZE;i++)
	    			{
	    				//if(!(i%16))
					//	Uart_Printf("\n");
	    				 buffer[i] = ep3Buf[i];
	    				//Uart_Printf("0x%2x ",buffer[i+EP3_PKT_SIZE*Data_loop]);
	    			}
					//Uart_Printf("\n");
				bufLen-=EP3_PKT_SIZE;
				if(((Data_loop+1)%8==0)&&/*bufLen>EP1_PKT_SIZE*/(LBA_Length>(Data_loop+1)/8))//one sector read
				{
					F_WriteSector(hFile,LBA_START,1,buffer1);
					LBA_START++;
					buffer = buffer1;
				}
				else 
					buffer+=EP3_PKT_SIZE;
				
				Data_loop ++;
	    		}
			else
			{
	    			for(i=0;i<bufLen;i++)
	    			{
	    			//	if(!(i%16))
				//		Uart_Printf("\n");
	    				buffer[i] = ep3Buf[i];
				//	Uart_Printf("0x%2x ",buffer[i]);
	    			}
				//Uart_Printf("\n\n");
				bufLen = 0;
				//if(((Data_loop+1)%8==0)&&(LBA_Length>(Data_loop+1)/8))//one sector read
				{
					F_WriteSector(hFile,LBA_START,1,buffer1);
					LBA_START++;
					buffer = buffer1;
				}
				for(j=4;j<8;j++)//TAG
				{
					CSWbuf[j] = CBWTable[j];
				}
				Data_flg = 3;
				PrepareEp1Fifo_1(CSWbuf,13);
			}
	    	}
	}
	else if(CBW_flg == 3)
	{
		CBW_flg = 0;
		//Uart_Printf("read10\n");
		if(Data_flg==1)
	    	{
			Data_loop ++;
	    		if(bufLen>EP1_PKT_SIZE)
	    		{
	    			for(i=0;i<EP1_PKT_SIZE;i++)
	    			{
	    				tempbuf[i] = buffer[i];
	    			}
				bufLen-=EP1_PKT_SIZE;
				if(((Data_loop+1)%8==0)&&/*bufLen>EP1_PKT_SIZE*/(LBA_Length>(Data_loop+1)/8))//one sector read
				{
					LBA_START++;
					F_ReadSector(hFile,LBA_START/*+((Data_loop+1)/8)*/,1,buffer1);
					buffer = buffer1;
				}
				else 
					buffer+=EP1_PKT_SIZE;
	    			PrepareEp1Fifo_1(tempbuf,EP1_PKT_SIZE); 
	    		}
			else
			{
	    			for(i=0;i<bufLen;i++)
	    			{
	    				//if(!(i%16))
					//	Uart_Printf("\n");
	    				tempbuf[i] = buffer[i];
					//Uart_Printf("0x%2x ",tempbuf[i]);
	    			}
				//Uart_Printf("\n\n");
	    			PrepareEp1Fifo_1(tempbuf,bufLen); 
				bufLen = 0;
				Data_flg = 2;
			}
	    	}
		else if(Data_flg==2)
		{
			for(i=4;i<8;i++)//TAG
			{
				CSWbuf[i] = CBWTable[i];
			}
			Data_flg=0;
			PrepareEp1Fifo_1(CSWbuf,13); 
		}
		else if(Data_flg==3)
		{
			Data_flg=0;
			//Uart_Printf("write10 CSW write OK!\n");
		}
	}
	CBW_flg = 0;
}
#endif


void Ep3Handler(void)
{
    U8 out_csr3;
    int fifoCnt;
    rINDEX_REG=3;

    out_csr3=rOUT_CSR1_REG;
    
    //DbgPrintf("<3:%x]",out_csr3);
#ifdef _FOR_UDISK_
	if(USBD_flg==0)
	{
	    if(out_csr3 & EPO_OUT_PKT_READY)
	    {   
			fifoCnt=rOUT_FIFO_CNT1_REG; 

			#if 0
			RdPktEp3(ep3Buf,fifoCnt);
			//PrintEpoPkt(ep3Buf,fifoCnt);
			#else

			if(DownloadFileSize==0)
			{
				//DownloadAddress=(U32)TempMem;
				//downPt=(unsigned char *)DownloadAddress;
				downPt=TempMem;
	   			RdPktEp3((U8 *)downPt,8); 	
				downPt=TempMem;  	    
	   			if(DownLoadOK==1)
	   			{
					DownloadAddress=TempDownloadAddress;
				}
				else
				{	//extern volatile U32 DownloadAddress,
					
		    		DownloadAddress=	
					 *((U8 *)(downPt+0))+
					(*((U8 *)(downPt+1))<<8)+
					(*((U8 *)(downPt+2))<<16)+
					(*((U8 *)(downPt+3))<<24);
				}
				DownloadFileSize=
				 *((U8 *)(downPt+4))+
				(*((U8 *)(downPt+5))<<8)+
				(*((U8 *)(downPt+6))<<16)+
				(*((U8 *)(downPt+7))<<24);

				checkSum=0;//extern volatile U16 checkSum;
				downPt=(U8 *)DownloadAddress;

	  			RdPktEp3_CheckSum((U8 *)downPt,fifoCnt-8);//The first 8-bytes are deleted (have been read).	    
	  			downPt+=fifoCnt-8;  
	  			
	  			#if USBDMA
	     			//CLR_EP3_OUT_PKT_READY() is not executed. 
	     			//So, USBD may generate NAK until DMA2 is configured for USB_EP3;
	     			rINTMSK|=BIT_USBD;//for debug
	      			return;	
	  			#endif	
			}
			else
			{
				#if USBDMA    	
	//				Uart_Printf("<ERROR>");
				#endif    
				RdPktEp3_CheckSum((U8 *)downPt,fifoCnt); 	    
				downPt+=fifoCnt;//fifoCnt=32
			}
			#endif

	   		CLR_EP3_OUT_PKT_READY();
			return;
	    }//End of if(out_csr3 & EPO_OUT_PKT_READY)
	}
	else if(USBD_flg==1)
	{
		if(out_csr3 & EPO_OUT_PKT_READY)
		 {
		 	fifoCnt=rOUT_FIFO_CNT1_REG; 

			RdPktEp3(ep3Buf,fifoCnt);
			if(!CheckCBWTable(ep3Buf))//CBW
			{
				u8 j;
				for(j=0;j<31;j++)
				{
					CBWTable[j] = ep3Buf[j];
				}
				CBW_flg = 1;
			}
			else//PC write data
			{
				CBW_flg = 2;
			}
			Mass_acces();
			return;
		 }
	}
#else
	if(out_csr3 & EPO_OUT_PKT_READY)
    	{   
		fifoCnt=rOUT_FIFO_CNT1_REG; 

		#if 0
		RdPktEp3(ep3Buf,fifoCnt);
		//PrintEpoPkt(ep3Buf,fifoCnt);
		#else

		if(DownloadFileSize==0)
		{
			downPt=TempMem;
   			RdPktEp3((U8 *)downPt,8); 	
			downPt=TempMem;  	    
   			if(DownLoadOK==1)
   			{
				DownloadAddress=TempDownloadAddress;
			}
			else
			{	//extern volatile U32 DownloadAddress,
				
	    		DownloadAddress=	
				 *((U8 *)(downPt+0))+
				(*((U8 *)(downPt+1))<<8)+
				(*((U8 *)(downPt+2))<<16)+
				(*((U8 *)(downPt+3))<<24);
			}
			DownloadFileSize=
			 *((U8 *)(downPt+4))+
			(*((U8 *)(downPt+5))<<8)+
			(*((U8 *)(downPt+6))<<16)+
			(*((U8 *)(downPt+7))<<24);

			checkSum=0;//extern volatile U16 checkSum;
			downPt=(U8 *)DownloadAddress;

  			RdPktEp3_CheckSum((U8 *)downPt,fifoCnt-8);//The first 8-bytes are deleted (have been read).	    
  			downPt+=fifoCnt-8;  
  			
  			#if USBDMA
     			//CLR_EP3_OUT_PKT_READY() is not executed. 
     			//So, USBD may generate NAK until DMA2 is configured for USB_EP3;
     			rINTMSK|=BIT_USBD;//for debug
      			return;	
  			#endif	
		}
		else
		{
			#if USBDMA    	
//				Uart_Printf("<ERROR>");
			#endif    
			RdPktEp3_CheckSum((U8 *)downPt,fifoCnt); 	    
			downPt+=fifoCnt;//fifoCnt=32
		}
		#endif

   		CLR_EP3_OUT_PKT_READY();
		return;
    	}//End of if(out_csr3 & EPO_OUT_PKT_READY)
#endif  

    //I think that EPO_SENT_STALL will not be set to 1.
    if(out_csr3 & EPO_SENT_STALL)
    {   
   		//DbgPrintf("[STALL]");
   		CLR_EP3_SENT_STALL();
   		return;
    }	
}

void RdPktEp3_CheckSum(U8 *buf,int num)
{
    int i;
    	
    for(i=0;i<num;i++)
    {
        buf[i]=(U8)rEP3_FIFO;
        checkSum+=buf[i];
    }
}



void __irq IsrDma2(void)
{
    U8 out_csr3;
    U32 nextTotalDmaCount;
    U8 saveIndexReg=rINDEX_REG;//Push to buffer

    rINDEX_REG=3;
    out_csr3=rOUT_CSR1_REG;
    
    ClearPending(BIT_DMA2);	    
    
    totalDmaCount+=0x80000;//extern volatile U32 totalDmaCount;

    if(totalDmaCount>=DownloadFileSize)// is last?
    {
    	totalDmaCount=DownloadFileSize;
	
    	ConfigEp3IntMode();	

    	if(out_csr3& EPO_OUT_PKT_READY)
    	{
   	    	CLR_EP3_OUT_PKT_READY();
		}
        rINTMSK|=BIT_DMA2;  
        rINTMSK&=~(BIT_USBD);  
    }
    else
    {
    	if((totalDmaCount+0x80000)<DownloadFileSize)	
    	{
	    	nextTotalDmaCount=totalDmaCount+0x80000;
    
    	    if((nextTotalDmaCount+0x80000)<DownloadFileSize)
    		{
       			//for (2~n)th autoreload.	 
				while((rDSTAT2&0xfffff)==0); //wait until autoreload occurs.
				rDIDST2=((U32)DownloadAddress+nextTotalDmaCount-8);  
				rDIDSTC2=(0<<1)|(0<<0);  
    	    	rDCON2=rDCON2&~(0xfffff)|(0x80000); 
    	    	
    	    	while(rEP3_DMA_TTC<0xfffff)
    	    	{
    	    	    rEP3_DMA_TTC_L=0xff;
    	    	    rEP3_DMA_TTC_M=0xff;
    	    	    rEP3_DMA_TTC_H=0xf;
    	    	    //0xfffff;
    	    	}
    	    }
 	   		else
 	   		{
				while((rDSTAT2&0xfffff)==0); //wait until autoreload occurs.
   	       		rDIDST2=((U32)DownloadAddress+nextTotalDmaCount-8);  
      	        rDIDSTC2=(0<<1)|(0<<0);  
      	        rDCON2=rDCON2&~(0xfffff)|(DownloadFileSize-nextTotalDmaCount); 		
    	    	while(rEP3_DMA_TTC<0xfffff)
    	    	{
    	    	    rEP3_DMA_TTC_L=0xff;
    	    	    rEP3_DMA_TTC_M=0xff;
    	    	    rEP3_DMA_TTC_H=0xf;
    	    	    //0xfffff;
    	    	}
	    	}
		}
		else
		{
	 	   while((rDSTAT2&0xfffff)==0); //wait until autoreload occurs.
  	 	   rDIDST2=((U32)DownloadAddress+DownloadFileSize-8);  //for next autoreload.	    		
  	 	   rDIDSTC2=(0<<1)|(0<<0);
  	 	   rDCON2=rDCON2&~(0xfffff)|(0); 		
		   //There is no 2nd autoreload. This will not be used.  	    
	       //rDMA_TX+=0x0; //USBD register		
		}
    }
    rINDEX_REG=saveIndexReg;
}


void ClearEp3OutPktReady(void)
{
    U8 out_csr3;
    rINDEX_REG=3;//USB EndPoint3
    out_csr3=rOUT_CSR1_REG;
    CLR_EP3_OUT_PKT_READY();//IN_CSR1_REG(0x52000190)=0x0
}

⌨️ 快捷键说明

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