Fat.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. unsigned char xdata FATBUF[512];
  11. ////////////////////////////////////////
  12. unsigned long FirstSectorofCluster(unsigned int 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 int ThisFatSecNum(unsigned int clusterNum)
  21. {
  22.    unsigned int temp;
  23.    temp=clusterNum/(DeviceInfo.BPB_BytesPerSec/2);
  24.    temp=temp+DeviceInfo.FatStartSector;
  25.    return temp;
  26. }
  27. unsigned int ThisFatEntOffset(unsigned int clusterNum)
  28. {    
  29. return (clusterNum%(DeviceInfo.BPB_BytesPerSec/2))*2;
  30. }
  31. unsigned int GetNextClusterNum(unsigned int clusterNum)
  32. {
  33. unsigned int FatSecNum,FatEntOffset;
  34. FatSecNum=ThisFatSecNum(clusterNum);
  35. FatEntOffset=ThisFatEntOffset(clusterNum);
  36. if(ThisFile.FatSectorPointer!=FatSecNum)
  37. {
  38. if(!SdReadSector(FatSecNum,1,FATBUF))
  39. return 0xFFFF;
  40. ThisFile.FatSectorPointer=FatSecNum;
  41. }
  42. ///////////////////////////////////////////////////
  43. clusterNum=FATBUF[FatEntOffset+1];
  44. clusterNum=clusterNum<<8;
  45. clusterNum+=FATBUF[FatEntOffset];
  46. return clusterNum;
  47. }
  48. unsigned char GoToPointer(unsigned long pointer)
  49. {
  50. unsigned int clusterSize;
  51. clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
  52. ThisFile.ClusterPointer=ThisFile.StartCluster;
  53. while(pointer>clusterSize)
  54. {
  55. pointer-=clusterSize;
  56. ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
  57. if(ThisFile.ClusterPointer==0xffff)
  58. {
  59. return FALSE;
  60. }
  61. }
  62. ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec;
  63. ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster;
  64. ThisFile.OffsetofSector=pointer%DeviceInfo.BPB_BytesPerSec;
  65. ThisFile.FatSectorPointer=0;
  66. return TRUE;
  67. }
  68. unsigned char DeleteClusterLink(unsigned int clusterNum)
  69. {
  70. unsigned int FatSecNum,FatEntOffset;
  71. unsigned char i;
  72. while((clusterNum>1)&&(clusterNum<0xfff0))
  73. {
  74. FatSecNum=ThisFatSecNum(clusterNum);
  75. FatEntOffset=ThisFatEntOffset(clusterNum);
  76. if(SdReadSector(FatSecNum,1,DBUF))
  77. {
  78.  if(clusterNum<DeviceInfo.LastFreeCluster) DeviceInfo.LastFreeCluster=clusterNum; 
  79.  clusterNum=DBUF[FatEntOffset+1];
  80.  clusterNum=clusterNum<<8;
  81.  clusterNum+=DBUF[FatEntOffset];  
  82. }
  83. else
  84. return FALSE;
  85. DBUF[FatEntOffset]=0x00;
  86. DBUF[FatEntOffset+1]=0x00;
  87. for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
  88. {
  89. DelayMs(5);
  90. if(!SdWriteSector(FatSecNum+i*DeviceInfo.BPB_FATSz16,1,DBUF))
  91. return FALSE;
  92. }
  93. }
  94. return TRUE;
  95. }
  96. unsigned int GetFreeCusterNum(void)
  97. {
  98. unsigned int clusterNum,i;
  99. unsigned long sectorNum;
  100. unsigned char j;
  101. clusterNum=0;
  102. // sectorNum=DeviceInfo.FatStartSector;
  103.     sectorNum=ThisFatSecNum(DeviceInfo.LastFreeCluster);
  104.     clusterNum=(sectorNum-DeviceInfo.FatStartSector)*(DeviceInfo.BPB_BytesPerSec/2);
  105. while(sectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
  106. {
  107. if(!SdReadSector(sectorNum,1,DBUF))
  108. return 0x0;
  109. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+2)
  110.    {
  111.     if((DBUF[i]==0)&&(DBUF[i+1]==0))
  112.      {
  113.      DBUF[i]=0xff;
  114.      DBUF[i+1]=0xff;
  115. for(j=0;j<DeviceInfo.BPB_NumFATs;j++)
  116. {
  117. DelayMs(5);
  118. if(!SdWriteSector(sectorNum+j*DeviceInfo.BPB_FATSz16,1,DBUF))
  119. return FALSE;
  120. }
  121.                 clusterNum=clusterNum+i/2;
  122.         if((clusterNum-2)<DeviceInfo.TotCluster) 
  123.   {
  124.    if(clusterNum>DeviceInfo.LastFreeCluster) DeviceInfo.LastFreeCluster=clusterNum;
  125.    return clusterNum;
  126.   }
  127.       else return FALSE; 
  128.      }
  129.     
  130.    }
  131. clusterNum+=DeviceInfo.BPB_BytesPerSec/2;
  132. sectorNum++;
  133. DelayMs(1);
  134. }
  135. return 0x0;
  136. }
  137. unsigned int CreateClusterLink(unsigned int currentCluster)
  138. {
  139. unsigned int newCluster;
  140. unsigned int FatSecNum,FatEntOffset;
  141. unsigned char i;
  142. newCluster=GetFreeCusterNum();
  143. if(newCluster==0)
  144. return 0x00;
  145. FatSecNum=ThisFatSecNum(currentCluster);
  146. FatEntOffset=ThisFatEntOffset(currentCluster);
  147. if(SdReadSector(FatSecNum,1,DBUF))
  148. {
  149. DBUF[FatEntOffset]=newCluster;
  150. DBUF[FatEntOffset+1]=newCluster>>8;
  151. for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
  152. {
  153. DelayMs(5);
  154. if(!SdWriteSector(FatSecNum+i*DeviceInfo.BPB_FATSz16,1,DBUF))
  155. return FALSE;
  156. }
  157. }
  158. else
  159. return 0x00;
  160. return newCluster;
  161. }