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

📄 fat.lst

📁 采用811芯片实现单片机读写U盘的源代码
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V7.00  FAT                                                                    09/22/2005 17:15:32 PAGE 8   

 428   4                              }
 429   3                      }       
 430   2              }
 431   1      
 432   1              if(full){
 433   2                      if(mid_sec==deviceinfo.FATFirstSec+deviceinfo.FATSz-1)
 434   2                              return NOT_SEEK;
 435   2                      else{
 436   3                              fat_info.free_fat_sec=mid_sec+1;
 437   3                              if(!RBC_Read(fat_info.free_fat_sec,1,free_fat_item.date)) return DEVICEC_OP_ERR;
 438   3                      }       
 439   2              }else{
 440   2                      fat_info.free_fat_sec=mid_sec;
 441   2              }       
 442   1              
 443   1              end_offset=(deviceinfo.type)?128:256;
 444   1              for(i=0;i<end_offset;i++){
 445   2                      if(deviceinfo.type){
 446   3                              if(!free_fat_item.fat32[i]) break;
 447   3                      }else{
 448   3                              if(!free_fat_item.fat16[i]) break;
 449   3                      }
 450   2              }
 451   1              if(i==end_offset) return NOT_SEEK;
 452   1              fat_info.next_free_clus=GetClusterNum(i,fat_info.free_fat_sec);
 453   1              return 0;
 454   1      }
 455          
 456          /*****************************************************************
 457          遍历搜索查找空簇
 458          说明:该方法用于二分法查找失败的情况
 459          *****************************************************************/
 460          uchar search_one_free(void)
 461          {
 462   1              ulong start_sec,end_sec;
 463   1              uint i,end_offset;
 464   1              uchar find_flag=0;
 465   1              
 466   1              start_sec=deviceinfo.FATFirstSec;
 467   1              end_sec=deviceinfo.FATFirstSec+deviceinfo.FATSz;
 468   1              end_offset=(deviceinfo.type)?128:256;
 469   1              
 470   1              while(start_sec!=end_sec){
 471   2                      if(!RBC_Read(start_sec, 1,free_fat_item.date)) return DEVICEC_OP_ERR;
 472   2                      for(i=0;i<end_offset;i++){
 473   3                              if(deviceinfo.type){
 474   4                                      if(!free_fat_item.fat32[i]){
 475   5                                              find_flag=1;
 476   5                                              break;
 477   5                                      }       
 478   4                              }else{
 479   4                                      if(!free_fat_item.fat16[i]){
 480   5                                              find_flag=1;
 481   5                                              break;
 482   5                                      }       
 483   4                              }
 484   3                      }
 485   2                      start_sec++;
 486   2              }
 487   1      
 488   1              if(find_flag){
 489   2                      fat_info.free_fat_sec=start_sec;
C51 COMPILER V7.00  FAT                                                                    09/22/2005 17:15:32 PAGE 9   

 490   2                      fat_info.next_free_clus=GetClusterNum(i,fat_info.free_fat_sec);
 491   2                      return 0;
 492   2              }
 493   1              
 494   1              return DEVICE_FULL;
 495   1      
 496   1      }
 497          
 498          /******************************************************************************
 499          初始化分区信息
 500          ******************************************************************************/
 501          uchar init_part(void)
 502          {
 503   1              PMBR_BLOCK pmbr;
 504   1              
 505   1              // 初始化全局变量
 506   1              memset_cur(&partition_table[0], 0, MBR_INFO_MAX*MBR_INFO_LEN);
 507   1      
 508   1              if(!RBC_Read(0,1, DBUF)) return FALSE;
 509   1              if((DBUF[0]!=0xeb)&&(DBUF[0]!=0xe9)){
 510   2                      //是MBR区
 511   2                      pmbr=(PMBR_BLOCK)DBUF;
 512   2                      //目前只支持一个区
 513   2                      partition_table[0].id=1;
 514   2                      partition_table[0].type=3;//类型还不确定
 515   2                      partition_table[0].total_sector=pmbr->pt[0].TotalSectors;
 516   2                      partition_table[0].start_sector=pmbr->pt[0].RelativeSectors;
 517   2              }else{
 518   2                      //是BPB区
 519   2                      partition_table[0].id=1;
 520   2                      partition_table[0].type=3;//类型还不确定
 521   2                      if(DBUF[19]||DBUF[20])
 522   2                              partition_table[0].total_sector=(((uint)DBUF[20])<<8)|(DBUF[19]);
 523   2                      else
 524   2                              partition_table[0].total_sector=SwapINT32(*((ulong *)(&DBUF[32])));
 525   2                      partition_table[0].start_sector=0;
 526   2              }
 527   1              
 528   1              #ifdef ZLH_DEBUG
 529   1              disp_data(2, "A2");
 530   1              disp_data(10,&partition_table[0]);
 531   1              while(!getkey());
 532   1              DelayMs(100);
 533   1              #endif
 534   1              
 535   1              return TRUE;
 536   1      }
 537          
 538          
 539          /******************************************************************************
 540          初始化BPB
 541          入口参数: part  分区号(目前只支持第1分区)
 542          ******************************************************************************/
 543          uchar init_file_system(uchar part)
 544          {
 545   1              PBPB_BLOCK_16  pbpb16;
 546   1              PBPB_BLOCK_32  pbpb32;
 547   1              ulong total_cur;
 548   1              
 549   1              //初始化全局变量
 550   1              memset_cur(&deviceinfo, 0,SYS_INFO_BLOCK_LEN);
 551   1              memset_cur(&fat_info,0, FAT_INFO_LEN);
C51 COMPILER V7.00  FAT                                                                    09/22/2005 17:15:32 PAGE 10  

 552   1              memset_cur(&file_point,0,FILE_INFO_LEN);
 553   1              memset_cur(&cur_dir_info,0,DIR_INFO_LEN);
 554   1              //由分区号转为下标
 555   1              part--;
 556   1              //读BPB区
 557   1              if(!RBC_Read(partition_table[part].start_sector,1,DBUF)) return FALSE;
 558   1      
 559   1              pbpb16=(PBPB_BLOCK_16)DBUF;
 560   1              pbpb32=(PBPB_BLOCK_32)DBUF;
 561   1              //不接受扇区数不为512的文件系统
 562   1              if(SwapINT16(pbpb16->BPB_BytesPerSec)!=512) return FALSE;
 563   1              deviceinfo.BytePerSec=512;
 564   1              deviceinfo.part_id=part;
 565   1              deviceinfo.SecPerClus=pbpb16->BPB_SecPerClus;
 566   1              deviceinfo.RootEntCnt=SwapINT16(pbpb16->BPB_RootEntCnt);
 567   1              deviceinfo.NumFATs=pbpb16->BPB_NumFATs;
 568   1              deviceinfo.RsvdSecCnt=SwapINT16(pbpb16->BPB_RsvdSecCn);
 569   1              deviceinfo.TotalSec=partition_table[part].total_sector;
 570   1              deviceinfo.StartSec=partition_table[part].start_sector;
 571   1              if(pbpb16->BPB_FATSz16){
 572   2                      deviceinfo.FATSz=SwapINT16(pbpb16->BPB_FATSz16);
 573   2              }else{
 574   2                      deviceinfo.FATSz=SwapINT32(pbpb32->BPB_FATSz32);
 575   2              }       
 576   1              total_cur=deviceinfo.TotalSec-(deviceinfo.RsvdSecCnt
 577   1                      +deviceinfo.NumFATs*deviceinfo.FATSz+deviceinfo.RootEntCnt);
 578   1              if(total_cur<65525){
 579   2                      //是FAT16文件系统
 580   2                      deviceinfo.type=0;
 581   2                      partition_table[part].type=0;
 582   2                      deviceinfo.FSInfoSec=0;
 583   2              }else{
 584   2                      //是FAT32文件系统
 585   2                      deviceinfo.type=1;
 586   2                      partition_table[part].type=1;
 587   2                      deviceinfo.FSInfoSec=(ulong)(SwapINT16(pbpb32->BPB_FSInof))+deviceinfo.StartSec;
 588   2              }       
 589   1              deviceinfo.FATFirstSec=deviceinfo.StartSec+deviceinfo.RsvdSecCnt;
 590   1              if(deviceinfo.type){
 591   2                      deviceinfo.DataFirstSec=deviceinfo.FATFirstSec+deviceinfo.FATSz*deviceinfo.NumFATs;
 592   2                      deviceinfo.RootFirstSec=deviceinfo.DataFirstSec;
 593   2              }else{
 594   2                      deviceinfo.RootFirstSec=deviceinfo.FATFirstSec+deviceinfo.FATSz*deviceinfo.NumFATs;
 595   2                      deviceinfo.DataFirstSec=deviceinfo.RootFirstSec+deviceinfo.RootEntCnt>>4;  //deviceinfo.RootEntCnt*32/51
             -2
 596   2              }       
 597   1      
 598   1              #ifdef ZLH_DEBUG
 599   1              disp_data(2, "A3");
 600   1              disp_data(SYS_INFO_BLOCK_LEN,&deviceinfo);
 601   1              while(!getkey());
 602   1              DelayMs(100);
 603   1              #endif
 604   1      
 605   1              if(init_free_cur()) return FALSE;
 606   1              
 607   1              //当前目录为根目录
 608   1              cur_dir_info.root_flag=1;
 609   1              if(deviceinfo.type){
 610   2                      cur_dir_info.start_clue=2;
 611   2                      cur_dir_info.start_sec=deviceinfo.DataFirstSec;
 612   2              }else{
C51 COMPILER V7.00  FAT                                                                    09/22/2005 17:15:32 PAGE 11  

 613   2                      cur_dir_info.start_clue=0;
 614   2                      cur_dir_info.start_sec=deviceinfo.RootFirstSec;
 615   2              }
 616   1      
 617   1              #ifdef ZLH_DEBUG
 618   1              disp_data(2, "A4");
 619   1              disp_data(DIR_INFO_LEN,&cur_dir_info);
 620   1              while(!getkey());
 621   1              DelayMs(100);
 622   1              #endif
 623   1              
 624   1              return TRUE;
 625   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   5722    ----
   CONSTANT SIZE    =      9    ----
   XDATA SIZE       =   1283     126
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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