📄 nandfat.h
字号:
/*
* FileName: fat.h
*Copyright(c) 2004 VIA Technologies, Inc. All Rights Reserved.
*
* Author: penson Version: 1.00 Date: 2004-10-25
* DeSCRiption: this file define functions about sd card
* Version:
* Function List:
* 1.
* History:
* <penson> <10-25> <v1.00 > <desc>
*/
/*
#ifndef _MY_DEF
#include "Mydef.h"
#endif
*/
#ifndef _FAT_H
#define _FAT_H
// Some useful cluster numbers.
#define MSDOSFSROOT 0 /* cluster 0 means the root dir */
#define CLUST_FREE 0 /* cluster 0 also means a free cluster */
#define MSDOSFSFREE CLUST_FREE
#define CLUST_FIRST 2 /* first legal cluster number */
#define CLUST_RSRVD 0xfffffff6 /* reserved cluster range */
#define CLUST_BAD 0xfffffff7 /* a cluster with a defect */
#define CLUST_EOFS 0xfffffff8 /* start of eof cluster range */
#define CLUST_EOFE 0xffffffff /* end of eof cluster range */
#define FAT16_MASK 0x0000ffff /* mask for 16 bit cluster numbers */
/* Partition Type used in the partition record */
#define PART_TYPE_UNKNOWN 0x00
#define PART_TYPE_FAT12 0x01
#define PART_TYPE_FAT16 0x06
#define HW_ERR_SYSTEM 0
#define SW_PARA_OK 1
#define HW_ERR_NONE 1
#define SW_PARA_ERR 2
#define CLOSED 0
#define READ 1
#define WRITE 2
#define APPEND 3
#define CHAIN 0
#define SINGLE 1
#define END_CHAIN 2
/******************************************************************************/
#define SLOT_EMPTY 0x00 /* slot has never been used */
#define SLOT_E5 0x05 /* the real value is 0xe5 */
#define SLOT_DELETED 0xe5 /* file in this slot deleted */
#define ATTR_NORMAL 0x00 /* normal file */
#define ATTR_READONLY 0x01 /* file is readonly */
#define ATTR_HIDDEN 0x02 /* file is hidden */
#define ATTR_SYSTEM 0x04 /* file is a system file */
#define ATTR_VOLUME 0x08 /* entry is a volume label */
#define ATTR_LONG_FILENAME 0x0f /* this is a long filename entry */
#define ATTR_DIRECTORY 0x10 /* entry is a directory name */
#define ATTR_ARCHIVE 0x20 /* file is new or modified */
/*********************FILE's data structure***************************************/
//1 master boot record and partition table
//2 partition boot sector
//3 file allacotion table
//4 root directory
//5 user data
//**************master boot record and partition table****************************/
typedef struct /* length 16 bytes */
{
UINT8 prIsActive; /* 0x80 indicates active partition */
UINT8 prStartHead; /* starting head for partition */
UINT16 prStartCylSect; /* starting cylinder and sector */
UINT8 prPartType; /* partition type (see above) */
UINT8 prEndHead; /* ending head for this partition */
UINT16 prEndCylSect; /* ending cylinder and sector */
UINT32 prStartLBA; /* first LBA sector for this partition */
UINT32 prSize; /* size of this partition (bytes or sectors ?) */
}PARTRECORD;//Partition Table <---a
/******************************************************************************/
/**********************partition boot sector************************************/
typedef struct
{
UINT8 bsJump[3]; /* jump inst E9xxxx or EBxx90 */
UINT8 unused ;
UINT8 bsOemName[8]; /* OEM name and version */
UINT8 bsBPB[25]; /* BIOS parameter block */
UINT8 unused1[3] ;
UINT8 bsExt[26]; /* Bootsector Extension */
UINT8 unused3[2] ;
UINT8 bsBootCode[448]; /* pad so structure is 512b */
UINT8 bsBootSectSig0;
UINT8 bsBootSectSig1;
UINT8 unused2[2] ;
}BOOTSECTOR ;//Partition Boot Sector <---a
typedef struct
{
UINT8 exDriveNumber; /* drive number (0x80) */
UINT8 exReserved1; /* reserved */
UINT8 exBootSignature; /* ext. boot signature (0x29) */
UINT8 unused ;
UINT8 exVolumeID[4]; /* volume ID number */
UINT8 exVolumeLabel[11]; /* volume label */
UINT8 unused1 ;
UINT8 exFileSysType[8]; /* fs type (FAT12 or FAT16) */
}EXTBOOT; //Extended FDC DeSCRiptor 36
typedef struct
{
UINT16 bpbBytesPerSec; /* bytes per sector */
UINT16 bpbResSectors; /* number of reserved sectors */
UINT8 bpbMedia; /* media deSCRiptor */
UINT8 bpbSecPerClust; /* sectors per cluster */
UINT8 bpbFATs; /* number of FATs */
UINT8 unused ;
UINT16 bpbRootDirEnts; /* number of root directory entries */
UINT16 bpbSectors; /* total number of sectors */
UINT16 bpbFATsecs; /* number of sectors per FAT */
UINT16 bpbSecPerTrack; /* sectors per track */
UINT16 bpbHeads; /* number of heads */
UINT8 unused1[2] ;
UINT32 bpbHiddenSecs; /* # of hidden sectors */
/* 3.3 compat ends here */
UINT32 bpbHugeSectors; /* # of sectors if bpbSectors == 0 */
}BPB50 ; //UINT8 bsBPB[25];
/******************************************************************************/
/***********************root directory*******************************************/
typedef struct
{
UINT8 deName[8]; /* filename, blank filled */
UINT8 deExtension[3]; /* extension, blank filled */
UINT8 deAttributes; /* file attributes */
UINT8 deLowerCase; /* NT VFAT lower case flags */
UINT8 deCHundredth; /* hundredth of seconds in CTime */
UINT8 deCTime[2]; /* create time */
UINT8 deCDate[2]; /* create date */
UINT8 deADate[2]; /* access date */
UINT16 deHighClust; /* high bytes of cluster number */
UINT8 deMTime[2]; /* last update time */
UINT8 deMDate[2]; /* last update date */
UINT16 deStartCluster; /* starting cluster of file */
UINT32 deFileSize; /* size of file in bytes */
}DIRENTRY; //Directory Entry Field
/******************************************************************************/
/*********************FILE's data structure***************************************/
typedef struct
{
EXTBOOT ExtBoot ;
PARTRECORD PartRecord ;
BPB50 Bpb50 ;
BOOTSECTOR BootSector ;
UINT32 FAT1_addr ;
UINT32 FAT2_addr ;
UINT32 DIR_adrr ;
UINT32 RootAddr ;
UINT32 RootDirSectors;
UINT32 FirstDataSector;
UINT32 FirstSectorofCluster;
UINT8 FAT_type ;
UINT32 PartitionAddr ;
UINT32 DataClusTot;
}FATSYS_STRUCT ; // fAT system's related infomation
typedef struct
{
UINT32 *FAT_Table_1; /* FAT Table 1 start address */
UINT32 *FAT_Table_2; /* FAT Table 2 start address */
UINT16 Total_FAT_Cluster_Number;
UINT8 unused[2] ;
}FAT_Table_t;
typedef struct
{
UINT8 name[12]; // Filename as 8.3 0x20-padded string
UINT32 clust_start; // Starting cluster of open file
UINT32 clust_current; // Current cluster of open file
UINT32 clust_next; // Next (reading) cluster of open file
UINT32 clust_prev; // Previous cluster of open file
UINT32 cur_sec ; // Current sector withing current cluster
UINT32 entry_sec_addr; // Sector address of File entry of open file
UINT32 entry_offset; // Entry addr offset within sector for open file
UINT8 buff[512]; // Open file read/write buffer
UINT32 file_length; // Length of file
UINT32 cur_position; // Current position in file relative to begining
UINT8 mode; // 0=>closed, 1=>read, 2=>write, 3=>append and so on
UINT8 error; // Error indicator
UINT8 IsEOF; // End of File Flag 1 : is end 0: is no end
UINT8 *pdata; // Pointer for file data use
} myFILE;
/*
This function is used to Format the Sd/MMC card ,it should be called after MediaInitialization()
*/
UINT8 FATFormat(void) ;
/*
This function is used to quickly format the Sd/MMC card,it should be called after MediaInitialization()
and InitializeFat()
*/
UINT8 QuickFormat(void) ;
/*
This founction is used to get the current card FAT information, and set the current direntry to root entry
and any operation related with FAT system ,this function must be called first .
*/
UINT8 InitializeFat(void) ;
/*
this function is used to find the next cluster according to the current cluster number
*/
UINT16 NextCluster(UINT32 current_cluster);
/*
this function is used to write the value(NextNum) to the position (CurNum),to update Fat table
*/
UINT8 FatTableRecord(UINT16 CurNum, UINT16 NextNum);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -