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

📄 u_test.c

📁 基于Embest开发环境
💻 C
字号:
//#include "host_811.h"
#include "stdio.h"
#include "44blib.h"
#include "44b.h"
#include "flash.h"
#include "USB_SYS.h"

#define	filenum	9

WORD   	TotalWriteSize,ErrorCapacity;		//~~去掉xdata
LWORD  	ValidCapacity;
BYTE 	Directory[32];//文件信息
ShapeFile 	aShapeFile[20];//用来存放文件的名字,在flash中的起始存放位子和长度 
LWORD	flashadd =0x1c1000;
LWORD	iniflashadd=0x1c1000;		    //存放在flash中起始地址
//WORD CheckFreeCapacity(WORD TotalWriteSize,WORD *FreeList,WORD ErrorCapacity);//查询U盘空闲容量,得到空闲簇的地址列表
//BYTE ResultExport(WORD *FreeList);//结果导出到一个文件
									//~~没发现以上两个函数的定义



///////////////////////////////////////////////////////////////////////
BYTE GetFileInfo(WORD whichfile)//参数是第几个文件(第几个目录项),从0开始,将
{  
   LWORD temp,temp1,temp2,i;					//文件信息放入全局变量byte Directory[32]
   temp=whichfile<<5;
   temp=temp/SectorSize;//sectorsize是每扇区字节数
   temp=temp+RootDirAddr;//fataddr是fat表起始地址,此时temp中为此目录项所在的扇区数
   							
	temp1=whichfile<<5;
	temp2=temp1/SectorSize;
	temp1=temp1-temp2*SectorSize;//temp1中是这个目录项所在扇区中的开始字节数

	if(UFLReadSector(temp))
		{
		 for(i=0;i<32;i++)
			{
			Directory[i]=MassStorageDataBuf[temp1+i];
			Uart_Printf("%c ",Directory[i]);//GC
			}
			Uart_SendString("*\n");
		}
			
	else	
	   	return FALSE;
	   	
	return	TRUE;
}

///////////////////////////////////////////////////////////////////////////////
//由于u盘中的目录区中并不是只有各个文件的目录项,还有很多杂项,并且有些文件的目录项
//是占64个bytes(正常是占32个bytes),所以可以根据winhex中的信息确定要读的文件是在第几个
//目录项(从0开始数),然后确定filenum的值
///////////////////////////////////////////////////////////////////////////////


BYTE GetFileSaveintoFlash(WORD whichfile,LWORD startaddinflash)//读出u盘中文件,存放在
{	BYTE i,j;					//flash中,其中的回车之类的一律按原来文件格式存放,最后一个
	WORD cluster;           //簇中的内容(应该是2个扇区)全部(也就是1024个字节)存在flash中
	LWORD sector;        
	
	GetFileInfo(whichfile);//得到文件信息
	for(i=0;i<8;i++)
	aShapeFile[whichfile].name[i]=Directory[i];//填写文件信息ashapefile	
	aShapeFile[whichfile].startadd=startaddinflash;
	aShapeFile[whichfile].length=(LWORD)Directory[31]<<24|(LWORD)Directory[30]<<16|
								  (LWORD)Directory[29]<<8|(LWORD)Directory[28];
	cluster=(WORD)Directory[27]<<8|(WORD)Directory[26];
	Uart_Printf("%d,%d,%s,%x",cluster,aShapeFile[whichfile].length,
					aShapeFile[whichfile].name,FileDataAddr);
	
	while(!(cluster>=0xfff8&&cluster<=0xffff))
	{   sector=FirstSectorofCluster(cluster);
		Uart_Printf("%x\n",sector);
		for(i=0;i<ClusterSize;i++)
			{	if(UFLReadSector(sector))
					{	for(j=0;j<0x40;j++)
						{//print
						Uart_Printf("%x ",MassStorageDataBuf[j]);
						if((j+1)%16==0)
						Uart_Printf("\n");
						}
					if( Program(flashadd,MassStorageDataBuf,SectorSize) == TRUE )
							flashadd+=SectorSize;		
						else	return FALSE;
					}
				else return FALSE;
				sector++;
			}
		cluster=GetNextClusterNum(cluster);
	}								  
	return TRUE;
}

//将所有扩展名为dst的文件打印到超级终端

BYTE PrintDSTFile()
{
	int a=0;
	int b;
	
	
	
	while(1);
	
	///////////////////gc
	for(b=0;b<512;b++)
	{  
	  
	  
	  
	  if(GetFileInfo(0))
	  {
	  	if((Directory[8]=='d' || Directory[8]=='D')&(Directory[9]=='s' || Directory[9]=='S')
	  	   &(Directory[10]=='t' || Directory[10]=='T'))
	  	{ 
	  	 {if(a==0)
	  	 		{Uart_Printf("DST File name:\n");
 				 Uart_Printf("%d. %c%c%c%c%c%c%c%c	",a,Directory[0],Directory[1],Directory[2],
 						Directory[3],Directory[4],Directory[5],Directory[6],Directory[7]);
 				}
          else
           	Uart_Printf("%d. %c%c%c%c%c%c%c%c	",a,Directory[0],Directory[1],Directory[2],
 						Directory[3],Directory[4],Directory[5],Directory[6],Directory[7]);
 		 }
 		 if((a+1)%3==0)
 		 	Uart_Printf("\n");					  
	  	 a++;
	  	}
	  }
	  else
	  	return FALSE;
	  }
}





									
unsigned char U_Disk_Test(void)
{
	BYTE i=0,offset,chr;
	BYTE  FATNum;
	WORD j;

	
	while(U_Disk_DETECT()!=0) Uart_Printf("ERR = %d \n",U_Disk_DETECT());	
	
//   文件系统操作开始

/*********************************************************************************		
Read Main Boot Record(MBR)  Sector=0x00
	MassStorageDataBuf[446]==0x80 --> First partition is active partition
	MassStorageDataBuf[450]==0x04 --> FAT16
							 0x01 --> FAT12
							 0x06 --> BigFAT16
							 0x07 --> NTFS
							 0x0B --> FAT32
							 0x0C --> FAT32 LBA
**********************************************************************************/

/*GC
//读起始扇区0
		if(UFLReadSector(0x00))
			{

		DBRAddr=(WORD)MassStorageDataBuf[455]<<8|(WORD)MassStorageDataBuf[454];
		USBCapacity=(LWORD)MassStorageDataBuf[461]<<24|(LWORD)MassStorageDataBuf[460]<<16|
					(LWORD)MassStorageDataBuf[459]<<8|(LWORD)MassStorageDataBuf[458];		//总扇区数
		}
		else{
			return 0x09;//USB设备不支持本通信协议
		}

GC*/
/*********************************************************************************		
	Read DOS Boot Record  Sector=DBRAddr
	MassStorageDataBuf[11,12]	w Byte Pre Sector
	MassStorageDataBuf[13]		b Sector Pre Cluster
	MassStorageDataBuf[14,15]	w Reserved sector
	MassStorageDataBuf[16]		b Number of FATs
	MassStorageDataBuf[17,18]	w Entry of Root 
	MassStorageDataBuf[22,23]	w Sector Pre FAT

	读取DBR 所在扇区
	
**********************************************************************************/


/*GC
	i=0;
	while(i<20){
		
		if(UFLReadSector(DBRAddr)) 
			break;

		USB_Delay(200);	
		Reset_U();	
		USB_Delay(200);	
		UFLRequestSense();
		
		USB_Delay(200);	
		i++;	
	}	
	if(i==20) return 0x0a;//USB设备不支持本通信协议
	

	Uart_Printf("get DBR info \n");

	SectorSize=(WORD)MassStorageDataBuf[12]<<8|(WORD)MassStorageDataBuf[11];//每扇区字节数
	ClusterSize=MassStorageDataBuf[13];//每簇扇区数
	Uart_Printf("SectorSize = %d,ClusterSize = %d\n",SectorSize,ClusterSize);
	
	FATAddr=DBRAddr+((WORD)MassStorageDataBuf[15]<<8|(WORD)MassStorageDataBuf[14]);
									//fat表地址=系统引导记录(长为一个扇区)地址+保留扇区数
	FATNum=MassStorageDataBuf[16];  //fat的个数
	
	Uart_Printf("FATNum = %d \n",FATNum);
	Uart_Printf("FAT Addr = %ld \n",FATAddr);
	
/*	
	//////////XXXXXXXXXXXXXXXXXXX  fat16
	RootDirNum=(WORD)MassStorageDataBuf[18]<<8|(WORD)MassStorageDataBuf[17];//根目录项数
	
	Uart_Printf("RootDirNum = %d \n",RootDirNum);
	
	RootDirSize=(RootDirNum<<5)/SectorSize;//根目录占多少个扇区
	
	Uart_Printf("RootDirSize = %d \n",RootDirSize);
	
	
	
	FATLength =(WORD)MassStorageDataBuf[23]<<8|(WORD)MassStorageDataBuf[22];//每个fat占的扇区数
	RootDirAddr=FATAddr+FATLength*FATNum;//根目录地址
	FileDataAddr=RootDirAddr+RootDirSize;//数据区地址
	
	//Uart_Printf("%x,%x,%x,%x,%x,%x,%x,%x,%x\n",SectorSize,ClusterSize,FATAddr,FATNum,RootDirNum,
	//			RootDirSize,FATLength,RootDirAddr,FileDataAddr);
//	if(MaxLUN)//U盘为爱国者128M
//		USBCapacity=(LWORD)MassStorageDataBuf[35]<<24|(LWORD)MassStorageDataBuf[34]<<16|
//			(LWORD)MassStorageDataBuf[33]<<8|(LWORD)MassStorageDataBuf[32];
	ValidCapacity=USBCapacity-1-RootDirSize-FATLength*FATNum;
	
	ErrorCapacity=(LWORD)(FATLength*256-2)*ClusterSize-ValidCapacity;
	
*/	
	
	//////////         GC CODE       ///////
	
	//RootDirNum=  ???? ;//根目录项数
	
	//Uart_Printf("RootDirNum = %d \n",RootDirNum);
	
/*GC	
	FATLength =(WORD)MassStorageDataBuf[37]<<8|(WORD)MassStorageDataBuf[36];//每个fat占的扇区数
	RootDirAddr=FATAddr+FATLength*FATNum;//根目录地址
	
	//得到根目录地址 和 FAT表地址
	
	RootDirSize=ClusterSize;//根目录占多少个扇区
	
	Uart_Printf("RootDirSize = %d \n\n\n",RootDirSize);
	
	
	
	i=0;
	while(i<20){
		if(UFLReadSector(RootDirAddr)) break;

		USB_Delay(200);				

		Reset_U();
	
		USB_Delay(200);				
	
		UFLRequestSense();
		
		USB_Delay(200);	
		i++;	
	}	
	if(i==20) return 0x0a;//USB设备不支持本通信协议
	
	for(i=0;i<16;i++)
	{
	
		if(i==0)
		   Uart_Printf("DISK NAME :");
		else if(i==1)
		   Uart_Printf("\n Found file :  ");
		   else 
		   Uart_Printf("                 ");

		
		for(offset=0;offset<=10;offset++)
			{
				
				if(offset==8)
				Uart_Printf(".");
				
				chr=MassStorageDataBuf[i*32+offset];
				
				if(chr>(0x20))
				Uart_Printf("%c",chr);
			}
			
		Uart_Printf("\n");		
	
	}
	
	
	//////////         GC CODE       ///////

	
	
//读出u盘中数据区中的第一个扇区中的内容,并打印出来,已实验成功
/*	i=0;
	while(i<20){
		if(UFLRead10(FileDataAddr)) break;

		USB_Delay(200);				

		Reset_U();
	
		USB_Delay(200);				
	
		UFLRequestSense();
		
		USB_Delay(200);	
		i++;	
	}	
	if(i==20) return 0x0a;
	
	for(j=0;j<0x200;j++){
			Uart_Printf("%x ",MassStorageDataBuf[j]);
			if(j%16==0)
					Uart_Printf("\n");
			}
*/ 
 	
//读第几个文件,存放在flash中,再从flash中读出数据,打印到超级终端 	
/* 	if(GetFileInfo(filenum))			//print the file's name
 		Uart_Printf("its name is: %c%c%c%c%c%c%c%c",Directory[0],Directory[1],Directory[2],
 						Directory[3],Directory[4],Directory[5],Directory[6],Directory[7]);
 	else 
 		return FALSE;
 	if(!GetFileSaveintoFlash(filenum,flashadd))	//get a file from u flash, save it in flash
 				return FALSE;
 	Uart_Printf("the name is %s",aShapeFile[filenum].name);
 	for(j=0;j<aShapeFile[filenum].length;j++)
 	{ 	//get the file from flash, and print it
 			Uart_Printf("%x ",(*(volatile BYTE *)(iniflashadd+j)));
 					if((j+1)%16==0)
						Uart_Printf("\n");
    }
*/
//Uart_SendString("print *.dst !\n");
//PrintDSTFile();

Analyse_Sector0();
Analyse_DBR();
Analyse_ROOT();
Analyse_FILE();

Uart_SendString("------OK------\n");

	  return 0x00;
	
}

⌨️ 快捷键说明

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