www.pudn.com > bu1566.rar > fat32.c


#include "fat32.h" 
#include "hpi32.h" 
#include "string.h" 
#include "sdconfig.h" 
#include "sddriver.h"						/* SD卡操作的相关函数 */ 
 
SYS_INFO_BLOCK DeviceInfo; 
FILE_INFO  ThisFile; 
UINT8 DBUF[BUFFER_LENGTH]; 
UINT8 FATBUF[512]; 
UINT32  DirStartCluster32,NowCluster32; 
UINT32 NowSector; 
UINT8 UARTBUF[UARTBUF_LENGTH]; 
//////////////////////////////////////// 
 
UINT16 LSwapINT16(UINT16 dData1,UINT16 dData2) 
{ 
    UINT16 dData; 
    dData = ((dData2<<8)&0xff00)|(dData1&0x00ff); 
	return dData; 
} 
 
 
UINT32 LSwapINT32(UINT32 dData1,UINT32 dData2,UINT32 dData3,UINT32 dData4) 
{ 
    UINT32 dData; 
    dData = ((dData4<<24)&0xff000000)|((dData3<<16)&0xff0000)|((dData2<<8)&0xff00)|(dData1&0xff); 
	return dData; 
} 
 
UINT8 EnumSdDev(void) 
{ 
UINT16 ReservedSectorsNum; 
UINT8 temp; 
 
	temp=MMC_read_sector(0x00,DBUF);//引导记录信息 
	if (temp==0x00) 
	{ 
		if(DBUF[0]==0xeb||DBUF[0]==0xe9) 
		{ 
			DeviceInfo.StartSector=0; 
		} 
		else 
		{ 
			DeviceInfo.StartSector=LSwapINT32(DBUF[454],DBUF[455],DBUF[456],DBUF[457]); 
		} 
		temp=MMC_read_sector (DeviceInfo.StartSector,DBUF);  
		if (temp!=0x00) 
			return FALSE; 
 
	} 
	else 
		return FALSE; 
	DeviceInfo.BPB_BytesPerSec=LSwapINT16(DBUF[11],DBUF[12]); 
	DeviceInfo.BPB_SecPerClus=DBUF[13]; 
	ReservedSectorsNum=LSwapINT16(DBUF[14],DBUF[15]); 
	DeviceInfo.BPB_NumFATs=DBUF[16]; 
	if(DBUF[82]=='F'&&DBUF[83]=='A'&&DBUF[84]=='T'&&DBUF[85]=='3'&&DBUF[86]=='2') 
	{ 
		DeviceInfo.BPB_TotSec32=LSwapINT32(DBUF[32],DBUF[33],DBUF[34],DBUF[35]); 
		DeviceInfo.BPB_FATSz32=LSwapINT32(DBUF[36],DBUF[37],DBUF[38],DBUF[39]); 
		DeviceInfo.RootStartCluster=LSwapINT32(DBUF[44],DBUF[45],DBUF[46],DBUF[47]); 
		DeviceInfo.FatStartSector=DeviceInfo.StartSector+ReservedSectorsNum; 
		DeviceInfo.FirstDataSector=DeviceInfo.FatStartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz32; 
		DeviceInfo.TotCluster=(DeviceInfo.BPB_TotSec32-DeviceInfo.FirstDataSector+1)/DeviceInfo.BPB_SecPerClus+1; 
		DirStartCluster32=DeviceInfo.RootStartCluster; 
		ThisFile.bFileOpen=0;	 
		return TRUE; 
	} 
	else 
	{		 
		return FALSE; 
	}		 
} 
 
UINT32 FirstSectorofCluster(UINT32 clusterNum) 
{ 
	UINT32 temp; 
	temp=clusterNum-2; 
	temp=temp*DeviceInfo.BPB_SecPerClus; 
	temp=temp+DeviceInfo.FirstDataSector; 
	return temp; 
} 
 
 
 
UINT32 ThisFatSecNum(UINT32 clusterNum) 
{ 
   UINT32 temp; 
   temp=clusterNum*4; 
   temp=temp/DeviceInfo.BPB_BytesPerSec; 
   temp=temp+DeviceInfo.FatStartSector; 
   return temp; 
} 
 
UINT32 ThisFatEntOffset(UINT32 clusterNum) 
{ 
UINT32 temp1,temp2; 
 
	temp1=4*clusterNum; 
	temp2=temp1/DeviceInfo.BPB_BytesPerSec; 
	temp1=temp1-temp2*DeviceInfo.BPB_BytesPerSec; 
	return temp1; 
} 
 
 
UINT32 GetNextClusterNum(UINT32 clusterNum) 
{ 
UINT32 FatSecNum,FatEntOffset; 
	 
	FatSecNum=ThisFatSecNum32(clusterNum); 
	FatEntOffset=ThisFatEntOffset32(clusterNum); 
	if(ThisFile.FatSectorPointer!=FatSecNum) 
	{	 
		 
		if(!RBC_Read(FatSecNum,1,FATBUF)) 
			return 0xFFFFFFFF; 
		ThisFile.FatSectorPointer=FatSecNum; 
	} 
	 
	/////////////////////////////////////////////////// 
	clusterNum=LSwapINT32(FATBUF[FatEntOffset],FATBUF[FatEntOffset+1],FATBUF[FatEntOffset+2],FATBUF[FatEntOffset+3]); 
	return clusterNum; 
} 
 
UINT8 GoToPointer(UINT32 pointer) 
{ 
	 
	UINT32 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==0xffffffff) 
		{ 
			return FALSE; 
		} 
	} 
	ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec; 
	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster; 
	ThisFile.OffsetofSector=pointer-ThisFile.SectorofCluster*DeviceInfo.BPB_BytesPerSec; 
	ThisFile.FatSectorPointer=0; 
	return TRUE; 
} 
 
UNTT32 GetFreeCusterNum(void) 
{ 
UNTT32 clusterNum,i; 
UNTT32 sectorNum; 
UNTT8 j; 
	 
	clusterNum=0; 
	sectorNum=DeviceInfo.FatStartSector; 
	while(sectorNum>8; 
		    DBUF[FatEntOffset+2]=newCluster>>16; 
		    DBUF[FatEntOffset+3]=newCluster>>24; 
			DBUF[FatEntOffset+4]=0xff; 
		    DBUF[FatEntOffset+5]=0xff; 
		    DBUF[FatEntOffset+6]=0xff; 
		    DBUF[FatEntOffset+7]=0xff; 
	        for(i=0;i>8; 
		    DBUF[FatEntOffset+2]=newCluster>>16; 
		    DBUF[FatEntOffset+3]=newCluster>>24; 
	        for(i=0;i