#include"FAT.h"
DWORD FirstDirClust; //第一个目录簇号
DWORD FirstDataSector; // 数据区的第一个扇区
WORD BytesPerSector; // 每扇区字节数
DWORD FATsectors; // 一个FAT表所占扇区数
WORD SectorsPerClust; // 每簇扇区数
DWORD FirstFATSector; // 第一个FAT表所在扇区
DWORD FirstDirSector; // 第一个目录扇区
DWORD RootDirSectors; // 根目录所在扇区
DWORD RootDirCount; // 根目录下目录项数
BYTE FAT32_Enable;
BYTE TABLE_READ = 0;
DWORD START_CLUSTER = 0x0ffffff8; //when the mcu has large ram
BYTE FAT_TABLE[512]; //when the mcu has large ram
BYTE LongNameBuffer[MAX_LONG_NAME_SIZE];
BYTE LongNameFlag = 0;
//显示一次数据流
void show_data(u8 *p,u16 num)
{
u16 i=0;
printf("TEMP DATA:\n");
delay_ms(1000);
for(i=0;i<num;i++)printf(" %x",*p++);
printf("\ndata over\n");
}
struct FileInfoStruct FileInfo;//temporarily buffer for file information
//FAT初始化,不含SD的初始化,用之前应先调用sd的初始化
unsigned char FAT_Init()//Initialize of FAT need initialize SD first
{
struct bootsector710 *bs = 0;
struct bpb710 *bpb = 0;
// struct partsector *ps = 0;
struct partrecord *pr = 0;
BYTE buffer[512];
DWORD hidsec=0;
DWORD Capacity;
Capacity = SD_GetCapacity();
if(Capacity<0xff)return 1;
if(SD_ReadSingleBlock(0,buffer))return 1;
bs = (struct bootsector710 *)buffer;
pr = (struct partrecord *)((struct partsector *)buffer)->psPart;//first partition
hidsec = pr->prStartLBA;//the hidden sectors
if(hidsec >= Capacity/512)
{
hidsec = 0;
}
else
{
if(SD_ReadSingleBlock(pr->prStartLBA,buffer))return 1;//read the bpb sector
bs = (struct bootsector710 *)buffer;
if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB)
{
hidsec = 0;
if(SD_ReadSingleBlock(0,buffer))return 1;//read the bpb sector
bs = (struct bootsector710 *)buffer;
}
}
if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB)//对付没有bootsect的sd卡 //dead with the card which has no bootsect
{
return 1;
}
bpb = (struct bpb710 *)bs->bsBPB;
if(bpb->bpbFATsecs)//detemine thd FAT type //do not support FAT12
{
FAT32_Enable=0; //FAT16
FATsectors = bpb->bpbFATsecs;//FAT表占用的扇区数
FirstDirClust = 2;
}
else
{
FAT32_Enable=1; //FAT32
FATsectors = bpb->bpbBigFATsecs;//FAT占用的扇区数 //the sectors number occupied by one fat talbe
FirstDirClust = bpb->bpbRootClust;
}
BytesPerSector = bpb->bpbBytesPerSec;//每扇区字节数
SectorsPerClust = (BYTE)bpb->bpbSecPerClust;//每簇扇区数
FirstFATSector = bpb->bpbResSectors+hidsec;//第一个FAT表扇区
RootDirCount = bpb->bpbRootDirEnts; //根目录项数
RootDirSectors = (RootDirCount*32)>>9; //根目录占用的扇区数
FirstDirSector = FirstFATSector+bpb->bpbFATs*FATsectors;//第一个目录扇区
FirstDataSector = FirstDirSector+RootDirSectors;//第一个数据扇区
return 0;
}
//读一个簇中的一个扇区
unsigned char FAT_LoadPartCluster(unsigned long cluster,unsigned part,BYTE * buffer)
{
DWORD sector;
sector=(DWORD)FirstDataSector+(DWORD)(cluster-2)*(DWORD)SectorsPerClust;//calculate the actual sector number
if(SD_ReadSingleBlock(sector+part,buffer))return 1;
else return 0;
}
//读整个簇
//Read the a cluster
//Not suitable for system which has few data RAM
unsigned char FAT_LoadCluster(unsigned long cluster,BYTE * buffer)
{
DWORD sector;
unsigned char i;
sector=FirstDataSector+(DWORD)(cluster-2)*(DWORD)SectorsPerClust;//calculate the actual sector number
for(i=0;i<SectorsPerClust;i++)
{
if(SD_ReadSingleBlock(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 long FAT_NextCluster(unsigned long cluster)
{
BYTE buffer[512];
DWORD sector;
DWORD offset;
if(FAT32_Enable)offset = cluster/128;
else offset = cluster/256;
if(cluster<2)return 0x0ffffff8;
sector=FirstFATSector+offset;//calculate the actual sector
if(SD_ReadSingleBlock(sector,buffer))return 0x0ffffff8;//read fat table / return 0xfff8 when error occured
if(FAT32_Enable)
{
offset=cluster%128;//find the position
sector=((unsigned long *)buffer)[offset];
}
else
{
offset=cluster%256;//find the position
sector=((unsigned int *)buffer)[offset];
}
return (unsigned long)sector;//return the cluste number
}
//显示给定路径下的文件
//display the content of a foler , 0 as root directory
unsigned char FAT_DisDir(BYTE *dir)
{
BYTE *buffer;
DWORD sector;
DWORD cluster;
DWORD tempclust;
unsigned int cnt;
unsigned int offset;
unsigned char i;
struct direntry *item = 0;
cluster = FAT_OpenDir(dir);
if(cluster == 1)return 1;
if(cluster==0 && FAT32_Enable==0)// root directory
{
buffer=malloc(512);//apply memory
if(buffer==0)return 1;//if failed
for(cnt=0;cnt<RootDirSectors;cnt++)
{
if(SD_ReadSingleBlock(FirstDirSector+cnt,buffer)){free(buffer);return 1;}
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);//pointer convert
//find a valid item and display it
if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5) && (item->deAttributes != 0x0f))
{
putchar(0x0d);
putchar(0x0a);
for(i=0;i<8;i++)//name
{
putchar(item->deName[i]);
}
if((item->deAttributes & 0x10)==0)putchar('.');
for(i=0;i<3;i++)//extention
{
putchar(item->deExtension[i]);
}
}
}
}
free(buffer);//release
}
else//other folders
{
tempclust=cluster;
while(1)
{
sector=(DWORD)FirstDataSector+(DWORD)(tempclust-2)*(DWORD)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(SD_ReadSingleBlock(sector+cnt,buffer)){free(buffer);return 1;}
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);
if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5) && (item->deAttributes != 0x0f))
{
putchar(0x0d);
putchar(0x0a);
for(i=0;i<8;i++)
{
putchar(item->deName[i]);
}
if((item->deAttributes & 0x10)==0)putchar('.');
for(i=0;i<3;i++)
{
putchar(item->deExtension[i]);
}
}
}
}
free(buffer);//release
tempclust=FAT_NextCluster(tempclust);//next cluster
if( tempclust == 0x0ffffff8 )return 1;
if((FAT32_Enable == 0 && tempclust == 0xffff) || tempclust == 0x0fffffff)break;
}
}
return 0;
}
//在给定目录下查找文件
//Find a item in the directory which specify by the parameter "cluster"
//Return the start cluster number
unsigned int FAT_FindItem(unsigned long cluster, BYTE *name, struct FileInfoStruct *FileInfo)
{
BYTE *buffer;
DWORD tempclust;
DWORD sector;
unsigned int cnt;
unsigned int offset;
unsigned char i;
struct direntry *item = 0;
if((cluster==0) && (FAT32_Enable == 0))// root directory
{
buffer=malloc(512);//apply memory
if(buffer==0)return 1;//if failed
for(cnt=0;cnt<RootDirSectors;cnt++)
{
if(SD_ReadSingleBlock(FirstDirSector+cnt,buffer)){free(buffer);return 1;}
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);
if((item->deName[0] != 0x00) && (item->deName[0] != 0xe5) && (item->deAttributes != 0x0f))
{
for(i=0;i<11;i++)
{
if(buffer[offset+i]!=name[i])break;
}
if(i==11)
{
//return the parameter of the item
FileInfo->StartCluster = item->deStartCluster + (((unsigned long)item->deHighClust)<<16);//don't care
FileInfo->Size = item->deFileSize;
FileInfo->Attr = item->deAttributes;
FileInfo->Sector = FirstDirSector+cnt;
FileInfo
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于stm32和vs1003的mp3播放器设计与实现MDK (181个子文件)
jpegdecode.__i 260B
temperate.__i 259B
rgbledplay.__i 259B
mp3player.__i 255B
calendar.__i 251B
tea5767.__i 251B
fm24c16.__i 251B
vs1003x.__i 250B
setting.__i 247B
untogb.__i 247B
tftlcd.__i 247B
mmc_sd.__i 247B
lrcpro.__i 247B
rgbled.__i 247B
picture.__i 247B
pt2314.__i 247B
touch.__i 243B
myiic.__i 243B
ptgame.__i 243B
radio.__i 239B
alarm.__i 239B
ebook.__i 239B
time.__i 235B
text.__i 235B
gui.__i 231B
fat.__i 231B
main.__i 228B
MP3.axf 364KB
MP3_Opt.Bak 6KB
MP3_Uv2.Bak 4KB
save.c 42KB
jpegdecode.c 31KB
alarm.c 23KB
MMC_SD.c 23KB
gui.c 21KB
tftlcd.c 19KB
FAT.c 18KB
touch.c 18KB
mp3player.c 17KB
ptgame.c 16KB
calendar.c 15KB
lrcpro.c 15KB
setting.c 12KB
rgbledplay.c 11KB
VS1003x.c 10KB
text.c 10KB
TEA5767.c 9KB
radio.c 9KB
main.c 6KB
time.c 3KB
temperate.c 3KB
pt2314.c 3KB
picture.c 3KB
myiic.c 3KB
fm24c16.c 2KB
untogb.c 1KB
rgbled.c 1KB
ebook.c 934B
iic.c 26B
jpegdecode.crf 189KB
tftlcd.crf 187KB
fat.crf 185KB
text.crf 183KB
alarm.crf 182KB
vs1003x.crf 181KB
mmc_sd.crf 181KB
touch.crf 180KB
ptgame.crf 180KB
gui.crf 179KB
calendar.crf 179KB
lrcpro.crf 179KB
mp3player.crf 179KB
main.crf 178KB
myiic.crf 177KB
rgbledplay.crf 177KB
setting.crf 177KB
tea5767.crf 177KB
radio.crf 176KB
temperate.crf 176KB
pt2314.crf 175KB
fm24c16.crf 175KB
time.crf 175KB
picture.crf 175KB
untogb.crf 174KB
rgbled.crf 174KB
ebook.crf 174KB
vs1003.crf 171KB
red.crf 162KB
strtext.crf 158KB
jpegdecode.d 2KB
rgbledplay.d 2KB
temperate.d 2KB
mp3player.d 2KB
vs1003x.d 2KB
calendar.d 2KB
tea5767.d 2KB
fm24c16.d 2KB
untogb.d 2KB
setting.d 2KB
picture.d 2KB
共 181 条
- 1
- 2
zzq134733
- 粉丝: 2
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 黑苹果OC引导-0.9.1
- Redis 服务等过期策略和内存淘汰策略解析
- debian配置FTP服务
- 基于Matlab和CPLEX的2变量机组组合调度程序(注释完全,可直接运行)(文档加Matlab源码)
- 基于TMS320F2812设计复合频率信号频率计AD09硬件(原理图+PCB )+CCS软件源码+详细设计文档资料.zip
- MultivariateAnalysis(目标规划、多元分析与插值的相关例子)(注释完全,可直接运行)(文档加Matlab源码)
- 黑苹果OC引导-0.9.2
- 数据库实验-王珊.doc
- unity读取excel工具 使用3.5即可
- Matplotlib 是一个 Python 的绘图库 Matplotlib 绘图指南与功能介绍.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页