📄 fat32.c
字号:
//输入量:writeLength-写入数据的长度;*pBuffer-要写入的数据。
//说明:写一个文件前需要成功打开或创建一个文件,成功写入一定字节后,文件指针会向后移动。
unsigned char WriteFile32(unsigned long writeLength,unsigned char *pBuffer)
{
unsigned int len,sector,i,tlen;
unsigned char bSuccess,bStop;
unsigned long cnum;
//if(!bFlags.bits.SLAVE_IS_ATTACHED)
// return FALSE;
if(!ThisFile.bFileOpen)
return FALSE;
ThisFile.bFileOpen=0;
bSuccess=1;
bStop=0;
tlen=0;
while(writeLength>0)
{
if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
len=DeviceInfo.BPB_BytesPerSec;
else
len=writeLength+ThisFile.OffsetofSector;
//////////////////////////////////////////////////////
if(ThisFile.OffsetofSector>0)
{
if(ReadSector(ThisFile.SectorPointer,1,diskbuff)==TRUE)
{
len=len-ThisFile.OffsetofSector;
for(i=0;i<len;i++)
diskbuff[ThisFile.OffsetofSector+i]=*(pBuffer+i);
if(WriteSector(ThisFile.SectorPointer,1,diskbuff)!=TRUE)
return FALSE;
ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
}
else
return FALSE;
}
else
{
if(WriteSector(ThisFile.SectorPointer,1,pBuffer+tlen)!=TRUE)
return FALSE;
ThisFile.OffsetofSector=len;
}
/////////////////////////////////////////////////////
writeLength-=len;
tlen+=len;
/////////////更新文件指针 ////////////////////////////
if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
{
ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
ThisFile.SectorofCluster+=1;
if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
{
ThisFile.SectorofCluster=0;
ThisFile.ClusterPointer=CreateClusterLink32(ThisFile.ClusterPointer);
if(ThisFile.ClusterPointer==0x00)
return FALSE;
ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer);
}
else
ThisFile.SectorPointer=ThisFile.SectorPointer+1;
}
}//end while
ThisFile.pointer+=tlen;
///////////更新文件目录信息/////////////////////////////
if(bSuccess==1)
{
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)
{
cnum=LSwapINT32(diskbuff[i+26],diskbuff[i+27],diskbuff[i+20],diskbuff[i+21]);
if((cnum==ThisFile.StartCluster)&&(diskbuff[i]!=0xe5))
{
if(ThisFile.pointer>ThisFile.LengthInByte)
ThisFile.LengthInByte=ThisFile.pointer;
diskbuff[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
diskbuff[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
diskbuff[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
diskbuff[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
if(WriteSector(NowSector+sector,1,diskbuff)!=TRUE)
return FALSE;
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;
//////////////////////////////////////////////
return TRUE;
}
unsigned char RemoveFile32(unsigned char *pBuffer)
{
unsigned int sector,i;
unsigned char bStop,j;
int k;
//if(!bFlags.bits.SLAVE_IS_ATTACHED)
// return FALSE;
////////////// 清除目录/////////////////////////////////////
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]=0xE5;
ThisFile.StartCluster=LSwapINT32(diskbuff[i+26],diskbuff[i+27],diskbuff[i+20],diskbuff[i+21]);
for(k=(i-32);k>=0;k=k-32)
{
if(diskbuff[k+11]==0x0F)
diskbuff[k]=0xE5;
else
break;
}
delay_ms(15);
if(WriteSector(NowSector+sector,1,diskbuff)!=TRUE)
return FALSE;
//////////////////// 清除FAT中的纪录////////////////////////
delay_ms(10);
if(!DeleteClusterLink32(ThisFile.StartCluster))
return FALSE;
bStop=1;
break;
}
}
if(bStop==1)break;
}
if(bStop==1)break;
NowCluster32=GetNextClusterNum32(NowCluster32);
}while(NowCluster32<=DeviceInfo.TotCluster);
if(NowCluster32>DeviceInfo.TotCluster)
return FALSE;
return TRUE;
}
unsigned char GetCapacity32(unsigned long *plong)
{
unsigned int sectorNum,i;
unsigned long FreeSize,Freesectorcnt;
//if(!bFlags.bits.SLAVE_IS_ATTACHED)
// return FALSE;
////////////////////////////////////////////////////////////////////////
sectorNum=DeviceInfo.FatStartSector;
Freesectorcnt=0;
while(sectorNum<DeviceInfo.BPB_FATSz32+DeviceInfo.FatStartSector)
{
if(ReadSector(sectorNum,1,diskbuff)==TRUE)
{
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+4)
{
if((diskbuff[i]||diskbuff[i+1]||diskbuff[i+2]||diskbuff[i+3])==0x00)
{
Freesectorcnt++;
}
}
}
else
return FALSE;
sectorNum++;
}
////////////////////////////////////////////////////////////////////////
FreeSize=DeviceInfo.BPB_BytesPerSec*DeviceInfo.BPB_SecPerClus;
FreeSize=Freesectorcnt*FreeSize;
*plong=FreeSize;
//UARTBUF[0]=(unsigned char)((FreeSize>>24)&0xff);
//UARTBUF[1]=(unsigned char)((FreeSize>>16)&0xff);
//UARTBUF[2]=(unsigned char)((FreeSize>>8)&0xff);
//UARTBUF[3]=(unsigned char)(FreeSize&0xff);
//Response.len=4;
return TRUE;
}
unsigned char CreateDir32(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;
ThisFile.FatSectorPointer=0;
cnum=GetFreeCusterNum32();
if(cnum<0x02)
return FALSE;
pBuffer[11]=0x10;
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);
if(NowCluster32>DeviceInfo.TotCluster)
return FALSE;
////////////////////////////////////////////////////////////////
for(i=64;i<DeviceInfo.BPB_BytesPerSec;i++) diskbuff[i]=0x00;
for(i=0;i<43;i++) diskbuff[i]=0x20;
diskbuff[0]=0x2e;
for(i=11;i<32;i++) diskbuff[i]=pBuffer[i];
diskbuff[32]=0x2e;diskbuff[33]=0x2e;
for(i=43;i<64;i++) diskbuff[i]=pBuffer[i-32];
if(DirStartCluster32==DeviceInfo.RootStartCluster) //Root Dir
{
diskbuff[53]=0;diskbuff[52]=0;diskbuff[59]=0;diskbuff[58]=0;
}
else
{
diskbuff[53]=(unsigned char)(DirStartCluster32>>24);
diskbuff[52]=(unsigned char)(DirStartCluster32>>16);
diskbuff[59]=(unsigned char)(DirStartCluster32>>8);
diskbuff[58]=(unsigned char)(DirStartCluster32);
}
NowSector=FirstSectorofCluster32(cnum);
if(WriteSector(NowSector,1,diskbuff)!=TRUE)
return FALSE;
DirStartCluster32=cnum;
// ThisFile.ClusterPointer=DirStartCluster32;
ThisFile.ClusterPointer=0;
return TRUE;
}
unsigned char DownDir32(unsigned char *pBuffer)
{
unsigned int i;
unsigned char j,bstop,sector;
//if(!bFlags.bits.SLAVE_IS_ATTACHED)
// return FALSE;
ThisFile.bFileOpen=0;
bstop=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))
{bstop=1;break;}
}
if(bstop==1)break;
}
if(bstop==1)break;
NowCluster32=GetNextClusterNum32(NowCluster32);
}while(NowCluster32<=DeviceInfo.TotCluster);
if(NowCluster32>DeviceInfo.TotCluster)
return FALSE;
DirStartCluster32=LSwapINT32(diskbuff[i+26],diskbuff[i+27],diskbuff[i+20],diskbuff[i+21]);
// ThisFile.ClusterPointer=DirStartCluster32;
ThisFile.ClusterPointer=0;
return TRUE;
}
unsigned char UpDir32(void)
{
//if(!bFlags.bits.SLAVE_IS_ATTACHED)
// return FALSE;
if(DirStartCluster32==DeviceInfo.RootStartCluster) //Root Dir
return TRUE;
ThisFile.bFileOpen=0;
NowSector=FirstSectorofCluster32(DirStartCluster32);
if(ReadSector(NowSector,1,diskbuff)!=TRUE)
return FALSE;
if(diskbuff[32]!=0x2e&&diskbuff[33]!=0x2e) //..
return FALSE;
DirStartCluster32=LSwapINT32(diskbuff[58],diskbuff[59],diskbuff[52],diskbuff[53]);
if(DirStartCluster32==0) //Root Dir
DirStartCluster32=DeviceInfo.RootStartCluster;
// ThisFile.ClusterPointer=DirStartCluster32;
ThisFile.ClusterPointer=0;
return TRUE;
}
unsigned char UpRootDir32(void)
{
//if(!bFlags.bits.SLAVE_IS_ATTACHED)
// return FALSE;
ThisFile.bFileOpen=0;
DirStartCluster32=DeviceInfo.RootStartCluster; //Root Dir
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -