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

📄 usbin.c

📁 USB_ON_s3c2410 USB_ON_s3c2410
💻 C
字号:
/****************************************************************
 NAME: usbin.c
 DESC: usb bulk-IN operation
 HISTORY:
 Mar.25.2002:purnnamu: ported for S3C2410X.
 ****************************************************************/
#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 "usbin.h"
#include "usb_implement.h"
#include "yaffsfs.h"

static void PrintEpiPkt(U8 *pt,int cnt);


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

#define SET_EP1_IN_PKT_READY()  rIN_CSR1_REG= ( in_csr1 &(~ EPI_WR_BITS)\
					| EPI_IN_PKT_READY )	 
#define SET_EP1_SEND_STALL()	rIN_CSR1_REG= ( in_csr1 & (~EPI_WR_BITS)\
					| EPI_SEND_STALL) )
#define CLR_EP1_SENT_STALL()	rIN_CSR1_REG= ( in_csr1 & (~EPI_WR_BITS)\
					&(~EPI_SENT_STALL) )
#define FLUSH_EP1_FIFO() 	rIN_CSR1_REG= ( in_csr1 & (~EPI_WR_BITS)\
					| EPI_FIFO_FLUSH) )


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

// EP1 = IN end point. 

U8 ep1Buf[EP1_PKT_SIZE];
int transferIndex=0;

#if 1
unsigned int data_send_flag=DATA_LEN_SEND;
unsigned int data_len;
unsigned char data_send_count=0;
unsigned int cmd_ext;
char file_info_buf[1024];
char usb_rdfile_buf[1024];
char usb_rdfile_path[1024];
char dump_dir_path[1024];
int usb_rdfile_length;
int file_info_len;
int file_send_index;
char dump_start_flag=0;
char usb_rdfile_flag=0;
char usb_trans_flag;
yaffs_DIR * ydir_temp;
int usb_read_fd;


extern int usb_wd_flag;
#endif

int usb_rdfile(const char *path)
{
	
	char buf[1024];
	int i;
	char c;
	int ret;
	if(usb_rdfile_flag == 0)
	{
		usb_read_fd=yaffs_open(path,O_RDWR, S_IREAD | S_IWRITE);	//usb_read_fd得到handle数组的下标??
		usb_rdfile_flag=1;
		
	}
	if(usb_read_fd<0)
	{
		Uart_Printf("Cannot read file %s\n",path);
		return 0;
	}
	ret=yaffs_read(usb_read_fd,usb_rdfile_buf,256);
	if(ret >0)
	{
		usb_rdfile_length=ret;
		usb_rdfile_buf[ret]='\0';
		//Uart_Printf("%s\n",usb_rdfile_buf);
	}
	else
	{
		yaffs_close(usb_read_fd);
		usb_rdfile_flag=0;
		usb_rdfile_length=0;
	}
#if 0
	while(ret > 0)
	{
		for(i=0; i<ret; i++)
		{
			Uart_Printf("%c",buf[i]);
			
		}
		ret=yaffs_read(usb_read_fd,buf,256);
	}
#endif	
}


void usb_dump_dir(const char * path)
{
	int fd;
	//char file_info_buf[1024];
	char file_name_temp[1024];
	//int file_info_len;
	int ret;
	int i;
	struct yaffs_stat stat;
	
	struct yaffs_dirent * ydrent;
	if(dump_start_flag ==0)
	{
		dump_start_flag=1;
		ydir_temp=yaffs_opendir(path);
		Uart_Printf("1usb_dump_dir\n");
		Uart_Printf("DumpDir %s\n",path);
	}
	
	ydrent=yaffs_readdir(ydir_temp);	
	if(ydrent)
	{
		sprintf(file_info_buf,"%s/%s",path,ydrent->d_name);
		sprintf(file_name_temp,"%s/%s",path,ydrent->d_name);
		//sprintf(file_info_buf,"%s/%s",path,ydrent->d_name);
		yaffs_lstat(file_info_buf,&stat);
		sprintf(file_info_buf,"%s  %s Length:%d  Mode:%X  ",file_info_buf,ydrent->d_name,stat.st_size,stat.st_mode);
		
		switch (stat.st_mode&S_IFMT)
		{
			case S_IFREG: 
				sprintf(file_info_buf,"%s %s",file_info_buf,"data file");
				//Uart_Printf("data file");
				break;
			case S_IFDIR:
				sprintf(file_info_buf,"%s %s",file_info_buf,"directory");
				//Uart_Printf("directory"); 
				break;
			case S_IFLNK: 
				sprintf(file_info_buf,"%s %s",file_info_buf,"symlink -->");
				//Uart_Printf("symlink -->");
				
				if(yaffs_readlink(file_info_buf,file_info_buf,100) < 0)
				{
					sprintf(file_info_buf,"%s %s",file_info_buf,"no alias");
					//Uart_Printf("no alias");
				}
				else
				{
					sprintf(file_info_buf,"%s %s",file_info_buf,file_name_temp);
					//Uart_Printf("\"%s\"",file_info_buf);	 
				}
				break;
			default: Uart_Printf("unknown"); break;
		}
		sprintf(file_info_buf,"%s %s",file_info_buf,"\n");
	
		Uart_Printf("%s",file_info_buf);
		file_info_len=strlen(file_info_buf);
	
		Uart_Printf("the file info length is %d\n",file_info_len);
	}
	else
	{
		file_info_len=0;
		yaffs_closedir(ydir_temp);
		dump_start_flag=0;
		Uart_Printf("2usb_dump_dir\n");
	}
	
	//yaffs_closedir(ydir);
	
	
}

void Usb_DataPrepair(void)
{

	int i;
	U8* temp_buf;
	int usb_send_len;
	temp_buf=ep1Buf;
	
	
#if 1
	
	switch(data_send_flag)
	{
	  	case DATA_LEN_SEND:
	  		data_send_flag=DATA_EXT_SEND;
	  		//while(file_info_len !=0)
	  		//	
	  		
#if 0	  		
			if(data_send_count <3)
			{
				data_len=128;
				data_send_count++;
			}
			else
			{
				data_len=0;
				data_send_count=0;
				data_send_flag=DATA_LEN_SEND;
			}
			*((unsigned int* )ep1Buf)=data_len;
#endif
#if 1
			usb_dump_dir(dump_dir_path);
			#if 0
			if((dump_dir_path[0] !='/') ||
				(dump_dir_path[1] !='b') ||
				(dump_dir_path[2] !='o') ||
				(dump_dir_path[3] !='o') ||
				(dump_dir_path[4] !='t'))
			{
				Uart_Printf("wrong dump dir:%s\n",dump_dir_path);		
			}
			Uart_Printf("wrong dump dir:%s\n",dump_dir_path);		
			#endif
			//usb_dump_dir("/boot");
	  		Uart_Printf("the file_info_len is %d\n",file_info_len);
	  		Uart_Printf("%s",file_info_buf);
			if(file_info_len != 0)
			{
				data_send_flag=DATA_EXT_SEND;
			}
			else
			{
				data_send_flag=DATA_LEN_SEND;
			}
			file_send_index=0;
			*((unsigned int* )ep1Buf)=(unsigned int )file_info_len;
			
			usb_send_len=EP1_PKT_SIZE;
			
#endif
			
			
			//data_len=0;
			Uart_Printf("sending len \n");
		break;
		
		case DATA_EXT_SEND:
#if 0		
			//*((((U8 *)temp_buf)))='\n';
			for(i=0;i<(EP1_PKT_SIZE);i++)
			{
				*((((U8 *)temp_buf)))=(U8)('a' + i);
 				if((i%32)==31)
 					*((((U8 *)temp_buf)))=(U8)('\n');
 				temp_buf++;
 				data_len--;
 			}
 			if(data_len <= 0)
 				data_send_flag=DATA_LEN_SEND;
#endif
			if(file_info_len >=EP1_PKT_SIZE)
			{
				usb_send_len=EP1_PKT_SIZE;
 				
			}
			else
			{
				usb_send_len=file_info_len;
			}
			for(i=0;i<(usb_send_len);i++)
			{
				*((((char *)temp_buf)))=file_info_buf[file_send_index];
				file_send_index++;
				temp_buf++;
 			}
 			file_info_len=file_info_len-usb_send_len;
 			if(file_info_len <= 0)
 			{
 				data_send_flag=DATA_LEN_SEND;
 			}
 			
 			Uart_Printf("sending data \n");
 			
		break;
			
		default:
		
		break;
	}
	WrPktEp1(ep1Buf,usb_send_len);
	
#endif
	
}
void usb_read_file_ext(void)
{
		int i;
	U8* temp_buf;
	int usb_send_len;
	temp_buf=ep1Buf;
	

#if 1
	
	switch(data_send_flag)
	{
	  	case DATA_LEN_SEND:
	  		data_send_flag=DATA_EXT_SEND;
	  		//while(file_info_len !=0)
	  		//	
	  		

#if 1
			if(usb_trans_flag == 0)
			{
				Uart_Printf("-----------new transmit ---------------------\n");
				usb_trans_flag=1;
			}
			//if(usb_trans_flag == 1)
			//{
				usb_rdfile(usb_rdfile_path);
				//usb_trans_flag=1;
	  		//}
	  		Uart_Printf("the usb_rdfile_length is %d\n",usb_rdfile_length);
	  		//Uart_Printf("%s",usb_rdfile_buf);
			if(usb_rdfile_length != 0)
			{
				data_send_flag=DATA_EXT_SEND;
			}
			else
			{
				data_send_flag=DATA_LEN_SEND;
				usb_trans_flag =0;
			}
			file_send_index=0;
			*((unsigned int* )ep1Buf)=(unsigned int )usb_rdfile_length;
			//*((unsigned int* )ep1Buf)=(unsigned int )256;
			usb_send_len=EP1_PKT_SIZE;
			
#endif
			
			
			//data_len=0;
			Uart_Printf("sending len \n");
		break;
		
		case DATA_EXT_SEND:
			if(usb_rdfile_length >=EP1_PKT_SIZE)
			{
				usb_send_len=EP1_PKT_SIZE;
 				
			}
			else
			{
				usb_send_len=usb_rdfile_length;
			}
			for(i=0;i<(usb_send_len);i++)
			{
				*((((char *)temp_buf)))=usb_rdfile_buf[file_send_index];
				file_send_index++;
				temp_buf++;
 			}
 			usb_rdfile_length=usb_rdfile_length-usb_send_len;
 			if((usb_rdfile_length <= 0) && (data_send_flag == DATA_EXT_SEND ))
 			{
 				data_send_flag=DATA_LEN_SEND;
 			//	usb_rdfile(usb_rdfile_path);				
 			}
 			
 			Uart_Printf("sending data \n");
 			
		break;
			
		default:
		
		break;
	}
	WrPktEp1(ep1Buf,usb_send_len);
	
	
#endif
	
}
void cmd_solve(void)
{
	int i;
	
	switch(cmd_ext)
	{
		case USB_DUMP_DIR:
			Uart_Printf("USB_DUMP_DIR\n");
			
			Usb_DataPrepair();
		break;
		case USB_READ_FILE:
			Uart_Printf("USB_READ_FILE\n");
			usb_read_file_ext();
		break;
		default:
			Uart_Printf("default sending\n");
			 for(i=0;i<EP1_PKT_SIZE;i++)
			 	ep1Buf[i]=(U8)('a'+i);
   			 WrPktEp1(ep1Buf,EP1_PKT_SIZE);
			//Usb_DataPrepair();
		break;
	}
}


void PrepareEp1Fifo(void) 
{
    int i;
    U8 in_csr1;
    rINDEX_REG=1;

    in_csr1=rIN_CSR1_REG;
	
#if 0
	Usb_DataPrepair();
#endif
	Uart_Printf("in the PrepareEp1Fifo\n");
#if 1
	cmd_solve();
#endif
	
    //for(i=0;i<EP1_PKT_SIZE;i++)ep1Buf[i]=(U8)(transferIndex+i);
    //WrPktEp1(ep1Buf,EP1_PKT_SIZE);
    SET_EP1_IN_PKT_READY(); 
}


void Ep1Handler(void)
{
    U8 in_csr1;
    int i;
    rINDEX_REG=1;
    in_csr1=rIN_CSR1_REG;
    
    DbgPrintf("<1:%x]",in_csr1);

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

    //IN_PKT_READY is cleared
    
    //The data transfered was ep1Buf[] which was already configured 

    PrintEpiPkt(ep1Buf,EP1_PKT_SIZE); 
#if 1
    transferIndex++;
#endif
    PrepareEp1Fifo(); 
    	//IN_PKT_READY is set   
    	//This packit will be used for next IN packit.	

    return;
}


    
void PrintEpiPkt(U8 *pt,int cnt)
{
    int i;
    DbgPrintf("[B_IN:%d:",cnt);
    for(i=0;i<cnt;i++)
    	DbgPrintf("%x,",pt[i]);
    DbgPrintf("]");
}

⌨️ 快捷键说明

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