📄 file_drv.c
字号:
#include "file_drv.h"
#include "fat32.h"
extern BYTE xdata DBUF[512];
extern BYTE data BPB_SecPerClus;
extern DWORD xdata ClusterSum;
DIRECTORY_INFO data DIR;
FILE_INFO data FILE;
DWORD pdata EntryClusterNum;
DWORD pdata EntrySectorNum;
WORD pdata EntryOffSet;
WORD xdata Mp3_File_Sum=0; //number of entries in current directory
void Init_Dir()
{
Init_Fat32();
DIR.StartCluster=0x03;
}
bit Get_Mp3_Sum(void)
{
BYTE sector;
Mp3_File_Sum=0;
EntryClusterNum=DIR.StartCluster;
while(TRUE)
{
for(sector=0;sector<BPB_SecPerClus;sector++)
{
EntrySectorNum=FirstSectorofCluster_32(EntryClusterNum)+sector;
if(!Mmc_Read_Sector(EntrySectorNum,DBUF))
return FALSE;
for(EntryOffSet=0;EntryOffSet<512;EntryOffSet+=32)
{
if(DBUF[EntryOffSet]==0x00)
return TRUE;
else if(DBUF[EntryOffSet]==0xE5) //delelled entry
continue;
else if(DBUF[EntryOffSet+15]==0xA5) //directory entry
continue;
else if(!(DBUF[EntryOffSet+8]=='M')&&(DBUF[EntryOffSet+9]=='P')&&(DBUF[EntryOffSet+10]=='3'))
continue;
Mp3_File_Sum++;
}
}
EntryClusterNum=GetNextClusterNum_32(EntryClusterNum);
if ((EntryClusterNum&0x0FFFFFFF)==0x0FFFFFFF)
{
return TRUE;
}
else if ((EntryClusterNum&0x0FFFFFFF)>(ClusterSum-1))
return FALSE;
}
return TRUE;
}
/*get position of the entry by index(for fat32)*/
bit Seek_Mp3_File(WORD index)
{
BYTE sector;
EntryClusterNum=DIR.StartCluster;
while(TRUE)
{
for(sector=0;sector<BPB_SecPerClus;sector++)
{
EntrySectorNum=FirstSectorofCluster_32(EntryClusterNum)+sector;
if(!Mmc_Read_Sector(EntrySectorNum,DBUF))
return FALSE;
for(EntryOffSet=0;EntryOffSet<512;EntryOffSet+=32)
{
if(DBUF[EntryOffSet]==0x00)
return TRUE;
else if(DBUF[EntryOffSet]==0xE5) //delelled entry
continue;
else if(DBUF[EntryOffSet+15]==0xA5) //directory entry
continue;
else if(!(DBUF[EntryOffSet+8]=='M')&&(DBUF[EntryOffSet+9]=='P')&&(DBUF[EntryOffSet+10]=='3'))
continue;
TempMp3Num++;
if(TempMp3Num==index)
return TRUE;
}
}
EntryClusterNum=GetNextClusterNum_32(EntryClusterNum);
if ((EntryClusterNum&0x0FFFFFFF)==0x0FFFFFFF)
{
return FALSE;
}
else if ((EntryClusterNum&0x0FFFFFFF)>(ClusterSum-1))
return FALSE;
}
return FALSE;
}
/* init file parameter according to file entry*/
void Open_MP3_File(void)
{
PDIR_INFO pDirInfo;
pDirInfo=(PDIR_INFO)(DBUF+EntryOffSet);
FILE.StartCluster=SwapINT16(pDirInfo->FirstClusHi);
FILE.StartCluster<<=16;
FILE.StartCluster|=SwapINT16(pDirInfo->FirstClusLow);
FILE.LengthInByte=SwapINT32(pDirInfo->Length);
FILE.ClusterPointer=FILE.StartCluster;
FILE.SectorPointer=FirstSectorofCluster_32(FILE.StartCluster);
FILE.OffsetofSector=0;
FILE.SectorofCluster=0;
FILE.Pointer=0;
FILE.EndOfFile=FALSE;
return;
}
/*max readLength is 512 byte*/
bit Read_File(WORD readLength,BYTE xdata *pBuffer)
{
WORD xdata i;
BYTE xdata TempBuf[512];
if(readLength>512) return FALSE;
if(readLength+FILE.Pointer>FILE.LengthInByte)
{
End_Of_File=TRUE;
readLength=FILE.LengthInByte-FILE.Pointer;
if(!Mmc_Read_Sector(FILE.SectorPointer,TempBuf)) return FALSE;
if(readLength+FILE.OffsetofSector<512)
{
for(i=0;i<readLength;i++)
*(pBuffer++)=TempBuf[FILE.OffsetofSector++];
}
else
{
for(i=0;i<(512-FILE.OffsetofSector);i++)
*(pBuffer++)=TempBuf[FILE.OffsetofSector+i];
readLength-=(512-FILE.OffsetofSector);
FILE.OffsetofSector=0;
if(++FILE.SectorofCluster>=BPB_SecPerClus)
{
FILE.SectorofCluster=0;
FILE.ClusterPointer=GetNextClusterNum(FILE.ClusterPointer);
FILE.SectorPointer=FirstSectorofCluster(FILE.ClusterPointer);
}
if(!Mmc_Read_Sector(FILE.SectorPointer,TempBuf)) return FALSE;
for(i=0,i<readLength,i++)
*(pBuffer++)=TempBuf[FILE.OffsetofSector+i];
}
FILE.Pointer=FILE.LengthInByte-1;
return TRUE;
}
if(FILE.OffsetofSector>0)
{
if(!Mmc_Read_Sector(FILE.SectorPointer,TempBuf)) return FALSE;
if(readLength+FILE.OffsetofSector<512)
{
for(i=0;i<readLength;i++)
*(pBuffer++)=TempBuf[FILE.OffsetofSector++];
}
else
{
for(i=0;i<(512-FILE.OffsetofSector);i++)
*(pBuffer++)=TempBuf[FILE.OffsetofSector+i];
readLength-=(512-FILE.OffsetofSector);
FILE.OffsetofSector=0;
if(++FILE.SectorofCluster>=BPB_SecPerClus)
{
FILE.SectorofCluster=0;
FILE.ClusterPointer=GetNextClusterNum(FILE.ClusterPointer);
FILE.SectorPointer=FirstSectorofCluster(FILE.ClusterPointer);
}
if(!Mmc_Read_Sector(FILE.SectorPointer,TempBuf)) return FALSE;
for(i=0,i<readLength,i++)
*(pBuffer++)=TempBuf[FILE.OffsetofSector+i];
}
}
else
{
if(!Mmc_Read_Sector(FILE.SectorPointer,pBuffer)) return FALSE;
if(++FILE.SectorofCluster>=BPB_SecPerClus)
{
FILE.SectorofCluster=0;
FILE.ClusterPointer=GetNextClusterNum(FILE.ClusterPointer);
FILE.SectorPointer=FirstSectorofCluster(FILE.ClusterPointer);
}
}
FILE.Pointer+=readLength;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -