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

📄 fat32.c

📁 mega128+ch375实现读写u盘功能iccavr源码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "main.h"
SYS_INFO_BLOCK DeviceInfo;
FILE_INFO ThisFile;
unsigned char FATBUF[512];//far表的扇区缓冲


unsigned long  DirStartCluster32=0,NowCluster32=0;
unsigned long  NowSector=0;

unsigned long FirstSectorofCluster32(unsigned long clusterNum)
{
	unsigned long temp;
	temp=clusterNum-2;
	temp=temp*DeviceInfo.BPB_SecPerClus;
	temp=temp+DeviceInfo.FirstDataSector;
	return temp;
}

unsigned long ThisFatSecNum32(unsigned long clusterNum)
{
   unsigned long temp;
   temp=clusterNum*4;
   temp=temp/DeviceInfo.BPB_BytesPerSec;
   temp=temp+DeviceInfo.FatStartSector;
   return temp;
}

unsigned long ThisFatEntOffset32(unsigned long clusterNum)
{
	unsigned long temp1,temp2;
	temp1=4*clusterNum;
	temp2=temp1/DeviceInfo.BPB_BytesPerSec;
	temp1=temp1-temp2*DeviceInfo.BPB_BytesPerSec;
	return temp1;
}

unsigned long GetNextClusterNum32(unsigned long clusterNum)
{
	unsigned long FatSecNum,FatEntOffset;
	
	FatSecNum=ThisFatSecNum32(clusterNum);
	FatEntOffset=ThisFatEntOffset32(clusterNum);
	if(ThisFile.FatSectorPointer!=FatSecNum)
	{	
		
		if(ReadSector(FatSecNum,1,FATBUF)!=TRUE)
			return 0xFFFFFFFF;
		ThisFile.FatSectorPointer=FatSecNum;
	}
	
	///////////////////////////////////////////////////
	clusterNum=LSwapINT32(FATBUF[FatEntOffset],FATBUF[FatEntOffset+1],FATBUF[FatEntOffset+2],FATBUF[FatEntOffset+3]);
	return clusterNum;
}

unsigned char GoToPointer32(unsigned long pointer)
{
	
	unsigned int clusterSize;
	
	clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	while(pointer>clusterSize)
	{
		pointer-=clusterSize;	
		ThisFile.ClusterPointer=GetNextClusterNum32(ThisFile.ClusterPointer);
		if(ThisFile.ClusterPointer==0xffffffff)
		{
		return 0;
		}
	}
	ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec;
	ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer)+ThisFile.SectorofCluster;
	ThisFile.OffsetofSector=pointer-ThisFile.SectorofCluster*DeviceInfo.BPB_BytesPerSec;
	ThisFile.FatSectorPointer=0;
	return 1;
	
}

unsigned char DeleteClusterLink32(unsigned long clusterNum)
{
	unsigned long FatSecNum,FatEntOffset;
	unsigned char i;
	while((clusterNum>1)&&(clusterNum<DeviceInfo.TotCluster))
	{
	FatSecNum=ThisFatSecNum32(clusterNum);
	FatEntOffset=ThisFatEntOffset32(clusterNum);
	if(ReadSector(FatSecNum,1,diskbuff)==TRUE)
		clusterNum=LSwapINT32(diskbuff[FatEntOffset],diskbuff[FatEntOffset+1],diskbuff[FatEntOffset+2],diskbuff[FatEntOffset+3]);
	else
		return 0;
	diskbuff[FatEntOffset]=0x00;diskbuff[FatEntOffset+1]=0x00;diskbuff[FatEntOffset+2]=0x00;diskbuff[FatEntOffset+3]=0x00;			
	for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
		{
		delay_ms(5);
		if(WriteSector(FatSecNum+i*DeviceInfo.BPB_FATSz32,1,diskbuff)!=TRUE)
			return 0;
		}	
	}
	return 1;
}

unsigned long GetFreeCusterNum32(void)
{
	unsigned long  clusterNum,i;
	unsigned long  sectorNum;
	unsigned char  j;
	clusterNum=0;
	sectorNum=DeviceInfo.FatStartSector;
	while(sectorNum<DeviceInfo.BPB_FATSz32+DeviceInfo.FatStartSector)
	{		
		if(ReadSector(sectorNum,1,diskbuff)!=TRUE)
			return 0x0;
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+4)
		  	{
		  	 if((diskbuff[i]==0)&&(diskbuff[i+1]==0)&&(diskbuff[i+2]==0)&&(diskbuff[i+3]==0))
		  	 	{	
		  	 	diskbuff[i]=0xff;diskbuff[i+1]=0xff;diskbuff[i+2]=0xff;diskbuff[i+3]=0xff;
				for(j=0;j<DeviceInfo.BPB_NumFATs;j++)
					{
					delay_ms(5);
					if(WriteSector(sectorNum+j*DeviceInfo.BPB_FATSz32,1,diskbuff)!=TRUE)
						return 0;
					}	
		  	  	return	clusterNum; 
		  	 	}
		  	 clusterNum++;
		  	}					
		sectorNum=4*clusterNum/DeviceInfo.BPB_BytesPerSec+DeviceInfo.FatStartSector;	
		delay_ms(10);
	}	
	return 0x0;
}

unsigned long CreateClusterLink32(unsigned long currentCluster)
{
	unsigned long  newCluster;
	unsigned long  FatSecNum,FatEntOffset;
	unsigned char  i;

	newCluster=GetFreeCusterNum32();
		
	FatSecNum=ThisFatSecNum32(currentCluster);
	FatEntOffset=ThisFatEntOffset32(currentCluster);
	if(ReadSector(FatSecNum,1,diskbuff)==TRUE)
		{
		diskbuff[FatEntOffset]=newCluster;
		diskbuff[FatEntOffset+1]=newCluster>>8;
		diskbuff[FatEntOffset+2]=newCluster>>16;
		diskbuff[FatEntOffset+3]=newCluster>>24;
		for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
			{
			delay_ms(5);
			if(WriteSector(FatSecNum+i*DeviceInfo.BPB_FATSz32,1,diskbuff)!=TRUE)
				return 0;
			}		
		}
	else
		return 0x00;
	
	return newCluster;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//文件操作
////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char OpenFile32(unsigned char *pBuffer)
{
	unsigned int i;
	unsigned char j,bstop,sector;
	unsigned char UARTBUF[32];
			
	ThisFile.bFileOpen=0;
	NowCluster32=DirStartCluster32;		
		do
		{
			NowSector=FirstSectorofCluster32(NowCluster32);
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	    	{   
				if(ReadSector(NowSector+sector,1,diskbuff)!=TRUE)
					return FALSE;				
				for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
				{
					if(diskbuff[i]==0x00)
						return FALSE;
					j=0;
					while(diskbuff[i+j]==*(pBuffer+j))
					{
						 j=j+1;
						 if(j>10)
						 	break;
					}
					if(j>10&&(diskbuff[i+11]&0x10)!=0x10)
				    	{ 
						for(j=0;j<32;j++)
			    			UARTBUF[j]=diskbuff[i+j];			    
			    		bstop=1;
			    		break;
						}
				}
				if(bstop==1)break;		
	    	}
			if(bstop==1)break;	
			NowCluster32=GetNextClusterNum32(NowCluster32);			
		}while(NowCluster32<=DeviceInfo.TotCluster);
		
	if(NowCluster32>DeviceInfo.TotCluster)
	   	return FALSE;

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


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

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

	
	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(ReadSector(ThisFile.SectorPointer,1,diskbuff)==TRUE)
		   		{
		   		
		   		len=len-ThisFile.OffsetofSector;
		   		for(i=0;i<len;i++)
		   			
		   			*(pBuffer+i)=diskbuff[ThisFile.OffsetofSector+i];
		   		ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
		   		}
		   	else
		   		return FALSE;		   		
		   	}
		   else
		   	{
		   		if(ReadSector(ThisFile.SectorPointer,1,pBuffer+tlen)!=TRUE)
		   				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=GetNextClusterNum32(ThisFile.ClusterPointer);
		 		 if(ThisFile.ClusterPointer>DeviceInfo.TotCluster)
		 		 	   return FALSE;			 		 	
		 		 ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer); 	
		   	}
		   	else
		   		ThisFile.SectorPointer=ThisFile.SectorPointer+1;
		    }
		   //////////////////////////////////////////////////////////////////
		}//end while
		
	ThisFile.bFileOpen=1;
	ThisFile.pointer+=tlen;
	//////////////////////////////////////////////
	//Response.len=blen;

	return TRUE;
}

unsigned char SetFilePointer32(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(!GoToPointer32(ThisFile.pointer))
	{
	ThisFile.bFileOpen=0;
	return FALSE;	
	}
	//////////////////////////////////////////////
	return TRUE;
}


//输入量:len-长文件名所占的字节数,必须为32 的整数倍。当
//为0 时表示该文件为短文件名;
//*pBuffer-32 字节的短文件名目录项;
//*pName-长文件名的目录项。

unsigned char CreateFile32(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
{
	unsigned int sector,i,j,DirCount;
	unsigned long cnum;
	unsigned char  bstop,InByte,bwrite;
	unsigned long ClusterPointer;
	
	//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=GetFreeCusterNum32();
	if(cnum<0x02)
		return FALSE;	

	pBuffer[21]=(unsigned char)(cnum>>24);
	pBuffer[20]=(unsigned char)(cnum>>16);
	pBuffer[27]=(unsigned char)(cnum>>8);
	pBuffer[26]=(unsigned char)(cnum);

	pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
	bstop=0;

	NowCluster32=DirStartCluster32;		
		do
		{
			NowSector=FirstSectorofCluster32(NowCluster32);
			ClusterPointer=NowCluster32;
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	    	{   
				if(ReadSector(NowSector+sector,1,diskbuff)!=TRUE)
					return FALSE;
				DirCount=0;bwrite=0;

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

				if((DirCount*32)>=(len+32))
					{
					for(j=0;j<len;j++)
						diskbuff[InByte+j]=*(pName+j);
					for(j=0;j<32;j++)
						diskbuff[InByte+len+j]=*(pBuffer+j);
					if(WriteSector(NowSector+sector,1,diskbuff)!=TRUE)
		  		 		return FALSE;		  	 		
					bstop=1;
					break;
					}
				 }
				}
				if(bstop==1)break;
				
				if((len!=0)&&(bwrite==1))
				{
				if(WriteSector(NowSector+sector,1,diskbuff)!=TRUE)
		  			return FALSE;
	    		}
	    	}
			if(bstop==1)break;
	
			NowCluster32=GetNextClusterNum32(NowCluster32);
			if(NowCluster32>DeviceInfo.TotCluster)
	    	{
			NowCluster32=CreateClusterLink32(ClusterPointer);
		 	if(NowCluster32==0x00)
		 		 return FALSE;
			NowSector=FirstSectorofCluster32(NowCluster32);
			for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) diskbuff[i]=0x00;
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
				{
				if(WriteSector(NowSector+sector,1,diskbuff)!=TRUE)
		  	 		return FALSE;
				}
			}
		}while(NowCluster32<=DeviceInfo.TotCluster);	
////////////////////////////////////////////////////////////////
	
	ThisFile.StartCluster=cnum;
	ThisFile.LengthInByte=0;
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.StartCluster);
	ThisFile.OffsetofSector=0;
	ThisFile.SectorofCluster=0;
	ThisFile.bFileOpen=1;
	ThisFile.pointer=0;
	ThisFile.FatSectorPointer=0;
	
	return TRUE;
}

⌨️ 快捷键说明

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