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

📄 hpi16.c

📁 TMS2812实现U盘FAT文件系统C代码
💻 C
📖 第 1 页 / 共 3 页
字号:
					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]=0xE5;
					 	ThisFile.StartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
						for(k=(i-32);k>=0;k=k-32)
						{
						if(DBUF[k+11]==0x0F)
							DBUF[k]=0xE5;
						else
							break;
						}
					 	DelayMs(150);
					 	if(!RBC_Write(NowSector+sector,1,DBUF))
							return FALSE;					
				//////////////////// 清除FAT中的纪录////////////////////////
						DelayMs(100);
						if(!DeleteClusterLink16(ThisFile.StartCluster))
							return FALSE;					
					 	bStop=1;
					 	break;
						}
				}
				if(bStop==1)break;		
	    	}
			if(bStop==1)break;	
			NowCluster=GetNextClusterNum16(NowCluster);			
		}while(NowCluster<=0xffef);
		
		if(NowCluster>0xffef)
	    	return FALSE;
	}
	return TRUE;
}

unsigned long GetCapacity16(void)
{
	unsigned int sectorNum,Freesectorcnt,i;	
	unsigned long FreeSize;

	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;
		
	////////////////////////////////////////////////////////////////////////
	sectorNum=DeviceInfo.FatStartSector;
	Freesectorcnt=0;
	while(sectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
	{
		
		if(RBC_Read(sectorNum,1,DBUF))
		{
		  for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+2)
		  	{		  	 
		  	 if((DBUF[i]==0x00)&&(DBUF[i+1]==0x00))
		  	 	{	
		  	 	Freesectorcnt++;
		  	 	}	  
		  	}	
		}
		else			
			return FALSE;			
		sectorNum++;
	}
	
	////////////////////////////////////////////////////////////////////////
	FreeSize=DeviceInfo.BPB_BytesPerSec*DeviceInfo.BPB_SecPerClus;
	FreeSize=Freesectorcnt*FreeSize;
	
	return FreeSize;
}

unsigned char CreateDir16(char *str)
{   
	unsigned int  	sector,i,j,DirCount;
	unsigned long  	cnum;
	unsigned char  	bstop,InByte,bwrite;
	unsigned long 	ClusterPointer;
	unsigned char 	pBuffer[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	unsigned char 	ch;
	unsigned char 	len=0;
	unsigned char 	*pName;
	
	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;
	if (*str == '.')	
		return	FALSE;
	i=0;			
	while (str[i] !=0)
	{
		ch = str[i];
		if(ch == '\\' || ch == '/' || ch == ':' || ch == '*' || ch == '?' 
		|| ch == '"' || ch == '<' || ch == '>' || ch == '|') return FALSE;
		if (islower(ch))		str[i] = toupper(ch);
		i++;
	}	
	for (i=0;i<11;i++)
		pBuffer[i] = 0x20;
	len = 0;
	i=0;	
	while(str[i]!='.')
	{
		len++;
		i++;
	}
	if (len<8)
	{	
		i = 0;
		while(str[i]!='.')
		{
			pBuffer[i] = str[i];
			i++;
		}
		len = 0;
	}
	else
	{
		for (i=0;i<6;i++)
			pBuffer[i] = str[i];
		pBuffer[6] = '~';
		pBuffer[7] = '1';
		i=0;
		len = 0;
	}
	ThisFile.bFileOpen=0;
	ThisFile.FatSectorPointer=0;
	cnum=GetFreeCusterNum32();
	if(cnum<0x02)
		return FALSE;	
	pBuffer[11]= 0x10;
	pBuffer[21]= cnum>>24;
	pBuffer[20]= cnum>>16;
	pBuffer[27]= cnum>>8;
	pBuffer[26]= cnum;
	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=FirstSectorofCluster16(NowCluster);
			ClusterPointer=NowCluster;
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	    	{   
				if(!RBC_Read(NowSector+sector,1,DBUF))
					return FALSE;
				DirCount=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;					
					}
				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)
				{
				if(!RBC_Write(NowSector+sector,1,DBUF))
		  			return FALSE;
	    		}
	    	}
			if(bstop==1)break;
	
			NowCluster=GetNextClusterNum16(NowCluster);
			if(NowCluster>0xffef)
	    	{
			NowCluster=CreateClusterLink16(ClusterPointer);
		 	if(NowCluster==0x00)
		 		 return FALSE;
			NowSector=FirstSectorofCluster16(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;
	}
////////////////////////////////////////////////////////////////
	for(i=64;i<DeviceInfo.BPB_BytesPerSec;i++)	DBUF[i]=0x00;
	
	for(i=0;i<43;i++) DBUF[i]=0x20;
	
	DBUF[0]=0x2e;
	for(i=11;i<32;i++) DBUF[i]=pBuffer[i];

	DBUF[32]=0x2e;DBUF[33]=0x2e;
	for(i=43;i<64;i++) DBUF[i]=pBuffer[i-32];
	DBUF[58]=(unsigned char)(DirStartCluster);
	DBUF[59]=(unsigned char)(DirStartCluster>>8);

	NowSector=FirstSectorofCluster16(cnum);
	if(!RBC_Write(NowSector,1,DBUF))
		return FALSE;	

	DirStartCluster=cnum;
	ThisFile.ClusterPointer=0;		
	return TRUE;	
}

unsigned char DownDir16(char *pBuffer)
{
	unsigned int i;
	unsigned char j,bstop,sector;	
		
	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;	
	
	ThisFile.bFileOpen=0;
	bstop=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))
			    {			   
			    bstop=1;
			     break;}
			
			}
		if(bstop==1)break;		
	    }
	    
	    if(sector>=DeviceInfo.BPB_RootEntCnt)
	    	return FALSE;		
	    	
	DirStartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
//	ThisFile.ClusterPointer=DirStartCluster;
	ThisFile.ClusterPointer=0;
	return TRUE;
	}
	////////////////////////////////////////////
	else
	{
		NowCluster=DirStartCluster;		
		do
		{
			NowSector=FirstSectorofCluster16(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))
				    	{bstop=1;break;}
				}
				if(bstop==1)break;		
	    	}
			if(bstop==1)break;	
			NowCluster=GetNextClusterNum16(NowCluster);			
		}while(NowCluster<=0xffef);
		
	if(NowCluster>0xffef)
	    	return FALSE;
		
	DirStartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
//	ThisFile.ClusterPointer=DirStartCluster;
	ThisFile.ClusterPointer=0;		
	return TRUE;		
	}
}

unsigned char UpDir16()
{
	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;	
	if(DirStartCluster==0)	//Root Dir
		return TRUE;	
	
	ThisFile.bFileOpen=0;

	NowSector=FirstSectorofCluster16(DirStartCluster);
	if(!RBC_Read(NowSector,1,DBUF))
			return FALSE;
	if(DBUF[32]!=0x2e&&DBUF[33]!=0x2e)	//..
			return FALSE;
	
	DirStartCluster=LSwapINT16(DBUF[58],DBUF[59]);
	ThisFile.ClusterPointer=0;		
	return TRUE;
}

unsigned char UpRootDir16()
{
	if(!bFlags.bits.SLAVE_IS_ATTACHED)
		return FALSE;

	ThisFile.bFileOpen=0;
	DirStartCluster=0;	//Root Dir
			
	return TRUE;
}

⌨️ 快捷键说明

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