SD卡FAT16文件系统源代码要点.docx

上传人:时光煮雨 文档编号:11766914 上传时间:2021-09-05 格式:DOCX 页数:42 大小:174.94KB
返回 下载 相关 举报
SD卡FAT16文件系统源代码要点.docx_第1页
第1页 / 共42页
SD卡FAT16文件系统源代码要点.docx_第2页
第2页 / 共42页
SD卡FAT16文件系统源代码要点.docx_第3页
第3页 / 共42页
SD卡FAT16文件系统源代码要点.docx_第4页
第4页 / 共42页
SD卡FAT16文件系统源代码要点.docx_第5页
第5页 / 共42页
点击查看更多>>
资源描述

《SD卡FAT16文件系统源代码要点.docx》由会员分享,可在线阅读,更多相关《SD卡FAT16文件系统源代码要点.docx(42页珍藏版)》请在三一文库上搜索。

1、/* FATLib.h:interface for the FATLib.class FATLib: a portable FAT decoder class which is hardware independent. All hardware specific operations are abstracted with theclass HALayer. The FATLib class operates with only the buffer which it passes to the class HALayer* Author: Ivan Sham* Date: JUly 1,

2、2004* Version: 2.0* Note: Developed for William Hue and Pete Rizun* Change Log* Date | Author*| Reason for change* Aug31/04William Hue*Changed char types to unsigned char.*Put more parentheses in abs()*macro.* Jul18/04Alex Jiang*Ported to FAB belt clip. Made local variables and functions*static and

3、moved to fatlib.c.*Prefixed public funtios with*fat_.* Jan02/05William HueVarious bugfixes and clean-up for*Circuit Cellar article.#ifndef _FATLIB_#define _FATLIB_#include HALayer.h#define UNKNOWN 0#define FAT16 1#define DIRECTORY TRUE#define FILE FALSE#define READ TRUE#define WRITE FALSE#define BUF

4、FER_SIZE 2#define abs(x) (x) 0) ? (x) : (-(x)/ member functions:/* initialize the system* return0* return1* return 3* return4UNKNOWN file systemFAT16 file systemcould not set block lengthcould not initialize memory card*/unsigned char fat_initialize(void);/*closes the file indicated by the inputpara

5、mfileHandle handle of file to be closedreturnreturnreturn0-1-2file sucessfully closed invalid file handle invalid file system*/signed char fat_close(signed char fileHandle);/* opens the file indicated by the input path name. If the pathname* points to a valid file, the file is added to the list of c

6、urrently* opened files for reading and the unique file handle is returned.* param buf*the buffer to be used to access the MMC/SD card* return-1* return-2* return-3* return-4invalid pathnamefile does not existfile already opened for writingfile already opened for reading* param pathnamea pointer to t

7、he location of the file to be openedmemory card error other errorfile handle of sucessfully opened file* return-10* return-20* return-128* return.no handles available*/*signed char fat_openRead(const char * Change Log* Date | Author| Reason for change pathname);opens the file indicated by the input

8、path name. If the pathname* points to a valid path, the file is created and added to the list of* currently opened files for writing and the unique file handle is returned.*parampathnamea pointer to the location of the file to be openedreturn return return return-1-2-3-4return -10invalid pathnamefil

9、e already existfile already opened for writing no directory entries leftno handles availablereturnreturnreturn-20-128memory card error other error(non-negative) file handle of sucessfully opened file*param bufparam handleparam nByte*return -10*return -1*return -32768*return .*/*/ signed char fat_ope

10、nWrite(const char * pathname);/* reads the content of the file identified by the input handle. It reads from * where the last read operation on the same file ended. If its the first time * the file is being read, it starts from the beginning of the file.* pre nByte SECTOR_SIZE*the buffer to be used

11、to access the MMC/SD card handle of file to be read number of bytes to readmemory card errorinvalid handleother errornumber of bytes read signed int fat_read(signed char handle, unsigned char *buf, unsigned int nByte);/* writes the content in the buffer to the file identified by the input handle. It

12、 writes* to where the last write operation on the same file ended. If its the first time* the file is being written to, it starts from the beginning of the file.* pre nByte Unknown file system fileSys = 1- FAT16*/static unsigned char fileSys;/* number of files opened for reading*/ static unsigned ch

13、ar filesOpenedRead;/* number of files opened for writing*/static unsigned char filesOpenedWrite;/* FAT sector number last searched for an empty cluster*/ static unsigned long int last_FAT_sector_searched_for_empty;/* Local Prototypes */static void FATLibInit();static unsigned char identifyFileSystem

14、(unsigned char *buf);static signed char readFileSystemInfo(unsigned char *buf);static unsigned long findEmptyCluster(unsigned char *buf);static unsigned long getNextFAT(unsigned long cluster, unsigned char *buf);static void updateFAT(unsigned long oldCluster, unsigned long newCluster, unsigned char

15、*buf);static signed char getFileHandle();static unsigned long getFirstCluster(const char * pathname, unsigned long cluster, unsigned char*buf, boolean control, unsigned long * fileSize);static unsigned long createNewEntry( const char * entryName, unsigned long cluster, unsigned char *buf, boolean co

16、ntrol);static signed char findFileIndex(signed char handle);static signed char openedCheck(unsigned long cluster);static void updateDirectory(unsigned long dirCluster, unsigned long firstCluster, unsigned longfilesize, unsigned char *buf);static void updateAccessDate(unsigned long dirCluster, unsign

17、ed long firstCluster, unsigned char*buf);/* default constructor*/static void FATLibInit() unsigned char i;sectorZero = 0;filesOpenedRead = 0;filesOpenedWrite = 0;fileSys = UNKNOWN;for(i = 0; i BUFFER_SIZE;i+)openedReadi.fileHandle = -1;openedWritei.fileHandle = -1;last_FAT_sector_searched_for_empty

18、= 0; /* initialize the system*UNKNOWN file systemFAT16 file systemcould not set block lengthcould not initialize memory card* return 0* return 1* return 3* return 4*/ unsigned char fat_initialize(void)unsigned char bufSECTOR_SIZE;FATLibInit();HALayerInit();memCardInit(); if(memCardInit() if(setBLock

19、Length() /roger modi startsectorZero = getPartitionOffset();/sectorZero = 0;/rogermodi endelsereturn 3; else return 4;identifyFileSystem(buf);readFileSystemInfo(buf);return fileSys;/*identifies the file system used by the MMC/SD cardparambufthe buffer to be used to access the MMC/SD cardreturnreturn

20、for unknow file system for FAT16*/ static unsigned char identifyFileSystem(unsigned char *buf) readSector(0, buf);fileSys = UNKNOWN;if( (buf54 = F) &(buf55 = A) &(buf56 = T) &(buf57 = 1) & (buf58 = 6) fileSys = FAT16;return fileSys;/*initialize variables for file system* param buf the buffer to be u

21、sed to access the MMC/SD card* return 0* return -1* return -2variables sucessfully initialized unknown file system error reading from MMC/SD card*/static signed char readFileSystemInfo(unsigned char *buf)unsigned int maxRootEntry = 0;if(fileSys = UNKNOWN)return -1;if(readSector(0, buf) SECTOR_SIZE)r

22、eturn -2;sectorsPerCluster = buf13;reservedSectors = buf14;reservedSectors |= (unsigned long int)buf15 8;numOfFATs = buf16;if(fileSys = FAT16) maxRootEntry = buf17;maxRootEntry |= (unsigned long int)buf18 8;sectorsPerFAT = buf22;sectorsPerFAT |= (unsigned long int)buf23 8; else sectorsPerFAT = buf36

23、;sectorsPerFAT |= (unsigned long int)buf37 8;sectorsPerFAT |= (unsigned long int)buf38 16;sectorsPerFAT |= (unsigned long int)buf39 24;rootDirectory = reservedSectors + sectorsPerFAT * numOfFATs;rootSectors = ( maxRootEntry / SECTOR_SIZE ) * 32;dataStarts = rootDirectory + rootSectors;return 0;/* fi

24、nds an unoccupied cluster in the FAT and returns the location.* The previously empty location in the FAT will be changed to a* end of file marker* pre file system is properly initialized and fileSys != UNKNOWN* param buf* return 0* return .the buffer to be used to access the MMC/SD cardno more clust

25、ers available (disk full)location of empty cluster*/ static unsigned long findEmptyCluster(unsigned char *buf) unsigned long sector, end_sector;unsigned long returnCluster;unsigned int i;unsigned char j;if (last_FAT_sector_searched_for_empty = 0)sector = reservedSectors;elsesector = last_FAT_sector_

26、searched_for_empty;end_sector = reservedSectors + sectorsPerFAT;if(fileSys = FAT16)/ 256 entries in a 512-byte FAT sectorreturnCluster = (sector - reservedSectors) * 256;elsereturn 0;while (TRUE)while(sector end_sector) readSector(sector, buf);for(i = 0; i SECTOR_SIZE; i += 2*fileSys) if(bufi = 0x00

27、 & bufi+1 = 0) / This cluster is unused.bufi = 0xFF;bufi + 1 = 0xFF;for(j = 0; j reservedSectors) / Yes.sector = reservedSectors;end_sector = last_FAT_sector_searched_for_empty;last_FAT_sector_searched_for_empty = 0;returnCluster = 0;/ We will take the else block next time if we dont find anything.e

28、lsereturn 0;* gets the next cluster in the FAT chain given the current cluster *the current cluster* param bufthe buffer to be used to access the MMC/SD card* return .the location of the next cluster or end of file marker*/static unsigned long getNextFAT(unsigned long cluster, unsigned char *buf)uns

29、igned long sectorOffset;unsigned int byteOffset;cluster = 2 * cluster * fileSys;sectorOffset = (cluster 9);sectorOffset = sectorOffset + reservedSectors;byteOffset = cluster & 0x01FF;readSector(sectorOffset, buf);cluster = bufbyteOffset;cluster |= (unsigned long int)bufbyteOffset + 1 9);sectorOffset

30、 = sectorOffset + reservedSectors;byteOffset = oldCluster & 0x01FF;readSector(sectorOffset, buf);bufbyteOffset = newCluster & 0x000000FF;bufbyteOffset+1 = (newCluster & 0x0000FF00) 8;for(j = 0; j numOfFATs; j+) writeSector(sectorOffset + (j * sectorsPerFAT), buf);/* gets the next available file hand

31、le* Note: all files have unique handle (no overlap between read and write)* return -10no more file handles available* return .file handle*/ static signed char getFileHandle()signed char i = 0;signed char newHandle = 0;while(newHandle 2 * BUFFER_SIZE)i = 0;while(i = 0)if(openedReadi.fileHandle != -1)if(openedReadi.fileHandle=newHandle) i = -128;if(openedWritei.fileHandle != -1)&(i=0)if(openedWritei.fileHandle=newHandle)i = -128;i+;if(i = BUFFER_SIZE)return newHandle;newHandle+;return -#;/*return -1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1