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

📄 hpi.c

📁 基于Embest开发环境
💻 C
📖 第 1 页 / 共 3 页
字号:
			     break;}
			
			}
		///////////////////////////////////////////////////////
		if(bstop==1)break;
		///////////////////////////////////////////////////////
		//if(DeviceInfo.BPB_RootEntCnt)
		
	    }
	    
	    if(sector>=DeviceInfo.BPB_RootEntCnt)
	    	{
	    	UartRspBlock.errcode=ERC_FILENOTFOUND;
	    	//UartRspBlock.errcode=ERC_DEVICEERR;
		return FALSE;		
	    	}
	////////////////////////////////////////////
	pDirInfo=(PDIR_INFO)RspBlockOpenFile.item;
	ThisFile.StartCluster=SwapINT16(pDirInfo->startCluster);
	ThisFile.LengthInByte=SwapINT32(pDirInfo->length);
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
	ThisFile.OffsetofSector=0;
	ThisFile.SectorofCluster=0;
	//=ThisFatSecNum(clusterNum);
	//xxgFatEntOffset=ThisFatEntOffset(clusterNum);
	ThisFile.FatSectorPointer=0;
	//ThisFile.bFileOpen=1;
	ThisFile.pointer=0;
	///////////////////////////////////////////
	return TRUE;
#undef RspBlockOpenFile
}

unsigned char ReadFile(unsigned int readLength,unsigned char *pBuffer)
{
#define RspBlockReadFile UartRspBlock.RspBlock.Rsp_ReadFile
	unsigned int len,i;
	unsigned char bSuccess;
	//unsigned char sector;
	//unsigned long lba;
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
	UartRspBlock.errcode=ERC_NODEVICE;
	return FALSE;		
	}
	if(!ThisFile.bFileOpen)
	{
	UartRspBlock.errcode=ERC_FILENOTOPENED;
	return FALSE;		
	}
	///////////////////////////////////////////////////////////
	ThisFile.bFileOpen=0;
	bSuccess=1;
	UartRspBlock.len=0;
	//lba=GetSecNumFromPointer();
	//cluster=GetClusterNumFromSectorNum(lba);
	//cluster=ThisFile.StartCluster;
	//lba=FirstSectorofCluster(ThisFile.StartCluster);
	//readLength=SwapINT16(UartCmdBlock.CmdBlock.Cmd_ReadFile.readLength);
	if(readLength>MAX_READ_LENGTH)
		{
		UartRspBlock.errcode=ERC_LENGTHEXCEED;
		return FALSE;	
		}
	if(readLength+ThisFile.pointer>ThisFile.LengthInByte)
		{
		UartRspBlock.errcode=ERC_LENGTHEXCEED;
		return FALSE;	
		}
	////////////////////////////////////////////
		
		///////////////////////////////////////////////////
		while(readLength>0)
		{
		   if(readLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
		   	len=DeviceInfo.BPB_BytesPerSec;
		   else
		   	len=readLength+ThisFile.OffsetofSector;
		   
		   //////////////////////////////////////////////////////
		   if(ThisFile.OffsetofSector>0)
		   	{
		   	if(RBC_Read(ThisFile.SectorPointer,1,DBUF))
		   		{
		   		//ThisFile.OffsetofSector=len;
		   		len=len-ThisFile.OffsetofSector;
		   		for(i=0;i<len;i++)
		   			//UARTBUF[i]=DBUF[ThisFile.OffsetofSector+i];
		   			*(pBuffer+i)=DBUF[ThisFile.OffsetofSector+i];
		   		ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
		   		}
		   	else
		   		{
		   		UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
		   		}
		   	}
		   else
		   	{
		   		if(!RBC_Read(ThisFile.SectorPointer,1,pBuffer+UartRspBlock.len))
		   		{
		   		UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
		   		}
		   		ThisFile.OffsetofSector=len;
		   	}
		   ////////////////////////////////////////////////////////////
		  // if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
		  // 	ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
		   readLength-=len;
		   UartRspBlock.len+=len;
		  // ThisFile.OffsetofSector=;
		   /////////////////////////////////////////////////////////
		   if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
		   {	
		   	ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
		   	ThisFile.SectorofCluster+=1;
		   	if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
		   	{
		   		ThisFile.SectorofCluster=0;
		 		 ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
		 		 if(ThisFile.ClusterPointer>0xffef)
		 		 	{
		 		 	   //RspBlockReadFile.errcode=ERC_REACHEND;
		   			   //RspBlockReadFile.result=0x0;
		   			   UartRspBlock.errcode=ERC_REACHEND;
					   return FALSE;	
		 		 	}
		 		 ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer); 	
		   	}
		   	else
		   		ThisFile.SectorPointer=ThisFile.SectorPointer+1;
		    }
		   //////////////////////////////////////////////////////////////////
		}//end while
	
	
	ThisFile.bFileOpen=1;
	ThisFile.pointer+=UartRspBlock.len;
	//////////////////////////////////////////////
	RspBlockReadFile.readLength=SwapINT16(UartRspBlock.len);
	return TRUE;
#undef RspBlockReadFile
}

unsigned char CreateFile(unsigned char *pBuffer)
{
#define RspBlockCreateFile UartRspBlock.RspBlock.Rsp_CreateFile
	//unsigned long sectorNum;
	unsigned int sector,i,j;
	unsigned char bstop;
	PDIR_INFO pDirInfo;
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
	UartRspBlock.errcode=ERC_NODEVICE;
	return FALSE;		
	}
	///////////////////////////////////////////////////////////
	//RspBlockCreateFile.result=0x1;
	//RspBlockCreateFile.errcode=ERC_OK;
	pDirInfo=(PDIR_INFO)pBuffer;
	///////// Search the file of the same name  ///////////
	//UartCmdBlock.CmdBlock.Cmd_CreateFile.filename[j]
	//if(!SPC_TestUnit())
	//	return FALSE;
	//if(!RBC_Read(1,1,DBUF))
	//	return FALSE;
	//if(!RBC_Write(1,1,DBUF))
	//	return FALSE;
	//////// Search the fat for a free cluster  ////////////
	pDirInfo->startCluster=SwapINT16(GetFreeCusterNum());
	
	if(pDirInfo->startCluster<0x2)
	{
	UartRspBlock.errcode=ERC_NODEVICE;
	return FALSE;		
	}
	pDirInfo->length=0;
	/////// Search a free space in the root dir space and build the item ///
	ThisFile.bFileOpen=0;
	bstop=0;
	for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
	    {   
		//////////////////////////////////////////////////
		if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
			{
			
			UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
			}
		///////////////////////////////////////////////////
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
			{
			if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
				{
				for(j=0;j<32;j++)
					//DBUF[i+j]=UartCmdBlock.CmdBlock.Cmd_CreateFile.item[j];
					DBUF[i+j]=*(pBuffer+j);
				if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
		  	 		{
		  	 		UartRspBlock.errcode=ERC_DEVICEERR;
					return FALSE;	
		  	 		}
				bstop=1;
				break;
				}
			}
		///////////////////////////////////////////////////////
		if(bstop==1)break;
		
	    }
	/////////////////////////////////////////////
	//pDirInfo=(PDIR_INFO)RspBlockOpenFile.item;
	//ThisFile.FatSectorPointer=ThisFatSecNum(ThisFile.StartCluster);
	ThisFile.StartCluster=SwapINT16(pDirInfo->startCluster);
	ThisFile.LengthInByte=0;
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
	ThisFile.OffsetofSector=0;
	ThisFile.SectorofCluster=0;
	ThisFile.bFileOpen=1;
	ThisFile.pointer=0;
	ThisFile.FatSectorPointer=0;
	//////////////////////////////////////////////
	return TRUE;
#undef RspBlockCreateFile
}

unsigned char WriteFile(unsigned int writeLength,unsigned char *pBuffer)
{
#define RspBlockWriteFile UartRspBlock.RspBlock.Rsp_WriteFile
	unsigned int len,sector,i;
	PDIR_INFO pDirInfo;
	unsigned char bSuccess,bStop;
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
	UartRspBlock.errcode=ERC_NODEVICE;
	return FALSE;		
	}
	if(!ThisFile.bFileOpen)
	{
	UartRspBlock.errcode=ERC_FILENOTOPENED;
	return FALSE;		
	}
	///////////////////////////////////////////////////////////
	//if(UartCmdBlock.CmdBlock.Cmd_WriteFile.writeLength>0)
	//	{
	//	for(i=UartCmdBlock.CmdBlock.Cmd_WriteFile.writeLength;i<MAX_WRITE_LENGTH;i++)
	//	UARTBUF[i]=0;
	//	}
	///////////////////////////////////////////////////////////
	ThisFile.bFileOpen=0;
	bSuccess=1;
	bStop=0;

	UartRspBlock.len=0;
	//if(UartCmdBlock.CmdBlock.Cmd_WriteFile.writeLength)
	while(writeLength>0)
	{
		if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
		   	len=DeviceInfo.BPB_BytesPerSec;
		else
		   	len=writeLength+ThisFile.OffsetofSector;
		   
		 //////////////////////////////////////////////////////
		 if(ThisFile.OffsetofSector>0)
		 	{
		 	if(RBC_Read(ThisFile.SectorPointer,1,DBUF))
		   		{
		   		//ThisFile.OffsetofSector=len;
		   		len=len-ThisFile.OffsetofSector;
		   		for(i=0;i<len;i++)
		   			//DBUF[ThisFile.OffsetofSector+i]=UARTBUF[i];
		   			DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
		   		if(!RBC_Write(ThisFile.SectorPointer,1,DBUF))
		   			{
		   			UartRspBlock.errcode=ERC_DEVICEERR;
					return FALSE;	
		   			}
		   		ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
		   		}
		   	else
		   		{
		   		UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
		   		}
		 	}
		 else
		 	{
		 	if(!RBC_Write(ThisFile.SectorPointer,1,pBuffer+UartRspBlock.len))
		   		{
		   		UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
		   		}
		   	ThisFile.OffsetofSector=len;
		 	}
		 /////////////////////////////////////////////////////
		 //if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
		 //  	ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
		   writeLength-=len;
		   UartRspBlock.len+=len;
		   //ThisFile.LengthInByte+=len;
		/////////////更新文件指针 //////////////////////////////
		  if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
		   {	
		   	ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
		   	ThisFile.SectorofCluster+=1;
		   	if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
		   	{
		   		ThisFile.SectorofCluster=0;
		 		 ThisFile.ClusterPointer=CreateClusterLink(ThisFile.ClusterPointer);//GetNextClusterNum(ThisFile.ClusterPointer);
		 		 if(ThisFile.ClusterPointer==0x00)
		 		 	{
		 		 //	   RspBlockReadFile.errcode=ERC_REACHEND;
		   			//   RspBlockReadFile.result=0x0;
		   			UartRspBlock.errcode=ERC_DEVICEERR;
					return FALSE;	
		 			}
		 		 ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer); 	
		   	}
		   	else
		   		ThisFile.SectorPointer=ThisFile.SectorPointer+1;
		    }
		
	
	}//end while
	ThisFile.pointer+=UartRspBlock.len;
	///////////更新文件目录信息/////////////////////////////
	if(bSuccess==1)
	{
		
		for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
	    	{   
		//////////////////////////////////////////////////
		if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
			{
			UartRspBlock.errcode=ERC_DEVICEERR;
			return FALSE;	
			}
		///////////////////////////////////////////////////
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
			{
			pDirInfo=(PDIR_INFO)(DBUF+i);
			
			if(pDirInfo->startCluster==SwapINT16(ThisFile.StartCluster))
				{
				if(ThisFile.pointer>ThisFile.LengthInByte)
					ThisFile.LengthInByte=ThisFile.pointer;
				//else
				//	ThisFile.pointer=;
				
				pDirInfo->length=SwapINT32(ThisFile.LengthInByte);
				if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
		   		{
		   		UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
		   		}
				 bStop=1;
				 break;
				}
			}
		if(bStop==1)
			break;
		////////////////////////////////////////////////////
	       }
	
	}
	/*
	if(bSuccess==0)
	{
	RspBlockWriteFile.errcode=ERC_DEVICEERR;
	RspBlockWriteFile.result=0x0;
	}
	*/
	UartRspBlock.len=0;
	ThisFile.bFileOpen=1;
	//////////////////////////////////////////////
	return TRUE;
#undef RspBlockWriteFile
}

unsigned char RemoveFile(unsigned char *pBuffer)
{
#define RspBlockRemoveFile UartRspBlock.RspBlock.Rsp_RemoveFile
	unsigned int sector,i;
	unsigned char bStop,j;
	PDIR_INFO pDirInfo;
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
	UartRspBlock.errcode=ERC_NODEVICE;
	return FALSE;		

⌨️ 快捷键说明

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