📄 fat32.c
字号:
{
if(!RBC_Read((NowSector+sector),1,DBUF))
return FALSE;
//DirCount=0;
//bwrite=0;
i=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);//填充32 个目录项
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;
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((NowSector+sector),1,DBUF))
return FALSE;
bstop=1;
break;
}
}*/
}
if(bstop==1)break;
/*if((len!=0)&&(bwrite==1))
{
if(!RBC_Write((NowSector+sector),1,DBUF))
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++) DBUF[i]=0x00;
for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
{
if(!RBC_Write((NowSector+sector),1,DBUF))
return FALSE;
}
}
}while(NowCluster32<=DeviceInfo.TotCluster);
////////////////////////////////////////////////////////////////
ThisFile.StartCluster=cnum;
ThisFile.LengthInByte=0;
ThisFile.ClusterPointer=ThisFile.StartCluster;
ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.StartCluster);
ThisFile.OffsetofSector=0;
ThisFile.SectorofCluster=0;
ThisFile.bFileOpen=1;
ThisFile.pointer=0;
ThisFile.FatSectorPointer=0;
return TRUE;
}
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(RBC_Read(ThisFile.SectorPointer,1,DBUF))
{
len=len-ThisFile.OffsetofSector;
for(i=0;i<len;i++)
DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
if(!RBC_Write(ThisFile.SectorPointer,1,DBUF))
return FALSE;
ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
}
else
return FALSE;
}
else
{
if(!RBC_Write(ThisFile.SectorPointer,1,(pBuffer+tlen)))
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(!RBC_Read(NowSector+sector,1,DBUF))
return FALSE;
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
cnum=LSwapINT32(DBUF[i+26],DBUF[i+27],DBUF[i+20],DBUF[i+21]);
if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
{
if(ThisFile.pointer>ThisFile.LengthInByte)
ThisFile.LengthInByte=ThisFile.pointer;
DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
if(!RBC_Write(NowSector+sector,1,DBUF))
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;
bStop=0;
//if(!bFlags.bits.SLAVE_IS_ATTACHED)
// return FALSE;
////////////// 清除目录/////////////////////////////////////
NowCluster32=DirStartCluster32;
do
{
NowSector=FirstSectorofCluster32(NowCluster32);
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]=0xE5;
ThisFile.StartCluster=LSwapINT32(DBUF[i+26],DBUF[i+27],DBUF[i+20],DBUF[i+21]);
/*for(k=(i-32);k>=0;k=k-32)
{
if(DBUF[k+11]==0x0F)
DBUF[k]=0xE5;
else
break;
}*/
DelayMs(15);
if(!RBC_Write((NowSector+sector),1,DBUF))
return FALSE;
//////////////////// 清除FAT中的纪录////////////////////////
DelayMs(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;
}
void InitSdFileSystem(void)
{
unsigned int ReservedSectorsNum,kj,date;
unsigned char Fat32;
unsigned char ut[3],bkt;
Fat32=0;
DirStartCluster=0;
ThisFile.LengthInByte=0;
RBC_Read(0,1,DBUF);
if(DBUF[0]==0xEB||DBUF[0]==0xE9) //得到DBR的起始扇区:或为0,或为63(0x0000003F)
{
DeviceInfo.StartSector=0;
}
else
{
DeviceInfo.StartSector=LSwapINT32(DBUF[454],DBUF[455],DBUF[456],DBUF[457]);
}
RBC_Read(DeviceInfo.StartSector,1,DBUF);
if((DBUF[82]=='F')&&(DBUF[83]=='A')&&(DBUF[84]=='T')&&(DBUF[85]=='3')&&(DBUF[86]=='2')) Fat32++;
else
{
DisNoFat();
while(1);
}
DeviceInfo.BPB_BytesPerSec=LSwapINT16(DBUF[11],DBUF[12]);
DeviceInfo.BPB_SecPerClus=DBUF[13];
ReservedSectorsNum=LSwapINT16(DBUF[14],DBUF[15]);
DeviceInfo.BPB_NumFATs=DBUF[16];
//DeviceInfo.BPB_RootEntCnt=LSwapINT16(DBUF[17],DBUF[18]);//根目录项数
//DeviceInfo.BPB_RootEntCnt=(DeviceInfo.BPB_RootEntCnt)*32/DeviceInfo.BPB_BytesPerSec;//根目录区所占扇区数
//DeviceInfo.BPB_TotSec16=LSwapINT16(DBUF[19],DBUF[20]); //小扇区数
//DeviceInfo.BPB_FATSz16=LSwapINT16(DBUF[22],DBUF[23]);//每FAT扇区数
//DeviceInfo.FatStartSector=DeviceInfo.StartSector+ReservedSectorsNum;
//DeviceInfo.RootStartSector=DeviceInfo.StartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz16+ReservedSectorsNum;
//DeviceInfo.FirstDataSector=DeviceInfo.FatStartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz16+DeviceInfo.BPB_RootEntCnt;
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;
DeviceInfo.FAT=1; //FAT16=0,FAT32=1;
zzSecNums=DeviceInfo.FirstDataSector+DeviceInfo.BPB_SecPerClus;
zzFAT1=DeviceInfo.FatStartSector;
zzFDT=DeviceInfo.FirstDataSector;
zzSecPerClus=DeviceInfo.BPB_SecPerClus;
//DeviceInfo.FAT=0;
for(kj=0;kj<32;kj++) NewFile[kj]=0x20;
NewFile[0]=0x4c;NewFile[1]=0x41;NewFile[2]=0x4e;
NewFile[3]=0x44;NewFile[4]=0x43;NewFile[5]=0x4f;NewFile[6]=0x4d;
NewFile[8]=0x45;
NewFile[9]=0x43;
NewFile[10]=0x47;
zzFileFlag=OpenFile32(NewFile); //ReadFile
//zzFileFlag=TRUE;
if(zzFileFlag==TRUE)
{
if(ThisFile.LengthInByte!=0)
{
DisDelFile(); //是续记,还是删除文件重新开始记录
if(GetKey()) { DisDelIng(); RemoveFile32(NewFile); zzFileFlag=FALSE; }
}
}
//else
if(zzFileFlag==FALSE)
{
NewFile[11]=0x20;
NewFile[0x0C]=0x00;
NewFile[0x0D]=0x00;
NewFile[0x14]=0x00;
NewFile[0x15]=0x00;
//date=0x12*2048+0x30*32+0/2;Year,Month,Date,Hour,Min,Sec
//CurTime[0]=(ct[2]/16)*10+ct[2]%16;
//CurTime[1]=(ct[1]/16)*10+ct[1]%16;
v_Get1302(ut);
bkt=(ut[2]/16)*10+ut[2]%16; ut[2]=bkt;
bkt=(ut[1]/16)*10+ut[1]%16; ut[1]=bkt;
bkt=(ut[0]/16)*10+ut[0]%16; //ut[0]=bkt;
//date=12*2048+30*32+0/2;
date=ut[2]*2048+ut[1]*32+bkt/2;
NewFile[0x16]=(uchar)date; //time
NewFile[0x17]=(uchar)(date>>8);
NewFile[0x0e]=(uchar)date; //time
NewFile[0x0f]=(uchar)(date>>8);
v_Get1302Date(ut);
bkt=(ut[2]/16)*10+ut[2]%16; ut[2]=bkt;
bkt=(ut[1]/16)*10+ut[1]%16; ut[1]=bkt;
bkt=(ut[0]/16)*10+ut[0]%16; //ut[0]=bkt;
//date=(2000-1980+0x08)*512+0x03*32+0x30;
//date=(2000-1980+0x08)*512+0x05*32+23;
date=(2000-1980+ut[2])*512+ut[1]*32+bkt;
NewFile[0x18]=(uchar)date; //date
NewFile[0x19]=(uchar)(date>>8);
NewFile[0x10]=(uchar)date; //date
NewFile[0x11]=(uchar)(date>>8);
NewFile[0x12]=(uchar)date; //date
NewFile[0x13]=(uchar)(date>>8);
NewFile[0x1A]=0x00; //文件开始簇
NewFile[0x1B]=0x00;
NewFile[0x1C]=0x0;
NewFile[0x1D]=0x0;
NewFile[0x1E]=0x0;
NewFile[0x1F]=0x0;
Fat32=CreateFile32(0, NewFile);
if(Fat32==FALSE) { DisNoFile(); while(1); }
}
/*else
{
DisDelFile();
if(GetKey()) RemoveFile32(NewFile);
}*/
if(ThisFile.LengthInByte==0) zzFileFlag=FALSE;//文件长度为0,不进行续记,
//因为续记标识未写入
}
unsigned char GetCapacity32(void)
{
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(RBC_Read(sectorNum,1,DBUF))
{
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+4)
{
if((DBUF[i]||DBUF[i+1]||DBUF[i+2]||DBUF[i+3])==0x00)
{
Freesectorcnt++;
}
}
}
else
return FALSE;
sectorNum++;
}
////////////////////////////////////////////////////////////////////////
FreeSize=DeviceInfo.BPB_BytesPerSec*DeviceInfo.BPB_SecPerClus;
FreeSize=Freesectorcnt*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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -