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

📄 hpi32.c

📁 bu1566dsp芯片用来处理ov7660或其他30万摄像模组的图像预览.图像拍照(jpeg压缩)
💻 C
字号:
#include "fat32.h"
#include "hpi32.h"
#include "string.h"
#include "sdconfig.h"
#include "sddriver.h"						/* SD卡操作的相关函数 */
////////////////////////////////////////////
UINT16 filenum;

UINT8 OpenFile(UINT8 *pBuffer)
{
UINT16 i;
UINT8 j,bstop,sector;
	
	bstop=0;
	ThisFile.bFileOpen=0;
	pBuffer[11]=0x20;
	NowCluster32=DirStartCluster32;		
		do
		{
			NowSector=FirstSectorofCluster(NowCluster32);
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	    	{   
				if(MMC_read_sector(NowSector+sector,DBUF)!=0x0)
					return FALSE;				
				for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
				{
					if(DBUF[i]==0x00)
						return FALSE;
					j=0;
					while(DBUF[i+j]==*(pBuffer+j))
					{
						 j=j+1;
						 if(j>10)
						 	break;
					}
					if(j>10&&(DBUF[i+11]&0x10)!=0x10)
				    	{ 
						for(j=0;j<32;j++)
			    			UARTBUF[j]=DBUF[i+j];			    
			    		bstop=1;
			    		break;
						}
				}
				if(bstop==1)break;		
	    	}
			if(bstop==1)break;	
			NowCluster32=GetNextClusterNum(NowCluster32);			
		}while(NowCluster32<=DeviceInfo.TotCluster);
	if(NowCluster32>DeviceInfo.TotCluster)
	   	return FALSE;
	ThisFile.bFileOpen=1;
	ThisFile.StartCluster=LSwapINT32(UARTBUF[26],UARTBUF[27],UARTBUF[20],UARTBUF[21]);
	ThisFile.LengthInByte=LSwapINT32(UARTBUF[28],UARTBUF[29],UARTBUF[30],UARTBUF[31]);
	ThisFile.ClusterPointer=ThisFile.StartCluster;	
	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
	ThisFile.OffsetofSector=0;
	ThisFile.SectorofCluster=0;	
	ThisFile.FatSectorPointer=0;	
	ThisFile.pointer=0;
	return TRUE;	
}

UINT8 SetFilePointer(UINT32 pointer)
{
	if(!ThisFile.bFileOpen)
		return FALSE;		
	ThisFile.pointer=pointer;
	if(ThisFile.pointer>ThisFile.LengthInByte)
		return FALSE;	
	if(!GoToPointer(ThisFile.pointer))
	{
		ThisFile.bFileOpen=0;
		return FALSE;	
	}
	return TRUE;
}



UINT8 CreateFile(UINT32 len,UINT8 *pBuffer,UINT8 *pName)
{
UINT16 sector,i,j,DirCount;
UINT32 cnum;
UINT8 bstop,InByte,bwrite;
UINT32 ClusterPointer;
	
	if((len%32)!=0)
		return FALSE;
	if((len+32)>DeviceInfo.BPB_BytesPerSec)
		return FALSE;
	ThisFile.bFileOpen=0;	
	cnum=GetFreeCusterNum();
	if(cnum<0x02)
		return FALSE;	

	pBuffer[21]=(UINT8)(cnum>>24);
	pBuffer[20]=(UINT8)(cnum>>16);
	pBuffer[27]=(UINT8)(cnum>>8);
	pBuffer[26]=(UINT8)(cnum);

	pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
	bstop=0;

	NowCluster32=DirStartCluster32;		
		do
		{
			NowSector=FirstSectorofCluster32(NowCluster32);
			ClusterPointer=NowCluster32;
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	    	{   
				if(!RBC_Read(NowSector+sector,1,DBUF))
					return FALSE;
				DirCount=0;bwrite=0;

				for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
				{
				if(len==0)
					{
					if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
					{
					for(j=0;j<32;j++)
						DBUF[i+j]=*(pBuffer+j);
					if(!RBC_Write(NowSector+sector,1,DBUF))
		  	 			return FALSE;		  	 		
					bstop=1;
					break;
					}		
					}
				else
				{
				if(DirCount==0)
					InByte=i;
				if(DBUF[i]==0xE5)				
					DirCount++;				
				else if(DBUF[i]==0x00)
					{	
					DirCount++;	
					DBUF[i]=0xE5;	
					bwrite=1;				
					}
				else
					DirCount=0;

				if((DirCount*32)>=(len+32))
					{
					for(j=0;j<len;j++)
						DBUF[InByte+j]=*(pName+j);
					for(j=0;j<32;j++)
						DBUF[InByte+len+j]=*(pBuffer+j);
					if(!RBC_Write(NowSector+sector,1,DBUF))
		  		 		return FALSE;		  	 		
					bstop=1;
					break;
					}
				 }
				}
				if(bstop==1)break;
				
				if((len!=0)&&(bwrite==1))
				{
				if(!RBC_Write(NowSector+sector,1,DBUF))
		  			return FALSE;
	    		}
	    	}
			if(bstop==1)break;
	
			NowCluster32=GetNextClusterNum32(NowCluster32);
			if(NowCluster32>DeviceInfo.TotCluster)
	    	{
			NowCluster32=CreateClusterLink32(ClusterPointer);
		 	if(NowCluster32==0x00)
		 		 return FALSE;
			NowSector=FirstSectorofCluster32(NowCluster32);
			for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
			for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
				{
				if(!RBC_Write(NowSector+sector,1,DBUF))
		  	 		return FALSE;
				}
			}
		}while(NowCluster32<=DeviceInfo.TotCluster);	
////////////////////////////////////////////////////////////////
	
	ThisFile.StartCluster=cnum;
	ThisFile.LengthInByte=0;
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.StartCluster);
	ThisFile.OffsetofSector=0;
	ThisFile.SectorofCluster=0;
	ThisFile.bFileOpen=1;
	ThisFile.pointer=0;
	ThisFile.FatSectorPointer=0;
	
	return TRUE;
}

UINT8 WriteFile(UINT32 writeLength,UINT8 *pBuffer)
{
UINT16  len,sector,i;
UINT8 bSuccess,bStop,temp,retry;
UINT32 cnum,tlen;
	
	if(!ThisFile.bFileOpen)
		return FALSE;
	ThisFile.bFileOpen=0; 
	bSuccess=1;
	bStop=0;
	tlen=0;
	while(writeLength>0)
	{
		if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
		   	len=DeviceInfo.BPB_BytesPerSec;
		else
		   	len=writeLength+ThisFile.OffsetofSector;
		if(ThisFile.OffsetofSector>0)
		 	{
			if(MMC_read_sector(ThisFile.SectorPointer,DBUF)==0x00)
		   	{
		   		len=len-ThisFile.OffsetofSector;
		   		for(i=0;i<len;i++)		   			
		   		{
					DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
				}
				retry=0;
				do{
					temp=MMC_write_sector(ThisFile.SectorPointer,DBUF);
					retry++;
				}while((temp!=0x00)&&(retry<100));
				if (temp!=0x00)	
					return FALSE;
				ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
		   		}
		   	else
		   		return FALSE;		   		
		 	}
		 else
		 	{
		   		for(i=0;i<len;i++)		   			
		   		{
					DBUF[i]=*(pBuffer+i+tlen);
				}
				retry=0;
				do{
					temp=MMC_write_sector(ThisFile.SectorPointer,DBUF);
					retry++;
				}while((temp!=0x00)&&(retry<100));
				if (temp!=0x00)	
					return FALSE;
			ThisFile.OffsetofSector=len;
		 	}
		   writeLength-=len;
		   tlen+=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);
		 		 if(ThisFile.ClusterPointer==0x00)
		 		 	return FALSE;		 		
		 		 ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer); 	
		   	}
		   	else
		   		ThisFile.SectorPointer=ThisFile.SectorPointer+1;
		    }
	}
	ThisFile.pointer+=tlen;
	if(bSuccess==1)
	{
		NowCluster32=DirStartCluster32;		
			do
			{
				NowSector=FirstSectorofCluster(NowCluster32);
				for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
	   		 	{   
					if(MMC_read_sector(NowSector+sector,DBUF)!=0x00)
						return FALSE;				
					for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
					{
						cnum=LSwapINT32(DBUF[i+26],DBUF[i+27],DBUF[i+20],DBUF[i+21]);
						if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
							{
							if(ThisFile.pointer>ThisFile.LengthInByte)
								ThisFile.LengthInByte=ThisFile.pointer;				
							DBUF[i+28]=(UINT8)(ThisFile.LengthInByte&0xff);
							DBUF[i+29]=(UINT8)((ThisFile.LengthInByte>>8)&0xff);
							DBUF[i+30]=(UINT8)((ThisFile.LengthInByte>>16)&0xff);
							DBUF[i+31]=(UINT8)((ThisFile.LengthInByte>>24)&0xff);
							retry=0;
							do{
								temp=MMC_write_sector(NowSector+sector,DBUF);
								retry++;
							}while((temp!=0x00)&&(retry<100));
							if (temp!=0x00)	
								return FALSE;
							bStop=1;
							break;
						}
	    			}
					if(bStop==1)
						break;
				}
				if(bStop==1)
					break;
				NowCluster32=GetNextClusterNum(NowCluster32);							
			}while(NowCluster32<=DeviceInfo.TotCluster);
		if(NowCluster32>DeviceInfo.TotCluster)
			return FALSE;
	}
	ThisFile.bFileOpen=1;
	return TRUE;
	
}

⌨️ 快捷键说明

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