#include "uart.h"
#include "sd.h"
#include "FAT16.h"
unsigned int FirstDataSector; // The first sector number of data
unsigned int BytesPerSector; // unsigned chars per sector
unsigned int FATsectors; // The amount sector a FAT occupied
unsigned int SectorsPerClust; // Sector per cluster
unsigned int FirstFATSector; // The first FAT sector
unsigned int FirstDirSector; // The first Dir sector
unsigned int RootDirSectors; // The sector number a Root dir occupied
unsigned int RootDirCount; // The count of directory in root dir
struct FileInfoStruct FileInfo;//temporarily buffer for file information
unsigned char xdata buffer[512];
unsigned long lb2bb(unsigned char *dat,unsigned char len)
{
unsigned long temp=0;
unsigned long fact=1;
unsigned char i=0;
for(i=0;i<len;i++)
{
temp+=dat[i]*fact;
fact*=256;
}
return temp;
}
unsigned char FAT16_ReadSector(unsigned long lba,unsigned char *buf)
{
MMC_get_data_LBA(lba,512,buf);
return 0;
}
unsigned char FAT16_WriteSector(unsigned long lba,unsigned char *buf)
{
return MMC_write_sector(lba,buf);
}
//unsigned char (* FAT16_ReadSector)(unsigned long,unsigned char *);
//unsigned char (* FAT16_WriteSector)(unsigned long,unsigned char *);
//unsigned char (* FAT16_ReadSector)(unsigned long sector, unsigned char * buffer)=MMC_SD_ReadSingleBlock;//device read
//unsigned char (* FAT16_WriteSector)(unsigned long sector, unsigned char * buffer)=MMC_SD_WriteSingleBlock;//device write
void free(unsigned char *b)
{
unsigned int i=0;
for(i=0;i<512;i++)
{
b[i]=0;
}
}
unsigned char FAT16_Init()//Initialize of FAT16
{
struct bootsector50 *bs;
struct bpb50 *bpb;
unsigned int hidsec;
for(hidsec=100;hidsec<200;hidsec++)//Some card the BPB is not locat at secter 0
{
if(FAT16_ReadSector(hidsec,buffer))
return 1;
if(buffer[0]==0xeb || buffer[0]==0xe9)
break;
}
if(hidsec==200)
return 1; //error maybe the card hasn't been formated
bs=(struct bootsector50 *)buffer;
bs->bsOemName[7]=0;
send_s(bs->bsOemName);
if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB)
return 1;
bpb=(struct bpb50 *)bs->bsBPB;
BytesPerSector =lb2bb((unsigned char *)(&(bpb->bpbBytesPerSec)),2);
Printf("BytesPerSector",BytesPerSector);
FATsectors = lb2bb((unsigned char *)(&(bpb->bpbFATsecs)),2);
Printf("FATsectors=",FATsectors);
SectorsPerClust = (unsigned char)bpb->bpbSecPerClust;
Printf("SectorsPerClust=",SectorsPerClust);
FirstFATSector = lb2bb((unsigned char *)(&(bpb->bpbResSectors)),2)+hidsec;
Printf("FirstFATSector=",FirstFATSector);
RootDirCount = lb2bb((unsigned char *)(&(bpb->bpbRootDirEnts)),2);
Printf("RootDirCount=",RootDirCount);
RootDirSectors = (RootDirCount*32)>>9;
Printf("RootDirSectors=",RootDirSectors);
FirstDirSector = FirstFATSector+bpb->bpbFATs*FATsectors;
Printf("FirstDirSector=",FirstDirSector);
FirstDataSector = FirstDirSector+RootDirSectors;
Printf("FirstDataSector=",FirstDataSector);
return 0;
}
unsigned char FAT16_LoadPartCluster(unsigned int cluster,unsigned long part,unsigned char * buffer)
{
unsigned long sector;
sector=FirstDataSector+(unsigned long)(cluster-2)*(unsigned long)SectorsPerClust;//calculate the actual sector number
if(FAT16_ReadSector(sector+part,buffer))
return 1;
else
return 0;
}
//Read the a cluster
//Not suitable for system which has few data RAM
unsigned char FAT16_LoadCluster(unsigned int cluster,unsigned char * buffer)
{
unsigned long sector;
unsigned char i;
sector=FirstDataSector+(unsigned long)(cluster-2)*(unsigned long)SectorsPerClust;//calculate the actual sector number
for(i=0;i<SectorsPerClust;i++)
{
if(FAT16_ReadSector(sector+i,buffer+(i<<9)))
break;
}
if(i==SectorsPerClust)
return 0;
else
return 1;
}
//Return the cluster number of next cluster of file
//Suitable for system which has limited RAM
unsigned int FAT16_NextCluster(unsigned int cluster)
{
//unsigned char buffer[512];
unsigned long sector;
unsigned long offset=cluster/256;
if(cluster<2)return 0xfff8;
sector=FirstFATSector+offset;//calculate the actual sector
if(FAT16_ReadSector(sector,buffer))return 0xfff8;//read fat table / return 0xfff8 when error occured
offset=cluster%256;//find the position
//offset<<=1;
//sector=buffer[offset+1];
// sector<<=8;
//sector+=buffer[offset];
sector=((unsigned int *)buffer)[offset];
return (unsigned int)sector;//return the cluste number
}
//Find a free cluster return the cluster number
unsigned int FAT16_FindFreeCluster()
{
//unsigned char buffer[512];
//unsigned long sector;
unsigned int i;
unsigned int cnt;
// sector=FirstFATSector+offset;//calculate the actual sector
//sector=FirstFATSector;
for(cnt=0;cnt<FATsectors;cnt++)//find in the FAT table
{
if(FAT16_ReadSector(FirstFATSector+cnt,buffer))
return 1;//error
for(i=0;i<256;i++)
{
if(((unsigned int *)buffer)[i]==0x0000)
break;//an unused cluster
}
if(i!=256)
{
cnt=cnt*512+i;
return cnt;//return the free cluster number
}
}
return 1;//error
}
//find a position to place a item withe the given directory, the parameter is FileInfo who brought the message
unsigned int FAT16_FindFreeItem(unsigned int cluster, struct FileInfoStruct *FileInfo)
{
//unsigned char *buffer;
unsigned long tempclust;
unsigned long sector;
unsigned char cnt;
unsigned int offset;
// unsigned char i;
struct direntry *item;
if(cluster==0)// root directory
{
//buffer=malloc(512);//apply memory
//if(buffer==0)return 1;//if failed
for(cnt=0;cnt<RootDirSectors;cnt++)
{
if(FAT16_ReadSector(FirstDirSector+cnt,buffer))
{
free(buffer);
return 1;
}//read sector
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);
//used item
if((item->deName[0] != 0x00) & (item->deName[0] != 0xe5) & (item->deAttributes != 0x0f));
//unused item
else
{
FileInfo->StartCluster = item->deStartCluster;//don't care
FileInfo->Size = item->deFileSize;//don't care
FileInfo->Attr = item->deAttributes;//don't care
FileInfo->Sector = FirstDirSector+cnt;//The key parameter record the secoter nuber which the item stored
FileInfo->Offset = offset; //The key parameter record the offset in the sector
free(buffer);//realease
return 0;//done
}
}
}
free(buffer);//release
}
else//other folders
{
tempclust=cluster;
while(1)
{
sector=FirstDataSector+(tempclust-2)*SectorsPerClust;//calculate the actual sector number
//buffer=malloc(512);//apply memory
//if(buffer==0)return 1;//if failed
for(cnt=0;cnt<SectorsPerClust;cnt++)
{
if(FAT16_ReadSector(sector+cnt,buffer))
{
free(buffer);
return 1;
}
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);
if((item->deName[0] != 0x2e) & (item->deName[0] != 0x00) & (item->deName[0] != 0xe5) & (item->deAttributes != 0x0f));
else
{
FileInfo->StartCluster = item->deStartCluster;
FileInfo->Size = item->deFileSize;
FileInfo->Attr = item->deAttributes;
FileInfo->Sector = sector+cnt;
FileInfo->Offset = offset;
free(buffer);
return 0;
}
}
}
free(buffer);//release
tempclust=FAT16_NextCluster(tempclust);//next cluster
if(tempclust == 0xffff || tempclust == 0xfff8)
return 1;
}
}
return 1;
}
//Find a item in the directory which specify by the parameter "cluster"
//Return the start cluster number
unsigned int FAT16_FindItem(unsigned int cluster, unsigned char *name, struct FileInfoStruct *FileInfo)
{
//unsigned char *buffer;
unsigned long tempclust;
unsigned long sector;
unsigned char cnt;
unsigned int offset;
unsigned c
没有合适的资源?快使用搜索试试~ 我知道了~
SD卡上的FAT32文件系统源代码
共33个文件
h:6个
c:3个
bak:3个
5星 · 超过95%的资源 需积分: 15 178 下载量 122 浏览量
2010-08-15
12:49:46
上传
评论 5
收藏 149KB RAR 举报
温馨提示
SD卡上的FAT32文件系统源代码SD卡上的FAT32文件系统源代码SD卡上的FAT32文件系统源代码
资源推荐
资源详情
资源评论
收起资源包目录
SD卡上的FAT32文件系统源代码.RAR (33个子文件)
SD卡成功.bmp 965KB
SD原理图.JPG 14KB
SD CARD
SD CARD_Uv2.Bak 0B
SD CARD.Uv2 2KB
sd.LST 3KB
SD CARD.Opt 1KB
SD CARD.hex 19KB
SD卡成功.bmp 965KB
SD CARD.plg 10KB
uart.h 2KB
SD原理图.JPG 14KB
SD CARD
SD CARD_Uv2.Bak 2KB
FAT32.rar 3KB
SD CARD.Uv2 2KB
FAT16.h 8KB
sd.LST 3KB
SD CARD.Opt 2KB
SD CARD.hex 31KB
SD CARD.plg 2KB
SD CARD.lnp 41B
FAT32.h 13KB
uart.h 3KB
FAT16.c 36KB
SD CARD 58KB
SD CARD_Opt.Bak 2KB
sd.OBJ 63KB
sd.h 16KB
sd.c 1KB
SD CARD.M51 57KB
sd.OBJ 31KB
sd.h 16KB
sd.c 2KB
SD CARD.M51 34KB
共 33 条
- 1
Oliver_zlsu
- 粉丝: 14
- 资源: 118
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页