⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat32.h

📁 fat32文件系统源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
 {
  return iCurSector;    //如果是根目录,直接返回当前扇区号
 }
 for(iDirSector=iCurSector;iDirSector<(Init_Arg.FirstDirSector)+(Init_Arg.SectorsPerClust);iDirSector++)
 {
  FAT32_ReadSector(iDirSector,FAT32_Buffer);
  for(iDir=0;iDir<Init_Arg.BytesPerSector;iDir+=sizeof(struct direntry))
  {
   pDir=((struct direntry *)(FAT32_Buffer+iDir));
   if((pDir->deName)[0]!=0x00 /*无效目录项*/ && (pDir->deName)[0]!=0xe5 /*无效目录项*/ && (pDir->deName)[0]!=0x0f /*无效属性*/)
   {
    Printf_File_Name(pDir->deName);
   }
  }
 }
}

unsigned char FAT32_CompareName(unsigned char *sname,unsigned char *dname)
{
 unsigned char i,j=8;
 unsigned char name_temp[12];
 for(i=0;i<11;i++) name_temp[i]=0x20;
 name_temp[11]=0;
 i=0;
 while(sname[i]!='.')
 {
  name_temp[i]=sname[i];
  i++;
 }
 i++;
 while(sname[i]!=0)
 {
  name_temp[j++]=sname[i];
  i++;
 }
 //Printf(name_temp,0);
 for(i=0;i<11;i++)
 {
  if(name_temp[i]!=dname[i]) return 0;
 }
  //Printf(name_temp,0);
 return 1;
}

unsigned long FAT32_GetNextCluster(unsigned long LastCluster)
{
 unsigned long temp;
 struct FAT32_FAT *pFAT;
 struct FAT32_FAT_Item *pFAT_Item;
 temp=((LastCluster/128)+Init_Arg.FirstFATSector);
 FAT32_ReadSector(temp,FAT32_Buffer);
 pFAT=(struct FAT32_FAT *)FAT32_Buffer;
 pFAT_Item=&((pFAT->Items)[LastCluster%128]);
 return lb2bb(pFAT_Item,4);
}

struct FileInfoStruct * FAT32_OpenFile(char *filepath)
{
 unsigned char depth=0;
 unsigned char i,index;
 unsigned long iFileSec,iCurFileSec,iFile;
 struct direntry *pFile;
 unsigned char len=strlen(filepath);
 for(i=0;i<len;i++)
 {
  if(filepath[i]=='\\')
  { 
   depth++;
   index=i+1;
  }
 }
 iCurFileSec=FAT32_EnterDir(filepath)/*Init_Arg.FirstDirSector*/;
 Printf("iCurFileSec",iCurFileSec);
 for(iFileSec=iCurFileSec;iFileSec<iCurFileSec+(Init_Arg.SectorsPerClust);iFileSec++)
 {
  FAT32_ReadSector(iFileSec,FAT32_Buffer);
  for(iFile=0;iFile<Init_Arg.BytesPerSector;iFile+=sizeof(struct direntry))
  {
   pFile=((struct direntry *)(FAT32_Buffer+iFile));
   if(FAT32_CompareName(filepath+index,pFile->deName))
   {
    //PutHex(pFile->deFileSize[0]);
	//PutHex(pFile->deFileSize[1]);
	//PutHex(pFile->deFileSize[2]);
	//PutHex(pFile->deFileSize[3]);
    FileInfo.FileSize=lb2bb(pFile->deFileSize,4);
	strcpy(FileInfo.FileName,filepath+index);
	FileInfo.FileStartCluster=lb2bb(pFile->deLowCluster,2)+lb2bb(pFile->deHighClust,2)*65536;
	FileInfo.FileCurCluster=FileInfo.FileStartCluster;
	FileInfo.FileNextCluster=FAT32_GetNextCluster(FileInfo.FileCurCluster);
	FileInfo.FileOffset=0;
	Printf("FileStartCluster",FileInfo.FileStartCluster);
	return &FileInfo;
   }
  }
 }
}

void spi_write(unsigned char x)
{

 unsigned char i=0;
 for(i=0;i<8;i++)
 {
  CLK=0;
  DATA=(x<<i)&0x80;
  CLK=1;
 }
}

wr_commad(unsigned char addr,unsigned char hdat,unsigned char ldat )
{  
  DREQ=1;
  while(!DREQ);
  XCS=0;
  spi_write(0x02);
  spi_write(addr);
  spi_write(hdat);
  spi_write(ldat);    
  XCS=1;
}

void Mp3Reset(void)
{
  XRESET=0;
  delay(100);
  XDCS=XCS=XRESET=1;
  wr_commad(0x00,0x08,0x04);
  delay(10);
  DREQ=1;
  while(!DREQ);
  wr_commad(0x03,0xd0,0x00);
  delay(10);
  wr_commad(0x05,0xbb,0x81);
  delay(10);
  wr_commad(0x02,0x00,0x55);
  delay(10);
  wr_commad(0x0b,VOL_VALUE,VOL_VALUE);   // 音量 
  delay(10); 
  spi_write(0);
  spi_write(0);
  spi_write(0);
  spi_write(0);
}

void Sintest(unsigned char x)
{ 
 wr_commad(0x00,0x08,0x20);
 DREQ=1;
 while(!DREQ);
 XDCS=0;XCS=1;
 spi_write(0x53);
 spi_write(0xef); 
 spi_write(0x6e);
 spi_write(x);
 spi_write(0);
 spi_write(0);
 spi_write(0);
 spi_write(0);
 delay(5000);
 spi_write(0x45);
 spi_write(0x78); 
 spi_write(0x69);
 spi_write(0x74);
 spi_write(0);
 spi_write(0);
 spi_write(0);
 spi_write(0);
 delay(5000);
 XDCS=1;
}

void FAT32_ReadFile(struct FileInfoStruct *pstru)
{
 unsigned long Sub=pstru->FileSize-pstru->FileOffset;
 unsigned long iSectorInCluster=0;
 unsigned long i=0;
 unsigned long temp=0;
 Mp3Reset();
 Sintest(200);
 delay(50000);

 XDCS=0; 				 
 Printf("enter the cluster"  ,0);            
 while(pstru->FileNextCluster!=0x0fffffff)  //如果FAT中的簇项为0x0fffffff,说明无后继簇
 {
  for(iSectorInCluster=0;iSectorInCluster<Init_Arg.SectorsPerClust;iSectorInCluster++)   //读出整簇数据
  {
   FAT32_ReadSector((((pstru->FileCurCluster)-2)*(Init_Arg.SectorsPerClust))+Init_Arg.FirstDataSector+(iSectorInCluster),FAT32_Buffer);
   for(i=0;i<Init_Arg.BytesPerSector;i++)
   {
    //send(FAT32_Buffer[i]);   //将数据发送到终端上显示
	DREQ=1;
	while(!DREQ);
	spi_write(FAT32_Buffer[i]);
   }
  } 
  pstru->FileOffset+=(Init_Arg.BytesPerSector*Init_Arg.SectorsPerClust);
  Sub=pstru->FileSize-pstru->FileOffset;
  Printf("Sub"  ,Sub);  
  pstru->FileCurCluster=pstru->FileNextCluster;
  pstru->FileNextCluster=FAT32_GetNextCluster(pstru->FileCurCluster);   //这里是FAT簇链的传递
 }
 iSectorInCluster=0; 
 Printf("enter the sectors"  ,0);
 while(Sub>=Init_Arg.BytesPerSector)   //处理不足一簇,而足扇区的数据
 {
  FAT32_ReadSector((((pstru->FileCurCluster)-2)*(Init_Arg.SectorsPerClust))+Init_Arg.FirstDataSector+(iSectorInCluster++),FAT32_Buffer);
  pstru->FileOffset+=Init_Arg.BytesPerSector;
  Sub=pstru->FileSize-pstru->FileOffset;
  for(i=0;i<Init_Arg.BytesPerSector;i++)
  {
   //send(FAT32_Buffer[i]);
	DREQ=1;
	while(!DREQ);
	spi_write(FAT32_Buffer[i]);
  }
 
 }
 Printf("enter the bytes"  ,0);
 FAT32_ReadSector((((pstru->FileCurCluster)-2)*(Init_Arg.SectorsPerClust))+Init_Arg.FirstDataSector+(iSectorInCluster),FAT32_Buffer); //读取最后一个扇区
 for(i=0;i<Sub;i++)    //Sub为最后剩余的字节数
 {
  //send(FAT32_Buffer[i]);
	DREQ=1;
	while(!DREQ);
	spi_write(FAT32_Buffer[i]);
 }
 for(temp=0;temp<2048;temp++)
 {
  DREQ=1;
  while(!DREQ);
  spi_write(0);
 }
 XDCS=1;	
}












⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -