?? fat.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 + -