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

📄 fat.lst

📁 檔案系統FAT讀寫USB pen driver sample code
💻 LST
📖 第 1 页 / 共 5 页
字号:
C51 COMPILER V7.06   FAT                                                                   11/22/2004 22:08:31 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE FAT
OBJECT MODULE PLACED IN FAT.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE FAT.C DEBUG OBJECTEXTEND CODE

stmt level    source

   1          #include "common.h"
   2          
   3          ////////////////////////////////////////
   4          extern XXGFLAGS bdata bXXGFlags;
   5          extern SYS_INFO_BLOCK xdata DeviceInfo;
   6          extern FILE_INFO xdata ThisFile;
   7          extern unsigned char xdata DBUF[BUFFER_LENGTH];
   8          unsigned char xdata FATBUF[512];
   9          unsigned char xdata CurFatSector[512];
  10          FREE_FAT_INFO xdata FreeFat;
  11          ////////////////////////////////////////
  12          unsigned long FirstSectorofCluster(unsigned int clusterNum)
  13          {
  14   1              unsigned long temp;
  15   1              temp=clusterNum-2;
  16   1              temp=temp*DeviceInfo.BPB_SecPerClus;
  17   1              temp=temp+DeviceInfo.FirstDataSector;
  18   1              return temp;
  19   1      }
  20          
  21          unsigned int ThisFatSecNum(unsigned int clusterNum)
  22          {
  23   1         unsigned int temp;
  24   1         temp=clusterNum*2;
  25   1         temp=temp/DeviceInfo.BPB_BytesPerSec;
  26   1         temp=temp+DeviceInfo.FatStartSector;
  27   1         return temp;
  28   1      }
  29          
  30          unsigned int ThisFatEntOffset(unsigned int clusterNum)
  31          {
  32   1              unsigned int temp1,temp2;
  33   1              temp1=2*clusterNum;
  34   1              temp2=temp1/DeviceInfo.BPB_BytesPerSec;
  35   1              temp1=temp1-temp2*DeviceInfo.BPB_BytesPerSec;
  36   1              return temp1;
  37   1      }
  38          
  39          unsigned int GetNextClusterNum(unsigned int clusterNum)
  40          {
  41   1              unsigned int xxgFatSecNum,xxgFatEntOffset;
  42   1              
  43   1              xxgFatSecNum=ThisFatSecNum(clusterNum);
  44   1              xxgFatEntOffset=ThisFatEntOffset(clusterNum);
  45   1              //ThisFile.FatSectorPointer=xxgFatSecNum;
  46   1              if(ThisFile.FatSectorPointer!=xxgFatSecNum)
  47   1              {       
  48   2                      
  49   2                      if(!RBC_Read(xxgFatSecNum,1,FATBUF))
  50   2                              return 0xFFFF;
  51   2                      ThisFile.FatSectorPointer=xxgFatSecNum;
  52   2              }
  53   1              
  54   1              ///////////////////////////////////////////////////
  55   1              clusterNum=FATBUF[xxgFatEntOffset+1];
C51 COMPILER V7.06   FAT                                                                   11/22/2004 22:08:31 PAGE 2   

  56   1              clusterNum=clusterNum<<8;
  57   1              clusterNum+=FATBUF[xxgFatEntOffset];    
  58   1              return clusterNum;
  59   1      }
  60          
  61          unsigned char DeleteClusterLink(unsigned int clusterNum)
  62          {
  63   1              //unsigned int nextClusterNum;
  64   1              unsigned int xxgFatSecNum,xxgFatEntOffset;
  65   1              //nextClusterNum=GetNextClusterNum(clusterNum);
  66   1              ////////////////////////////////////////////
  67   1              //nextClusterNum=clusterNum;
  68   1              while((clusterNum>1)&&(clusterNum<0xfff0))
  69   1              {
  70   2              xxgFatSecNum=ThisFatSecNum(clusterNum);
  71   2              xxgFatEntOffset=ThisFatEntOffset(clusterNum);
  72   2              if(RBC_Read(xxgFatSecNum,1,DBUF))
  73   2                      {
  74   3                      clusterNum=DBUF[xxgFatEntOffset+1];
  75   3                      clusterNum=clusterNum<<8;
  76   3                      clusterNum+=DBUF[xxgFatEntOffset];      
  77   3                      //return clusterNum;
  78   3                      }
  79   2              else
  80   2                      return FALSE;
  81   2              DBUF[xxgFatEntOffset]=0x00;
  82   2              DBUF[xxgFatEntOffset+1]=0x00;   
  83   2              //DelayMs(5);
  84   2              if(!RBC_Write(xxgFatSecNum,1,DBUF))
  85   2                      return FALSE;
  86   2              //DelayMs(5);
  87   2              if(!RBC_Write(xxgFatSecNum+DeviceInfo.BPB_FATSz16,1,DBUF))
  88   2                      return FALSE;
  89   2              ////////////////////////////////////////////
  90   2              }
  91   1              return TRUE;
  92   1      }
  93          
  94          /*
  95          unsigned int GetClusterNumFromSectorNum(unsigned long sectorNum)
  96          {
  97                  unsigned long temp;
  98                  temp=sectorNum-DeviceInfo.FirstDataSector;
  99                  temp=temp/DeviceInfo.BPB_SecPerClus;
 100                  temp=temp+2;
 101                  return (unsigned int)temp;
 102          }
 103          */
 104          
 105          /*
 106          unsigned long GetSecNumFromPointer(void)
 107          {
 108                  unsigned int clusterNum,clusterSize;
 109                  unsigned long temp,pointer;
 110                  pointer=ThisFile.FilePointer;
 111                  clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
 112                  clusterNum=ThisFile.StartCluster;
 113                  while(pointer>clusterSize)
 114                  {
 115                          pointer-=clusterSize;   
 116                          clusterNum=GetNextClusterNum(clusterNum);
 117                  }
C51 COMPILER V7.06   FAT                                                                   11/22/2004 22:08:31 PAGE 3   

 118                  temp=FirstSectorofCluster(clusterNum)+pointer/DeviceInfo.BPB_BytesPerSec;
 119                  return temp;
 120          }
 121          */
 122          unsigned char GoToPointer(unsigned long pointer)
 123          {
 124   1              //unsigned char temp;
 125   1              unsigned int clusterSize;
 126   1              //unsigned long temp;
 127   1              //pointer=ThisFile.FilePointer;
 128   1              clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
 129   1              ThisFile.ClusterPointer=ThisFile.StartCluster;
 130   1              while(pointer>clusterSize)
 131   1              {
 132   2                      pointer-=clusterSize;   
 133   2                      ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
 134   2                      if(ThisFile.ClusterPointer==0xffff)
 135   2                      {
 136   3                      return FALSE;
 137   3                      }
 138   2              }
 139   1              ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec;
 140   1              ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster;
 141   1              ThisFile.OffsetofSector=pointer-ThisFile.SectorofCluster*DeviceInfo.BPB_BytesPerSec;
 142   1              ThisFile.FatSectorPointer=0;
 143   1              return TRUE;
 144   1              
 145   1      }
 146          
 147          unsigned int GetFreeCusterNum(void)
 148          {
 149   1              unsigned int clusterNum,i;
 150   1              unsigned long sectorNum;
 151   1              clusterNum=0;
 152   1              sectorNum=DeviceInfo.FatStartSector;
 153   1              while(sectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
 154   1              {
 155   2                      
 156   2                      if(!RBC_Read(sectorNum,1,DBUF))
 157   2                              return 0x0;
 158   2                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+2)
 159   2                              {
 160   3                               //clusterNum++;        
 161   3                               
 162   3                               if((DBUF[i]==0)&&(DBUF[i+1]==0))
 163   3                                      {       
 164   4                                      DBUF[i]=0xff;
 165   4                                      DBUF[i+1]=0xff;
 166   4                                      //DelayMs(10);
 167   4                                      if(!RBC_Write(sectorNum,1,DBUF))
 168   4                                              return 0x00;
 169   4                                      //DelayMs(10);
 170   4                                      if(!RBC_Write(sectorNum+DeviceInfo.BPB_FATSz16,1,DBUF))
 171   4                                              return 0x00;
 172   4                                      
 173   4                                      return  clusterNum; 
 174   4                                      }
 175   3                               clusterNum++;
 176   3                              }       
 177   2                                      
 178   2                      sectorNum=2*clusterNum/DeviceInfo.BPB_BytesPerSec+DeviceInfo.FatStartSector;    
 179   2                      //clusterNum+=DeviceInfo.BPB_BytesPerSec;
C51 COMPILER V7.06   FAT                                                                   11/22/2004 22:08:31 PAGE 4   

 180   2                      //DelayMs(10);
 181   2              }
 182   1              
 183   1              return 0x0;
 184   1      }
 185          
 186          unsigned int CreateClusterLink(unsigned int currentCluster)
 187          {
 188   1              unsigned char bFound;
 189   1              unsigned int clusterNum;
 190   1              unsigned int xxgFatSecNum,xxgFatEntOffset;
 191   1              unsigned long temp;
 192   1              bFound=0;
 193   1              //////////////////////////////////////////////
 194   1              //第一次读FAT
 195   1              if((FreeFat.SectorNum==DeviceInfo.FatStartSector)&&(FreeFat.OffsetofSector<3))
 196   1                      {       
 197   2                      if(!RBC_Read(FreeFat.SectorNum,1,CurFatSector))
 198   2                              return 0x0;     
 199   2                      }
 200   1              //////////////////////////////
 201   1              temp=FreeFat.SectorNum-DeviceInfo.FatStartSector;
 202   1              temp=temp*DeviceInfo.BPB_BytesPerSec;
 203   1              temp=temp/2;
 204   1              clusterNum=temp+FreeFat.OffsetofSector/2;
 205   1                      
 206   1              while(FreeFat.SectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
 207   1              {
 208   2                      
 209   2                      while(FreeFat.OffsetofSector<DeviceInfo.BPB_BytesPerSec)
 210   2                              {
 211   3                              
 212   3                               ///////////////////////////////////////////////
 213   3                               if((CurFatSector[FreeFat.OffsetofSector]==0)&&(CurFatSector[FreeFat.OffsetofSector+1]==0))
 214   3                                      {       
 215   4                                      CurFatSector[FreeFat.OffsetofSector]=0xff;
 216   4                                      CurFatSector[FreeFat.OffsetofSector+1]=0xff;
 217   4                                      
 218   4                                      FreeFat.OffsetofSector=FreeFat.OffsetofSector+2;
 219   4                                      //return        clusterNum; 
 220   4                                      bXXGFlags.bits.bFatChanged=1;
 221   4                                      bFound=1;
 222   4                                      break;
 223   4                                      }
 224   3                               FreeFat.OffsetofSector=FreeFat.OffsetofSector+2;
 225   3                               clusterNum++;
 226   3                              }       
 227   2                      if(bFound==1)
 228   2                              break;  
 229   2                      //FreeFat.SectorNum=2*clusterNum/DeviceInfo.BPB_BytesPerSec+DeviceInfo.FatStartSector;  
 230   2                      //FreeFat.OldSectorNum=FreeFat.SectorNum;
 231   2                      //FreeFat.SectorNum++;
 232   2                      //FreeFat.OffsetofSector=0;
 233   2                      UpdateFat(FreeFat.SectorNum);
 234   2                      
 235   2                      FreeFat.SectorNum++;
 236   2                      FreeFat.OffsetofSector=0;
 237   2                      
 238   2                      if(!RBC_Read(FreeFat.SectorNum,1,CurFatSector))
 239   2                              return 0x0;     
 240   2                      
 241   2                      
C51 COMPILER V7.06   FAT                                                                   11/22/2004 22:08:31 PAGE 5   

 242   2                      //clusterNum+=DeviceInfo.BPB_BytesPerSec;
 243   2                      //DelayMs(10);
 244   2              }
 245   1              //////////////////////////////////////////////
 246   1              if(bFound==0)
 247   1                      return 0x00;
 248   1              
 249   1              /////////////////////////////////////////////////////////////////////   
 250   1              xxgFatSecNum=ThisFatSecNum(currentCluster);
 251   1              xxgFatEntOffset=ThisFatEntOffset(currentCluster);
 252   1              
 253   1              if(xxgFatSecNum!=FreeFat.SectorNum)
 254   1                {
 255   2                      RBC_Read(xxgFatSecNum,1,DBUF);
 256   2              
 257   2              //FreeFat.OffsetofSector=FreeFat.OffsetofSector+2;
 258   2              
 259   2                      DBUF[xxgFatEntOffset]=clusterNum;
 260   2                      DBUF[xxgFatEntOffset+1]=clusterNum>>8;
 261   2                      //DelayMs(5);
 262   2                      if(!RBC_Write(xxgFatSecNum,1,DBUF))
 263   2                              return 0x00;
 264   2                      //DelayMs(10);
 265   2                      if(!RBC_Write(xxgFatSecNum+DeviceInfo.BPB_FATSz16,1,DBUF))
 266   2                              return 0x00;
 267   2                 }
 268   1               else
 269   1                 {
 270   2                      CurFatSector[xxgFatEntOffset]=clusterNum;
 271   2                      CurFatSector[xxgFatEntOffset+1]=clusterNum>>8;
 272   2                      

⌨️ 快捷键说明

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