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

📄 hpi.lst

📁 这是在SL811上实现的对U盘进行FAT32或FAT16读写的程序
💻 LST
📖 第 1 页 / 共 4 页
字号:
 527   1      	ThisFile.StartCluster=0x00000000;//SwapINT16(pDirInfo->startCluster);
 528   1      	ThisFile.LengthInByte=0;
 529   1      	ThisFile.ClusterPointer=0x00;//ThisFile.StartCluster;
 530   1      	ThisFile.SectorPointer=0x00;//FirstSectorofCluster(ThisFile.StartCluster);
 531   1      	ThisFile.OffsetofSector=0;
 532   1      	ThisFile.SectorofCluster=0;
 533   1      	ThisFile.bFileOpen=1;
 534   1      	ThisFile.pointer=0;
 535   1      	//ThisFile.FatSectorPointer=0;
 536   1      	for(j=0;j<11;j++)
 537   1      		ThisFile.FileName[j]=*(pBuffer+j);
 538   1      	//////////////////////////////////////////////
 539   1      	// 读入FAT表的第一个扇区  ////////////////////
 540   1      	FreeClusterNum=DeviceInfo.FSI_Nxt_Free;
 541   1      	FreeSecNum=ThisFatSecNum(DeviceInfo.FSI_Nxt_Free);
 542   1      	if(!RBC_Read(FreeSecNum,1,CurFatSector))
 543   1      			return FALSE;	
 544   1      	///////////////////////////////////////////////
 545   1      	return TRUE;
 546   1      #undef RspBlockCreateFile
 547   1      }
 548          
 549          unsigned char WriteFile(unsigned int writeLength,unsigned char *pBuffer,U8 bEnd)
 550          {
C51 COMPILER V6.20c  HPI                                                                   04/07/2005 10:50:50 PAGE 10  

 551   1      #define RspBlockWriteFile UartRspBlock.RspBlock.Rsp_WriteFile
 552   1      	unsigned int len,i;
 553   1      	//PDIR_INFO pDirInfo;
 554   1      	unsigned char bSuccess,bStop,step,SecCnt2Write,bUpdate;
 555   1      	
 556   1      	if(!bXXGFlags.UDiskOK)	//U盘是否已经连接
 557   1      	{
 558   2      	UartRspBlock.errcode=ERC_NODEVICE;
 559   2      	return FALSE;		
 560   2      	}
 561   1      	if(!ThisFile.bFileOpen)			//文件是否已经打开
 562   1      	{
 563   2      	UartRspBlock.errcode=ERC_FILENOTOPENED;
 564   2      	return FALSE;		
 565   2      	}
 566   1      	if(writeLength>MAX_WRITE_LENGTH)
 567   1      		return FALSE;	
 568   1      		
 569   1      	///////////////////////////////////////////////////////////
 570   1      	///////////////////////////////////////////////////////////
 571   1      	ThisFile.bFileOpen=0;
 572   1      	bSuccess=1;
 573   1      	bStop=0;
 574   1      	bUpdate=1;
 575   1      	UartRspBlock.len=0;
 576   1      	 //////////////////////////////////////////////////////
 577   1      		 //GetFreeCusterNum();
 578   1      	   if(ThisFile.StartCluster<1)
 579   1      	      {
 580   2      		ThisFile.StartCluster=CreateClusterLink(0x00000000);//GetFreeCusterNum();//SwapINT16(pDirInfo->startClus
             -ter);
 581   2      		ThisFile.LengthInByte=0;
 582   2      		ThisFile.ClusterPointer=ThisFile.StartCluster;
 583   2      		ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
 584   2      		ThisFile.OffsetofSector=0;
 585   2      		ThisFile.SectorofCluster=0;
 586   2      		//ThisFile.bFileOpen=1;
 587   2      		ThisFile.pointer=0;
 588   2      		ThisFile.FatSectorPointer=0;
 589   2      	       } 	 
 590   1      		 //////////////////////////////////////////////////////
 591   1      		 	if(ThisFile.OffsetofSector>0)
 592   1      		 	{
 593   2      		 		if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
 594   2      		   			len=DeviceInfo.BPB_BytesPerSec;
 595   2      				else
 596   2      		   			len=writeLength+ThisFile.OffsetofSector;
 597   2      			//////////////////////////////////////////////////
 598   2      		 		if(!RBC_Read(ThisFile.SectorPointer,1,DBUF))
 599   2      					{
 600   3      		   			UartRspBlock.errcode=ERC_DEVICEERR;
 601   3      					return FALSE;	
 602   3      		   			}
 603   2      		   			//ThisFile.OffsetofSector=len;
 604   2      				len=len-ThisFile.OffsetofSector;
 605   2      		   		for(i=0;i<len;i++)
 606   2      		   			DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
 607   2      		   		if(!RBC_Write(ThisFile.SectorPointer,1,DBUF))
 608   2      		   			{
 609   3      		   			UartRspBlock.errcode=ERC_DEVICEERR;
 610   3      					return FALSE;	
 611   3      		   			}
C51 COMPILER V6.20c  HPI                                                                   04/07/2005 10:50:50 PAGE 11  

 612   2      		  		ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
 613   2      		  	
 614   2      		  		step=ThisFile.OffsetofSector/DeviceInfo.BPB_BytesPerSec;
 615   2      		  		writeLength-=len;
 616   2      		 		UartRspBlock.len+=len;
 617   2      		 		ThisFile.pointer+=len;
 618   2      		 	
 619   2      		 		//OldCluster=;
 620   2      		  		if(step>0)
 621   2      		  			FileInfoUpdate(step);
 622   2      		 	}// end if
 623   1      		 
 624   1      		 	////////////////////////////////////////////////////
 625   1      		 	while(writeLength>0)
 626   1      		 	{
 627   2      		 		
 628   2      		 		if(bUpdate==1)
 629   2      		 			{
 630   3      		 			StartSec2Write=ThisFile.SectorPointer;
 631   3      		 			BufStartAddress=pBuffer+UartRspBlock.len;
 632   3      		 			SecCnt2Write=0;
 633   3      		 			bUpdate=0;
 634   3      		 			}
 635   2      		 		//////////////////////////////
 636   2      		   		step=(writeLength-1)/DeviceInfo.BPB_BytesPerSec+1;
 637   2      		   		if(step+ThisFile.SectorofCluster<DeviceInfo.BPB_SecPerClus+1)
 638   2      		   			{
 639   3      		   			SecCnt2Write+=step;
 640   3      		   			len=writeLength;
 641   3      		   			ThisFile.OffsetofSector=len;
 642   3      		   			
 643   3      		   			writeLength-=len;
 644   3      		 			UartRspBlock.len+=len;
 645   3      		 			ThisFile.pointer+=len;
 646   3      		 			
 647   3      		 			//SecCnt2Write=ThisFile.OffsetofSector/DeviceInfo.BPB_BytesPerSec;
 648   3      		 			if(!RBC_Write(StartSec2Write,SecCnt2Write,BufStartAddress))
 649   3      		   				{
 650   4      		   				UartRspBlock.errcode=ERC_DEVICEERR;
 651   4      						return FALSE;	
 652   4      		   				}
 653   3      		   			step=ThisFile.OffsetofSector/DeviceInfo.BPB_BytesPerSec;
 654   3      		   			FileInfoUpdate(step);
 655   3      		   			
 656   3      		   			//break;
 657   3      		   			}
 658   2      		   		else
 659   2      		   		{
 660   3      		   		while(step+ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus)
 661   3      		   			{
 662   4      		   			
 663   4      		   			step=DeviceInfo.BPB_SecPerClus-ThisFile.SectorofCluster;
 664   4      		   			SecCnt2Write+=step;
 665   4      		   			len=step*DeviceInfo.BPB_BytesPerSec;
 666   4      		   			ThisFile.OffsetofSector=len;
 667   4      		   			
 668   4      		   			writeLength-=len;
 669   4      		 			UartRspBlock.len+=len;
 670   4      		 			ThisFile.pointer+=len;
 671   4      		   			
 672   4      		   			OldClusterPointer=ThisFile.ClusterPointer;
 673   4      		   			FileInfoUpdate(step);
C51 COMPILER V6.20c  HPI                                                                   04/07/2005 10:50:50 PAGE 12  

 674   4      		   			//step=0;
 675   4      		   			/////如果FAT不连续
 676   4      		   			if(ThisFile.ClusterPointer!=OldClusterPointer+1)
 677   4      		   					{
 678   5      		   					//step=0;
 679   5      		   					if(!RBC_Write(StartSec2Write,SecCnt2Write,BufStartAddress))
 680   5      		   						{
 681   6      		   						UartRspBlock.errcode=ERC_DEVICEERR;
 682   6      								return FALSE;	
 683   6      		   						}
 684   5      		   					bUpdate=1;
 685   5      		   					break;
 686   5      		   					}
 687   4      		   			//// 如果超过每次最大写的数量
 688   4      		   			if(SecCnt2Write>63)
 689   4      		   				{
 690   5      		   				if(!RBC_Write(StartSec2Write,SecCnt2Write,BufStartAddress))
 691   5      		   					{
 692   6      		   					UartRspBlock.errcode=ERC_DEVICEERR;
 693   6      							return FALSE;	
 694   6      		   					}
 695   5      		   				bUpdate=1;
 696   5      		   				break;
 697   5      		   				}
 698   4      		   			step=(writeLength-1)/DeviceInfo.BPB_BytesPerSec+1;
 699   4      		   			
 700   4      		   			}//end while
 701   3      		   		
 702   3      		   		}//end else
 703   2      		 	}// end while
 704   1      	///////////更新文件目录信息/////////////////////////////
 705   1      	//WriteFileEnd();
 706   1      	if(bEnd)
 707   1      		WriteFileEnd();
 708   1      	UartRspBlock.len=0;
 709   1      	ThisFile.bFileOpen=1;
 710   1      	//////////////////////////////////////////////
 711   1      	return TRUE;
 712   1      #undef RspBlockWriteFile
 713   1      }
 714          unsigned char FileInfoUpdate(unsigned char step)
 715          {
 716   1      	ThisFile.OffsetofSector-=step*DeviceInfo.BPB_BytesPerSec;
 717   1      	ThisFile.SectorofCluster+=step;
 718   1      	
 719   1      	if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
 720   1      	   	{
 721   2      		   while(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
 722   2      		      	{
 723   3      		   		ThisFile.SectorofCluster-=DeviceInfo.BPB_SecPerClus;
 724   3      		   		if(ThisFile.pointer<ThisFile.LengthInByte)
 725   3      		   			ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
 726   3      		   		else
 727   3      		 		 	ThisFile.ClusterPointer=CreateClusterLink(ThisFile.ClusterPointer);//GetNextClusterNum(ThisFile.Clu
             -sterPointer);
 728   3      		 		 if(ThisFile.ClusterPointer==0x00)
 729   3      		 		 	{
 730   4      		 			UartRspBlock.errcode=ERC_DEVICEERR;
 731   4      					return FALSE;	
 732   4      		 			}
 733   3      			 }
 734   2      		   ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster; 	
C51 COMPILER V6.20c  HPI                                                                   04/07/2005 10:50:50 PAGE 13  

 735   2      	   	}
 736   1      	else
 737   1      		ThisFile.SectorPointer=ThisFile.SectorPointer+step;
 738   1      	return TRUE;
 739   1      
 740   1      }
 741          
 742          unsigned char WriteFileEnd(void)
 743          {
 744   1      	unsigned int xdata sector,i;
 745   1      	PDIR_STRUC pDir;
 746   1      	unsigned char j,bStop;
 747   1      	unsigned long sectorToRead,dirClusterPointer;
 748   1      	////////// update fat info  ///////////////
 749   1      	//UpdateFat(FreeSecNum);
 750   1      	if(bXXGFlags.bFatChanged==1)
 751   1      			{
 752   2      			if(!RBC_Write(FreeSecNum,1,CurFatSector))
 753   2      				return 0;
 754   2      			if(!RBC_Write(FreeSecNum+DeviceInfo.FATSz,1,CurFatSector))
 755   2      				return 0;
 756   2      			bXXGFlags.bFatChanged=0;
 757   2      			}
 758   1      	///////// update dir info   ////////////////
 759   1      	sector=0;
 760   1      	dirClusterPointer=ThisDir.StartCluster;
 761   1      	for(;;)
 762   1      	    	{   
 763   2      		//////////////////////////////////////////////////
 764   2      		if(!SeekSectorToRead(&dirClusterPointer,&sector,&sectorToRead))
 765   2      			return FALSE;
 766   2      		
 767   2      		//////////////////////////////////////////////////
 768   2      		if(!RBC_Read(sectorToRead,1,DBUF))
 769   2      			{
 770   3      			UartRspBlock.errcode=ERC_DEVICEERR;
 771   3      			return FALSE;	
 772   3      			}
 773   2      		///////////////////////////////////////////////////
 774   2      		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 775   2      			{
 776   3      			pDir=(PDIR_STRUC)(DBUF+i);
 777   3      			
 778   3      			if(DBUF[i]==0x00)
 779   3      				{
 780   4      				//UartRspBlock.errcode=ERC_DEVICEERR;
 781   4      				return FALSE;	
 782   4      				}
 783   3      			///////look for the item ///////////////
 784   3      			j=0;
 785   3      			while(DBUF[i+j]==ThisFile.FileName[j])
 786   3      				{
 787   4      				 j=j+1;
 788   4      				 if(j>10)
 789   4      				 	break;
 790   4      				}
 791   3      			if(DBUF[i+11]&0x10==0x10)	//not directory?
 792   3      				continue;

⌨️ 快捷键说明

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