www.pudn.com > bu1566.rar > hpi32.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;sector10) 
						 	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=(len+32)) 
					{ 
					for(j=0;jDeviceInfo.TotCluster) 
	    	{ 
			NowCluster32=CreateClusterLink32(ClusterPointer); 
		 	if(NowCluster32==0x00) 
		 		 return FALSE; 
			NowSector=FirstSectorofCluster32(NowCluster32); 
			for(i=0;i0) 
	{ 
		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;iDeviceInfo.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;sectorThisFile.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; 
	 
}