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

📄 hpi.c

📁 利用51单片机对u盘进行读写操作
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "common.h"
#include "TPBULK.H"
#include "HPI.H"
#include "HAL.H"
#include "Fat.h"
#include "Fat32.h"
#include "HPI32.H"

extern FLAGS bdata bFlags;
extern unsigned char xdata DBUF[BUFFER_LENGTH];
//////////////////////////////////////////
unsigned char xdata UARTBUF[UARTBUF_LENGTH];
SYS_INFO_BLOCK xdata DeviceInfo;
FILE_INFO xdata ThisFile;

Command_Def xdata Command;
Response_Def xdata Response;

unsigned int xdata DirStartCluster,NowCluster;
unsigned long xdata NowSector;
ShowFileName_Def xdata ShowFileName[MaxLFNum];	//long file struct
/////////////////////////////////////////////

void UartSendRsp(void)
{
	unsigned int i;
	unsigned char *pBuf=(unsigned char *)&Response;
	MCU_LED4=0;
	for(i=0;i<5;i++)
		ComSendByte(*(pBuf+i));

	if(Response.len!=0)
		{
		for(i=0;i<Response.len;i++)
			ComSendByte(UARTBUF[i]);
	    }
	MCU_LED4=1;
}

unsigned char UartHandler(void)
{
  Response.Result=0;Response.len=0;
  switch(Command.CLass)
  {
    case 0x00:	//List		
    	Response.Result=List();
    	break;  
  	case 0x01:	//Open File
 		Response.Result=OpenFile(Command.Parameter);		
     	break;
	case 0x02:	//Read File		
		Response.Result=ReadFile(Command.len,UARTBUF);
		break;   
    case 0x03:	//Set Pointer
		Response.Result=SetFilePointer(Command.len);	
		break;
	case 0x04:	//Great File
		Response.Result=OpenFile(Command.Parameter);
		if(Response.Result)
			{
			Response.Result=SetFilePointer(ThisFile.LengthInByte);
			}
		else
    		Response.Result=CreateFile(Command.len,Command.Parameter,UARTBUF);    	
    	break; 
	case 0x05:	//Write File
    	Response.Result=WriteFile(Command.len,UARTBUF);
    	break; 
    case 0x06:	//Remove File
    	ThisFile.bFileOpen=0;	
    	Response.Result=RemoveFile(Command.Parameter);
    	break; 	
	case 0x07:	//Get Space    	
    	Response.Result=GetCapacity();
    	break;     
	case 0x08:  //DetectDisk
		if(bFlags.bits.SLAVE_IS_ATTACHED)
			Response.Result=1;
		break;
	case 0x09:  //Great Dir
		Response.Result=DownDir(Command.Parameter);
		if(!Response.Result)
			Response.Result=CreateDir(Command.len,Command.Parameter,UARTBUF);    	
    	break; 
	case 0x0a:	//Down Dir
		Response.Result=DownDir(Command.Parameter);
		break;
	case 0x0b:	//Up Dir
		Response.Result=UpDir();
		break;	
	case 0x0c:	//Up RootDir
		Response.Result=UpRootDir();
		break;	
  }

  UartSendRsp();
  ///////////////////////////////
  return TRUE;  
}

unsigned char List(void)
{
	unsigned int item,i;
	unsigned char k,bstop,sector;
	unsigned char Lcount,Ncount,base;
	
	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;			
	item=0;
	bstop=0;
	////////////////////////////////////
	Lcount=0;
	for(i=0;i<MaxLFNum;i++)
		{
		ShowFileName[i].LongName[0]=0x00;
		ShowFileName[i].LongName[1]=0x00;
		}
	/////////////////////////////////////

	if(DirStartCluster==0)	//Root Dir
	{
	for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
	    {   
		if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
			return FALSE;			
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
			{
			if(DBUF[i]==0x00)
				{bstop=1;
				break;}
			else if(DBUF[i]==0xE5)
				continue;			
			else
				{
				for(k=0;k<32;k++)
					UARTBUF[item*32+k]=DBUF[i+k];
				item=item+1;
				/////////////////////////////////
				if(DBUF[i+11]==0x0F)
					{
					base=((DBUF[i]&0x1F)-1)*26;
					if(base<=224)
					{
					Ncount=0;
					for(k=1;k<11;k++)
						{
						ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
						Ncount++;
						}
					for(k=14;k<26;k++)
						{
						ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
						Ncount++;
						}
					for(k=28;k<32;k++)
						{
						ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
						Ncount++;
						}
					}
					}
				else
					{
					for(k=0;k<32;k++)
						ShowFileName[Lcount].item[k]=DBUF[i+k];
				    Lcount++;					
					}			
				/////////////////////////////////
				}
			}
		
		if(bstop==1)break;		
	    }
	Response.len=item*32;
	return TRUE;
	}
//////////////////////////////////////////////////////////////////   
	else		//Son Dir
	{
		NowCluster=DirStartCluster;		
		do
		{
			NowSector=FirstSectorofCluster(NowCluster);
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	    	{   
				if(!RBC_Read(NowSector+sector,1,DBUF))
					return FALSE;				
				for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
				{
					if(DBUF[i]==0x00)
						{bstop=1;break;}
					else if(DBUF[i]==0xE5)
						continue;			
					else
					{
						for(k=0;k<32;k++)
							UARTBUF[item*32+k]=DBUF[i+k];
						item=item+1;
						/////////////////////////////////
						if(DBUF[i+11]==0x0F)
						{
							base=((DBUF[i]&0x1F)-1)*26;
							if(base<=224)
							{
								Ncount=0;
								for(k=1;k<11;k++)
								{
									ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
									Ncount++;
								}
								for(k=14;k<26;k++)
								{
									ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
									Ncount++;
								}
								for(k=28;k<32;k++)
								{
									ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
									Ncount++;
								}
							}
						}
						else
						{
							for(k=0;k<32;k++)
								ShowFileName[Lcount].item[k]=DBUF[i+k];
				 		   Lcount++;							
						}			
				/////////////////////////////////
					}
				}
				if(bstop==1)break;		
	    	}

			if(bstop==1)break;
	
			NowCluster=GetNextClusterNum(NowCluster); 
			
		}while(NowCluster<=0xffef);

	Response.len=item*32;
	return TRUE;
	}
}

unsigned char OpenFile(unsigned char *pBuffer)
{
	unsigned int i;
	unsigned char j,bstop,sector;
		
	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;			
	ThisFile.bFileOpen=0;

	if(DirStartCluster==0)	//Root Dir
	{
	for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
	    {   
		if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
			return FALSE;
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
			{
			if(DBUF[i]==0x00)
				return FALSE;
			j=0;
			while(DBUF[i+j]==*(pBuffer+j))
				{
				 j=j+1;
				 if(j>10)
				 	break;
				}
			
			if(j>10&&(DBUF[i+11]&0x10)!=0x10)
			    {
			    for(j=0;j<32;j++)
			    	UARTBUF[j]=DBUF[i+j];		    
			    bstop=1;
			    break;}
			
			}
		if(bstop==1)break;
		}
	    
	    if(sector>=DeviceInfo.BPB_RootEntCnt)
	    	return FALSE;	
	}
///////////////////////////////////////////////////////////////////////////////////////
	else
	{
		NowCluster=DirStartCluster;		
		do
		{
			NowSector=FirstSectorofCluster(NowCluster);
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	    	{   
				if(!RBC_Read(NowSector+sector,1,DBUF))
					return FALSE;				
				for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
				{
					if(DBUF[i]==0x00)
						return FALSE;
					j=0;
					while(DBUF[i+j]==*(pBuffer+j))
					{
						 j=j+1;
						 if(j>10)
						 	break;
					}
					if(j>10&&(DBUF[i+11]&0x10)!=0x10)
				    	{ 
						for(j=0;j<32;j++)
			    			UARTBUF[j]=DBUF[i+j];			    
			    		bstop=1;
			    		break;
						}
				}
				if(bstop==1)break;		
	    	}
			if(bstop==1)break;	
			NowCluster=GetNextClusterNum(NowCluster);			
		}while(NowCluster<=0xffef);
		
		if(NowCluster>0xffef)
	    	return FALSE;
	}

	ThisFile.bFileOpen=1;
	ThisFile.StartCluster=LSwapINT16(UARTBUF[26],UARTBUF[27]);
	ThisFile.LengthInByte=LSwapINT32(UARTBUF[28],UARTBUF[29],UARTBUF[30],UARTBUF[31]);
	ThisFile.ClusterPointer=ThisFile.StartCluster;	
	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
	ThisFile.OffsetofSector=0;
	ThisFile.SectorofCluster=0;	
	ThisFile.FatSectorPointer=0;	
	ThisFile.pointer=0;
	
	Response.len=32;
	return TRUE;	
}

unsigned char ReadFile(unsigned long readLength,unsigned char *pBuffer)
{

	unsigned int len,i;
	unsigned int tlen;	
	unsigned long blen;

	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;		
	if(!ThisFile.bFileOpen)
		return FALSE;

	blen=readLength;

	tlen=0;
	if(readLength>MAX_READ_LENGTH)
		return FALSE;	
	
	if(readLength+ThisFile.pointer>ThisFile.LengthInByte)
		return FALSE;	
		
	////////////////////////////////////////////		
		while(readLength>0)
		{
		   if(readLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
		   	len=DeviceInfo.BPB_BytesPerSec;
		   else
		   	len=readLength+ThisFile.OffsetofSector;
		   
		   //////////////////////////////////////////////////////
		   if(ThisFile.OffsetofSector>0)
		   	{
		   	if(RBC_Read(ThisFile.SectorPointer,1,DBUF))
		   		{
		   		
		   		len=len-ThisFile.OffsetofSector;
		   		for(i=0;i<len;i++)
		   			
		   			*(pBuffer+i)=DBUF[ThisFile.OffsetofSector+i];
		   		ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
		   		}
		   	else
		   		return FALSE;		   		
		   	}
		   else
		   	{
		   		if(!RBC_Read(ThisFile.SectorPointer,1,pBuffer+tlen))
		   				return FALSE;	
		   		ThisFile.OffsetofSector=len;
		   	}
		   ////////////////////////////////////////////////////////////
		   readLength-=len;
		   tlen+=len;
		  
		   /////////////////////////////////////////////////////////
		   if((ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)&&(tlen+ThisFile.pointer<ThisFile.LengthInByte))
		   {	
		   	ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
		   	ThisFile.SectorofCluster+=1;
		   	if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
		   	{
		   		ThisFile.SectorofCluster=0;
		 		 ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
		 		 if(ThisFile.ClusterPointer>0xffef)
		 		 	   return FALSE;			 		 	
		 		 ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer); 	
		   	}
		   	else
		   		ThisFile.SectorPointer=ThisFile.SectorPointer+1;
		    }
		   //////////////////////////////////////////////////////////////////
		}//end while
		
	ThisFile.bFileOpen=1;
	ThisFile.pointer+=tlen;
	//////////////////////////////////////////////
	Response.len=blen;

	return TRUE;
}

unsigned char SetFilePointer(unsigned long pointer)
{
	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;		
	if(!ThisFile.bFileOpen)
		return FALSE;		
	///////////////////////////////////////////////////////////
	ThisFile.pointer=pointer;
	if(ThisFile.pointer>ThisFile.LengthInByte)
		return FALSE;	

	if(!GoToPointer(ThisFile.pointer))
	{
	ThisFile.bFileOpen=0;
	return FALSE;	
	}
	//////////////////////////////////////////////
	return TRUE;
}

unsigned char CreateFile(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
{
	unsigned int sector,i,j,DirCount;
	unsigned int cnum,ClusterPointer;
	unsigned char xdata bstop,InByte,bwrite;
	
	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;
	if((len%32)!=0)
		return FALSE;
	if((len+32)>DeviceInfo.BPB_BytesPerSec)
		return FALSE;

	ThisFile.bFileOpen=0;	

	cnum=GetFreeCusterNum();
	if(cnum<0x02)
		return FALSE;	

	pBuffer[26]=(unsigned char)(cnum);
	pBuffer[27]=(unsigned char)(cnum>>8);
	pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
	bstop=0;
	
	if(DirStartCluster==0)	
	{
	/////// Search a free space in the root dir space and build the item ///
	for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
	    {   
		if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
			return FALSE;
		DirCount=0;bwrite=0;
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
			{
			if(len==0)
				{
				if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
				{
				for(j=0;j<32;j++)
					DBUF[i+j]=*(pBuffer+j);
				if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
		  	 		return FALSE;		  	 		
				bstop=1;
				break;
				}
				}
			else
			{
			if(DirCount==0)
				InByte=i;
			if(DBUF[i]==0xE5)				
				DirCount++;				
			else if(DBUF[i]==0x00)
				{	
				DirCount++;	
				DBUF[i]=0xE5;
				bwrite=1;				
				}
			else
				DirCount=0;

			if((DirCount*32)>=(len+32))
				{
				for(j=0;j<len;j++)
					DBUF[InByte+j]=*(pName+j);

				for(j=0;j<32;j++)
					DBUF[InByte+len+j]=*(pBuffer+j);

				if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
		  	 		return FALSE;		  	 		
				bstop=1;
				break;
				}
			 }
			}		
		if(bstop==1)break;
		
		if((len!=0)&&(bwrite==1))
			{
			if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
		  		return FALSE;
	    	}
		}

	if(sector>=DeviceInfo.BPB_RootEntCnt)
		return FALSE;
	}
////////////////////////////////////////////////////////////
	else
	{
		NowCluster=DirStartCluster;		
		do
		{
			NowSector=FirstSectorofCluster(NowCluster);
			ClusterPointer=NowCluster;
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	    	{   
				if(!RBC_Read(NowSector+sector,1,DBUF))
					return FALSE;
				DirCount=0;bwrite=0;

				for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
				{
				if(len==0)
					{
					if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
					{
					for(j=0;j<32;j++)
						DBUF[i+j]=*(pBuffer+j);
					if(!RBC_Write(NowSector+sector,1,DBUF))
		  	 			return FALSE;		  	 		
					bstop=1;
					break;
					}		
					}
				else
				{
				if(DirCount==0)
					InByte=i;
				if(DBUF[i]==0xE5)				
					DirCount++;				
				else if(DBUF[i]==0x00)
					{	
					DirCount++;	
					DBUF[i]=0xE5;
					bwrite=1;
					}
				else
					DirCount=0;

				if((DirCount*32)>=(len+32))
					{
					for(j=0;j<len;j++)
						DBUF[InByte+j]=*(pName+j);
					for(j=0;j<32;j++)
						DBUF[InByte+len+j]=*(pBuffer+j);
					if(!RBC_Write(NowSector+sector,1,DBUF))
		  		 		return FALSE;		  	 		
					bstop=1;
					break;
					}
				 }
				}
				if(bstop==1)break;
				
				if((len!=0)&&(bwrite==1))
				{
				if(!RBC_Write(NowSector+sector,1,DBUF))
		  			return FALSE;
	    		}
	    	}
			if(bstop==1)break;
	
			NowCluster=GetNextClusterNum(NowCluster);
			if(NowCluster>0xffef)
	    	{
			NowCluster=CreateClusterLink(ClusterPointer);
		 	if(NowCluster==0x00)
		 		 return FALSE;
			NowSector=FirstSectorofCluster(NowCluster);
			for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
				{
				if(!RBC_Write(NowSector+sector,1,DBUF))
		  	 		return FALSE;
				}
			}
		}while(NowCluster<=0xffef);
	
	
	if(NowCluster>0xffef)
	    return FALSE;
	}
////////////////////////////////////////////////////////////////
	
	ThisFile.StartCluster=cnum;
	ThisFile.LengthInByte=0;
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
	ThisFile.OffsetofSector=0;
	ThisFile.SectorofCluster=0;
	ThisFile.bFileOpen=1;
	ThisFile.pointer=0;
	ThisFile.FatSectorPointer=0;
	
	return TRUE;
}

unsigned char WriteFile(unsigned long writeLength,unsigned char *pBuffer)

⌨️ 快捷键说明

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