📄 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<DeviceInfo.BPB_FATSz32+DeviceInfo.FatStartSector)
{
if(!RBC_Read(sectorNum,1,DBUF))
return 0x0;
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+4)
{
if((DBUF[i]==0)&&(DBUF[i+1]==0)&&(DBUF[i+2]==0)&&(DBUF[i+3]==0))
{
DBUF[i]=0xff;DBUF[i+1]=0xff;DBUF[i+2]=0xff;DBUF[i+3]=0xff;
for(j=0;j<DeviceInfo.BPB_NumFATs;j++)
{
DelayMs(5);
if(!RBC_Write(sectorNum+j*DeviceInfo.BPB_FATSz32,1,DBUF))
return FALSE;
}
return clusterNum;
}
clusterNum++;
}
sectorNum=4*clusterNum/DeviceInfo.BPB_BytesPerSec+DeviceInfo.FatStartSector;
DelayMs(10);
}
return 0x0;
}
UINT32 CreateClusterLink(UINT32 currentCluster)
{
UINT32 newCluster;
UINT32 FatSecNum,FatSecNum1,FatEntOffset;
UINT8 i,temp,retry;
newCluster=currentCluster+1;
FatSecNum=ThisFatSecNum(currentCluster);
FatSecNum1=ThisFatSecNum(newCluster);
if (FatSecNum==FatSecNum1)
{
FatEntOffset=ThisFatEntOffset(currentCluster);
if(MMC_read_sector(FatSecNum,DBUF)==0x00)
{
DBUF[FatEntOffset]=newCluster;
DBUF[FatEntOffset+1]=newCluster>>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<DeviceInfo.BPB_NumFATs;i++)
{
retry=0;
do{
temp=MMC_write_sector(FatSecNum+i*DeviceInfo.BPB_FATSz32,DBUF);
retry++;
}while((temp!=0x00)&&(retry<100));
if (temp!=0x00)
return FALSE;
}
}
else
return 0x00;
}
else
{
FatEntOffset=ThisFatEntOffset(currentCluster);
if(MMC_read_sector(FatSecNum,DBUF)==0x00)
{
DBUF[FatEntOffset]=newCluster;
DBUF[FatEntOffset+1]=newCluster>>8;
DBUF[FatEntOffset+2]=newCluster>>16;
DBUF[FatEntOffset+3]=newCluster>>24;
for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
{
retry=0;
do{
temp=MMC_write_sector(FatSecNum+i*DeviceInfo.BPB_FATSz32,DBUF);
retry++;
}while((temp!=0x00)&&(retry<100));
if (temp!=0x00)
return FALSE;
}
}
else
return 0x00;
FatEntOffset=ThisFatEntOffset(newCluster);
if(MMC_read_sector(FatSecNum1,DBUF)==0x00)
{
DBUF[FatEntOffset]=0xff;
DBUF[FatEntOffset+1]=0xff;
DBUF[FatEntOffset+2]=0xff;
DBUF[FatEntOffset+3]=0xff;
for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
{
retry=0;
do{
temp=MMC_write_sector(FatSecNum1+i*DeviceInfo.BPB_FATSz32,DBUF);
retry++;
}while((temp!=0x00)&&(retry<100));
if (temp!=0x00)
return FALSE;
}
}
else
return 0x00;
}
return newCluster;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -