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

📄 fat.lst

📁 这是在SL811上实现的对U盘进行FAT32或FAT16读写的程序
💻 LST
字号:
C51 COMPILER V6.20c  FAT                                                                   04/07/2005 10:50:51 PAGE 1   


C51 COMPILER V6.20c, COMPILATION OF MODULE FAT
OBJECT MODULE PLACED IN Fat.OBJ
COMPILER INVOKED BY: D:\Hardware\Keilc620\C51\Bin\C51.EXE Fat.c DB OE SMALL ROM(LARGE) 

stmt level    source

   1          #include "common.h"
   2          #include "Fat.h"
   3          #include "SL811.H"
   4          #include "TPBULK.H"
   5          #include "HAL.H"
   6          //#include "RBCCMD.H"
   7          ////////////////////////////////////////
   8          extern XXGFLAGS bXXGFlags;
   9          extern SYS_INFO_BLOCK xdata DeviceInfo;
  10          extern FILE_INFO xdata ThisFile;
  11          extern DIR_INFO xdata ThisDir;
  12          extern unsigned char xdata DBUF[BUFFER_LENGTH];
  13          unsigned char xdata FATBUF[512];
  14          unsigned char xdata CurFatSector[512];
  15          unsigned long idata FreeClusterNum,FreeSecNum;
  16          ////////////////////////////////////////
  17          unsigned long FirstSectorofCluster(unsigned long clusterNum)
  18          {
  19   1      	return (clusterNum-2)*DeviceInfo.BPB_SecPerClus+DeviceInfo.FirstDataSector;
  20   1      }
  21          
  22          unsigned long ThisFatSecNum(unsigned long clusterNum)
  23          {
  24   1         //unsigned int idata result;
  25   1         unsigned long temp;
  26   1         if(bXXGFlags.bIsFat32==0)
  27   1         	temp=DeviceInfo.BPB_BytesPerSec/2;
  28   1         else
  29   1         	temp=DeviceInfo.BPB_BytesPerSec/4;
  30   1         temp=clusterNum/temp;
  31   1         temp=temp+DeviceInfo.FatStartSector;
  32   1         //result=temp;
  33   1         return temp;
  34   1      }
  35          
  36          unsigned int ThisFatEntOffset(unsigned long clusterNum)
  37          {
  38   1      	unsigned int temp;
  39   1      	unsigned long temp1;
  40   1      	if(bXXGFlags.bIsFat32==0)
  41   1      		temp1=2*clusterNum;
  42   1      	else
  43   1      		temp1=4*clusterNum;
  44   1      	temp=temp1/DeviceInfo.BPB_BytesPerSec;
  45   1      	temp1=temp1-temp*DeviceInfo.BPB_BytesPerSec;
  46   1      	//temp=temp1;
  47   1      	return temp1;
  48   1      }
  49          
  50          unsigned long GetNextClusterNum(unsigned long clusterNum)
  51          {
  52   1      	unsigned int xxgFatEntOffset;
  53   1      	unsigned long xxgFatSecNum;
  54   1      	
  55   1      	xxgFatSecNum=ThisFatSecNum(clusterNum);
C51 COMPILER V6.20c  FAT                                                                   04/07/2005 10:50:51 PAGE 2   

  56   1      	xxgFatEntOffset=ThisFatEntOffset(clusterNum);
  57   1      	//ThisFile.FatSectorPointer=xxgFatSecNum;
  58   1      	if(ThisFile.FatSectorPointer!=xxgFatSecNum)
  59   1      	{	
  60   2      		
  61   2      		if(!RBC_Read(xxgFatSecNum,1,FATBUF))
  62   2      			return 0x0;
  63   2      		ThisFile.FatSectorPointer=xxgFatSecNum;
  64   2      	}
  65   1      	
  66   1      	///////////////////////////////////////////////////
  67   1      	if(bXXGFlags.bIsFat32==0)
  68   1      		clusterNum=MakeU32(0,0,FATBUF[xxgFatEntOffset+1],FATBUF[xxgFatEntOffset]);
  69   1      	else
  70   1      		clusterNum=MakeU32(FATBUF[xxgFatEntOffset+3],FATBUF[xxgFatEntOffset+2],FATBUF[xxgFatEntOffset+1],FATBUF[
             -xxgFatEntOffset]);
  71   1      	return clusterNum;
  72   1      }
  73          
  74          unsigned char DeleteClusterLink(unsigned long clusterNum)
  75          {
  76   1      	//unsigned int nextClusterNum;
  77   1      	unsigned long xxgFatSecNum,maxClusterNum;
  78   1      	unsigned int xxgFatEntOffset;
  79   1      	U32 ThisSector;
  80   1      	U8 bChanged;
  81   1      	
  82   1      	ThisSector=0;
  83   1      	bChanged=0;
  84   1      	
  85   1      	if(bXXGFlags.bIsFat32==0)
  86   1      		maxClusterNum=0xfff8;
  87   1      	else
  88   1      		maxClusterNum=0x0ffffff8;
  89   1      	while((clusterNum>1)&&(clusterNum<maxClusterNum))
  90   1      	{
  91   2      	xxgFatSecNum=ThisFatSecNum(clusterNum);
  92   2      	xxgFatEntOffset=ThisFatEntOffset(clusterNum);
  93   2      	if(ThisSector!=xxgFatSecNum)
  94   2      		{
  95   3      		if(bChanged)
  96   3      			{
  97   4      			if(!RBC_Write(ThisSector,1,DBUF))
  98   4      				return FALSE;
  99   4      
 100   4      			if(!RBC_Write(ThisSector+DeviceInfo.FATSz,1,DBUF))
 101   4      				return FALSE;
 102   4      			bChanged=0;
 103   4      			}
 104   3      		
 105   3      		if(!RBC_Read(xxgFatSecNum,1,DBUF))
 106   3      			{
 107   4      			return FALSE;
 108   4      			}
 109   3      		ThisSector=xxgFatSecNum;
 110   3      		}
 111   2      	
 112   2      	if(bXXGFlags.bIsFat32==0)
 113   2      		{
 114   3      		clusterNum=MakeU32(0,0,DBUF[xxgFatEntOffset+1],DBUF[xxgFatEntOffset]);
 115   3      		DBUF[xxgFatEntOffset]=0x00;
 116   3      		DBUF[xxgFatEntOffset+1]=0x00;
C51 COMPILER V6.20c  FAT                                                                   04/07/2005 10:50:51 PAGE 3   

 117   3      		bChanged=1;	
 118   3      		}
 119   2      	else
 120   2      		{
 121   3      		clusterNum=MakeU32(DBUF[xxgFatEntOffset+3],DBUF[xxgFatEntOffset+2],DBUF[xxgFatEntOffset+1],DBUF[xxgFatEn
             -tOffset]);
 122   3      		DBUF[xxgFatEntOffset]=0x00;
 123   3      		DBUF[xxgFatEntOffset+1]=0x00;	
 124   3      		DBUF[xxgFatEntOffset+2]=0x00;	
 125   3      		DBUF[xxgFatEntOffset+3]=0x00;	
 126   3      		bChanged=1;
 127   3      		}
 128   2      	}
 129   1      	
 130   1      	
 131   1      	////////////////////////////////////////////
 132   1      	
 133   1      	if(bChanged)
 134   1      			{
 135   2      			if(!RBC_Write(ThisSector,1,DBUF))
 136   2      				return FALSE;
 137   2      
 138   2      			if(!RBC_Write(ThisSector+DeviceInfo.FATSz,1,DBUF))
 139   2      				return FALSE;
 140   2      			bChanged=0;
 141   2      			}
 142   1      	return TRUE;
 143   1      }
 144          
 145          
 146          unsigned char GoToPointer(unsigned long pointer)
 147          {
 148   1      	unsigned int clusterSize;
 149   1      	clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
 150   1      	ThisFile.ClusterPointer=ThisFile.StartCluster;
 151   1      	while(pointer+1>clusterSize)   //altered by xxg:2003.09.05
 152   1      	{
 153   2      		pointer-=clusterSize;	
 154   2      		ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
 155   2      		
 156   2      	}
 157   1      	ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec;
 158   1      	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster;
 159   1      	ThisFile.OffsetofSector=pointer-ThisFile.SectorofCluster*DeviceInfo.BPB_BytesPerSec;
 160   1      	ThisFile.FatSectorPointer=0;
 161   1      	return TRUE;
 162   1      	
 163   1      }
 164          
 165          unsigned long CreateClusterLink(unsigned long currentCluster)
 166          {
 167   1      	unsigned char bFound;
 168   1      	unsigned int xxgFatEntOffset;
 169   1      	unsigned long xxgFatSecNum,maxClusterCnt;
 170   1      	
 171   1      	bFound=0;
 172   1      	if(bXXGFlags.bIsFat32==0)
 173   1      		maxClusterCnt=DeviceInfo.FATSz*(DeviceInfo.BPB_BytesPerSec/2);
 174   1      	else
 175   1      		maxClusterCnt=DeviceInfo.FATSz*(DeviceInfo.BPB_BytesPerSec/4);;
 176   1      	//////////////////////////////
 177   1      	while(FreeClusterNum<maxClusterCnt)		
C51 COMPILER V6.20c  FAT                                                                   04/07/2005 10:50:51 PAGE 4   

 178   1      	{
 179   2      		xxgFatSecNum=ThisFatSecNum(FreeClusterNum);
 180   2      		xxgFatEntOffset=ThisFatEntOffset(FreeClusterNum);
 181   2      		if(xxgFatSecNum!=FreeSecNum)
 182   2      			{
 183   3      			//UpdateFat(FreeSecNum);
 184   3      			if(bXXGFlags.bFatChanged==1)
 185   3      				{
 186   4      				if(!RBC_Write(FreeSecNum,1,CurFatSector))
 187   4      					return 0;
 188   4      				if(!RBC_Write(FreeSecNum+DeviceInfo.FATSz,1,CurFatSector))
 189   4      					return 0;
 190   4      				bXXGFlags.bFatChanged=0;
 191   4      				}
 192   3      			FreeSecNum=xxgFatSecNum;
 193   3      			if(!RBC_Read(FreeSecNum,1,CurFatSector))
 194   3      				return 0x0;	
 195   3      			}
 196   2      		if(bXXGFlags.bIsFat32==0)
 197   2      			{
 198   3      			if((CurFatSector[xxgFatEntOffset]==0)&&(CurFatSector[xxgFatEntOffset+1]==0))
 199   3      		  	{	
 200   4      		  	CurFatSector[xxgFatEntOffset]=0xff;
 201   4      		  	CurFatSector[xxgFatEntOffset+1]=0xff;
 202   4      		  	 	
 203   4      		  	 bXXGFlags.bFatChanged=1;
 204   4      		  	 bFound=1;
 205   4      		  	 }
 206   3      		  	 }
 207   2      		  else
 208   2      		  	{
 209   3      		  	if((CurFatSector[xxgFatEntOffset]==0)&&(CurFatSector[xxgFatEntOffset+1]==0)&&(CurFatSector[xxgFatEntO
             -ffset+2]==0)&&(CurFatSector[xxgFatEntOffset+3]==0))
 210   3      		  	{	
 211   4      		  	CurFatSector[xxgFatEntOffset]=0xff;
 212   4      		  	CurFatSector[xxgFatEntOffset+1]=0xff;
 213   4      		  	CurFatSector[xxgFatEntOffset+2]=0xff;
 214   4      		  	CurFatSector[xxgFatEntOffset+3]=0x0f; 	
 215   4      		  	
 216   4      		  	 bXXGFlags.bFatChanged=1;
 217   4      		  	 bFound=1;
 218   4      		  	 }
 219   3      		  	}
 220   2      		 
 221   2      		 if(bFound==1)
 222   2      		 	break;
 223   2      		 else
 224   2      		 	FreeClusterNum++;
 225   2      	
 226   2      	}//end while
 227   1      	
 228   1      	//////////////////////////////////////////////
 229   1      	if(bFound==0)
 230   1      		return 0x00;
 231   1      	
 232   1      	/////////////////////////////////////////////////////////////////////	
 233   1      	//clusterNum=GetClusterNumFromPara(FreeFat.SectorNum,FreeFat.OffsetofSector);
 234   1      	if(currentCluster==0x00)
 235   1      		return FreeClusterNum++;
 236   1      	
 237   1      	xxgFatSecNum=ThisFatSecNum(currentCluster);
 238   1      	xxgFatEntOffset=ThisFatEntOffset(currentCluster);
C51 COMPILER V6.20c  FAT                                                                   04/07/2005 10:50:51 PAGE 5   

 239   1      	
 240   1      	if(xxgFatSecNum!=FreeSecNum)
 241   1      	  {
 242   2      		RBC_Read(xxgFatSecNum,1,DBUF);
 243   2      		if(bXXGFlags.bIsFat32==0)
 244   2      			{
 245   3      			DBUF[xxgFatEntOffset]=FreeClusterNum;
 246   3      			DBUF[xxgFatEntOffset+1]=FreeClusterNum>>8;
 247   3      			}
 248   2      		else
 249   2      			{
 250   3      			DBUF[xxgFatEntOffset]=FreeClusterNum;
 251   3      			DBUF[xxgFatEntOffset+1]=FreeClusterNum>>8;
 252   3      			DBUF[xxgFatEntOffset+2]=FreeClusterNum>>16;
 253   3      			DBUF[xxgFatEntOffset+3]=FreeClusterNum>>24;
 254   3      			}
 255   2      		
 256   2      		if(!RBC_Write(xxgFatSecNum,1,DBUF))
 257   2      			return 0x00;
 258   2      		if(!RBC_Write(xxgFatSecNum+DeviceInfo.FATSz,1,DBUF))
 259   2      			return 0x00;
 260   2      	   }
 261   1      	 else
 262   1      	   {
 263   2      	   	if(bXXGFlags.bIsFat32==0)
 264   2      	   		{
 265   3      	   		CurFatSector[xxgFatEntOffset]=FreeClusterNum;
 266   3      			CurFatSector[xxgFatEntOffset+1]=FreeClusterNum>>8;
 267   3      	   		}
 268   2      	   	else
 269   2      	   		{
 270   3      	   		CurFatSector[xxgFatEntOffset]=FreeClusterNum;
 271   3      			CurFatSector[xxgFatEntOffset+1]=FreeClusterNum>>8;
 272   3      			CurFatSector[xxgFatEntOffset+2]=FreeClusterNum>>16;
 273   3      			CurFatSector[xxgFatEntOffset+3]=FreeClusterNum>>24;
 274   3      	   		}
 275   2      	   			
 276   2      		bXXGFlags.bFatChanged=1;
 277   2      	   }	
 278   1      	  //////////////////////////////////////////////////////////////////////////
 279   1      	return FreeClusterNum++;
 280   1      }
 281          U8 SeekSectorToRead(U32 * pClusterNum,U16 *pSector,U32 *pSectorToRead)
 282          {
 283   1      	unsigned long idata clusterNum,sectorToRead;
 284   1      	unsigned char idata sector;
 285   1      	clusterNum=*pClusterNum;
 286   1      	sectorToRead=*pSectorToRead;
 287   1      	sector=*pSector;
 288   1      	
 289   1      	if(bXXGFlags.bIsFat32==0)	//Is fat16
 290   1      		{
 291   2      			if(ThisDir.bRootDir)	//Is Root directory
 292   2      				{
 293   3      				if(sector>DeviceInfo.BPB_RootEntCnt>>4)
 294   3      					{
 295   4      						//UartRspBlock.errcode=ERC_FILENOTFOUND;
 296   4      						return FALSE;
 297   4      					}
 298   3      					sectorToRead=ThisDir.StartSector+sector++;
 299   3      				}
 300   2      			else					//Not Root directory
C51 COMPILER V6.20c  FAT                                                                   04/07/2005 10:50:51 PAGE 6   

 301   2      				{
 302   3      				
 303   3      				if(sector>DeviceInfo.BPB_SecPerClus-1)
 304   3      					{
 305   4      					clusterNum=GetNextClusterNum(clusterNum);
 306   4      					if(clusterNum>0xfff8)
 307   4      						return FALSE;
 308   4      					//sectorToRead=FirstSectorofCluster(clusterNum);
 309   4      					sector=0;
 310   4      					}
 311   3      				sectorToRead=FirstSectorofCluster(clusterNum)+sector++;
 312   3      				}
 313   2      		}
 314   1      		else	//Is FAT32
 315   1      		{
 316   2      			if(sector>DeviceInfo.BPB_SecPerClus-1)
 317   2      				{
 318   3      					clusterNum=GetNextClusterNum(clusterNum);
 319   3      					if(clusterNum>0x0ffffff8)
 320   3      						{
 321   4      						//UartRspBlock.errcode=ERC_FILENOTFOUND;
 322   4      						return FALSE;
 323   4      						}
 324   3      					//dirSectorPointer=FirstSectorofCluster(clusterNum);
 325   3      					sector=0;
 326   3      				}
 327   2      			sectorToRead=FirstSectorofCluster(clusterNum)+sector++;
 328   2      		}
 329   1      	
 330   1      	
 331   1      	*pClusterNum=clusterNum;
 332   1      	*pSectorToRead=sectorToRead;
 333   1      	*pSector=sector;
 334   1      	return TRUE;
 335   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   2689    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   1024    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      79
   IDATA SIZE       =      8       9
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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