📄 fat16.c
字号:
//-------------------------------------------------------------------------
//#include <avr/io.h>
//#include <stdint.h>
#include "FAT16.h"
#include "mmc.h"
#include "sdconfig.h"
#include "sdhal.h"
#include "sdcmd.h"
#include "sddriver.h"
#include "sdcrc.h"
//------------------------------------------------------------------------
#define SEC_Size 512
#define MBR_Sector 0 //绝对地址
#define FAT_Sector 0 //逻辑地址
//-------------------------------------------------------------------------
Uint8 BUFFER[514];
Uint8 PB_RelativeSector;
Uint16 BPB_BytesPerSec;
Uint8 BPB_SecPerClus;
Uint16 BPB_RsvdSecCnt;
Uint8 BPB_NumFATs;
Uint16 BPB_RootEntCnt;
Uint16 BPB_TotSec16;
Uint16 BPB_FATSz16; //FAT占用的sectors
Uint32 BPB_HiddSec;
Uint8 tee=0;
//extern Uint16 clsre;
//extern volatile Uint8 tras[];
//-------------------------------------------------------------------------
Uint8 ReadBlock(Uint32 LBA){ //绝对地址读一个扇区
Uint8 ret;
//ret = SD_ResetSD(); /* 5. 发出CMDO命令复位SD卡 send CMD0 command to reset sd card */
//if (ret != 0)
//printf("Invalid data Response token.\n");
//EVMDM642_waitusec(60000);
//EVMDM642_waitusec(600000);
if(SD_ReadBlock(LBA,BUFFER)!=0)
return SD_FAIL;
ret = SD_ActiveInit(); /* 6. 激活卡进入初始化过程. active card initialize process */
if (ret != 0)
printf("Invalid data Response token.\n");
return SD_SUCC;
}
//-------------------------------------------------------------------------
Uint8 WriteBlock(Uint32 LBA){ //绝对地址写一个扇区
Uint8 ret;
/*ret = SD_ResetSD();
if (ret != 0)
printf("Invalid data Response token.\n");
EVMDM642_waitusec(60000);
ret = SD_ActiveInit();
if (ret != 0)
printf("Invalid data Response token.\n");*/
if(SD_WriteBlock(LBA,BUFFER)!=0)
return SD_FAIL;
ret = SD_ActiveInit();
if (ret != 0)
printf("Invalid data Response token.\n");
return SD_SUCC;
}
//-------------------------------------------------------------------------
Uint8 ReadFatBlock(Uint32 Add){ //逻辑地址读一个扇区
return ReadBlock(Add+PB_RelativeSector);
}
//-------------------------------------------------------------------------
Uint8 WriteFatBlock(Uint32 Add){ //逻辑地址写一个扇区
return WriteBlock(Add+PB_RelativeSector);
}
//-------------------------------------------------------------------------
void CopyBytes(Uint8 *ps,Uint8 *pd,Uint16 size){ //内存拷贝
for(;size;size--)*pd++=*ps++;
}
//-------------------------------------------------------------------------
Uint8 IsEqual(Uint8 *pa,Uint8 *pb,Uint8 size){ //内存比较
for(;size;size--)if(*pa++!=*pb++)return 0;
return 1;
}
//-------------------------------------------------------------------------
void EmptyBytes(Uint8 *pd,Uint16 size){ //内存清空
for(;size;size--)*pd++ =0;
}
//-------------------------------------------------------------------------
void correctdir(Uint8 *ad,Uint32 size,Uint16 start)
{
*(ad+26)=start&0xff;
*(ad+27)=start>>8;
*(ad+28)=size&0xff;
*(ad+29)=(size>>8)&0xff;
*(ad+30)=(size>>16)&0xff;
*(ad+31)=(size>>24)&0xff;
}
/**************************************************************************/
void recorrectdir(Uint8 *ad)
{
*(ad+28)=*(ad+26);
*(ad+29)=*(ad+27);
}
/****************************************************************************/
Uint8 ReadMBR(void){
//读取MBR数据结构
Uint8 ok,exchange[66];
Uint8 i;
FAT_MBR * MBR=(FAT_MBR*)BUFFER;
ok=ReadBlock(MBR_Sector);
if(ok==SD_FAIL)return SD_FAIL;
for(i=0;i<66;i++)
exchange[i]=BUFFER[i+446];
for(i=0;i<66;i++)
BUFFER[i+448]=exchange[i];
if(MBR->MBR_Signature!=0xAA55)return SD_FAIL; //读有效标志
//获取参数
PB_RelativeSector=MBR->MBR_pb[0].PB_RelativeSector;//读逻辑地址与绝对地址的偏移
return SD_SUCC;
}
//-------------------------------------------------------------------------
Uint8 ReadBPB(void){ //读取BPB数据结构
Uint8 ok;
char BS_FilSysType[8];
FAT_BPB * BPB=(FAT_BPB*)BUFFER;
ok=ReadFatBlock(FAT_Sector);
if(ok==SD_FAIL)return SD_FAIL;
/*for(i=0;i<512;i++)
{
exchange[i]=BUFFER[i];
}
BUFFER[12]=exchange[11];
BUFFER[13]=exchange[12];
BUFFER[14]=exchange[13];
BUFFER[14]=exchange[13];*/
//获取参数
BPB_BytesPerSec = BUFFER[11]+(BUFFER[12]<<8);//BPB->BPB_BytesPerSec;
BPB_SecPerClus = BUFFER[13];//BPB->BPB_SecPerClus;
BPB_RsvdSecCnt = BUFFER[14]+(BUFFER[15]<<8);//BPB->BPB_RsvdSecCnt;
BPB_NumFATs = BUFFER[16];//BPB->BPB_NumFATs;
BPB_RootEntCnt = BUFFER[17]+(BUFFER[18]<<8);//BPB->BPB_RootEntCnt;
BPB_TotSec16 = BUFFER[19]+(BUFFER[20]<<8);//BPB->BPB_TotSec16;
BPB_FATSz16 = BUFFER[22]+(BUFFER[23]<<8);//BPB->BPB_FATSz16;
BPB_HiddSec = BUFFER[28]+(BUFFER[29]<<8)+(BUFFER[30]<<16)+(BUFFER[31]<<24);//BPB->BPB_HiddSec;
BS_FilSysType[0] = BUFFER[54];
BS_FilSysType[1] = BUFFER[55];
BS_FilSysType[2] = BUFFER[56];
BS_FilSysType[3] = BUFFER[57];
BS_FilSysType[4] = BUFFER[58];
BS_FilSysType[5] = BUFFER[59];
BS_FilSysType[6] = BUFFER[60];
BS_FilSysType[7] = BUFFER[61];
return SD_SUCC;
}
//-------------------------------------------------------------------------
Uint32 DirStartSec(void){ //获取根目录开始扇区号
return BPB_RsvdSecCnt+BPB_NumFATs*BPB_FATSz16;
}
//-------------------------------------------------------------------------
Uint16 GetDirSecCount(void){ //目录项占用的扇区数
return BPB_RootEntCnt*32/BPB_BytesPerSec;
}
//-------------------------------------------------------------------------
Uint32 DataStartSec(void){ //获取数据区开始扇区号
return DirStartSec()+GetDirSecCount();
}
//-------------------------------------------------------------------------
Uint32 ClusConvLBA(Uint16 ClusID){ //获取一个簇的开始扇区
return DataStartSec()+BPB_SecPerClus*(ClusID-2);
}
//-------------------------------------------------------------------------
Uint16 ReadFAT(Uint16 Index){ //读取文件分配表的指定项
Uint16 *RAM=(Uint16*)BUFFER;
Uint32 SecID;
SecID=BPB_RsvdSecCnt+Index/256;
ReadFatBlock(SecID);
return RAM[Index%256];
}
//-------------------------------------------------------------------------
void WriteFAT(Uint16 Index,Uint16 Value,Uint32 j){
Uint16 *RAM=(Uint16*)BUFFER;
Uint32 SecID;
tee=tee+1;
SecID=BPB_RsvdSecCnt+Index/256;
ReadFatBlock(SecID);
RAM[Index%256]=Value;
WriteFatBlock(SecID);
if((j==0)/*||(j==1)*/)
EVMDM642_waitusec(1000000);
else
EVMDM642_waitusec(6000);
SD_ResetSD();
SD_ActiveInit();
WriteFatBlock(SecID+BPB_FATSz16);
EVMDM642_waitusec(30);
}
/*void WriteFAT1(Uint16 Index,Uint16 Value){ //写文件分配表的指定项
Uint16 *RAM=(Uint16*)BUFFER;
Uint32 SecID;
SecID=BPB_RsvdSecCnt+Index/256;
ReadFatBlock(SecID);
RAM[Index%256]=Value;
WriteFatBlock(SecID);
}*/
//-------------------------------------------------------------------------
Uint16 GetEmptyDIR(void){ //获取根目录中可以使用的一项
Uint16 i,DirSecCut,DirStart,m,ID=0;
Uint8 ret;
DirSecCut=GetDirSecCount();
DirStart=DirStartSec();
ret = SD_ResetSD(); /* 5. 发出CMDO命令复位SD卡 send CMD0 command to reset sd card */
if (ret != 0)
printf("Invalid data Response token.\n");
EVMDM642_waitusec(60000);
//EVMDM642_waitusec(600000);
ret = SD_ActiveInit(); /* 6. 激活卡进入初始化过程. active card initialize process */
if (ret != 0)
printf("Invalid data Response token.\n");
for(i=0;i<DirSecCut;i++){
ReadFatBlock(DirStart+i);
for(m=0;m<16;m++){
if(BUFFER[m*32]==0)return ID;
if(BUFFER[m*32]==0xe5)return ID;
ID++;
}
}
return ID;
}
//-------------------------------------------------------------------------
Uint16 GetNextFAT(Uint16 clsid){ //获取一个空的FAT项
Uint16 FAT_Count,i;
FAT_Count=BPB_FATSz16*256; //FAT表总项数
for(i=clsid;i<FAT_Count;i++){
if(ReadFAT(i)==0)return i;
}
return 0;
}
//-------------------------------------------------------------------------
void ReadDIR(Uint16 Index, DIR* Value){ //读取根目录的指定项
Uint32 LBA = DirStartSec()+Index/16;
ReadFatBlock(LBA);
CopyBytes((Uint8 *)&BUFFER[(Index%16)*32],(Uint8 *)Value,32);
}
//-------------------------------------------------------------------------
void WriteDIR(Uint16 Index, DIR* Value){ //写根目录的指定项
Uint32 LBA = DirStartSec()+Index/16;
ReadFatBlock(LBA);
CopyBytes((Uint8 *)Value,(Uint8 *)&BUFFER[(Index%16)*32],32);
WriteFatBlock(LBA);
}
//-------------------------------------------------------------------------
void CopyFAT(void){
Uint16 i;
Uint8 ret;
//Uint32 fat=0x80500000;
ret = SD_ResetSD();
if (ret != 0)
printf("Invalid data Response token.\n");
EVMDM642_waitusec(60000);
//EVMDM642_waitusec(600000);
ret = SD_ActiveInit();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -