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

📄 fat.c

📁 ATMEGA32+应用程序+Proteus仿真原理图
💻 C
字号:

//=====================================================================
//FAT for AVR(MMC/SD) 
//=====================================================================

//---------------------------------------------------------------------
//    _______________________________________
//   |      |         |     |      |      | |
//   |  DBR | RsvdSec | FAT | FDT  | DATA | |
//   |______|_________|_____|______|______|_|

//    启动扇区  保留扇区 FAT区 根目录区  数据区


//根目录开始扇区 = DBR的扇区地址  +     保留扇区 +  FAT数 *  每FAT扇区数


//数据区开始簇号 = 目录扇区总数+ 根目录开始扇区号


//---------------------------------------------------------------------
#include "fat.h"

//---------------------------------------------------------------------
U08 cluster_size;             //每簇扇区数
U16 fat_offset;               //保留扇区数
U16 cluster_offset;           //数据区开始簇号
U16 volume_boot_record_addr;  //DBR的扇区地址


//---------------------------------------------------------------------
//Selections that Adresse of the Volume Boot Record on MBR
//根据MBR计算DBR的扇区地址
//---------------------------------------------------------------------
U16 fat_addr (U08 *Buffer)
{
	U16 volume_boot_record_addr;
	mmc_read_sector (MASTER_BOOT_RECORD,Buffer);      //read MBR to buffer
    volume_boot_record_addr = Buffer[VBR_ADDR] + (Buffer[VBR_ADDR+1] << 8);  
 	//Computed Volume Boot Record 
	mmc_read_sector (volume_boot_record_addr,Buffer); //Read DBR to buffer
    return (volume_boot_record_addr);
}
//---------------------------------------------------------------------
//获取根目录开始扇区号
//---------------------------------------------------------------------

U16 fat_root_dir_addr (U08 *Buffer)    
{
	struct BootSec *bootp; //bootp : the structure of DBR
	U16 FirstRootDirSecNum;
	//Selections of the Volume Boot Record of that MMC/SD Karte 
	mmc_read_sector (volume_boot_record_addr,Buffer);
	bootp=(struct BootSec *)Buffer;

	//Computed that first Sector of the Root Directory
	FirstRootDirSecNum = ( bootp->BPB_RsvdSecCnt +
	                       (bootp->BPB_NumFATs * bootp->BPB_FATSz16));
                         //保留扇区数 +  FAT个数 *  每FAT扇区数
	
	FirstRootDirSecNum+= volume_boot_record_addr;//根目录开始扇区 =  保留扇区数 +  FAT个数 *  每FAT扇区数 
	                                                              // + DBR的扇区地址
	
	return(FirstRootDirSecNum);

}

//---------------------------------------------------------------------
//	Expenditure of the indicated Directory Entry in Entry_Count
//	is none Entry available, is that Entry in 
//	...kgabe Cluster 0xFFFF.it becomes always only ein Entry spent
//	over Storage location too save umit also f黵 small Atmels too use

//读FDT 根目录中文件名  属性  起始簇 ....
//---------------------------------------------------------------------

U16 fat_read_dir_ent (U16 dir_cluster, //Dir Cluster     //0   
					U08 Entry_Count,   //which Dir Entry 已经读到第几个目录结构数据   
					U32 *Size, 		   //the size of File or directory
					U08 *Dir_Attrib,   //the Attributs of File or directory
					U08 *Buffer) 	   //Working Buffer 
{
	U08 *pointer;
	U16 TMP_Entry_Count = 0;
	U32 Block = 0;
	struct DirEntry *dir; //dir...struct

	pointer = Buffer;

	if (dir_cluster == 0)
		{
		Block = fat_root_dir_addr(Buffer);//获取根目录开始扇区号
		}
	else
		{
		//computation of the Blocks out Block Count and Cluster out FAT Tabelle
		//computation which Cluster too load is
		//Selections that FAT - Tabelle


		//???????????
		fat_load (dir_cluster,&Block,Buffer);	
				 
		Block = ((Block-2) * cluster_size) + cluster_offset;  //目录扇区号= (目录扇区号-2) * 每簇扇区数  +  数据区开始簇号

		}

	//Selections of the Entry Root Directory
    U16 blk;
	for (blk = Block;;blk++)
	{
		mmc_read_sector (blk,Buffer);	//one Blocks of the Root Directory  512 bytes

        U16 a;
		 
		for (a=0;a<BlockSize; a = a + 32)  //BlockSize=512
		{
		 dir=(struct DirEntry *)&Buffer[a]; //Pointer up current Listing Entry get
		 
			if (dir->DIR_Name[0] == 0) //name=00H 表示此项未用
			{
			return (0xFFFF);
			}
			
			//Pr黤en obit ein 8.3 Entry is
			//that is that case if it itself not over one Entry f黵 are enough  File name
			//or over one as gel鰏cht marked Entry acts.

   			if ((dir->DIR_Attr != ATTR_LONG_NAME) &&     //不是长文件
				(dir->DIR_Name[0] != DIR_ENTRY_IS_FREE)) //没有删除.
			{
				//is it that gew黱schte Listing Entry
				if (TMP_Entry_Count == Entry_Count) 
				{
					//store of the Listing Entryes in that R點kgabe Buffer
                    U08 b;
					for(b=0;b<11;b++)
					{
					if (dir->DIR_Name[b] != SPACE)
						{
						if (b == 8)
							{
							*pointer++= '.';
							}
						*pointer++=dir->DIR_Name[b];
						}
					}						
					*pointer++='\0';


					*Dir_Attrib = dir->DIR_Attr;//the Attributs of File or directory
					 
					*Size=dir->DIR_FileSize;// the size of file
					
					dir_cluster = dir->DIR_FstClusLO;//the first cluster(low)
					 
					return(dir_cluster);//返回起始簇
				}
			TMP_Entry_Count++;
			}
		}
	}
	return (0xFFFF); //none Entry more found return with 0xFFFF
}
//---------------------------------------------------------------------
//	Selections that Cluster f黵 ein File out that FAT
//	in that Buffer(512Byte). one 128MB MMC/SD 
//	Karte is those Cluster gr鲞e normal proves 16KB gro

⌨️ 快捷键说明

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