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

📄 bulkonly.c

📁 基于ARM7的U盘程序开发代码,可以
💻 C
字号:

#include "datatype.h"
#include "console.h"
#include "D12Config.h"
#include "D12CI.h"
#include "D12Driver.h"
#include "BulkOnly.h"
#include "fat.h"
 

CBW  cbw1;
CSW  csw1;
PUFI pufi1;
U8   SendBuf[64];

INT8U  ReqSense[18];
INT8U  LBA[MAXLEN]; //2Mbytes
DEVICE_MODE_PAGE DevModePage;

extern void Delay(U32 time);

INT8U  query_ret[] = {
	0x00,
	0x80,
	0x02,
	0x01,
	0x1F,
	0,0,0,
	'S','k','y','s','i','n','e',' ', 
	'-','-','J','e','f','f','-','-',  '-','-','w','u','q','i','-','-',
	0x31,0x2E,0x35,0x37 
};

INT8U  MODE00_RET[] = {
  0x00, 
  0x00,
  0x00,0x00
};


void exec_UFI()
{
	INT8U  i, *p, *q, cnt=0, len=0;
	U32  Addr=0, tmp=0, sum=0, old=0, FLAG=0;
	
	switch(pufi1->bOpCode)
	{
		case 0x12: //INQUIRY
			//printf("INQUIRY\n");
			if(((pufi1->bLun & 0xE0) == 0 ) && ((pufi1->dLBA[2]) == 0x24 ))
			{
				//printf("process INQUIRY...\n");
				
				D12_WriteEndpoint(5, 36, query_ret);
			  
			  csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - 0x24; 
			  csw1.bCSWStatus = 0x00;

			  D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			}	
		  break;
		
/*		case 0x04: //FORMAT
       //fat_format();
			  
			 ReqSense[2] = 0x00; //sense data : NO SENSE
 
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
*/			
		case 0x55: //MODE SELECT
			 ReqSense[2] = 0x00; //sense data : NO SENSE
 
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
			
		case 0x1A: //MODE SENSE(6)
       p = (INT8U*)pufi1;
       switch((p[2]&0x3F))
       {
       	case 0x00:
       		 D12_WriteEndpoint(5, 4, MODE00_RET);
       		break;
        case 0x01:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x12;
        	 D12_WriteEndpoint(5, 20, (INT8U*)DevModePage.MediumType);
        	break;
        case 0x05:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x26;
        	 for(i=0;i<8;i++)
        	   SendBuf[i] = DevModePage.MediumType[i];
        	 for(i=0;i<32;i++)
        	   SendBuf[8+i] = DevModePage.FlexDiskPage[i];
        	 D12_WriteEndpoint(5, 40, SendBuf);    
        	break;
        case 0x1B:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x12;
        	 for(i=0;i<8;i++)
        	   SendBuf[i] = DevModePage.MediumType[i];
        	 for(i=0;i<32;i++)
        	   SendBuf[8+i] = DevModePage.BlockAccessCapacity[i];
        	 D12_WriteEndpoint(5, 20, SendBuf);          	 
        	break;
        case 0x1C:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x0E;
        	 for(i=0;i<8;i++)
        	   SendBuf[i] = DevModePage.MediumType[i];
        	 for(i=0;i<32;i++)
        	   SendBuf[8+i] = DevModePage.TimerPage[i];
        	 D12_WriteEndpoint(5, 16, SendBuf);             	
        	break;
        case 0x3F:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x46;
        	 q = (INT8U*)&DevModePage;
        	 for(i=0;i<64;i++)
        	  SendBuf[i] = *q++;
        	 D12_WriteEndpoint(5, 64, SendBuf);  
        	 
        	 for(i=0;i<8;i++)
        	  SendBuf[i] = *q++;
        	 D12_WriteEndpoint(5, 8, SendBuf);         	 
        	break;					
       	default:
       		break;       	
       }
			 
			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - DevModePage.MediumType[1] - 2; 
			 csw1.bCSWStatus = 0x00;
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;	
		case 0x5A: //MODE SENSE(10)
			//printf("IN MODE SENSE.\n");
       p = (INT8U*)pufi1;
       switch((p[2]&0x3F))
       {
        case 0x01:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x12;
        	 D12_WriteEndpoint(5, 20, (INT8U*)DevModePage.MediumType);
        	break;
        case 0x05:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x26;
        	 for(i=0;i<8;i++)
        	   SendBuf[i] = DevModePage.MediumType[i];
        	 for(i=0;i<32;i++)
        	   SendBuf[8+i] = DevModePage.FlexDiskPage[i];
        	 D12_WriteEndpoint(5, 40, SendBuf);    
        	break;
        case 0x1B:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x12;
        	 for(i=0;i<8;i++)
        	   SendBuf[i] = DevModePage.MediumType[i];
        	 for(i=0;i<32;i++)
        	   SendBuf[8+i] = DevModePage.BlockAccessCapacity[i];
        	 D12_WriteEndpoint(5, 20, SendBuf);          	 
        	break;
        case 0x1C:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x0E;
        	 for(i=0;i<8;i++)
        	   SendBuf[i] = DevModePage.MediumType[i];
        	 for(i=0;i<32;i++)
        	   SendBuf[8+i] = DevModePage.TimerPage[i];
        	 D12_WriteEndpoint(5, 16, SendBuf);             	
        	break;
        case 0x3F:
        	 DevModePage.MediumType[0] = 0x00;
        	 DevModePage.MediumType[1] = 0x46;
        	 q = (INT8U*)&DevModePage;
        	 for(i=0;i<64;i++)
        	  SendBuf[i] = *q++;
        	 D12_WriteEndpoint(5, 64, SendBuf);  
        	 
        	 for(i=0;i<8;i++)
        	  SendBuf[i] = *q++;
        	 D12_WriteEndpoint(5, 8, SendBuf);         	 
        	break;					
       	default:
       		break;       	
       }
			 
			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - DevModePage.MediumType[1] - 2; 
			 csw1.bCSWStatus = 0x00;
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
			
		case 0x1E: //PREVENT-ALLOW MEDIUM REMOVAL
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);			
			break;
			
		case 0x28: //READ(10)
			 Addr = 0;
			 tmp  = pufi1->dLBA[0];
			 Addr += tmp<<24;
			 tmp  = pufi1->dLBA[1];
			 Addr += tmp<<16;
			 tmp  = pufi1->dLBA[2];
			 Addr += tmp<<8;			 
			 tmp  = pufi1->dLBA[3];
			 Addr += tmp; //Addr is the address of the data to write
			 Addr = Addr * 512;			 			 
       tmp  = pufi1->wLength[0]*256 + pufi1->wLength[1]; //length of data to receive
       tmp  = tmp * 512;
       old  = tmp;
 
       len=0; 
      while(tmp > 0){
       	 if(tmp > 64)
           {len = 64; tmp -= 64;}
         else 
         	 {len = (U8)tmp; tmp = 0;}  	 
	       for(cnt=0;cnt<len;cnt++)
	         SendBuf[cnt] = LBA[Addr++];           
	       D12_WriteEndpoint(5, len, SendBuf);	    //写数据到端点2
	      //printf("tmp = %d\n", tmp);  
		   }  
		   
			 ReqSense[2] = 0x00; //sense data : NO SENSE
 
			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - old; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
			
		case 0xA8: //READ(12)
			 Addr = 0;
			 tmp  = pufi1->dLBA[0];
			 Addr += tmp<<24;
			 tmp  = pufi1->dLBA[1];
			 Addr += tmp<<16;
			 tmp  = pufi1->dLBA[2];
			 Addr += tmp<<8;			 
			 tmp  = pufi1->dLBA[3];
			 Addr += tmp; //Addr is the address of the data to write
			 Addr = Addr * 512;			 			 
			 p = (INT8U *)pufi1; 			 
       tmp  =  (U32)p[6]<<24;//length of data to receive
       tmp  += (U32)p[7]<<16;
       tmp  += (U32)p[8]<<8;
       tmp  += (U32)p[9];
       tmp  = tmp * 512;
       old  = tmp;
       
       len=0;
       while(tmp > 0){
       	 if(tmp > 64)
           {len = 64; tmp -= 64;}
         else 
         	 {len = (U8)tmp; tmp = 0;} 
	       for(cnt=0;cnt<len;cnt++)
	         SendBuf[cnt] = LBA[Addr++];          
	       D12_WriteEndpoint(5, len, SendBuf);	    //端点2写数据
 
		   }  
		   
			 ReqSense[2] = 0x00; //sense data : NO SENSE
 
			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - old; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);				
			break;
			
		case 0x25: //READ CAPACITY 
			 //printf("READ CAPACITY : 0x25.\n");
			 SendBuf[0] = 0x00;
			 SendBuf[1] = 0x00;
			 SendBuf[2] = 0x0f;
			 SendBuf[3] = 0xff; //max block number is 0xfff.
			 SendBuf[4] = 0x00;
			 SendBuf[5] = 0x00;
			 SendBuf[6] = 0x02;
			 SendBuf[7] = 0x00;//512bytes
			 
			 D12_WriteEndpoint(5, 8, (INT8U*)SendBuf);
			 //printf("READ CAPACITY : Send 8 bytes over.\n"); 
			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - 0x08;
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			 //printf("READ CAPACITY : Send CSW over.\n");
			break;
			
		case 0x23: //READ FORMAT CAPACITIES
			 SendBuf[0] = 0x00;
			 SendBuf[1] = 0x00;
			 SendBuf[2] = 0x00;
			 SendBuf[3] = 0x08;
			 SendBuf[4] = 0x00;//BIT[4..7] number of blocks 2048KB
			 SendBuf[5] = 0x00;
			 SendBuf[6] = 0x10;
			 SendBuf[7] = 0x00;
			 SendBuf[8] = 0x02; //formatted media, currect capacity
			 SendBuf[9] = 0x00; //512bytes
			 SendBuf[10] = 0x02;
			 SendBuf[11] = 0x00;			 
			 
			 D12_WriteEndpoint(5, 12, (INT8U*)SendBuf);

			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - 12; 

			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);
			break;
			
		case 0x03: //REQUEST SENSE
			 D12_WriteEndpoint(5, 18, ReqSense);
			
			 //csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - 18; 

			 //csw1.bCSWStatus = 0x00;
			
			 //D12_WriteEndpoint(5, 13, (INT8U*)&csw1);
			break;
			
		case 0x01: //REZERO UNIT
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
			
		case 0x2B: //SEEK
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
			
		case 0x1D: //SEND DIAGNOSTIC
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);			
			break;
			
		case 0x1B: //START-STOP UNIT
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
			
		case 0x00: //TEST UNIT READY
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
			
		case 0x2F: //VERIFY 
			 ReqSense[2] = 0x00; //sense data : NO SENSE
 
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);				
			break;
			
		case 0x2A: //WRITE(10)
			 Addr = 0;
			 tmp  = pufi1->dLBA[0];
			 Addr += tmp<<24;
			 tmp  = pufi1->dLBA[1];
			 Addr += tmp<<16;
			 tmp  = pufi1->dLBA[2];
			 Addr += tmp<<8;	
			 tmp  = pufi1->dLBA[3];
			 Addr += tmp; //Addr is the address of the data to write
			 Addr = Addr * 512;			 			 
       tmp  = pufi1->wLength[0]*256 + pufi1->wLength[1]; //length of data to receive
       tmp  = tmp * 512;
       old  = tmp;
       
       sum=0;
       while(sum < tmp){ 	
       	len = 0;
       	len = D12_ReadEndpoint(4, 64, EpBuf);
	      sum += len;
	      for(cnt=0;cnt<len;cnt++)
          LBA[Addr++] = EpBuf[cnt];	    
		   if(sum < tmp)
		   {
       	FLAG = 0;
       	while((FLAG&D12Ep4Int) == 0)
       	 FLAG = D12_ReadInterruptRegister();
       	D12_ReadLastTransactionStatus(4);			//复位中断寄存器 
       }	 		   
		  } 
	   
			 ReqSense[2] = 0x00; //sense data : NO SENSE

			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - sum;
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);
			break;
			
		case 0xAA: //WRITE(12)
			 Addr = 0;
			 tmp  = pufi1->dLBA[0];
			 Addr += tmp<<24;
			 tmp  = pufi1->dLBA[1];
			 Addr += tmp<<16;
			 tmp  = pufi1->dLBA[2];
			 Addr += tmp<<8;			 
			 tmp  = pufi1->dLBA[3];
			 Addr += tmp; //Addr is the address of the data to write
			 Addr = Addr * 512;			
			 p = (INT8U *)pufi1; 			 
       tmp  =  (U32)p[6]<<24;//length of data to receive
       tmp  += (U32)p[7]<<16;
       tmp  += (U32)p[8]<<8;
       tmp  += (U32)p[9];
       tmp  = tmp * 512;
       old  = tmp;
       
       sum=0;
       while(sum < tmp){ 	
       	len = 0;
       	len = D12_ReadEndpoint(4, 64, EpBuf);
	      sum += len;
	      for(cnt=0;cnt<len;cnt++)
          LBA[Addr++] = EpBuf[cnt];   
		   if(sum < tmp)
		   {
       	FLAG = 0;
       	while((FLAG&D12Ep4Int) == 0)
       	 FLAG = D12_ReadInterruptRegister();
       	D12_ReadLastTransactionStatus(4);			//复位中断寄存器 
       }	 		   
		  }   
		   
			 ReqSense[2] = 0x00; //sense data : NO SENSE
 
			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - sum; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;
			
		case 0x2E: //WRITE AND VERIFY
			 Addr = 0;
			 tmp  = pufi1->dLBA[0];
			 Addr += tmp<<24;
			 tmp  = pufi1->dLBA[1];
			 Addr += tmp<<16;
			 tmp  = pufi1->dLBA[2];
			 Addr += tmp<<8;			 
			 tmp  = pufi1->dLBA[3];
			 Addr += tmp; //Addr is the address of the data to write			
			 Addr = Addr * 512; 			 
       tmp  = pufi1->wLength[0]*256 + pufi1->wLength[1]; //length of data to receive
       tmp  = tmp * 512;
       old  = tmp;
       
       sum=0;
       while(sum < tmp){ 	
       	len = 0;
       	len = D12_ReadEndpoint(4, 64, EpBuf);
	      sum += len;
	      for(cnt=0;cnt<len;cnt++)
          LBA[Addr++] = EpBuf[cnt];    
		   if(sum < tmp)
		   {
       	FLAG = 0;
       	while((FLAG&D12Ep4Int) == 0)
       	 FLAG = D12_ReadInterruptRegister();
       	D12_ReadLastTransactionStatus(4);			//复位中断寄存器 
       }	 		   
		  }   
		   
			 ReqSense[2] = 0x00; //sense data : NO SENSE
 
			 csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - old; 
			 csw1.bCSWStatus = 0x00;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);				
			break;
																	 			
		default:
			 csw1.dCSWDataResidue = 0; 
			 csw1.bCSWStatus = 0x01;
			
			 D12_WriteEndpoint(5, 13, (INT8U*)&csw1);	
			break;		
	}
}

⌨️ 快捷键说明

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