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

📄 hpi.c

📁 一个U盘的文件系统源代码!!!C语言编写
💻 C
📖 第 1 页 / 共 2 页
字号:
	
	if(pDirInfo->startCluster<0x2)
	{
		UartRspBlock.errcode=ERC_NODEVICE;
		return FALSE;		
	}
	pDirInfo->length=0;
	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]=*(pBuffer+j);
					
				if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
		  	 	{
		  	 		UartRspBlock.errcode=ERC_DEVICEERR;
					return FALSE;	
		  	 	}
				bstop=1;
				break;
			}
		}
		if(bstop==1)	break;
	}
	
	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;

	FreeFat.SectorNum=DeviceInfo.FatStartSector;
	FreeFat.OldSectorNum=DeviceInfo.FatStartSector;
	FreeFat.OffsetofSector=2;
	
	return TRUE;
#undef RspBlockCreateFile
}

/*-----------------------------------------------------------------------------------
名称: unsigned char WriteFile(unsigned int writeLength,unsigned char *pBuffer)
功能: 写入数据到文件.
-----------------------------------------------------------------------------------*/
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,step;
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)	//U盘是否已经连接
	{
		UartRspBlock.errcode=ERC_NODEVICE;
		return FALSE;		
	}
	if(!ThisFile.bFileOpen)			//文件是否已经打开
	{
		UartRspBlock.errcode=ERC_FILENOTOPENED;
		return FALSE;		
	}
		
	ThisFile.bFileOpen=0;
	bSuccess=1;
	bStop=0;

	UartRspBlock.len=0;
	while(writeLength>0)				
	{
		 if(ThisFile.OffsetofSector>0)
		 {
		 	if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
		   		len=DeviceInfo.BPB_BytesPerSec;
			else
		   		len=writeLength+ThisFile.OffsetofSector;
		   		
		 	if(!RBC_Read(ThisFile.SectorPointer,1,DBUF))
			{
		   		UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
		   	}

			len=len-ThisFile.OffsetofSector;
		   	for(i=0;i<len;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
		 {
		 	if(writeLength>DeviceInfo.BPB_BytesPerSec)
		   	{
		   		step=writeLength/DeviceInfo.BPB_BytesPerSec;
		   		
		   		if(step>DeviceInfo.BPB_SecPerClus)
		   		{
		   			step=DeviceInfo.BPB_SecPerClus-ThisFile.SectorofCluster;
		   			len=step*DeviceInfo.BPB_BytesPerSec;	
		   		}
		   		else	
		   		{
		   			step=step-ThisFile.SectorofCluster;
		   			len=step*DeviceInfo.BPB_BytesPerSec;
		   		}
		    }
			else
		   	{
		   		step=1;
		   		len=writeLength;
		   	}
		   	
		 	if(!RBC_Write(ThisFile.SectorPointer,step,pBuffer+UartRspBlock.len))
		   	{
		   		UartRspBlock.errcode=ERC_DEVICEERR;
				return FALSE;	
		   	}
		   	ThisFile.OffsetofSector=len;
		}
		
		writeLength-=len;
		UartRspBlock.len+=len;

		/////////////更新文件指针 //////////////////////////////
		//改变参数:OffsetofSector,SectorofCluster,SectorPointer,ClusterPointer
		step=ThisFile.OffsetofSector/DeviceInfo.BPB_BytesPerSec;
		  
		if(step>0)
		{	
		   	ThisFile.OffsetofSector-=step*DeviceInfo.BPB_BytesPerSec;
		   	ThisFile.SectorofCluster+=step;
		   	
		   	if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
		   	{
		   		ThisFile.SectorofCluster=0;
		   		
		   		if(ThisFile.pointer+UartRspBlock.len<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;	
		 		}
		 		ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer); 	
		   	}
		   	else
		   		ThisFile.SectorPointer=ThisFile.SectorPointer+step;
		}
	}//end while
	ThisFile.pointer+=UartRspBlock.len;
	UpdateFat(FreeFat.SectorNum);
	
	/*更新文件目录信息*/
	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;
				
					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;
		}
	}
	UartRspBlock.len=0;
	ThisFile.bFileOpen=1;
	return TRUE;
#undef RspBlockWriteFile
}

/*-----------------------------------------------------------------------------------
名称: unsigned char RemoveFile(unsigned char *pBuffer)
功能: 删除文件.
-----------------------------------------------------------------------------------*/
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;		
	}
	
	/*清除目录*/
	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)
			{
				UartRspBlock.errcode=ERC_FILENOTFOUND;
				return FALSE;	
			}
			j=0;
			
			while(DBUF[i+j]==*(pBuffer+j))
			{
				 j=j+1;
				 if(j>10) break;
			}//end while
			
			if(j>10)
			{	
			 	DBUF[i]=0xE5;
			 	pDirInfo=(PDIR_INFO)(DBUF+i);
			 	ThisFile.StartCluster=SwapINT16(pDirInfo->startCluster);

			 	if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
				{
					UartRspBlock.errcode=ERC_DEVICEERR;
					return FALSE;	
				}
				
				/*清除FAT中的纪录*/
				
				if(!DeleteClusterLink(ThisFile.StartCluster))
				{
					UartRspBlock.errcode=ERC_DEVICEERR;
					return FALSE;	
				}
			 	bStop=1;
			 	break;
			}
			
		}//end for
		if(bStop==1)	break;
		
	}//end search
	if(sector>=DeviceInfo.BPB_RootEntCnt)
	{
		UartRspBlock.errcode=ERC_FILENOTFOUND;
		return FALSE;	
	}
	
	return TRUE;
#undef RspBlockRemoveFile
}

/*-----------------------------------------------------------------------------------
名称: unsigned char GetCapacity(void)
功能: 获取磁盘空间.
-----------------------------------------------------------------------------------*/
unsigned char GetCapacity(void)
{	
#define RspBlockGetCapacity UartRspBlock.RspBlock.Rsp_GetCapacity
	
	unsigned int sectorNum,freesectorcnt,i;
	
	PREAD_CAPACITY_RSP pBuf;
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
		UartRspBlock.errcode=ERC_NODEVICE;
		return FALSE;		
	}

	if(!RBC_ReadCapacity())
	{
		UartRspBlock.errcode=ERC_DEVICEERR;
		return FALSE;	
	}
	
	pBuf=(PREAD_CAPACITY_RSP)DBUF;
	RspBlockGetCapacity.disksize=SwapINT32((pBuf->LastLBA+1)*pBuf->BlockSize);
	sectorNum=DeviceInfo.FatStartSector;
	freesectorcnt=0;
	
	while(sectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
	{
		
		if(RBC_Read(sectorNum,1,DBUF))
		{
		  	for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+2)
		  	{
		  	 	if((DBUF[i]==0xff)&&(DBUF[i+1]==0xff))
		  	 	{	
		  	 		freesectorcnt++;
		  	 	}
		  	}	
		}
		else
		{
			UartRspBlock.errcode=ERC_DEVICEERR;
			return FALSE;	
		}
		sectorNum++;
	}
	
	RspBlockGetCapacity.freedisksize=DeviceInfo.BPB_BytesPerSec*DeviceInfo.BPB_SecPerClus;
	RspBlockGetCapacity.freedisksize=freesectorcnt*RspBlockGetCapacity.freedisksize;
	RspBlockGetCapacity.freedisksize=SwapINT32(RspBlockGetCapacity.disksize)-RspBlockGetCapacity.freedisksize;
	RspBlockGetCapacity.freedisksize=SwapINT32(RspBlockGetCapacity.freedisksize);
		
	return TRUE;
#undef RspBlockGetCapacity
}

/*-----------------------------------------------------------------------------------
名称: unsigned char GetFreeCapacity(void)
功能: 获取磁盘剩余空间.
-----------------------------------------------------------------------------------*/
unsigned char GetFreeCapacity(void)
{
#define RspBlockGetCapacity UartRspBlock.RspBlock.Rsp_GetFreeCapacity

	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
		UartRspBlock.errcode=ERC_NODEVICE;
		return FALSE;		
	}
	
	return TRUE;
#undef RspBlockGetFreeCapacity
}

/*-----------------------------------------------------------------------------------
名称: unsigned char SetFilePointer(unsigned long pointer)
功能: 将文件指针移动到指定位置.
-----------------------------------------------------------------------------------*/
unsigned char SetFilePointer(unsigned long pointer)
{
#define RspBlockSetFilePointer UartRspBlock.RspBlock.Rsp_SetFilePointer
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
		UartRspBlock.errcode=ERC_NODEVICE;
		return FALSE;		
	}
	if(!ThisFile.bFileOpen)
	{
		UartRspBlock.errcode=ERC_FILENOTOPENED;
		return FALSE;		
	}
	
	ThisFile.pointer=pointer;
	if(ThisFile.pointer>ThisFile.LengthInByte)
	{
		UartRspBlock.errcode=ERC_LENGTHEXCEED;
		return FALSE;	
	}
	
	if(!GoToPointer(ThisFile.pointer))
	{
		ThisFile.bFileOpen=0;
		UartRspBlock.errcode=ERC_DEVICEERR;
		return FALSE;	
	}
	return TRUE;

#undef RspBlockSetFilePointer
}

/*-----------------------------------------------------------------------------------
名称: unsigned char GetFirmwareVersion(void)
功能: 获取软件版本.
-----------------------------------------------------------------------------------*/
unsigned char GetFirmwareVersion(void)
{
#define RspBlockGetVersion UartRspBlock.RspBlock.Rsp_GetVersion
   RspBlockGetVersion.version=0x0102;
   return TRUE;
   #undef RspBlockGetVersion
}

⌨️ 快捷键说明

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