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

📄 hpi.c

📁 可以实现44B0的底层所有通信
💻 C
📖 第 1 页 / 共 2 页
字号:
		 		
			if(bUpdate==1)
				{
	 				StartSec2Write=ThisFile.SectorPointer;
	 				BufStartAddress=pBuffer+UartRspBlock.len;
	 				SecCnt2Write=0;
	 				bUpdate=0;
				}
			step=(writeLength-1)/DeviceInfo.BPB_BytesPerSec+1;	

			if(step+ThisFile.SectorofCluster<DeviceInfo.BPB_SecPerClus+1)
				{
	   				SecCnt2Write+=step;
	   				len=writeLength;
	   				ThisFile.OffsetofSector=len;
	   			
	   				writeLength-=len;
	 				UartRspBlock.len+=len;
	 				ThisFile.pointer+=len;
		 			
		 			if(!RBC_Write(StartSec2Write,SecCnt2Write,BufStartAddress))
	   					{
		   					UartRspBlock.errcode=ERC_DEVICEERR;
							return FALSE;	
						}
		
				
		   			
					step=ThisFile.OffsetofSector/DeviceInfo.BPB_BytesPerSec;
					
					//////////////////////////////////
					
					FileInfoUpdate(step);
		   			
		   			//break;
				}
			else
				{
		   			while(step+ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus)
		   				{
		   					
		   					step=DeviceInfo.BPB_SecPerClus-ThisFile.SectorofCluster;
		   					SecCnt2Write+=step;
		   					len=step*DeviceInfo.BPB_BytesPerSec;		//DeviceInfo.BPB_BytesPerSec==512
		   					////////////////////////////////////			
		   					   			
		   					ThisFile.OffsetofSector=len;
		   			
		   					writeLength-=len;
		 					UartRspBlock.len+=len;
		 					ThisFile.pointer+=len;
		   			
		   					OldClusterPointer=ThisFile.ClusterPointer;
		   					FileInfoUpdate(step);
		   					//step=0;
		  	 			/////如果FAT不连续
		   					if(ThisFile.ClusterPointer!=OldClusterPointer+1)
		   						{
		   						//step=0;
		   							if(!RBC_Write(StartSec2Write,SecCnt2Write,BufStartAddress))
		   								{
		   									UartRspBlock.errcode=ERC_DEVICEERR;
											return FALSE;	
		   								}
		   							bUpdate=1;
		   							break;
		   						}
		   					////////////////////////
		   					if(SecCnt2Write>DeviceInfo.BPB_SecPerClus-1)
		   						{
		   								if(!RBC_Write(StartSec2Write,SecCnt2Write,BufStartAddress))
		   									{
		   										UartRspBlock.errcode=ERC_DEVICEERR;
												return FALSE;	
		   									}
		   								bUpdate=1;
		   								break;
		   							}
		   							
		   							step=(writeLength-1)/DeviceInfo.BPB_BytesPerSec+1;
		   							
		   							/////////////////////////////////		   			
						}//end while
		   		
		   		}//end else
		 	}// end while
		 	
		 	
	///////////更新文件目录信息/////////////////////////////
	if(bEnd)
		WriteFileEnd();
	
	UartRspBlock.len=0;
	ThisFile.bFileOpen=1;
	//////////////////////////////////////////////
	return TRUE;
#undef RspBlockWriteFile
}





/*******************************************************

****	更新文件的FAT信息  							****
****	step	-->?????????????????????????		****		
		---edited by tianjh 05.03.28
*******************************************************/

U8 FileInfoUpdate(U8 step)		
{
	ThisFile.OffsetofSector-=step*DeviceInfo.BPB_BytesPerSec;
	ThisFile.SectorofCluster+=step;
	
	// if the Sector of Cluster is larger than (SecPerClus-1)
	if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
	   	{
		   while(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
		      	{
		   		ThisFile.SectorofCluster-=DeviceInfo.BPB_SecPerClus;
		   		if(ThisFile.pointer<ThisFile.LengthInByte)
		   			ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
		   		else
		 		 	ThisFile.ClusterPointer=CreateClusterLink(ThisFile.ClusterPointer);//GetNextClusterNum(ThisFile.ClusterPointer);
		 		 
		 		 if(ThisFile.ClusterPointer==0x00)
		 		 	{
		 			UartRspBlock.errcode=ERC_DEVICEERR;
					return FALSE;	
		 			}
			 	}//end while  
			 	
		   ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster; 	
	   	}
	
	else
	
		ThisFile.SectorPointer=ThisFile.SectorPointer+step;
	
	return TRUE;
}

////////////////////////////////////////////////////
/*************************************************************

****************更新文件的目录信息**************************** 
							
							--edited by tianjh 05.03.28
**************************************************************/

U8 WriteFileEnd(void)		
{
	U16  sector,i;
	PDIR_STRUC pDir;
	U8 j,bStop,BUF[512];
	U32 sectorToRead;
	U32 dirSectorPointer,dirClusterPointer;
	////////// update fat info  ///////////////
	//UpdateFat(FreeSecNum);
	if(bTJHFlags.bFatChanged==1)
			{
			if(!RBC_Write(FreeSecNum,1,CurFatSector))
				return 0;
			if(!RBC_Write(FreeSecNum+DeviceInfo.FATSz,1,CurFatSector))
				return 0;
			bTJHFlags.bFatChanged=0;
			}
	///////// update dir info   ////////////////
	sector=0;
	//clusterNum=DeviceInfo.RootClusterNum;
	dirClusterPointer=ThisDir.StartCluster;
	dirSectorPointer=FirstSectorofCluster(ThisDir.StartCluster);
	for(;;)
	    	{   
		//////////////////////////////////////////////////
		if(bTJHFlags.bIsFat32==0)			//Is FAT16
		{
		
			if(ThisDir.bRootDir)	//Is Root directory
				{
				
					
					if(sector>DeviceInfo.BPB_RootEntCnt>>4)
					{
						UartRspBlock.errcode=ERC_FILENOTFOUND;
						return FALSE;
					}
					sectorToRead=ThisDir.StartSector+sector++;
				}
			else					//Not Root directory
				{
				
				if(sector>DeviceInfo.BPB_SecPerClus-1)
					{
					dirClusterPointer=GetNextClusterNum(dirClusterPointer);
					if(dirClusterPointer>0xfff8)
						{
						// To do:
						
						return FALSE;
						//;
						}
					dirSectorPointer=FirstSectorofCluster(dirClusterPointer);
					sector=0;
					}
				sectorToRead=dirSectorPointer+sector++;
				}
		}
		else			// Is FAT32
		{
			if(sector>DeviceInfo.BPB_SecPerClus-1)
				{
					dirClusterPointer=GetNextClusterNum(dirClusterPointer);
					if(dirClusterPointer>0x0ffffff8)
						{
						UartRspBlock.errcode=ERC_FILENOTFOUND;
						return FALSE;
						}
					dirSectorPointer=FirstSectorofCluster(dirClusterPointer);
					sector=0;
				}
			sectorToRead=dirSectorPointer+sector++;
		}
		
		//////////////////////////////////////////////////
		if(!RBC_Read(sectorToRead,1,BUF))
			{
			UartRspBlock.errcode=ERC_DEVICEERR;
			return FALSE;	
			}
		///////////////////////////////////////////////////
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
			{
			pDir=(PDIR_STRUC)(BUF+i);
			
			if(BUF[i]==0x00)
				{
				//UartRspBlock.errcode=ERC_FILENOTFOUND;
				//UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
				}
			///////look for the item ///////////////
			j=0;
			while(BUF[i+j]==ThisFile.FileName[j])
				{
				 j=j+1;
				 if(j>10)
				 	break;
				}
			
			if(j>10)
			    {
			   
			    pDir->DIR_FstClusLO=ThisFile.StartCluster;
			    pDir->DIR_FstClusHI=ThisFile.StartCluster>>16;
			    	    
			    if(ThisFile.pointer>ThisFile.LengthInByte)
				ThisFile.LengthInByte=ThisFile.pointer;
			    pDir->DIR_FileSize=ThisFile.LengthInByte;
			    if(!RBC_Write(sectorToRead,1,BUF))
		   			{
		   			//UartRspBlock.errcode=ERC_DEVICEERR;
					return FALSE;	
		   			}
			    bStop=1;
			    break;
			    }
			/////////////////////////////////////////
			}
		if(bStop==1)
			break;
		////////////////////////////////////////////////////
	       }// end for sector
	 return TRUE;

}

⌨️ 快捷键说明

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