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

📄 hpi.c

📁 采用811芯片实现单片机读写U盘的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	}
	
	file_point.offset_point+=readLength;
		
	while(readLength>0)
	{
		if(readLength+file_point.offset_byte>deviceinfo.BytePerSec)
	   		len=deviceinfo.BytePerSec-file_point.offset_byte;
		else
	   		len=readLength;
	   
		if(!RBC_Read(file_point.current_sec,1,DBUF))
	   	{
			return READ_FILE_ERR;	
	   	}

		memcpy_cur(&para[position],&DBUF[file_point.offset_byte], len);
	  	file_point.offset_byte+=len;

	   	readLength-=len;
	   	position+=len;
	   
	   	if(file_point.offset_byte>deviceinfo.BytePerSec-1)
	   	{	
	   		file_point.offset_byte-=deviceinfo.BytePerSec;
		   	file_point.offset_sec_clue++;
	   		if(file_point.offset_sec_clue>deviceinfo.SecPerClus-1)
	   		{
	   			file_point.offset_sec_clue=0;
	 		 	file_point.current_clue=GetNextClusterNum(file_point.current_clue);
	 		 	if(file_point.current_clue>0x0fffffef)
	 		 	{
				   return FILE_EOF;	
	 		 	}
	 		 	file_point.current_sec=FirstSectorofCluster(file_point.current_clue); 	
	   		}
	   		else
	   			file_point.current_sec++;
	    	}
	}//end while
	
	
	file_point.file_open_flag=1;
	
	#ifdef ZLH_DEBUG
	disp_data(2,"B1");
	disp_data(22,&file_point);
	#endif
	
	return 0;
}


/****************************************************************
创建文件或目录
入口参数:  pBuffer  11个字节文件名(ASC)
                                         7个字节日期时间(HEX)
                                         1个字节类型标志(=0 目录=1 文件)
****************************************************************/                                         
unsigned char CreateFile(uint len,unsigned char *pBuffer)
{
	//unsigned long sectorNum;


	FILE_DIR_ITEM DirInfo,dir_tmp;

	uchar error;
	ulong clus_temp;
	ulong up_clus;
	uint clus_offset;

	uchar up_dir[11]={0x2e,0x2e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};
	uchar my_dir[11]={0x2e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};
	
	if(len!=19){
		return RECE_DATA_ERR;
    	}
    	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
		return NOT_DEVICE;		
	}
	
	error=find_file(pBuffer,&pBuffer[20],0);
	if(!error) return FILE_EXIST;
	if(error&&(error!=FILE_NON_EXIST)) return error;
	
	#ifdef ZLH_DEBUG
//	disp_data(2,"C1");
//	disp_data(2,&year);
//	disp_data(1,&month);
//	disp_data(1,&day);
//	disp_data(1,&hour);
//	disp_data(1,&min);
//	disp_data(1,&sec);
	#endif

	memset_cur(&DirInfo.dir_name,0, 32);
	memcpy_cur(&DirInfo.dir_name, pBuffer,11);
	compress_date(&pBuffer[11], &DirInfo.dir_crtdate, &DirInfo.dir_crttime);
	DirInfo.dir_wrtdate=DirInfo.dir_crtdate;
	DirInfo.dir_wrttime=DirInfo.dir_crttime;

	if(pBuffer[18]==1) DirInfo.dir_attr=0x20;     //是文件
	else DirInfo.dir_attr=0x10;    //是目录

	//如果是文件则打开该文件
	memset_cur(&file_point.file_open_flag, 0,FILE_INFO_LEN);
	memcpy_cur(file_point.name,DirInfo.dir_name,11);
	memcpy_cur(file_point.date,&pBuffer[11],7);
	if(DirInfo.dir_attr==0x20){
		file_point.type=1;
	}else{
		//如果是目录则需要创建簇
		file_point.type=0;

		if(cur_dir_info.root_flag) up_clus=0;
		else up_clus=cur_dir_info.start_clue;
		
		memcpy_cur(cur_dir_info.dir_name,DirInfo.dir_name,11);
		cur_dir_info.root_flag=0;
		cur_dir_info.start_clue=fat_info.next_free_clus;
		cur_dir_info.start_sec=FirstSectorofCluster(cur_dir_info.start_clue);
		cur_dir_info.fat_sec=fat_info.free_fat_sec;
		clus_offset=ThisFatEntOffset(fat_info.next_free_clus);
		if(deviceinfo.type){
			free_fat_item.fat32[clus_offset]=0x0fffffff;
		}else{
			free_fat_item.fat16[clus_offset]=0xffff;
		}
		fat_info.free_fat_updata=1;
		if(!updata_fat_free())  return DEVICEC_OP_ERR;
		if(!GetFreeCusterNum()) return DEVICEC_OP_ERR;

		clus_temp=SwapINT32(cur_dir_info.start_clue);
		
		DirInfo.dir_fstclushi=(uint)(clus_temp>>16);
		DirInfo.dir_fstcluslo=(uint)(clus_temp&0x0000ffff);
		memset_cur(&dir_tmp, 0,32);
		memcpy_cur(dir_tmp.dir_name,my_dir, 11);
		dir_tmp.dir_fstclushi=DirInfo.dir_fstclushi;
		dir_tmp.dir_fstcluslo=DirInfo.dir_fstcluslo;
		if(!RBC_Read(cur_dir_info.start_sec, 1, DBUF)) return DEVICEC_OP_ERR;
		memcpy_cur(DBUF, &dir_tmp, 32);
		memcpy_cur(dir_tmp.dir_name,up_dir, 11);

		clus_temp=SwapINT32(up_clus);
		
		dir_tmp.dir_fstclushi=(uint)(clus_temp>>16);
		dir_tmp.dir_fstcluslo=(uint)(clus_temp&0x0000ffff);
		memcpy_cur(&DBUF[32], &dir_tmp, 32);		
		if(!RBC_Write(cur_dir_info.start_sec, 1, DBUF)) return DEVICEC_OP_ERR;
	}
	error=add_file_item((uchar *)&DirInfo);
	if(error) return error;

	
	file_point.file_open_flag=1;
	
	return 0;
}

unsigned char WriteFile(unsigned int writeLength,unsigned char *pBuffer)
{
	unsigned int len,write_len=0;

	unsigned char step;
	uchar error;

	uint clue_len,utemp;
	ulong temp;
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)	//U盘是否已经连接
	{
		return NOT_DEVICE;		
	}
	
	if(!file_point.file_open_flag)
	{
		return FILE_NOT_OPEN;		
	}
	
	//目录不能写
	if(!file_point.type) return FILE_NOT_OPEN;

	if(!writeLength) return RECE_DATA_ERR;
	
	file_point.file_open_flag=0;
//	bSuccess=1;
//	bStop=0;

	if(writeLength+file_point.offset_point>file_point.file_size)
		writeLength++;
	
	clue_len=deviceinfo.BytePerSec<<(BitNum(deviceinfo.SecPerClus));
	
	if(!file_point.file_size){
		file_point.file_start_clue=fat_info.next_free_clus;
		if(deviceinfo.type){
			free_fat_item.fat32[ThisFatEntOffset(fat_info.next_free_clus)]=0x0fffffff;
		}else{
			free_fat_item.fat16[ThisFatEntOffset(fat_info.next_free_clus)]=0xffff;
		}
		fat_info.free_fat_updata=1;
		temp=SwapINT32(file_point.file_start_clue);
		cur_file_item.dir_fstclushi=(uint)(temp>>16);
		cur_file_item.dir_fstcluslo=(uint)(temp>>0x0000ffff);
	}	
	
	while(writeLength>0){
		step=0;
		len=clue_len-file_point.offset_byte-((uint)file_point.offset_sec_clue)<<9;   //file_point.offset_sec_clue*512
		if(writeLength>=len){
			step=deviceinfo.SecPerClus-file_point.offset_sec_clue;
		}else{
			len=deviceinfo.BytePerSec-file_point.offset_byte;
			if(writeLength>=len){
				utemp=writeLength-len;
				step=1;
				while(utemp>deviceinfo.BytePerSec){
					step++;
					utemp-=deviceinfo.BytePerSec;
				}
				if(utemp) step++;
			}else{
				len=writeLength;
				step=1;
			}
		}

		if((!file_point.offset_byte)&&(len&0x01ff)){
			//整扇区写,不用读出
			if(!RBC_Write(file_point.current_sec, step, pBuffer+write_len)) return DEVICEC_OP_ERR;
		}else{	
			//未考虑溢出的情况
			if(!RBC_Read(file_point.current_sec, step, DBUF)) return DEVICEC_OP_ERR;
			memcpy_cur(&DBUF[file_point.offset_byte],pBuffer+write_len, len);
			if(!RBC_Write(file_point.current_sec, step, DBUF)) return DEVICEC_OP_ERR;
		}

		writeLength-=len;
		write_len+=len;
		file_point.offset_point+=len;
		file_point.offset_byte+=len;
		while(file_point.offset_byte>=deviceinfo.BytePerSec){
			file_point.offset_byte-=deviceinfo.BytePerSec;
			file_point.offset_sec_clue++;
		}	
		while(file_point.offset_sec_clue>=deviceinfo.SecPerClus){
			file_point.offset_sec_clue-=deviceinfo.SecPerClus;
			file_point.current_clue=GetNextClusterNum(file_point.current_clue);
			if(!file_point.current_clue) return DEVICEC_OP_ERR;
			if((file_point.current_clue>0x0fffffef)&&writeLength){
				if(!CreateClusterLink(file_point.current_clue)) return DEVICEC_OP_ERR;
			}	
		}
	}//end while
//	file_point.offset_point+=write_len;
	
	//更新文件目录信息
	if(file_point.offset_point>file_point.file_size){
		cur_file_item.dir_file_size=file_point.offset_point;
		file_point.file_size=file_point.offset_point;
		compress_date(file_point.date,&cur_file_item.dir_wrtdate,&cur_file_item.dir_wrttime);
		error=find_file(file_point.name, (uchar *)&cur_file_item,1);
		if(error) return error;
	}		
	
	file_point.file_open_flag=1;
	
	#ifdef ZLH_DEBUG
	disp_data(2,"B2");
	disp_data(47,&file_point);
	#endif
	
	return 0;
}

unsigned char RemoveFile(unsigned char *pBuffer)
{


	uchar error,exist_flag=0,end_flag=0;
	ulong temp,start_clue,start_sec;
	uint clue_len,k;
	FILE_DIR_ITEM  file_temp;

	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
	return NOT_DEVICE;		
	}

	error=find_file(pBuffer, (uchar *)&file_temp, 0);
	if(error) return error;

	file_temp.dir_name[0]=0xe5;

	temp=((ulong)file_temp.dir_fstclushi)<<16+file_temp.dir_fstcluslo;
	temp=SwapINT32(temp);

	if(file_temp.dir_attr==0x10){
	//删除目录需要判断目录内文件是否都已删除
		start_clue=temp;
		clue_len=deviceinfo.BytePerSec<<(BitNum(deviceinfo.SecPerClus));
		while(1){
			start_sec=FirstSectorofCluster(start_clue);
			//还未考虑溢出DBUF的情况
			if(!RBC_Read(start_sec, deviceinfo.SecPerClus, DBUF)) return DEVICEC_OP_ERR;	
			for(k=0;k<clue_len;k+=32){
				if(!DBUF[k]){
					end_flag=1;
					break;
				}	
				if(DBUF[k]!=0xe5){
					exist_flag=1;
					break;
				}
			}	
			if(end_flag) break;
			start_clue=GetNextClusterNum(start_clue);
			if(start_clue>0x0fffffef){
				end_flag=1;
				break;
			}
		}
		if(exist_flag) return DIR_NOT_EMPTY;
	}

	//删除FAT链
	if(!DeleteClusterLink(temp)) return DEVICEC_OP_ERR;
	
	//删除目录项
	error=find_file(pBuffer, (uchar *)&file_temp, 1);
	if(error) return error;
	
	return 0;
}

//unsigned char GetCapacity(uchar *para)
//{
//	unsigned int sectorNum,freesectorcnt,i;
//	
////#define RspBlockGetCapacity UartRspBlock.RspBlock.Rsp_GetCapacity
//
//	PREAD_CAPACITY_RSP pBuf;
//	
//	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
//	{
//		return NOT_DEVICE;		
//	}
//	///////////////////////////////////////////////////////////
//	if(!RBC_ReadCapacity())
//	{
//		return DEVICEC_OP_ERR;	
//	}
//	
//	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)
//		  	{
//		  	 //clusterNum++;	
//		  	 
//		  	 if((DBUF[i]==0xff)&&(DBUF[i+1]==0xff))
//		  	 	{	
//		  	 	freesectorcnt++;
//		  	 	}
//		  	// clusterNum++;
//		  	}	
//		}
//		else
//			{
//			return DEVICEC_OP_ERR;	
//			}
//		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 0;
////#undef RspBlockGetCapacity
//}
//
//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)
{
	
	if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
	{
		return NOT_DEVICE;		
	}
	if(!file_point.file_open_flag)
	{
		return FILE_NOT_OPEN;		
	}
	
	#ifdef ZLH_DEBUG
	disp_data(2,"B4");
	disp_data(4,&pointer);
	disp_data(4,&file_point.file_size);
	#endif
	
	
	if(!GoToPointer(pointer))
	{
		file_point.file_open_flag=0;
		return DEVICEC_OP_ERR;	
	}
	#ifdef ZLH_DEBUG
	disp_data(2,"B2");
	disp_data(FILE_INFO_LEN,&file_point);
	#endif
	return 0;

}

/*********************************************************************************
压缩日期时间
入口参数:*para  年月日时分秒(HEX)(大端模式)
出口参数: date   uint   日期(小端模式)
                           time   uint   时间(小端模式)
*********************************************************************************/
void compress_date(uchar *para,uint *date,uint *time_com)
{
	uint year;
	uchar month,day,hour,min,sec;
	
	year=*((uint *)(para));
	month=*(para+2);
	day=*(para+3);
	hour=*(para+4);
	min=*(para+5);
	sec=*(para+6);
	
	*date=((year-1980)<<9)+(month<<5)+day;
	*date=SwapINT16(*date);
	*time_com=(hour<<11)+(min<<5)+(sec>>1);
	*time_com=SwapINT16(*time_com);
	return ;
}	

//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 + -