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

📄 hpi.lst

📁 采用811芯片实现单片机读写U盘的源代码
💻 LST
📖 第 1 页 / 共 4 页
字号:
 483   1              {
 484   2                      if(readLength+file_point.offset_byte>deviceinfo.BytePerSec)
 485   2                              len=deviceinfo.BytePerSec-file_point.offset_byte;
 486   2                      else
 487   2                              len=readLength;
 488   2                 
C51 COMPILER V7.00  HPI                                                                    09/22/2005 17:15:31 PAGE 9   

 489   2                      if(!RBC_Read(file_point.current_sec,1,DBUF))
 490   2                      {
 491   3                              return READ_FILE_ERR;   
 492   3                      }
 493   2      
 494   2                      memcpy_cur(&para[position],&DBUF[file_point.offset_byte], len);
 495   2                      file_point.offset_byte+=len;
 496   2      
 497   2                      readLength-=len;
 498   2                      position+=len;
 499   2                 
 500   2                      if(file_point.offset_byte>deviceinfo.BytePerSec-1)
 501   2                      {       
 502   3                              file_point.offset_byte-=deviceinfo.BytePerSec;
 503   3                              file_point.offset_sec_clue++;
 504   3                              if(file_point.offset_sec_clue>deviceinfo.SecPerClus-1)
 505   3                              {
 506   4                                      file_point.offset_sec_clue=0;
 507   4                                      file_point.current_clue=GetNextClusterNum(file_point.current_clue);
 508   4                                      if(file_point.current_clue>0x0fffffef)
 509   4                                      {
 510   5                                         return FILE_EOF;     
 511   5                                      }
 512   4                                      file_point.current_sec=FirstSectorofCluster(file_point.current_clue);   
 513   4                              }
 514   3                              else
 515   3                                      file_point.current_sec++;
 516   3                      }
 517   2              }//end while
 518   1              
 519   1              
 520   1              file_point.file_open_flag=1;
 521   1              
 522   1              #ifdef ZLH_DEBUG
 523   1              disp_data(2,"B1");
 524   1              disp_data(22,&file_point);
 525   1              #endif
 526   1              
 527   1              return 0;
 528   1      }
 529          
 530          
 531          /****************************************************************
 532          创建文件或目录
 533          入口参数:  pBuffer  11个字节文件名(ASC)
 534                                                   7个字节日期时间(HEX)
 535                                                   1个字节类型标志(=0 目录=1 文件)
 536          ****************************************************************/                                         
 537          unsigned char CreateFile(uint len,unsigned char *pBuffer)
 538          {
 539   1              //unsigned long sectorNum;
 540   1      
 541   1      
 542   1              FILE_DIR_ITEM DirInfo,dir_tmp;
 543   1      
 544   1              uchar error;
 545   1              ulong clus_temp;
 546   1              ulong up_clus;
 547   1              uint clus_offset;
 548   1      
 549   1              uchar up_dir[11]={0x2e,0x2e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};
 550   1              uchar my_dir[11]={0x2e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};
C51 COMPILER V7.00  HPI                                                                    09/22/2005 17:15:31 PAGE 10  

 551   1              
 552   1              if(len!=19){
 553   2                      return RECE_DATA_ERR;
 554   2              }
 555   1              
 556   1              if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
 557   1              {
 558   2                      return NOT_DEVICE;              
 559   2              }
 560   1              
 561   1              error=find_file(pBuffer,&pBuffer[20],0);
 562   1              if(!error) return FILE_EXIST;
 563   1              if(error&&(error!=FILE_NON_EXIST)) return error;
 564   1              
 565   1              #ifdef ZLH_DEBUG
 566   1      //      disp_data(2,"C1");
 567   1      //      disp_data(2,&year);
 568   1      //      disp_data(1,&month);
 569   1      //      disp_data(1,&day);
 570   1      //      disp_data(1,&hour);
 571   1      //      disp_data(1,&min);
 572   1      //      disp_data(1,&sec);
 573   1              #endif
 574   1      
 575   1              memset_cur(&DirInfo.dir_name,0, 32);
 576   1              memcpy_cur(&DirInfo.dir_name, pBuffer,11);
 577   1              compress_date(&pBuffer[11], &DirInfo.dir_crtdate, &DirInfo.dir_crttime);
 578   1              DirInfo.dir_wrtdate=DirInfo.dir_crtdate;
 579   1              DirInfo.dir_wrttime=DirInfo.dir_crttime;
 580   1      
 581   1              if(pBuffer[18]==1) DirInfo.dir_attr=0x20;     //是文件
 582   1              else DirInfo.dir_attr=0x10;    //是目录
 583   1      
 584   1              //如果是文件则打开该文件
 585   1              memset_cur(&file_point.file_open_flag, 0,FILE_INFO_LEN);
 586   1              memcpy_cur(file_point.name,DirInfo.dir_name,11);
 587   1              memcpy_cur(file_point.date,&pBuffer[11],7);
 588   1              if(DirInfo.dir_attr==0x20){
 589   2                      file_point.type=1;
 590   2              }else{
 591   2                      //如果是目录则需要创建簇
 592   2                      file_point.type=0;
 593   2      
 594   2                      if(cur_dir_info.root_flag) up_clus=0;
 595   2                      else up_clus=cur_dir_info.start_clue;
 596   2                      
 597   2                      memcpy_cur(cur_dir_info.dir_name,DirInfo.dir_name,11);
 598   2                      cur_dir_info.root_flag=0;
 599   2                      cur_dir_info.start_clue=fat_info.next_free_clus;
 600   2                      cur_dir_info.start_sec=FirstSectorofCluster(cur_dir_info.start_clue);
 601   2                      cur_dir_info.fat_sec=fat_info.free_fat_sec;
 602   2                      clus_offset=ThisFatEntOffset(fat_info.next_free_clus);
 603   2                      if(deviceinfo.type){
 604   3                              free_fat_item.fat32[clus_offset]=0x0fffffff;
 605   3                      }else{
 606   3                              free_fat_item.fat16[clus_offset]=0xffff;
 607   3                      }
 608   2                      fat_info.free_fat_updata=1;
 609   2                      if(!updata_fat_free())  return DEVICEC_OP_ERR;
 610   2                      if(!GetFreeCusterNum()) return DEVICEC_OP_ERR;
 611   2      
 612   2                      clus_temp=SwapINT32(cur_dir_info.start_clue);
C51 COMPILER V7.00  HPI                                                                    09/22/2005 17:15:31 PAGE 11  

 613   2                      
 614   2                      DirInfo.dir_fstclushi=(uint)(clus_temp>>16);
 615   2                      DirInfo.dir_fstcluslo=(uint)(clus_temp&0x0000ffff);
 616   2                      memset_cur(&dir_tmp, 0,32);
 617   2                      memcpy_cur(dir_tmp.dir_name,my_dir, 11);
 618   2                      dir_tmp.dir_fstclushi=DirInfo.dir_fstclushi;
 619   2                      dir_tmp.dir_fstcluslo=DirInfo.dir_fstcluslo;
 620   2                      if(!RBC_Read(cur_dir_info.start_sec, 1, DBUF)) return DEVICEC_OP_ERR;
 621   2                      memcpy_cur(DBUF, &dir_tmp, 32);
 622   2                      memcpy_cur(dir_tmp.dir_name,up_dir, 11);
 623   2      
 624   2                      clus_temp=SwapINT32(up_clus);
 625   2                      
 626   2                      dir_tmp.dir_fstclushi=(uint)(clus_temp>>16);
 627   2                      dir_tmp.dir_fstcluslo=(uint)(clus_temp&0x0000ffff);
 628   2                      memcpy_cur(&DBUF[32], &dir_tmp, 32);            
 629   2                      if(!RBC_Write(cur_dir_info.start_sec, 1, DBUF)) return DEVICEC_OP_ERR;
 630   2              }
 631   1              error=add_file_item((uchar *)&DirInfo);
 632   1              if(error) return error;
 633   1      
 634   1              
 635   1              file_point.file_open_flag=1;
 636   1              
 637   1              return 0;
 638   1      }
 639          
 640          unsigned char WriteFile(unsigned int writeLength,unsigned char *pBuffer)
 641          {
 642   1              unsigned int len,write_len=0;
 643   1      
 644   1              unsigned char step;
 645   1              uchar error;
 646   1      
 647   1              uint clue_len,utemp;
 648   1              ulong temp;
 649   1              
 650   1              if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)   //U盘是否已经连接
 651   1              {
 652   2                      return NOT_DEVICE;              
 653   2              }
 654   1              
 655   1              if(!file_point.file_open_flag)
 656   1              {
 657   2                      return FILE_NOT_OPEN;           
 658   2              }
 659   1              
 660   1              //目录不能写
 661   1              if(!file_point.type) return FILE_NOT_OPEN;
 662   1      
 663   1              if(!writeLength) return RECE_DATA_ERR;
 664   1              
 665   1              file_point.file_open_flag=0;
 666   1      //      bSuccess=1;
 667   1      //      bStop=0;
 668   1      
 669   1              if(writeLength+file_point.offset_point>file_point.file_size)
 670   1                      writeLength++;
 671   1              
 672   1              clue_len=deviceinfo.BytePerSec<<(BitNum(deviceinfo.SecPerClus));
 673   1              
 674   1              if(!file_point.file_size){
C51 COMPILER V7.00  HPI                                                                    09/22/2005 17:15:31 PAGE 12  

 675   2                      file_point.file_start_clue=fat_info.next_free_clus;
 676   2                      if(deviceinfo.type){
 677   3                              free_fat_item.fat32[ThisFatEntOffset(fat_info.next_free_clus)]=0x0fffffff;
 678   3                      }else{
 679   3                              free_fat_item.fat16[ThisFatEntOffset(fat_info.next_free_clus)]=0xffff;
 680   3                      }
 681   2                      fat_info.free_fat_updata=1;
 682   2                      temp=SwapINT32(file_point.file_start_clue);
 683   2                      cur_file_item.dir_fstclushi=(uint)(temp>>16);
 684   2                      cur_file_item.dir_fstcluslo=(uint)(temp>>0x0000ffff);
 685   2              }       
 686   1              
 687   1              while(writeLength>0){
 688   2                      step=0;
 689   2                      len=clue_len-file_point.offset_byte-((uint)file_point.offset_sec_clue)<<9;   //file_point.offset_sec_clu
             -e*512
 690   2                      if(writeLength>=len){
 691   3                              step=deviceinfo.SecPerClus-file_point.offset_sec_clue;
 692   3                      }else{
 693   3                              len=deviceinfo.BytePerSec-file_point.offset_byte;
 694   3                              if(writeLength>=len){
 695   4                                      utemp=writeLength-len;
 696   4                                      step=1;
 697   4                                      while(utemp>deviceinfo.BytePerSec){
 698   5                                              step++;
 699   5                                              utemp-=deviceinfo.BytePerSec;
 700   5                                      }
 701   4                                      if(utemp) step++;
 702   4                              }else{
 703   4                                      len=writeLength;
 704   4                                      step=1;
 705   4                              }
 706   3                      }
 707   2      
 708   2                      if((!file_point.offset_byte)&&(len&0x01ff)){
 709   3                              //整扇区写,不用读出
 710   3                              if(!RBC_Write(file_point.current_sec, step, pBuffer+write_len)) return DEVICEC_OP_ERR;
 711   3                      }else{  
 712   3                              //未考虑溢出的情况
 713   3                              if(!RBC_Read(file_point.current_sec, step, DBUF)) return DEVICEC_OP_ERR;
 714   3                              memcpy_cur(&DBUF[file_point.offset_byte],pBuffer+write_len, len);
 715   3                              if(!RBC_Write(file_point.current_sec, step, DBUF)) return DEVICEC_OP_ERR;
 716   3                      }
 717   2      
 718   2                      writeLength-=len;
 719   2                      write_len+=len;
 720   2                      file_point.offset_point+=len;
 721   2                      file_point.offset_byte+=len;
 722   2                      while(file_point.offset_byte>=deviceinfo.BytePerSec){
 723   3                              file_point.offset_byte-=deviceinfo.BytePerSec;
 724   3                              file_point.offset_sec_clue++;
 725   3                      }       
 726   2                      while(file_point.offset_sec_clue>=deviceinfo.SecPerClus){

⌨️ 快捷键说明

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