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

📄 hpi.lst

📁 用SL811USB接口芯片读写U盘FAT16固化程序(C51源代码)
💻 LST
📖 第 1 页 / 共 5 页
字号:
 638          unsigned char WriteFile(unsigned long writeLength,unsigned char *pBuffer)
 639          {
 640   1              unsigned int  len,sector,i,cnum,tlen;
 641   1              unsigned char bSuccess,bStop;
 642   1              
 643   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 644   1                      return FALSE;           
 645   1              if(!ThisFile.bFileOpen)
 646   1                      return FALSE;
 647   1              ThisFile.bFileOpen=0;
 648   1              bSuccess=1;
 649   1              bStop=0;
 650   1              tlen=0;
 651   1      
 652   1              while(writeLength>0)
 653   1              {
 654   2                      if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
 655   2                              len=DeviceInfo.BPB_BytesPerSec;
 656   2                      else
 657   2                              len=writeLength+ThisFile.OffsetofSector;
 658   2                         
 659   2                       //////////////////////////////////////////////////////
 660   2                       if(ThisFile.OffsetofSector>0)
 661   2                              {
 662   3                              if(RBC_Read(ThisFile.SectorPointer,1,DBUF))
 663   3                                      {
 664   4                                      
 665   4                                      len=len-ThisFile.OffsetofSector;
 666   4                                      for(i=0;i<len;i++)
 667   4                                              
 668   4                                              DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
 669   4                                      if(!RBC_Write(ThisFile.SectorPointer,1,DBUF))
 670   4                                              return FALSE;                                           
 671   4                                      ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
 672   4                                      }
 673   3                              else
 674   3                                      return FALSE;                           
 675   3                              }
C51 COMPILER V7.06   HPI                                                                   09/18/2004 01:20:08 PAGE 12  

 676   2                       else
 677   2                              {
 678   3                              if(!RBC_Write(ThisFile.SectorPointer,1,pBuffer+tlen))
 679   3                                      return FALSE;                           
 680   3                              ThisFile.OffsetofSector=len;
 681   3                              }
 682   2                       /////////////////////////////////////////////////////
 683   2                         writeLength-=len;
 684   2                         tlen+=len;
 685   2                       /////////////更新文件指针 //////////////////////////////
 686   2                        if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
 687   2                         {    
 688   3                              ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
 689   3                              ThisFile.SectorofCluster+=1;
 690   3                              if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
 691   3                              {
 692   4                                      ThisFile.SectorofCluster=0;
 693   4                                       ThisFile.ClusterPointer=CreateClusterLink(ThisFile.ClusterPointer);
 694   4                                       if(ThisFile.ClusterPointer==0x00)
 695   4                                              return FALSE;                           
 696   4                                       ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer);  
 697   4                              }
 698   3                              else
 699   3                                      ThisFile.SectorPointer=ThisFile.SectorPointer+1;
 700   3                          }
 701   2                      
 702   2              
 703   2              }//end while
 704   1              ThisFile.pointer+=tlen;
 705   1              ///////////更新文件目录信息/////////////////////////////
 706   1              if(bSuccess==1)
 707   1              {
 708   2                      if(DirStartCluster==0)  
 709   2                      {
 710   3                      for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
 711   3                      {   
 712   4                      //////////////////////////////////////////////////
 713   4                      if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
 714   4                              return FALSE;                           
 715   4                      ///////////////////////////////////////////////////
 716   4                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 717   4                              {
 718   5                              cnum=LSwapINT16(DBUF[i+26],DBUF[i+27]);
 719   5                              if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
 720   5                                      {
 721   6                                      if(ThisFile.pointer>ThisFile.LengthInByte)
 722   6                                              ThisFile.LengthInByte=ThisFile.pointer;                         
 723   6                                      DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
 724   6                                      DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
 725   6                                      DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
 726   6                                      DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
 727   6                                      if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
 728   6                                              return FALSE;                   
 729   6                                       bStop=1;
 730   6                                       break;
 731   6                                      }
 732   5                              }
 733   4                      if(bStop==1)
 734   4                              break;          
 735   4                     }
 736   3                      }
 737   2      //////////////////////////////////////////////////////////////////////////////
C51 COMPILER V7.06   HPI                                                                   09/18/2004 01:20:08 PAGE 13  

 738   2                      else
 739   2                      {
 740   3                              NowCluster=DirStartCluster;             
 741   3                              do
 742   3                              {
 743   4                                      NowSector=FirstSectorofCluster(NowCluster);
 744   4                                      for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
 745   4                                      {   
 746   5                                              if(!RBC_Read(NowSector+sector,1,DBUF))
 747   5                                                      return FALSE;                           
 748   5                                              for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 749   5                                              {
 750   6                                                      cnum=LSwapINT16(DBUF[i+26],DBUF[i+27]);
 751   6                                                      if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
 752   6                                                              {
 753   7                                                              if(ThisFile.pointer>ThisFile.LengthInByte)
 754   7                                                                      ThisFile.LengthInByte=ThisFile.pointer;                         
 755   7                                                              DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
 756   7                                                              DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
 757   7                                                              DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
 758   7                                                              DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
 759   7                                                              if(!RBC_Write(NowSector+sector,1,DBUF))
 760   7                                                                      return FALSE;                   
 761   7                                                               bStop=1;
 762   7                                                               break;
 763   7                                                              }                                                       
 764   6                                      }
 765   5                                              if(bStop==1)break;
 766   5                                      }
 767   4                                      if(bStop==1)break;
 768   4                                      NowCluster=GetNextClusterNum(NowCluster);                       
 769   4                              }while(NowCluster<=0xffef);
 770   3                      
 771   3                      if(NowCluster>0xffef)
 772   3                      return FALSE;
 773   3                      }
 774   2              }
 775   1              
 776   1              ThisFile.bFileOpen=1;
 777   1              //////////////////////////////////////////////
 778   1              return TRUE;
 779   1      }
 780          
 781          unsigned char RemoveFile(unsigned char *pBuffer)
 782          {
 783   1              unsigned int sector,i;
 784   1              unsigned char bStop,j;
 785   1              int k;
 786   1                      
 787   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 788   1                      return FALSE;           
 789   1              if(DirStartCluster==0)  
 790   1              {
 791   2              ////////////// 清除目录/////////////////////////////////////
 792   2              for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
 793   2                      {   
 794   3                      //////////////////////////////////////////////////
 795   3                      if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
 796   3                              return FALSE;                   
 797   3                      ///////////////////////////////////////////////////
 798   3                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 799   3                              {
C51 COMPILER V7.06   HPI                                                                   09/18/2004 01:20:08 PAGE 14  

 800   4                              if(DBUF[i]==0x00)
 801   4                                      return FALSE;                   
 802   4                              ///////////////////////////////////////////
 803   4                              j=0;
 804   4                              while(DBUF[i+j]==*(pBuffer+j))
 805   4                                      {
 806   5                                       j=j+1;
 807   5                                       if(j>10) break;
 808   5                                       }
 809   4                              
 810   4                              if(j>10)
 811   4                                      {       
 812   5                                      DBUF[i]=0xE5;
 813   5                                      ThisFile.StartCluster=LSwapINT16(DBUF[i+26],DBUF[i+27]);
 814   5                              
 815   5                                      for(k=(i-32);k>=0;k=k-32)
 816   5                                              {
 817   6                                              if(DBUF[k+11]==0x0F)
 818   6                                                      DBUF[k]=0xE5;
 819   6                                              else
 820   6                                                      break;
 821   6                                              }
 822   5                                      DelayMs(15);
 823   5                                      if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
 824   5                                              return FALSE;
 825   5      /*
 826   5                                      k=sector-1;
 827   5                                      if(k>=0)
 828   5                                              {
 829   5                                              if(!RBC_Read(DeviceInfo.RootStartSector+k,1,DBUF))
 830   5                                                      return FALSE;   
 831   5                                              for(k=DeviceInfo.BPB_BytesPerSec;k>=0;k=k-32)
 832   5                                                      {
 833   5                                                      if(DBUF[k+11]==0x0F)
 834   5                                                              DBUF[k]=0xE5;
 835   5                                                      else
 836   5                                                              break;
 837   5                                                      }
 838   5                                              DelayMs(15);
 839   5                                              if(!RBC_Write(DeviceInfo.RootStartSector+sector-1,1,DBUF))
 840   5                                                      return FALSE;
 841   5                                              }
 842   5      */                      
 843   5                                      //////////////////// 清除FAT中的纪录////////////////////////
 844   5                                      DelayMs(10);
 845   5                                      if(!DeleteClusterLink(ThisFile.StartCluster))
 846   5                                              return FALSE;                                   
 847   5                                      bStop=1;
 848   5                                      break;
 849   5                                      }
 850   4                              
 851   4                              }//end for
 852   3                      if(bStop==1)
 853   3                              break;

⌨️ 快捷键说明

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