Fat32.c
上传用户:jcsy2001
上传日期:2013-11-29
资源大小:201k
文件大小:5k
开发平台:

C/C++

  1. #include "common.h"
  2. #include "Fat.h"
  3. #include "Fat32.h"
  4. #include "DEVICE.H"
  5. #include "HAL.H"
  6. ////////////////////////////////////////
  7. extern SYS_INFO_BLOCK xdata DeviceInfo;
  8. extern FILE_INFO xdata ThisFile;
  9. extern unsigned char xdata DBUF[BUFFER_LENGTH];
  10. extern unsigned char xdata FATBUF[512];
  11. ////////////////////////////////////////
  12. unsigned long FirstSectorofCluster32(unsigned long clusterNum)
  13. {
  14. unsigned long temp;
  15. temp=clusterNum-2;
  16. temp=temp*DeviceInfo.BPB_SecPerClus;
  17. temp=temp+DeviceInfo.FirstDataSector;
  18. return temp;
  19. }
  20. unsigned long ThisFatSecNum32(unsigned long clusterNum)
  21. {
  22.    unsigned long temp;
  23.    temp=clusterNum/(DeviceInfo.BPB_BytesPerSec/4);
  24.    temp=temp+DeviceInfo.FatStartSector;
  25.    return temp;
  26. }
  27. unsigned long ThisFatEntOffset32(unsigned long clusterNum)
  28. {
  29. return (clusterNum%(DeviceInfo.BPB_BytesPerSec/4))*4;
  30. }
  31. unsigned long GetNextClusterNum32(unsigned long clusterNum)
  32. {
  33. unsigned long FatSecNum,FatEntOffset;
  34. FatSecNum=ThisFatSecNum32(clusterNum);
  35. FatEntOffset=ThisFatEntOffset32(clusterNum);
  36. if(ThisFile.FatSectorPointer!=FatSecNum)
  37. {
  38. if(!SdReadSector(FatSecNum,1,FATBUF))
  39. return 0xFFFFFFFF;
  40. ThisFile.FatSectorPointer=FatSecNum;
  41. }
  42. ///////////////////////////////////////////////////
  43. clusterNum=LSwapINT32(FATBUF[FatEntOffset],FATBUF[FatEntOffset+1],FATBUF[FatEntOffset+2],FATBUF[FatEntOffset+3]);
  44. return clusterNum;
  45. }
  46. unsigned char GoToPointer32(unsigned long pointer)
  47. {
  48. unsigned int clusterSize;
  49. clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
  50. ThisFile.ClusterPointer=ThisFile.StartCluster;
  51. while(pointer>clusterSize)
  52. {
  53. pointer-=clusterSize;
  54. ThisFile.ClusterPointer=GetNextClusterNum32(ThisFile.ClusterPointer);
  55. if(ThisFile.ClusterPointer==0xffffffff)
  56. {
  57. return FALSE;
  58. }
  59. }
  60. ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec;
  61. ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer)+ThisFile.SectorofCluster;
  62. ThisFile.OffsetofSector=pointer%DeviceInfo.BPB_BytesPerSec;
  63. ThisFile.FatSectorPointer=0;
  64. return TRUE;
  65. }
  66. unsigned char DeleteClusterLink32(unsigned long clusterNum)
  67. {
  68. unsigned long FatSecNum,FatEntOffset;
  69. unsigned char i;
  70. while((clusterNum>1)&&(clusterNum<DeviceInfo.TotCluster))
  71. {
  72. FatSecNum=ThisFatSecNum32(clusterNum);
  73. FatEntOffset=ThisFatEntOffset32(clusterNum);
  74. if(SdReadSector(FatSecNum,1,DBUF))
  75.        {
  76.          if(clusterNum<DeviceInfo.LastFreeCluster) DeviceInfo.LastFreeCluster=clusterNum; 
  77.  clusterNum=LSwapINT32(DBUF[FatEntOffset],DBUF[FatEntOffset+1],DBUF[FatEntOffset+2],DBUF[FatEntOffset+3]);
  78.        }
  79. else
  80. return FALSE;
  81. DBUF[FatEntOffset]=0x00;DBUF[FatEntOffset+1]=0x00;DBUF[FatEntOffset+2]=0x00;DBUF[FatEntOffset+3]=0x00;
  82. for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
  83. {
  84. DelayMs(5);
  85. if(!SdWriteSector(FatSecNum+i*DeviceInfo.BPB_FATSz32,1,DBUF))
  86. return FALSE;
  87. }
  88. }
  89. return TRUE;
  90. }
  91. unsigned long GetFreeCusterNum32(void)
  92. {
  93. unsigned long xdata clusterNum,i;
  94. unsigned long xdata sectorNum;
  95. unsigned char xdata j;
  96. sectorNum=ThisFatSecNum32(DeviceInfo.LastFreeCluster);
  97.     clusterNum=(sectorNum-DeviceInfo.FatStartSector)*(DeviceInfo.BPB_BytesPerSec/4);
  98. while(sectorNum<DeviceInfo.BPB_FATSz32+DeviceInfo.FatStartSector)
  99. {
  100. if(!SdReadSector(sectorNum,1,DBUF))
  101. return 0x0;
  102. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+4)
  103.    {
  104.     if((DBUF[i]==0)&&(DBUF[i+1]==0)&&(DBUF[i+2]==0)&&(DBUF[i+3]==0))
  105.      {
  106.      DBUF[i]=0xff;DBUF[i+1]=0xff;DBUF[i+2]=0xff;DBUF[i+3]=0xff;
  107. for(j=0;j<DeviceInfo.BPB_NumFATs;j++)
  108. {
  109. DelayMs(5);
  110. if(!SdWriteSector(sectorNum+j*DeviceInfo.BPB_FATSz32,1,DBUF))
  111. return FALSE;
  112. }
  113.                 clusterNum=clusterNum+i/4;
  114.         if((clusterNum-2)<DeviceInfo.TotCluster) 
  115.   {
  116.    if(clusterNum>DeviceInfo.LastFreeCluster) DeviceInfo.LastFreeCluster=clusterNum;
  117.    return clusterNum;
  118.   }
  119.       else return FALSE; 
  120.      }
  121.    }
  122. clusterNum+=DeviceInfo.BPB_BytesPerSec/4;
  123. sectorNum++;
  124. DelayMs(1);
  125. }
  126. return 0x0;
  127. }
  128. unsigned long CreateClusterLink32(unsigned long currentCluster)
  129. {
  130. unsigned long xdata newCluster;
  131. unsigned long xdata FatSecNum,FatEntOffset;
  132. unsigned char xdata i;
  133. newCluster=GetFreeCusterNum32();
  134. FatSecNum=ThisFatSecNum32(currentCluster);
  135. FatEntOffset=ThisFatEntOffset32(currentCluster);
  136. if(SdReadSector(FatSecNum,1,DBUF))
  137. {
  138. DBUF[FatEntOffset]=newCluster;
  139. DBUF[FatEntOffset+1]=newCluster>>8;
  140. DBUF[FatEntOffset+2]=newCluster>>16;
  141. DBUF[FatEntOffset+3]=newCluster>>24;
  142. for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
  143. {
  144. DelayMs(5);
  145. if(!SdWriteSector(FatSecNum+i*DeviceInfo.BPB_FATSz32,1,DBUF))
  146. return FALSE;
  147. }
  148. }
  149. else
  150. return 0x00;
  151. return newCluster;
  152. }