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

📄 fat.c

📁 利用MCS-51单片机和USB接口芯片
💻 C
字号:
#include "common.h"
#include "Fat.h"
#include "Fat32.h"
#include "SL811.H"
#include "TPBULK.H"
#include "HAL.H"
////////////////////////////////////////
extern SYS_INFO_BLOCK xdata DeviceInfo;
extern FILE_INFO xdata ThisFile;
extern unsigned char xdata DBUF[BUFFER_LENGTH];
unsigned char xdata FATBUF[512];
////////////////////////////////////////

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

}

unsigned int ThisFatSecNum(unsigned int clusterNum)
{
   return clusterNum/(DeviceInfo.BPB_BytesPerSec/2)+DeviceInfo.FatStartSector;
}

unsigned int ThisFatEntOffset(unsigned int clusterNum)
{
 return (clusterNum%(DeviceInfo.BPB_BytesPerSec/2))*2;
}

unsigned int GetNextClusterNum(unsigned int clusterNum)
{
	unsigned int FatSecNum,FatEntOffset;
	
	FatSecNum=ThisFatSecNum(clusterNum);
	FatEntOffset=ThisFatEntOffset(clusterNum);
	if(ThisFile.FatSectorPointer!=FatSecNum)
	{	
		
		if(!RBC_Read(FatSecNum,1,FATBUF))
			return 0xFFFF;
		ThisFile.FatSectorPointer=FatSecNum;
	}
	
	///////////////////////////////////////////////////
	AssignInt(clusterNum,FATBUF[FatEntOffset],FATBUF[FatEntOffset+1]);
	return clusterNum;
}

unsigned char GoToPointer(unsigned long pointer)
{
	
	unsigned int clusterSize;
	
	clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	while(pointer>clusterSize)
	{
		pointer-=clusterSize;	
		ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
		if(ThisFile.ClusterPointer==0xffff)
		{
		return FALSE;
		}
	}
	ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec;
	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster;
	ThisFile.OffsetofSector=pointer%DeviceInfo.BPB_BytesPerSec;
	ThisFile.FatSectorPointer=0;
	return TRUE;
	
}

unsigned char DeleteClusterLink(unsigned int clusterNum)
{
	unsigned int FatSecNum,FatEntOffset;
	unsigned char i;
	while((clusterNum>1)&&(clusterNum<0xfff0))
	{
	FatSecNum=ThisFatSecNum(clusterNum);
	FatEntOffset=ThisFatEntOffset(clusterNum);
	if(RBC_Read(FatSecNum,1,DBUF))
		{
		 AssignInt(clusterNum,DBUF[FatEntOffset],DBUF[FatEntOffset]);
		}
	else
		return FALSE;
	DBUF[FatEntOffset]=0x00;
	DBUF[FatEntOffset+1]=0x00;	
	for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
		{
		DelayMs(5);
		if(!RBC_Write(FatSecNum+i*DeviceInfo.BPB_FATSz16,1,DBUF))
			return FALSE;
		}	
	}
	return TRUE;
}

unsigned int GetFreeCusterNum(void)
{
	unsigned int clusterNum,i;
	unsigned long sectorNum;
	unsigned char j;
	clusterNum=0;
	sectorNum=DeviceInfo.FatStartSector;
	while(sectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
	{
		
		if(!RBC_Read(sectorNum,1,DBUF))
			return 0x0;
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+2)
		  	{
		  	 if((DBUF[i]==0)&&(DBUF[i+1]==0))
		  	 	{	
		  	 	DBUF[i]=0xff;
		  	 	DBUF[i+1]=0xff;
				for(j=0;j<DeviceInfo.BPB_NumFATs;j++)
					{
					DelayMs(5);
					if(!RBC_Write(sectorNum+j*DeviceInfo.BPB_FATSz16,1,DBUF))
						return FALSE;
					}	
		  	  	clusterNum=clusterNum+i/2;
		        if((clusterNum-2)<DeviceInfo.TotCluster) return clusterNum;
		  	  	else return	FALSE; 
		  	 	}
		  	}
	    clusterNum += (DeviceInfo.BPB_BytesPerSec/2);		
		sectorNum++;
		DelayMs(10);
	}
	
	return 0x0;
}

unsigned int CreateClusterLink(unsigned int currentCluster)
{
	unsigned int newCluster;
	unsigned int FatSecNum,FatEntOffset;
	unsigned char i;

	newCluster=GetFreeCusterNum();

	if(newCluster==0)
		return 0x00;
			
	FatSecNum=ThisFatSecNum(currentCluster);
	FatEntOffset=ThisFatEntOffset(currentCluster);
	if(RBC_Read(FatSecNum,1,DBUF))
		{
		DBUF[FatEntOffset]=((unsigned char *)newCluster)[1];
		DBUF[FatEntOffset+1]=((unsigned char *)newCluster)[0];
		for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
			{
			DelayMs(5);
			if(!RBC_Write(FatSecNum+i*DeviceInfo.BPB_FATSz16,1,DBUF))
				return FALSE;
			}		
		}
	else
		return 0x00;
	
	return newCluster;
}

⌨️ 快捷键说明

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