/*******************************************************************/
/* FAT diriver for MP3 Player */
/* */
/* Platform : MAPLAB + MCC18 */
/* optimize : Enable All */
/* Author : bozai(Zhang Qibo) */
/* E-mail : sudazqb@163.com */
/* MSN : zhangqibo_1985@hotmail.com */
/* Date : 2007-12-01 */
/*******************************************************************/
/*2007-12-01: migrate from AVR platform to PIC */
/*2007-11-09: now the songs may placed in any directory, but the */
/* total number of folder the card has should less than */
/* 127 for we use 512Bytes EEPROM to stored the info */
/*2007-10-08: fix a bug (but it never works), so stupid I am, */
/* take & as && */
/*2006-06-14: still the bug of FAT */
/*2006-05-04: fix bug of FAT init, add read capacity check */
/*******************************************************************/
#include"FAT.h"
#include<p18cxxx.h>
#include<stdio.h>
extern unsigned char tmpBuf[512];
DWORD FirstDirClust; //first directory cluster
DWORD FirstDataSector; // The first sector number of data
WORD BytesPerSector; // Bytes per sector
WORD FATsectors; // The amount sector a FAT occupied
WORD SectorsPerClust; // Sector per cluster
DWORD FirstFATSector; // The first FAT sector
DWORD FirstDirSector; // The first Dir sector
DWORD RootDirSectors; // The sector number a Root dir occupied
DWORD RootDirCount; // The count of directory in root dir
BYTE FAT32_Enable;
BYTE (* FAT_ReadSector)(DWORD,BYTE *);
BYTE (* FAT_WriteSector)(DWORD,BYTE *);
//函数指针指向sd卡的读写函数
//function pointer to the sd card read & write single block
//wirte sector are not use in this player
BYTE (* FAT_ReadSector)(DWORD sector, BYTE * buffer)=MMC_SD_ReadSingleBlock;//device read
BYTE (* FAT_WriteSector)(DWORD sector, BYTE * buffer)=MMC_SD_WriteSingleBlock;//device write
extern 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];
BYTE * buffer = tmpBuf;
DWORD hidsec=0;
DWORD Capacity;
Capacity = MMC_SD_ReadCapacity();
if(Capacity<0xff)return 1;
if(FAT_ReadSector(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(FAT_ReadSector(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(FAT_ReadSector(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占用的扇区数 //the sectors number occupied by one fat talbe
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;
}
//读一个簇中的一个扇区
//read one sector of one cluster, parameter part indicate which sector
unsigned char FAT_LoadPartCluster(unsigned long cluster,unsigned part,BYTE * buffer)
{
DWORD sector;
sector=FirstDataSector+(DWORD)(cluster-2)*(DWORD)SectorsPerClust;//calculate the actual sector number
if(FAT_ReadSector(sector+part,buffer))return 1;
else return 0;
}
//读下一簇簇号
//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];
BYTE * buffer = tmpBuf;
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(FAT_ReadSector(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
}
#if FIX_DIRECTORY
//在给定目录下查找文件
//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 char cnt;
unsigned int offset;
unsigned char i;
struct direntry *item = 0;
if((cluster==0) && (FAT32_Enable == 0))// root directory
{
buffer=tmpBuf;//apply memory
if(buffer==0)return 1;//if failed
for(cnt=0;cnt<RootDirSectors;cnt++)
{
if(FAT_ReadSector(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->Offset = offset;
/*free(buffer);*/
return 0;
}
}
}
}
/*free(buffer);*///release
}
else//other folders
{
tempclust=cluster;
while(1)
{
sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number
buffer=tmpBuf;//apply memory
if(buffer==0)return 1;//if failed
for(cnt=0;cnt<SectorsPerClust;cnt++)
{
if(FAT_ReadSector(sector+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)
{
FileInfo->StartCluster = item->deStartCluster + (((unsigned long)item->deHighClust)<<16);//don't care
FileInfo->Size = item->deFileSize;
FileInfo->Attr = item->deAttributes;
FileInfo->Sector = sector+cnt;
FileInfo->Offset = of