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

C/C++

  1. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 1   
  2. C51 COMPILER V8.02, COMPILATION OF MODULE HPI
  3. OBJECT MODULE PLACED IN HPI.OBJ
  4. COMPILER INVOKED BY: D:KeilC51BINC51.EXE HPI.C BROWSE DEBUG OBJECTEXTEND
  5. line level    source
  6.    1          #include "common.h"
  7.    2          #include "DEVICE.H"
  8.    3          #include "HPI.H"
  9.    4          #include "HAL.H"
  10.    5          #include "Fat.h"
  11.    6          #include "Fat32.h"
  12.    7          #include "HPI32.H"
  13.    8          
  14.    9          extern FLAGS bdata bFlags;
  15.   10          extern unsigned char xdata DBUF[BUFFER_LENGTH];
  16.   11          //////////////////////////////////////////
  17.   12          unsigned char xdata UARTBUF[UARTBUF_LENGTH];
  18.   13          SYS_INFO_BLOCK xdata DeviceInfo;
  19.   14          FILE_INFO xdata ThisFile;
  20.   15          
  21.   16          Command_Def xdata Command;
  22.   17          Response_Def xdata Response;
  23.   18          
  24.   19          unsigned int xdata DirStartCluster,NowCluster;
  25.   20          unsigned long xdata NowSector;
  26.   21          ShowFileName_Def xdata ShowFileName[MaxLFNum];  //long file struct
  27.   22          /////////////////////////////////////////////
  28.   23          
  29.   24          void UartSendRsp(void)
  30.   25          {
  31.   26   1              unsigned int i;
  32.   27   1              unsigned char *pBuf=(unsigned char *)&Response;
  33.   28   1              MCU_LED4=0;
  34.   29   1              for(i=0;i<5;i++)
  35.   30   1                      ComSendByte(*(pBuf+i));
  36.   31   1      
  37.   32   1              if(Response.len!=0)
  38.   33   1                      {
  39.   34   2                      for(i=0;i<Response.len;i++)
  40.   35   2                              ComSendByte(UARTBUF[i]);
  41.   36   2                  }
  42.   37   1              MCU_LED4=1;
  43.   38   1      }
  44.   39          
  45.   40          unsigned char UartHandler(void)
  46.   41          {
  47.   42   1        Response.Result=0;Response.len=0;
  48.   43   1        switch(Command.CLass)
  49.   44   1        {
  50.   45   2          case 0x00:  //List          
  51.   46   2              Response.Result=List();
  52.   47   2              break;  
  53.   48   2              case 0x01:      //Open File
  54.   49   2                      Response.Result=OpenFile(Command.Parameter);            
  55.   50   2              break;
  56.   51   2              case 0x02:      //Read File             
  57.   52   2                      Response.Result=ReadFile(Command.len,UARTBUF);
  58.   53   2                      break;   
  59.   54   2          case 0x03:  //Set Pointer
  60.   55   2                      Response.Result=SetFilePointer(Command.len);    
  61. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 2   
  62.   56   2                      break;
  63.   57   2              case 0x04:      //Great File
  64.   58   2                      Response.Result=OpenFile(Command.Parameter);
  65.   59   2                      if(Response.Result)
  66.   60   2                              {
  67.   61   3                              Response.Result=SetFilePointer(ThisFile.LengthInByte);
  68.   62   3                              }
  69.   63   2                      else
  70.   64   2                      Response.Result=CreateFile(Command.len,Command.Parameter,UARTBUF);      
  71.   65   2              break; 
  72.   66   2              case 0x05:      //Write File
  73.   67   2              Response.Result=WriteFile(Command.len,UARTBUF);
  74.   68   2              break; 
  75.   69   2          case 0x06:  //Remove File
  76.   70   2              ThisFile.bFileOpen=0;   
  77.   71   2              Response.Result=RemoveFile(Command.Parameter);
  78.   72   2              break;  
  79.   73   2              case 0x07:      //Get Space     
  80.   74   2              Response.Result=GetCapacity();
  81.   75   2              break;     
  82.   76   2              case 0x08:  //DetectDisk
  83.   77   2                      if(bFlags.bits.SLAVE_IS_ATTACHED)
  84.   78   2                              Response.Result=1;
  85.   79   2                      break;
  86.   80   2              case 0x09:  //Great Dir
  87.   81   2                      Response.Result=DownDir(Command.Parameter);
  88.   82   2                      if(!Response.Result)
  89.   83   2                              Response.Result=CreateDir(Command.len,Command.Parameter,UARTBUF);       
  90.   84   2              break; 
  91.   85   2              case 0x0a:      //Down Dir
  92.   86   2                      Response.Result=DownDir(Command.Parameter);
  93.   87   2                      break;
  94.   88   2              case 0x0b:      //Up Dir
  95.   89   2                      Response.Result=UpDir();
  96.   90   2                      break;  
  97.   91   2              case 0x0c:      //Up RootDir
  98.   92   2                      Response.Result=UpRootDir();
  99.   93   2                      break;  
  100.   94   2        }
  101.   95   1      
  102.   96   1        UartSendRsp();
  103.   97   1        ///////////////////////////////
  104.   98   1        return TRUE;  
  105.   99   1      }
  106.  100          
  107.  101          unsigned char List(void)
  108.  102          {
  109.  103   1              unsigned int item,i;
  110.  104   1              unsigned char k,bstop,sector;
  111.  105   1              unsigned char Lcount,Ncount,base;
  112.  106   1              
  113.  107   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  114.  108   1                      return FALSE;                   
  115.  109   1              item=0;
  116.  110   1              bstop=0;
  117.  111   1              ////////////////////////////////////
  118.  112   1              Lcount=0;
  119.  113   1              for(i=0;i<MaxLFNum;i++)
  120.  114   1                      {
  121.  115   2                      ShowFileName[i].LongName[0]=0x00;
  122.  116   2                      ShowFileName[i].LongName[1]=0x00;
  123.  117   2                      }
  124. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 3   
  125.  118   1              /////////////////////////////////////
  126.  119   1      
  127.  120   1              if(DirStartCluster==0)  //Root Dir
  128.  121   1              {
  129.  122   2              for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
  130.  123   2                  {   
  131.  124   3                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  132.  125   3                              return FALSE;                   
  133.  126   3                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  134.  127   3                              {
  135.  128   4                              if(DBUF[i]==0x00)
  136.  129   4                                      {bstop=1;
  137.  130   5                                      break;}
  138.  131   4                              else if(DBUF[i]==0xE5)
  139.  132   4                                      continue;                       
  140.  133   4                              else
  141.  134   4                                      {
  142.  135   5                                      for(k=0;k<32;k++)
  143.  136   5                                              UARTBUF[item*32+k]=DBUF[i+k];
  144.  137   5                                      item=item+1;
  145.  138   5                                  if(item>=(UARTBUF_LENGTH/32))  {bstop=1;break;}  //串口缓冲 只能有512项目录,强行结束
  146.  139   5                                      /////////////////////////////////
  147.  140   5                                      if(Lcount<MaxLFNum)
  148.  141   5                                      {
  149.  142   6                                      if(DBUF[i+11]==0x0F)
  150.  143   6                                              {
  151.  144   7                                              base=((DBUF[i]&0x1F)-1)*26;
  152.  145   7                                              if(base<=224)
  153.  146   7                                              {
  154.  147   8                                              Ncount=0;
  155.  148   8                                              for(k=1;k<11;k++)
  156.  149   8                                                      {
  157.  150   9                                                      ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  158.  151   9                                                      Ncount++;
  159.  152   9                                                      }
  160.  153   8                                              for(k=14;k<26;k++)
  161.  154   8                                                      {
  162.  155   9                                                      ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  163.  156   9                                                      Ncount++;
  164.  157   9                                                      }
  165.  158   8                                              for(k=28;k<32;k++)
  166.  159   8                                                      {
  167.  160   9                                                      ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  168.  161   9                                                      Ncount++;
  169.  162   9                                                      }
  170.  163   8                                              }
  171.  164   7                                              }
  172.  165   6                                      else
  173.  166   6                                              {
  174.  167   7                                              for(k=0;k<32;k++)
  175.  168   7                                                      ShowFileName[Lcount].item[k]=DBUF[i+k];
  176.  169   7                                          Lcount++;                                   
  177.  170   7                                              }
  178.  171   6                                      }//if(Lcount<MaxLFNum)
  179.  172   5                                      /////////////////////////////////
  180.  173   5                                      }
  181.  174   4                              }
  182.  175   3                      
  183.  176   3                      if(bstop==1)break;              
  184.  177   3                  }
  185.  178   2              Response.len=item*32;
  186.  179   2              return TRUE;
  187. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 4   
  188.  180   2              }
  189.  181   1      //////////////////////////////////////////////////////////////////   
  190.  182   1              else            //Son Dir
  191.  183   1              {
  192.  184   2                      NowCluster=DirStartCluster;             
  193.  185   2                      do
  194.  186   2                      {
  195.  187   3                              NowSector=FirstSectorofCluster(NowCluster);
  196.  188   3                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  197.  189   3                      {   
  198.  190   4                                      if(!SdReadSector(NowSector+sector,1,DBUF))
  199.  191   4                                              return FALSE;                           
  200.  192   4                                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  201.  193   4                                      {
  202.  194   5                                              if(DBUF[i]==0x00)
  203.  195   5                                                      {bstop=1;break;}
  204.  196   5                                              else if(DBUF[i]==0xE5)
  205.  197   5                                                      continue;                       
  206.  198   5                                              else
  207.  199   5                                              {
  208.  200   6                                                      for(k=0;k<32;k++)
  209.  201   6                                                              UARTBUF[item*32+k]=DBUF[i+k];
  210.  202   6                                                      item=item+1;
  211.  203   6                                                      /////////////////////////////////
  212.  204   6                                                      if(DBUF[i+11]==0x0F)
  213.  205   6                                                      {
  214.  206   7                                                              base=((DBUF[i]&0x1F)-1)*26;
  215.  207   7                                                              if(base<=224)
  216.  208   7                                                              {
  217.  209   8                                                                      Ncount=0;
  218.  210   8                                                                      for(k=1;k<11;k++)
  219.  211   8                                                                      {
  220.  212   9                                                                              ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  221.  213   9                                                                              Ncount++;
  222.  214   9                                                                      }
  223.  215   8                                                                      for(k=14;k<26;k++)
  224.  216   8                                                                      {
  225.  217   9                                                                              ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  226.  218   9                                                                              Ncount++;
  227.  219   9                                                                      }
  228.  220   8                                                                      for(k=28;k<32;k++)
  229.  221   8                                                                      {
  230.  222   9                                                                              ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  231.  223   9                                                                              Ncount++;
  232.  224   9                                                                      }
  233.  225   8                                                              }
  234.  226   7                                                      }
  235.  227   6                                                      else
  236.  228   6                                                      {
  237.  229   7                                                              for(k=0;k<32;k++)
  238.  230   7                                                                      ShowFileName[Lcount].item[k]=DBUF[i+k];
  239.  231   7                                                         Lcount++;                                                    
  240.  232   7                                                      }                       
  241.  233   6                                      /////////////////////////////////
  242.  234   6                                              }
  243.  235   5                                      }
  244.  236   4                                      if(bstop==1)break;              
  245.  237   4                      }
  246.  238   3      
  247.  239   3                              if(bstop==1)break;
  248.  240   3              
  249.  241   3                              NowCluster=GetNextClusterNum(NowCluster); 
  250. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 5   
  251.  242   3                              
  252.  243   3                      }while(NowCluster<=0xffef);
  253.  244   2      
  254.  245   2              Response.len=item*32;
  255.  246   2              return TRUE;
  256.  247   2              }
  257.  248   1      }
  258.  249          
  259.  250          unsigned char OpenFile(unsigned char *pBuffer)
  260.  251          {
  261.  252   1              unsigned int i;
  262.  253   1              unsigned char j,bstop,sector;
  263.  254   1                      
  264.  255   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  265.  256   1                      return FALSE;                   
  266.  257   1              ThisFile.bFileOpen=0;
  267.  258   1      
  268.  259   1              if(DirStartCluster==0)  //Root Dir
  269.  260   1              {
  270.  261   2              for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
  271.  262   2                  {   
  272.  263   3                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  273.  264   3                              return FALSE;
  274.  265   3                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  275.  266   3                              {
  276.  267   4                              if(DBUF[i]==0x00)
  277.  268   4                                      return FALSE;
  278.  269   4                              j=0;
  279.  270   4                              while(DBUF[i+j]==*(pBuffer+j))
  280.  271   4                                      {
  281.  272   5                                       j=j+1;
  282.  273   5                                       if(j>10)
  283.  274   5                                              break;
  284.  275   5                                      }
  285.  276   4                              
  286.  277   4                              if(j>10&&(DBUF[i+11]&0x10)!=0x10)
  287.  278   4                                  {
  288.  279   5                                  for(j=0;j<32;j++)
  289.  280   5                                      UARTBUF[j]=DBUF[i+j];               
  290.  281   5                                  bstop=1;
  291.  282   5                                  break;}
  292.  283   4                              
  293.  284   4                              }
  294.  285   3                      if(bstop==1)break;
  295.  286   3                      }
  296.  287   2                  
  297.  288   2                  if(sector>=DeviceInfo.BPB_RootEntCnt)
  298.  289   2                      return FALSE;   
  299.  290   2              }
  300.  291   1      ///////////////////////////////////////////////////////////////////////////////////////
  301.  292   1              else
  302.  293   1              {
  303.  294   2                      NowCluster=DirStartCluster;             
  304.  295   2                      do
  305.  296   2                      {
  306.  297   3                              NowSector=FirstSectorofCluster(NowCluster);
  307.  298   3                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  308.  299   3                      {   
  309.  300   4                                      if(!SdReadSector(NowSector+sector,1,DBUF))
  310.  301   4                                              return FALSE;                           
  311.  302   4                                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  312.  303   4                                      {
  313. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 6   
  314.  304   5                                              if(DBUF[i]==0x00)
  315.  305   5                                                      return FALSE;
  316.  306   5                                              j=0;
  317.  307   5                                              while(DBUF[i+j]==*(pBuffer+j))
  318.  308   5                                              {
  319.  309   6                                                       j=j+1;
  320.  310   6                                                       if(j>10)
  321.  311   6                                                              break;
  322.  312   6                                              }
  323.  313   5                                              if(j>10&&(DBUF[i+11]&0x10)!=0x10)
  324.  314   5                                              { 
  325.  315   6                                                      for(j=0;j<32;j++)
  326.  316   6                                                      UARTBUF[j]=DBUF[i+j];                       
  327.  317   6                                              bstop=1;
  328.  318   6                                              break;
  329.  319   6                                                      }
  330.  320   5                                      }
  331.  321   4                                      if(bstop==1)break;              
  332.  322   4                      }
  333.  323   3                              if(bstop==1)break;      
  334.  324   3                              NowCluster=GetNextClusterNum(NowCluster);                       
  335.  325   3                      }while(NowCluster<=0xffef);
  336.  326   2                      
  337.  327   2                      if(NowCluster>0xffef)
  338.  328   2                      return FALSE;
  339.  329   2              }
  340.  330   1      
  341.  331   1              ThisFile.bFileOpen=1;
  342.  332   1              ThisFile.StartCluster=LSwapINT16(UARTBUF[26],UARTBUF[27]);
  343.  333   1              ThisFile.LengthInByte=LSwapINT32(UARTBUF[28],UARTBUF[29],UARTBUF[30],UARTBUF[31]);
  344.  334   1              ThisFile.ClusterPointer=ThisFile.StartCluster;  
  345.  335   1              ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
  346.  336   1              ThisFile.OffsetofSector=0;
  347.  337   1              ThisFile.SectorofCluster=0;     
  348.  338   1              ThisFile.FatSectorPointer=0;    
  349.  339   1              ThisFile.pointer=0;
  350.  340   1              
  351.  341   1              Response.len=32;
  352.  342   1              return TRUE;    
  353.  343   1      }
  354.  344          
  355.  345          unsigned char ReadFile(unsigned long readLength,unsigned char *pBuffer)
  356.  346          {
  357.  347   1      
  358.  348   1              unsigned int len,i;
  359.  349   1              unsigned int tlen;      
  360.  350   1              unsigned long blen;
  361.  351   1      
  362.  352   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  363.  353   1                      return FALSE;           
  364.  354   1              if(!ThisFile.bFileOpen)
  365.  355   1                      return FALSE;
  366.  356   1      
  367.  357   1              blen=readLength;
  368.  358   1      
  369.  359   1              tlen=0;
  370.  360   1              if(readLength>MAX_READ_LENGTH)
  371.  361   1                      return FALSE;   
  372.  362   1              
  373.  363   1              if(readLength+ThisFile.pointer>ThisFile.LengthInByte)
  374.  364   1                      return FALSE;   
  375.  365   1                      
  376. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 7   
  377.  366   1              ////////////////////////////////////////////            
  378.  367   1                      while(readLength>0)
  379.  368   1                      {
  380.  369   2                         if(readLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
  381.  370   2                              len=DeviceInfo.BPB_BytesPerSec;
  382.  371   2                         else
  383.  372   2                              len=readLength+ThisFile.OffsetofSector;
  384.  373   2                         
  385.  374   2                         //////////////////////////////////////////////////////
  386.  375   2                         if(ThisFile.OffsetofSector>0)
  387.  376   2                              {
  388.  377   3                              if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
  389.  378   3                                      {
  390.  379   4                                      
  391.  380   4                                      len=len-ThisFile.OffsetofSector;
  392.  381   4                                      for(i=0;i<len;i++)
  393.  382   4                                              
  394.  383   4                                              *(pBuffer+i)=DBUF[ThisFile.OffsetofSector+i];
  395.  384   4                                      ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
  396.  385   4                                      }
  397.  386   3                              else
  398.  387   3                                      return FALSE;                           
  399.  388   3                              }
  400.  389   2                         else
  401.  390   2                              {
  402.  391   3                                      if(!SdReadSector(ThisFile.SectorPointer,1,pBuffer+tlen))
  403.  392   3                                                      return FALSE;   
  404.  393   3                                      ThisFile.OffsetofSector=len;
  405.  394   3                              }
  406.  395   2                         ////////////////////////////////////////////////////////////
  407.  396   2                         readLength-=len;
  408.  397   2                         tlen+=len;
  409.  398   2                        
  410.  399   2                         /////////////////////////////////////////////////////////
  411.  400   2                         if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
  412.  401   2                         {    
  413.  402   3                              ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
  414.  403   3                              ThisFile.SectorofCluster+=1;
  415.  404   3                              if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
  416.  405   3                              {
  417.  406   4                                      ThisFile.SectorofCluster=0;
  418.  407   4                                       ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
  419.  408   4                                       if(ThisFile.ClusterPointer>0xffef)
  420.  409   4                                                 return FALSE;                                                
  421.  410   4                                       ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer);  
  422.  411   4                              }
  423.  412   3                              else
  424.  413   3                                      ThisFile.SectorPointer=ThisFile.SectorPointer+1;
  425.  414   3                          }
  426.  415   2                         //////////////////////////////////////////////////////////////////
  427.  416   2                      }//end while
  428.  417   1                      
  429.  418   1              ThisFile.bFileOpen=1;
  430.  419   1              ThisFile.pointer+=tlen;
  431.  420   1              //////////////////////////////////////////////
  432.  421   1              Response.len=blen;
  433.  422   1      
  434.  423   1              return TRUE;
  435.  424   1      }
  436.  425          
  437.  426          unsigned char SetFilePointer(unsigned long pointer)
  438.  427          {
  439. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 8   
  440.  428   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  441.  429   1                      return FALSE;           
  442.  430   1              if(!ThisFile.bFileOpen)
  443.  431   1                      return FALSE;           
  444.  432   1              ///////////////////////////////////////////////////////////
  445.  433   1              ThisFile.pointer=pointer;
  446.  434   1              if(ThisFile.pointer>ThisFile.LengthInByte)
  447.  435   1                      return FALSE;   
  448.  436   1      
  449.  437   1              if(!GoToPointer(ThisFile.pointer))
  450.  438   1              {
  451.  439   2              ThisFile.bFileOpen=0;
  452.  440   2              return FALSE;   
  453.  441   2              }
  454.  442   1              //////////////////////////////////////////////
  455.  443   1              return TRUE;
  456.  444   1      }
  457.  445          
  458.  446          unsigned char CreateFile(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
  459.  447          {
  460.  448   1              unsigned int sector,i,j,DirCount;
  461.  449   1              unsigned int cnum,ClusterPointer;
  462.  450   1              unsigned char xdata bstop,InByte,bwrite;
  463.  451   1              
  464.  452   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  465.  453   1                      return FALSE;
  466.  454   1              if((len%32)!=0)
  467.  455   1                      return FALSE;
  468.  456   1              if((len+32)>DeviceInfo.BPB_BytesPerSec)
  469.  457   1                      return FALSE;
  470.  458   1      
  471.  459   1              ThisFile.bFileOpen=0;   
  472.  460   1      
  473.  461   1              cnum=GetFreeCusterNum();
  474.  462   1              if(cnum<0x02)
  475.  463   1                      return FALSE;   
  476.  464   1      
  477.  465   1              pBuffer[26]=(unsigned char)(cnum);
  478.  466   1              pBuffer[27]=(unsigned char)(cnum>>8);
  479.  467   1              pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
  480.  468   1              bstop=0;
  481.  469   1              
  482.  470   1              if(DirStartCluster==0)  
  483.  471   1              {
  484.  472   2              /////// Search a free space in the root dir space and build the item ///
  485.  473   2              for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
  486.  474   2                  {   
  487.  475   3                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  488.  476   3                              return FALSE;
  489.  477   3                      DirCount=0;bwrite=0;
  490.  478   3                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  491.  479   3                              {
  492.  480   4                              if(len==0)
  493.  481   4                                      {
  494.  482   5                                      if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
  495.  483   5                                      {
  496.  484   6                                      for(j=0;j<32;j++)
  497.  485   6                                              DBUF[i+j]=*(pBuffer+j);
  498.  486   6                                      if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  499.  487   6                                              return FALSE;                                   
  500.  488   6                                      bstop=1;
  501.  489   6                                      break;
  502. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 9   
  503.  490   6                                      }
  504.  491   5                                      }
  505.  492   4                              else
  506.  493   4                              {
  507.  494   5                              if(DirCount==0)
  508.  495   5                                      InByte=i;
  509.  496   5                              if(DBUF[i]==0xE5)                               
  510.  497   5                                      DirCount++;                             
  511.  498   5                              else if(DBUF[i]==0x00)
  512.  499   5                                      {       
  513.  500   6                                      DirCount++;     
  514.  501   6                                      DBUF[i]=0xE5;
  515.  502   6                                      bwrite=1;                               
  516.  503   6                                      }
  517.  504   5                              else
  518.  505   5                                      DirCount=0;
  519.  506   5      
  520.  507   5                              if((DirCount*32)>=(len+32))
  521.  508   5                                      {
  522.  509   6                                      for(j=0;j<len;j++)
  523.  510   6                                              DBUF[InByte+j]=*(pName+j);
  524.  511   6      
  525.  512   6                                      for(j=0;j<32;j++)
  526.  513   6                                              DBUF[InByte+len+j]=*(pBuffer+j);
  527.  514   6      
  528.  515   6                                      if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  529.  516   6                                              return FALSE;                                   
  530.  517   6                                      bstop=1;
  531.  518   6                                      break;
  532.  519   6                                      }
  533.  520   5                               }
  534.  521   4                              }               
  535.  522   3                      if(bstop==1)break;
  536.  523   3                      
  537.  524   3                      if((len!=0)&&(bwrite==1))
  538.  525   3                              {
  539.  526   4                              if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  540.  527   4                                      return FALSE;
  541.  528   4                      }
  542.  529   3                      }
  543.  530   2      
  544.  531   2              if(sector>=DeviceInfo.BPB_RootEntCnt)
  545.  532   2                      return FALSE;
  546.  533   2              }
  547.  534   1      ////////////////////////////////////////////////////////////
  548.  535   1              else
  549.  536   1              {
  550.  537   2                      NowCluster=DirStartCluster;             
  551.  538   2                      do
  552.  539   2                      {
  553.  540   3                              NowSector=FirstSectorofCluster(NowCluster);
  554.  541   3                              ClusterPointer=NowCluster;
  555.  542   3                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  556.  543   3                      {   
  557.  544   4                                      if(!SdReadSector(NowSector+sector,1,DBUF))
  558.  545   4                                              return FALSE;
  559.  546   4                                      DirCount=0;bwrite=0;
  560.  547   4      
  561.  548   4                                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  562.  549   4                                      {
  563.  550   5                                      if(len==0)
  564.  551   5                                              {
  565. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 10  
  566.  552   6                                              if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
  567.  553   6                                              {
  568.  554   7                                              for(j=0;j<32;j++)
  569.  555   7                                                      DBUF[i+j]=*(pBuffer+j);
  570.  556   7                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
  571.  557   7                                                      return FALSE;                                   
  572.  558   7                                              bstop=1;
  573.  559   7                                              break;
  574.  560   7                                              }               
  575.  561   6                                              }
  576.  562   5                                      else
  577.  563   5                                      {
  578.  564   6                                      if(DirCount==0)
  579.  565   6                                              InByte=i;
  580.  566   6                                      if(DBUF[i]==0xE5)                               
  581.  567   6                                              DirCount++;                             
  582.  568   6                                      else if(DBUF[i]==0x00)
  583.  569   6                                              {       
  584.  570   7                                              DirCount++;     
  585.  571   7                                              DBUF[i]=0xE5;
  586.  572   7                                              bwrite=1;
  587.  573   7                                              }
  588.  574   6                                      else
  589.  575   6                                              DirCount=0;
  590.  576   6      
  591.  577   6                                      if((DirCount*32)>=(len+32))
  592.  578   6                                              {
  593.  579   7                                              for(j=0;j<len;j++)
  594.  580   7                                                      DBUF[InByte+j]=*(pName+j);
  595.  581   7                                              for(j=0;j<32;j++)
  596.  582   7                                                      DBUF[InByte+len+j]=*(pBuffer+j);
  597.  583   7                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
  598.  584   7                                                      return FALSE;                                   
  599.  585   7                                              bstop=1;
  600.  586   7                                              break;
  601.  587   7                                              }
  602.  588   6                                       }
  603.  589   5                                      }
  604.  590   4                                      if(bstop==1)break;
  605.  591   4                                      
  606.  592   4                                      if((len!=0)&&(bwrite==1))
  607.  593   4                                      {
  608.  594   5                                      if(!SdWriteSector(NowSector+sector,1,DBUF))
  609.  595   5                                              return FALSE;
  610.  596   5                              }
  611.  597   4                      }
  612.  598   3                              if(bstop==1)break;
  613.  599   3              
  614.  600   3                              NowCluster=GetNextClusterNum(NowCluster);
  615.  601   3                              if(NowCluster>0xffef)
  616.  602   3                      {
  617.  603   4                              NowCluster=CreateClusterLink(ClusterPointer);
  618.  604   4                              if(NowCluster==0x00)
  619.  605   4                                       return FALSE;
  620.  606   4                              NowSector=FirstSectorofCluster(NowCluster);
  621.  607   4                              for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
  622.  608   4                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  623.  609   4                                      {
  624.  610   5                                      if(!SdWriteSector(NowSector+sector,1,DBUF))
  625.  611   5                                              return FALSE;
  626.  612   5                                      }
  627.  613   4                              }
  628. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 11  
  629.  614   3                      }while(NowCluster<=0xffef);
  630.  615   2              
  631.  616   2              
  632.  617   2              if(NowCluster>0xffef)
  633.  618   2                  return FALSE;
  634.  619   2              }
  635.  620   1      ////////////////////////////////////////////////////////////////
  636.  621   1              
  637.  622   1              ThisFile.StartCluster=cnum;
  638.  623   1              ThisFile.LengthInByte=0;
  639.  624   1              ThisFile.ClusterPointer=ThisFile.StartCluster;
  640.  625   1              ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
  641.  626   1              ThisFile.OffsetofSector=0;
  642.  627   1              ThisFile.SectorofCluster=0;
  643.  628   1              ThisFile.bFileOpen=1;
  644.  629   1              ThisFile.pointer=0;
  645.  630   1              ThisFile.FatSectorPointer=0;
  646.  631   1              
  647.  632   1              return TRUE;
  648.  633   1      }
  649.  634          
  650.  635          unsigned char WriteFile(unsigned long writeLength,unsigned char *pBuffer)
  651.  636          {
  652.  637   1              unsigned int  len,sector,i,cnum,tlen;
  653.  638   1              unsigned char bSuccess,bStop;
  654.  639   1              
  655.  640   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  656.  641   1                      return FALSE;           
  657.  642   1              if(!ThisFile.bFileOpen)
  658.  643   1                      return FALSE;
  659.  644   1              ThisFile.bFileOpen=0;
  660.  645   1              bSuccess=1;
  661.  646   1              bStop=0;
  662.  647   1              tlen=0;
  663.  648   1      
  664.  649   1              while(writeLength>0)
  665.  650   1              {
  666.  651   2                      if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
  667.  652   2                              len=DeviceInfo.BPB_BytesPerSec;
  668.  653   2                      else
  669.  654   2                              len=writeLength+ThisFile.OffsetofSector;
  670.  655   2                         
  671.  656   2                       //////////////////////////////////////////////////////
  672.  657   2                       if(ThisFile.OffsetofSector>0)
  673.  658   2                              {
  674.  659   3                              if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
  675.  660   3                                      {
  676.  661   4                                      
  677.  662   4                                      len=len-ThisFile.OffsetofSector;
  678.  663   4                                      for(i=0;i<len;i++)
  679.  664   4                                              
  680.  665   4                                              DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
  681.  666   4                                      if(!SdWriteSector(ThisFile.SectorPointer,1,DBUF))
  682.  667   4                                              return FALSE;                                           
  683.  668   4                                      ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
  684.  669   4                                      }
  685.  670   3                              else
  686.  671   3                                      return FALSE;                           
  687.  672   3                              }
  688.  673   2                       else
  689.  674   2                              {
  690.  675   3                              if(!SdWriteSector(ThisFile.SectorPointer,1,pBuffer+tlen))
  691. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 12  
  692.  676   3                                      return FALSE;                           
  693.  677   3                              ThisFile.OffsetofSector=len;
  694.  678   3                              }
  695.  679   2                       /////////////////////////////////////////////////////
  696.  680   2                         writeLength-=len;
  697.  681   2                         tlen+=len;
  698.  682   2                       /////////////更新文件指针 //////////////////////////////
  699.  683   2                        if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
  700.  684   2                         {    
  701.  685   3                              ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
  702.  686   3                              ThisFile.SectorofCluster+=1;
  703.  687   3                              if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
  704.  688   3                              {
  705.  689   4                                      ThisFile.SectorofCluster=0;
  706.  690   4                                       ThisFile.ClusterPointer=CreateClusterLink(ThisFile.ClusterPointer);
  707.  691   4                                       if(ThisFile.ClusterPointer==0x00)
  708.  692   4                                              return FALSE;                           
  709.  693   4                                       ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer);  
  710.  694   4                              }
  711.  695   3                              else
  712.  696   3                                      ThisFile.SectorPointer=ThisFile.SectorPointer+1;
  713.  697   3                          }
  714.  698   2                      
  715.  699   2              
  716.  700   2              }//end while
  717.  701   1              ThisFile.pointer+=tlen;
  718.  702   1          DelayMs(200);
  719.  703   1              ///////////更新文件目录信息/////////////////////////////
  720.  704   1              if(bSuccess==1)
  721.  705   1              {
  722.  706   2                      if(DirStartCluster==0)  
  723.  707   2                      {
  724.  708   3                      for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
  725.  709   3                      {   
  726.  710   4                      //////////////////////////////////////////////////
  727.  711   4                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  728.  712   4                              return FALSE;                           
  729.  713   4                      ///////////////////////////////////////////////////
  730.  714   4                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  731.  715   4                              {
  732.  716   5                              cnum=LSwapINT16(DBUF[i+26],DBUF[i+27]);
  733.  717   5                              if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
  734.  718   5                                      {
  735.  719   6                                      if(ThisFile.pointer>ThisFile.LengthInByte)
  736.  720   6                                              ThisFile.LengthInByte=ThisFile.pointer;                         
  737.  721   6                                      DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
  738.  722   6                                      DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
  739.  723   6                                      DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
  740.  724   6                                      DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
  741.  725   6                                      if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  742.  726   6                                              return FALSE;                   
  743.  727   6                                       bStop=1;
  744.  728   6                                       break;
  745.  729   6                                      }
  746.  730   5                              }
  747.  731   4                      if(bStop==1)
  748.  732   4                              break;          
  749.  733   4                     }
  750.  734   3                      }
  751.  735   2      //////////////////////////////////////////////////////////////////////////////
  752.  736   2                      else
  753.  737   2                      {
  754. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 13  
  755.  738   3                              NowCluster=DirStartCluster;             
  756.  739   3                              do
  757.  740   3                              {
  758.  741   4                                      NowSector=FirstSectorofCluster(NowCluster);
  759.  742   4                                      for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  760.  743   4                                      {   
  761.  744   5                                              if(!SdReadSector(NowSector+sector,1,DBUF))
  762.  745   5                                                      return FALSE;                           
  763.  746   5                                              for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  764.  747   5                                              {
  765.  748   6                                                      cnum=LSwapINT16(DBUF[i+26],DBUF[i+27]);
  766.  749   6                                                      if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
  767.  750   6                                                              {
  768.  751   7                                                              if(ThisFile.pointer>ThisFile.LengthInByte)
  769.  752   7                                                                      ThisFile.LengthInByte=ThisFile.pointer;                         
  770.  753   7                                                              DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
  771.  754   7                                                              DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
  772.  755   7                                                              DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
  773.  756   7                                                              DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
  774.  757   7                                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
  775.  758   7                                                                      return FALSE;                   
  776.  759   7                                                               bStop=1;
  777.  760   7                                                               break;
  778.  761   7                                                              }                                                       
  779.  762   6                                      }
  780.  763   5                                              if(bStop==1)break;
  781.  764   5                                      }
  782.  765   4                                      if(bStop==1)break;
  783.  766   4                                      NowCluster=GetNextClusterNum(NowCluster);                       
  784.  767   4                              }while(NowCluster<=0xffef);
  785.  768   3                      
  786.  769   3                      if(NowCluster>0xffef)
  787.  770   3                      return FALSE;
  788.  771   3                      }
  789.  772   2              }
  790.  773   1              
  791.  774   1              ThisFile.bFileOpen=1;
  792.  775   1              //////////////////////////////////////////////
  793.  776   1              return TRUE;
  794.  777   1      }
  795.  778          
  796.  779          unsigned char RemoveFile(unsigned char *pBuffer)
  797.  780          {
  798.  781   1              unsigned int sector,i;
  799.  782   1              unsigned char bStop,j;
  800.  783   1              int k;
  801.  784   1                      
  802.  785   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  803.  786   1                      return FALSE;           
  804.  787   1              if(DirStartCluster==0)  
  805.  788   1              {
  806.  789   2              ////////////// 清除目录/////////////////////////////////////
  807.  790   2              for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
  808.  791   2                      {   
  809.  792   3                      //////////////////////////////////////////////////
  810.  793   3                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  811.  794   3                              return FALSE;                   
  812.  795   3                      ///////////////////////////////////////////////////
  813.  796   3                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  814.  797   3                              {
  815.  798   4                              if(DBUF[i]==0x00)
  816.  799   4                                      return FALSE;                   
  817. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 14  
  818.  800   4                              ///////////////////////////////////////////
  819.  801   4                              j=0;
  820.  802   4                              while(DBUF[i+j]==*(pBuffer+j))
  821.  803   4                                      {
  822.  804   5                                       j=j+1;
  823.  805   5                                       if(j>10) break;
  824.  806   5                                       }
  825.  807   4                              
  826.  808   4                              if(j>10)
  827.  809   4                                      {       
  828.  810   5                                      DBUF[i]=0xE5;
  829.  811   5                                      ThisFile.StartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
  830.  812   5                              
  831.  813   5                                      for(k=(i-32);k>=0;k=k-32)
  832.  814   5                                              {
  833.  815   6                                              if(DBUF[k+11]==0x0F)
  834.  816   6                                                      DBUF[k]=0xE5;
  835.  817   6                                              else
  836.  818   6                                                      break;
  837.  819   6                                              }
  838.  820   5                                      DelayMs(15);
  839.  821   5                                      if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  840.  822   5                                              return FALSE;
  841.  823   5      /*
  842.  824   5                                      k=sector-1;
  843.  825   5                                      if(k>=0)
  844.  826   5                                              {
  845.  827   5                                              if(!SdReadSector(DeviceInfo.RootStartSector+k,1,DBUF))
  846.  828   5                                                      return FALSE;   
  847.  829   5                                              for(k=DeviceInfo.BPB_BytesPerSec;k>=0;k=k-32)
  848.  830   5                                                      {
  849.  831   5                                                      if(DBUF[k+11]==0x0F)
  850.  832   5                                                              DBUF[k]=0xE5;
  851.  833   5                                                      else
  852.  834   5                                                              break;
  853.  835   5                                                      }
  854.  836   5                                      //      DelayMs(15);
  855.  837   5                                              if(!SdWriteSector(DeviceInfo.RootStartSector+sector-1,1,DBUF))
  856.  838   5                                                      return FALSE;
  857.  839   5                                              }
  858.  840   5      */                      
  859.  841   5                                      //////////////////// 清除FAT中的纪录////////////////////////
  860.  842   5                                      DelayMs(10);
  861.  843   5                                      if(!DeleteClusterLink(ThisFile.StartCluster))
  862.  844   5                                              return FALSE;                                   
  863.  845   5                                      bStop=1;
  864.  846   5                                      break;
  865.  847   5                                      }
  866.  848   4                              
  867.  849   4                              }//end for
  868.  850   3                      if(bStop==1)
  869.  851   3                              break;
  870.  852   3                      
  871.  853   3                     }//end search
  872.  854   2              if(sector>=DeviceInfo.BPB_RootEntCnt)
  873.  855   2                      return FALSE;   
  874.  856   2              }
  875.  857   1              else
  876.  858   1              {
  877.  859   2              NowCluster=DirStartCluster;             
  878.  860   2                      do
  879.  861   2                      {
  880. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 15  
  881.  862   3                              NowSector=FirstSectorofCluster(NowCluster);
  882.  863   3                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  883.  864   3                      {   
  884.  865   4                                      if(!SdReadSector(NowSector+sector,1,DBUF))
  885.  866   4                                              return FALSE;                           
  886.  867   4                                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  887.  868   4                                      {
  888.  869   5                                              if(DBUF[i]==0x00)
  889.  870   5                                                      return FALSE;
  890.  871   5                                              j=0;
  891.  872   5                                              while(DBUF[i+j]==*(pBuffer+j))
  892.  873   5                                              {
  893.  874   6                                                       j=j+1;
  894.  875   6                                                       if(j>10)
  895.  876   6                                                              break;
  896.  877   6                                              }
  897.  878   5                                              if(j>10)
  898.  879   5                                              { 
  899.  880   6                                                      DBUF[i]=0xE5;
  900.  881   6                                                      ThisFile.StartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
  901.  882   6                                                      for(k=(i-32);k>=0;k=k-32)
  902.  883   6                                                      {
  903.  884   7                                                      if(DBUF[k+11]==0x0F)
  904.  885   7                                                              DBUF[k]=0xE5;
  905.  886   7                                                      else
  906.  887   7                                                              break;
  907.  888   7                                                      }
  908.  889   6                                                      DelayMs(15);
  909.  890   6                                                      if(!SdWriteSector(NowSector+sector,1,DBUF))
  910.  891   6                                                              return FALSE;                                   
  911.  892   6                                      //////////////////// 清除FAT中的纪录////////////////////////
  912.  893   6                                                      DelayMs(10);
  913.  894   6                                                      if(!DeleteClusterLink(ThisFile.StartCluster))
  914.  895   6                                                              return FALSE;                                   
  915.  896   6                                                      bStop=1;
  916.  897   6                                                      break;
  917.  898   6                                                      }
  918.  899   5                                      }
  919.  900   4                                      if(bStop==1)break;              
  920.  901   4                      }
  921.  902   3                              if(bStop==1)break;      
  922.  903   3                              NowCluster=GetNextClusterNum(NowCluster);                       
  923.  904   3                      }while(NowCluster<=0xffef);
  924.  905   2                      
  925.  906   2                      if(NowCluster>0xffef)
  926.  907   2                      return FALSE;
  927.  908   2              }
  928.  909   1              return TRUE;
  929.  910   1      }
  930.  911          
  931.  912          unsigned char GetCapacity(void)
  932.  913          {
  933.  914   1              unsigned int sectorNum,i;       
  934.  915   1              unsigned long FreeSize,Freesectorcnt;
  935.  916   1      
  936.  917   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  937.  918   1                      return FALSE;
  938.  919   1                      
  939.  920   1              ////////////////////////////////////////////////////////////////////////
  940.  921   1              sectorNum=DeviceInfo.FatStartSector;
  941.  922   1              Freesectorcnt=0;
  942.  923   1              while(sectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
  943. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 16  
  944.  924   1              {
  945.  925   2                      
  946.  926   2                      if(SdReadSector(sectorNum,1,DBUF))
  947.  927   2                      {
  948.  928   3                        for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+2)
  949.  929   3                              {                        
  950.  930   4                               if((DBUF[i]==0x00)&&(DBUF[i+1]==0x00))
  951.  931   4                                      {       
  952.  932   5                                      Freesectorcnt++;
  953.  933   5                                      }         
  954.  934   4                              }       
  955.  935   3                      }
  956.  936   2                      else                    
  957.  937   2                              return FALSE;                   
  958.  938   2                      sectorNum++;
  959.  939   2              }
  960.  940   1              
  961.  941   1              ////////////////////////////////////////////////////////////////////////
  962.  942   1              FreeSize=DeviceInfo.BPB_BytesPerSec*DeviceInfo.BPB_SecPerClus;
  963.  943   1              FreeSize=Freesectorcnt*FreeSize;
  964.  944   1              
  965.  945   1              UARTBUF[0]=(unsigned char)((FreeSize>>24)&0xff);
  966.  946   1              UARTBUF[1]=(unsigned char)((FreeSize>>16)&0xff);
  967.  947   1              UARTBUF[2]=(unsigned char)((FreeSize>>8)&0xff);
  968.  948   1              UARTBUF[3]=(unsigned char)(FreeSize&0xff);
  969.  949   1              Response.len=4;
  970.  950   1              return TRUE;
  971.  951   1      }
  972.  952          
  973.  953          unsigned char CreateDir(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
  974.  954          {
  975.  955   1              unsigned int sector,i,j,DirCount;
  976.  956   1              unsigned int cnum,ClusterPointer;
  977.  957   1              unsigned char xdata bstop,InByte,bwrite;
  978.  958   1              
  979.  959   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  980.  960   1                      return FALSE;
  981.  961   1              if((len%32)!=0)
  982.  962   1                      return FALSE;
  983.  963   1              if((len+32)>DeviceInfo.BPB_BytesPerSec)
  984.  964   1                      return FALSE;
  985.  965   1      
  986.  966   1              ThisFile.bFileOpen=0;
  987.  967   1              ThisFile.FatSectorPointer=0;
  988.  968   1      
  989.  969   1              cnum=GetFreeCusterNum();
  990.  970   1              if(cnum<0x02)
  991.  971   1                      return FALSE;   
  992.  972   1      
  993.  973   1              pBuffer[11]=0x10;
  994.  974   1              pBuffer[26]=(unsigned char)(cnum);
  995.  975   1              pBuffer[27]=(unsigned char)(cnum>>8);
  996.  976   1              pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
  997.  977   1              bstop=0;
  998.  978   1      
  999.  979   1              if(DirStartCluster==0)  
  1000.  980   1              {
  1001.  981   2              /////// Search a free space in the root dir space and build the item ///
  1002.  982   2              for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
  1003.  983   2                  {   
  1004.  984   3                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  1005.  985   3                              return FALSE;   
  1006. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 17  
  1007.  986   3                      DirCount=0;bwrite=0;
  1008.  987   3                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  1009.  988   3                              {
  1010.  989   4                              if(len==0)
  1011.  990   4                                      {
  1012.  991   5                                      if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
  1013.  992   5                                      {
  1014.  993   6                                      for(j=0;j<32;j++)
  1015.  994   6                                              DBUF[i+j]=*(pBuffer+j);
  1016.  995   6                                      if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  1017.  996   6                                              return FALSE;                                   
  1018.  997   6                                      bstop=1;
  1019.  998   6                                      break;
  1020.  999   6                                      }
  1021. 1000   5                                      }
  1022. 1001   4                              else
  1023. 1002   4                              {
  1024. 1003   5                              if(DirCount==0)
  1025. 1004   5                                      InByte=i;
  1026. 1005   5                              if(DBUF[i]==0xE5)                               
  1027. 1006   5                                      DirCount++;                             
  1028. 1007   5                              else if(DBUF[i]==0x00)
  1029. 1008   5                                      {       
  1030. 1009   6                                      DirCount++;     
  1031. 1010   6                                      DBUF[i]=0xE5;   
  1032. 1011   6                                      bwrite=1;                       
  1033. 1012   6                                      }
  1034. 1013   5                              else
  1035. 1014   5                                      DirCount=0;
  1036. 1015   5      
  1037. 1016   5                              if((DirCount*32)>=(len+32))
  1038. 1017   5                                      {
  1039. 1018   6                                      for(j=0;j<len;j++)
  1040. 1019   6                                              DBUF[InByte+j]=*(pName+j);
  1041. 1020   6      
  1042. 1021   6                                      for(j=0;j<32;j++)
  1043. 1022   6                                              DBUF[InByte+len+j]=*(pBuffer+j);
  1044. 1023   6      
  1045. 1024   6                                      if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  1046. 1025   6                                              return FALSE;                                   
  1047. 1026   6                                      bstop=1;
  1048. 1027   6                                      break;
  1049. 1028   6                                      }
  1050. 1029   5                               }
  1051. 1030   4                              }               
  1052. 1031   3                      if(bstop==1)break;      
  1053. 1032   3      
  1054. 1033   3                      if((len!=0)&&(bwrite==1))
  1055. 1034   3                              {
  1056. 1035   4                              if(!SdWriteSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  1057. 1036   4                                      return FALSE;
  1058. 1037   4                      }
  1059. 1038   3                  }
  1060. 1039   2      
  1061. 1040   2              if(sector>=DeviceInfo.BPB_RootEntCnt)
  1062. 1041   2                      return FALSE;
  1063. 1042   2              }
  1064. 1043   1      ////////////////////////////////////////////////////////////
  1065. 1044   1              else
  1066. 1045   1              {
  1067. 1046   2                      NowCluster=DirStartCluster;             
  1068. 1047   2                      do
  1069. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 18  
  1070. 1048   2                      {
  1071. 1049   3                              NowSector=FirstSectorofCluster(NowCluster);
  1072. 1050   3                              ClusterPointer=NowCluster;
  1073. 1051   3                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  1074. 1052   3                      {   
  1075. 1053   4                                      if(!SdReadSector(NowSector+sector,1,DBUF))
  1076. 1054   4                                              return FALSE;
  1077. 1055   4                                      DirCount=0;bwrite=0;
  1078. 1056   4                                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  1079. 1057   4                                      {
  1080. 1058   5                                      if(len==0)
  1081. 1059   5                                              {
  1082. 1060   6                                              if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
  1083. 1061   6                                              {
  1084. 1062   7                                              for(j=0;j<32;j++)
  1085. 1063   7                                                      DBUF[i+j]=*(pBuffer+j);
  1086. 1064   7                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
  1087. 1065   7                                                      return FALSE;                                   
  1088. 1066   7                                              bstop=1;
  1089. 1067   7                                              break;
  1090. 1068   7                                              }               
  1091. 1069   6                                              }
  1092. 1070   5                                      else
  1093. 1071   5                                      {
  1094. 1072   6                                      if(DirCount==0)
  1095. 1073   6                                              InByte=i;
  1096. 1074   6                                      if(DBUF[i]==0xE5)                               
  1097. 1075   6                                              DirCount++;                             
  1098. 1076   6                                      else if(DBUF[i]==0x00)
  1099. 1077   6                                              {       
  1100. 1078   7                                              DirCount++;     
  1101. 1079   7                                              DBUF[i]=0xE5;
  1102. 1080   7                                              bwrite=1;
  1103. 1081   7                                              }
  1104. 1082   6                                      else
  1105. 1083   6                                              DirCount=0;
  1106. 1084   6      
  1107. 1085   6                                      if((DirCount*32)>=(len+32))
  1108. 1086   6                                              {
  1109. 1087   7                                              for(j=0;j<len;j++)
  1110. 1088   7                                                      DBUF[InByte+j]=*(pName+j);
  1111. 1089   7                                              for(j=0;j<32;j++)
  1112. 1090   7                                                      DBUF[InByte+len+j]=*(pBuffer+j);
  1113. 1091   7                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
  1114. 1092   7                                                      return FALSE;                                   
  1115. 1093   7                                              bstop=1;
  1116. 1094   7                                              break;
  1117. 1095   7                                              }
  1118. 1096   6                                       }                      
  1119. 1097   5                                      }
  1120. 1098   4                                      if(bstop==1)break;
  1121. 1099   4                                      if((len!=0)&&(bwrite==1))
  1122. 1100   4                                      {
  1123. 1101   5                                      if(!SdWriteSector(NowSector+sector,1,DBUF))
  1124. 1102   5                                              return FALSE;
  1125. 1103   5                              }
  1126. 1104   4                      }
  1127. 1105   3                              if(bstop==1)break;
  1128. 1106   3              
  1129. 1107   3                              NowCluster=GetNextClusterNum(NowCluster);
  1130. 1108   3                              if(NowCluster>0xffef)
  1131. 1109   3                      {
  1132. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 19  
  1133. 1110   4                              NowCluster=CreateClusterLink(ClusterPointer);
  1134. 1111   4                              if(NowCluster==0x00)
  1135. 1112   4                                       return FALSE;
  1136. 1113   4                              NowSector=FirstSectorofCluster(NowCluster);
  1137. 1114   4                              for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
  1138. 1115   4                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  1139. 1116   4                                      {
  1140. 1117   5                                      if(!SdWriteSector(NowSector+sector,1,DBUF))
  1141. 1118   5                                              return FALSE;
  1142. 1119   5                                      }
  1143. 1120   4                              }
  1144. 1121   3                      }while(NowCluster<=0xffef);
  1145. 1122   2              
  1146. 1123   2              if(NowCluster>0xffef)
  1147. 1124   2                  return FALSE;
  1148. 1125   2              }
  1149. 1126   1      ////////////////////////////////////////////////////////////////
  1150. 1127   1              for(i=64;i<DeviceInfo.BPB_BytesPerSec;i++)      DBUF[i]=0x00;
  1151. 1128   1              
  1152. 1129   1              for(i=0;i<43;i++) DBUF[i]=0x20;
  1153. 1130   1              
  1154. 1131   1              DBUF[0]=0x2e;
  1155. 1132   1              for(i=11;i<32;i++) DBUF[i]=pBuffer[i];
  1156. 1133   1      
  1157. 1134   1              DBUF[32]=0x2e;DBUF[33]=0x2e;
  1158. 1135   1              for(i=43;i<64;i++) DBUF[i]=pBuffer[i-32];
  1159. 1136   1              DBUF[58]=(unsigned char)(DirStartCluster);
  1160. 1137   1              DBUF[59]=(unsigned char)(DirStartCluster>>8);
  1161. 1138   1      
  1162. 1139   1              NowSector=FirstSectorofCluster(cnum);
  1163. 1140   1              if(!SdWriteSector(NowSector,1,DBUF))
  1164. 1141   1                      return FALSE;   
  1165. 1142   1      
  1166. 1143   1              DirStartCluster=cnum;
  1167. 1144   1      //      ThisFile.ClusterPointer=DirStartCluster;
  1168. 1145   1              ThisFile.ClusterPointer=0;              
  1169. 1146   1              return TRUE;    
  1170. 1147   1      }
  1171. 1148          
  1172. 1149          unsigned char DownDir(unsigned char *pBuffer)
  1173. 1150          {
  1174. 1151   1              unsigned int i;
  1175. 1152   1              unsigned char j,bstop,sector;   
  1176. 1153   1                      
  1177. 1154   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  1178. 1155   1                      return FALSE;   
  1179. 1156   1              
  1180. 1157   1              ThisFile.bFileOpen=0;
  1181. 1158   1              bstop=0;
  1182. 1159   1      
  1183. 1160   1              if(DirStartCluster==0)  //Root Dir
  1184. 1161   1              {
  1185. 1162   2              for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
  1186. 1163   2                  {   
  1187. 1164   3                      if(!SdReadSector(DeviceInfo.RootStartSector+sector,1,DBUF))
  1188. 1165   3                              return FALSE;
  1189. 1166   3                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  1190. 1167   3                              {
  1191. 1168   4                              if(DBUF[i]==0x00)
  1192. 1169   4                                      return FALSE;           
  1193. 1170   4                              j=0;
  1194. 1171   4                              while(DBUF[i+j]==*(pBuffer+j))
  1195. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 20  
  1196. 1172   4                                      {
  1197. 1173   5                                       j=j+1;
  1198. 1174   5                                       if(j>10)
  1199. 1175   5                                              break;
  1200. 1176   5                                      }
  1201. 1177   4                              
  1202. 1178   4                              if(j>10&&(DBUF[i+11]&0x10))
  1203. 1179   4                                  {                      
  1204. 1180   5                                  bstop=1;
  1205. 1181   5                                   break;}
  1206. 1182   4                              
  1207. 1183   4                              }
  1208. 1184   3                      if(bstop==1)break;              
  1209. 1185   3                  }
  1210. 1186   2                  
  1211. 1187   2                  if(sector>=DeviceInfo.BPB_RootEntCnt)
  1212. 1188   2                      return FALSE;           
  1213. 1189   2                      
  1214. 1190   2              DirStartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
  1215. 1191   2      //      ThisFile.ClusterPointer=DirStartCluster;
  1216. 1192   2              ThisFile.ClusterPointer=0;
  1217. 1193   2              return TRUE;
  1218. 1194   2              }
  1219. 1195   1              ////////////////////////////////////////////
  1220. 1196   1              else
  1221. 1197   1              {
  1222. 1198   2                      NowCluster=DirStartCluster;             
  1223. 1199   2                      do
  1224. 1200   2                      {
  1225. 1201   3                              NowSector=FirstSectorofCluster(NowCluster);
  1226. 1202   3                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  1227. 1203   3                      {   
  1228. 1204   4                                      if(!SdReadSector(NowSector+sector,1,DBUF))
  1229. 1205   4                                              return FALSE;                           
  1230. 1206   4                                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  1231. 1207   4                                      {
  1232. 1208   5                                              if(DBUF[i]==0x00)
  1233. 1209   5                                                      return FALSE;
  1234. 1210   5                                              j=0;
  1235. 1211   5                                              while(DBUF[i+j]==*(pBuffer+j))
  1236. 1212   5                                              {
  1237. 1213   6                                                       j=j+1;
  1238. 1214   6                                                       if(j>10)
  1239. 1215   6                                                              break;
  1240. 1216   6                                              }
  1241. 1217   5                                              if(j>10&&(DBUF[i+11]&0x10))
  1242. 1218   5                                              {bstop=1;break;}
  1243. 1219   5                                      }
  1244. 1220   4                                      if(bstop==1)break;              
  1245. 1221   4                      }
  1246. 1222   3                              if(bstop==1)break;      
  1247. 1223   3                              NowCluster=GetNextClusterNum(NowCluster);                       
  1248. 1224   3                      }while(NowCluster<=0xffef);
  1249. 1225   2                      
  1250. 1226   2              if(NowCluster>0xffef)
  1251. 1227   2                      return FALSE;
  1252. 1228   2                      
  1253. 1229   2              DirStartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
  1254. 1230   2      //      ThisFile.ClusterPointer=DirStartCluster;
  1255. 1231   2              ThisFile.ClusterPointer=0;              
  1256. 1232   2              return TRUE;            
  1257. 1233   2              }
  1258. C51 COMPILER V8.02   HPI                                                                   04/29/2007 12:48:53 PAGE 21  
  1259. 1234   1      }
  1260. 1235          
  1261. 1236          unsigned char UpDir()
  1262. 1237          {
  1263. 1238   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  1264. 1239   1                      return FALSE;   
  1265. 1240   1              if(DirStartCluster==0)  //Root Dir
  1266. 1241   1                      return TRUE;    
  1267. 1242   1              
  1268. 1243   1              ThisFile.bFileOpen=0;
  1269. 1244   1      
  1270. 1245   1              NowSector=FirstSectorofCluster(DirStartCluster);
  1271. 1246   1              if(!SdReadSector(NowSector,1,DBUF))
  1272. 1247   1                              return FALSE;
  1273. 1248   1              if(DBUF[32]!=0x2e&&DBUF[33]!=0x2e)      //..
  1274. 1249   1                              return FALSE;
  1275. 1250   1              
  1276. 1251   1              DirStartCluster=LSwapINT16(DBUF[58],DBUF[59]);
  1277. 1252   1      //      ThisFile.ClusterPointer=DirStartCluster;
  1278. 1253   1              ThisFile.ClusterPointer=0;              
  1279. 1254   1              return TRUE;
  1280. 1255   1      }
  1281. 1256          
  1282. 1257          unsigned char UpRootDir()
  1283. 1258          {
  1284. 1259   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
  1285. 1260   1                      return FALSE;
  1286. 1261   1      
  1287. 1262   1              ThisFile.bFileOpen=0;
  1288. 1263   1              DirStartCluster=0;      //Root Dir
  1289. 1264   1                              
  1290. 1265   1              return TRUE;
  1291. 1266   1      }
  1292. MODULE INFORMATION:   STATIC OVERLAYABLE
  1293.    CODE SIZE        =  11226    ----
  1294.    CONSTANT SIZE    =   ----    ----
  1295.    XDATA SIZE       =  22283       6
  1296.    PDATA SIZE       =   ----    ----
  1297.    DATA SIZE        =   ----     134
  1298.    IDATA SIZE       =   ----    ----
  1299.    BIT SIZE         =   ----    ----
  1300. END OF MODULE INFORMATION.
  1301. C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)