FAT32 文件系统C语言接口.docx
《FAT32 文件系统C语言接口.docx》由会员分享,可在线阅读,更多相关《FAT32 文件系统C语言接口.docx(42页珍藏版)》请在冰豆网上搜索。
FAT32文件系统C语言接口
FATFileSystemModule
FatFsmoduleisanexperimentalprojecttoimplementaFATfilesystemtosmallembddedsystems.TheFatFsmoduleiswrittenincompliancewithANSIC,thereforeitisindependentofhardwarearchitecture.Itcanbeincorporatedintomost8-bitmicrocontrollers,suchas8051,PIC,AVR,H8,Z80andetc...,withoutanychange.Icreatedtwomodulesindifferentconfigurationsinconsiderationofvarioususe.
FeaturesofFatFsModule
1.SeparatedbufferforFATstructureandeachfile,suitableforfastmultiplefileaccsess.
2.Supportsmultipledrives/partitions.
3.SupportsFAT12,FAT16(+FAT64)andFAT32.(FAT64:
FAT16in64KB/cluster)
4.Supports8.3formatfilenameandNTlowercaseflag.(LFNisnotsupported)
5.Supportstwopartitioningrules:
FDISKandSuper-floppy.
6.Optimizedfor8/16-bitmicrocontrollers.
FeaturesofTiny-FatFsModule(differenttoFatFs)
1.Verylowmemoryconsumption,suitableforsmallmemorysystem.(RAM:
1KB)
2.Supportsonlysingledrive.
ApplicationInterface
FatFs/Tiny-FatFsmoduleprovidesfollowingfunctions.
∙f_mount-Register/UnregisteraWorkArea
∙f_open-Open/CreateaFile
∙f_close-CloseaFile
∙f_read-ReadFile
∙f_write-WriteFile
∙f_lseek-MoveR/WPointer
∙f_sync-FlushCachedData
∙f_opendir-OpenaDirectory
∙f_readdir-ReadaDirectoryItem
∙f_getfree-GetFreeClusters
∙f_stat-GetFileStatus
∙f_mkdir-CreateaDirectory
∙f_unlink-RemoveaFileorDirectory
∙f_chmod-ChangeAttribute
∙f_rename-Rename/MoveaFileorDirectory
∙f_mkfs-CreateaFileSystemontheDrive
f_mount
Thef_mountfucntionregisters/unregistersaworkareatotheFatFsmodule.
FRESULTf_mount(
BYTEDrive,/*Logicaldrivenumber*/
FATFS*FileSystemObject/*Pointertotheworkarea*/
);
Parameters
Drive
Logicaldrivenumber(0-9)toregister/unregistertheworkarea.Always0forTiny-FatFs.
FileSystemObject
Pointertotheworkarea(filesystemobject)toberegistered.
ReturnValues
FR_OK(0)
Thefunctionsucceeded.
FR_INVALID_DRIVE
Thedrivenumberisinvalid.
Description
Thef_mountfunctionregisters/unregistersaworkareatotheFatFsmodule.Theworkareamustbegiventothelogicaldrivewiththisfunctionbeforeusinganyfilefunction.Tounregisteraworkarea,specifyaNULLtotheFileSystemObject,andthentheworkareacanbediscarded.
Thisfunctiononlyinitializestheworkareaandregistersitsaddresstotheinternaltable,anyaccesstothediskI/Olayerdoesnotoccure.Actualmountingprocessisperformedinanyotherfilefuncitonswithpathnamewhenitisneeded.
References
FATFS
Return
f_open
Thef_openfunctioncreatesafileobjecttobeusedtoaccessthefile.
FRESULTf_open(
FIL*FileObject,/*Pointertotheblankfileobjectstructure*/
constchar*FileName,/*Pointertothefileneme*/
BYTEModeFlags/*Modeflags*/
);
Parameters
FileObject
Pointertothefileobjectstructuretobecreated.Afterthef_openfuncitonsucceeded,thefilecanbeaccessedwiththefileobjectstructureuntilitisclosed.
FileName
Pointertoanull-terminatedstringthatspecifiesthefilenametocreateoropen.
ModeFlags
Specifiesthetypeofaccessandopenmethodforthefile.Itisspecifiedbyacombinationoffollowingflags.
Value
Description
FA_READ
Specifiesreadaccesstotheobject.Datacanbereadfromthefile.
CombinewithFA_WRITEforread-writeaccess.
FA_WRITE
Specifieswriteaccesstotheobject.Datacanbewrittentothefile.
CombinewithFA_READforread-writeaccess.
FA_OPEN_EXISTING
Opensthefile.Thefunctionfailsifthefileisnotexisting.
FA_OPEN_ALWAYS
Opensthefile,ifitisexisting.Ifnot,thefunctioncreatesthenewfile.
FA_CREATE_NEW
Createsanewfile.Thefunctionfailsifthefileisalreadyexisting.
FA_CREATE_ALWAYS
Createsanewfile.Ifthefileisexisting,itistruncatedandoverwritten.
ReturnValues
FR_OK(0)
Thefunctionsucceededandthefileobjectisvalid.
FR_NO_FILE
Couldnotfindthefile.
FR_NO_PATH
Couldnotfindthepath.
FR_INVALID_NAME
Thefilenameisinvalid.
FR_INVALID_DRIVE
Thedrivenumberisinvalid.
FR_EXIST
Thefileisalreadyexisting.
FR_DENIED
Therequiredaccesswasdeniedduetoanyoffollowingreasons:
writemodeopenofafilethathasread-onlyattribute,filecreationunderexistingasamenamedirectoryorread-onlyfile,cannotbecreatedduetothedirectorytableordiskfull.
FR_NOT_READY
Thediskdrivecannotworkduetonomediuminthedriveoranyotherreason.
FR_WRITE_PROTECTED
Writemodeopenorcreationunderthemediumiswriteprotected.
FR_RW_ERROR
Thefunctionfailedduetoadiskerrororaninternalerror.
FR_NOT_ENABLED
Thelogicaldrivehasnoworkarea.
FR_NO_FILESYSTEM
ThereisnovalidFATpartitiononthedisk.
Description
Thecreatedfileobjectisusedforsubsequentcallstorefertothefile.Whencloseanopenfileobject,usef_closefunction.
Beforeusinganyfilefunction,workarea(filesystemobject)mustbegiventoeachlogicaldrivewithf_mountfunction.Allfilefunctionscanworkafterthisprocedure.
Themodeflags,FA_WRITE,FA_CREATE_ALWAYS,FA_CREATE_NEW,FA_OPEN_ALWAYS,arenotsupportedinread-onlyconfiguration.
Example(FileCopy)
voidmain()
{
FATFSfs;//Workarea(filesystemobject)forlogicaldrive
FILfsrc,fdst;//fileobjects
BYTEbuffer[4096];//filecopybuffer
FRESULTres;//FatFsfunctioncommonresultcode
WORDbr,bw;//FileR/Wcount
//Registeraworkareatologicaldrive0
f_mount(0,&fs);
//Opensourcefile
res=f_open(&fsrc,"srcfile.dat",FA_OPEN_EXISTING|FA_READ);
if(res)die(res);
//Createdestinationfile
res=f_open(&fdst,"dstfile.dat",FA_CREATE_ALWAYS|FA_WRITE);
if(res)die(res);
//Copysourcetodestination
for(;;){
res=f_read(&fsrc,buffer,sizeof(buffer),&br);
if(res||br==0)break;//errororeof
res=f_write(&fdst,buffer,br,&bw);
if(res||bw
}
//Closeallfiles
f_close(&fsrc);
f_close(&fdst);
//Unregisteraworkareabeforediscardit
f_mount(0,NULL);
}
References
f_read,f_write,f_close,FIL,FATFS
Return
f_close
Thef_closefunctionclosesanopenfile.
FRESULTf_close(
FIL*FileObject/*Pointertothefileobjectstructure*/
);
Parameter
FileObject
Pointertotheopenfileobjectstructuretobeclosed.
ReturnValues
FR_OK(0)
Thefileobjecthasbeenclosedsuccessfuly.
FR_RW_ERROR
Thefunctionfailedduetoadiskerrororaninternalerror.
FR_NOT_READY
Thediskdrivecannotworkduetonomediuminthedriveoranyotherreason.
FR_INVALID_OBJECT
Thefileobjectisinvalid.
Description
Thef_closefunctionclosesanopenfileobject.Ifanydatahasbeenwrittentothefile,thecachedinformationofthefileiswrittenbacktothedisk.Afterthefunctionsucceeded,thefileobjectisnolongervalidanditcanbediscarded.Ifthefileobjecthasbeenopenedinread-onlymode,itmaybediscardedwithoutclosingprocessbythisfunction.
References
f_open,f_read,f_write,f_sync,FIL,FATFS
Return
f_read
Thef_readfunctionreadsdatafromafile.
FRESULTf_read(
FIL*FileObject,/*Pointertothefileobjectstructure*/
void*Buffer,/*Pointertothebuffertostorereaddata*/
WORDByteToRead,/*Numberofbytestoread*/
WORD*ByteRead/*Pointertothevariabletoreturnnumberofbytesread*/
);
Parameters
FileObject
Pointertotheopenfileobject.
Buffer
Pointertothebuffertostorereaddata
ByteToRead
Numberofbytestoread
ByteRead
PointertotheWORDvariabletoreturnnumberofbytesread.
ReturnValues
FR_OK(0)
Thefunctionsucceeded.
FR_DENIED
Thefunctiondeniedduetothefilehasbeenopenedinwriteonlymode.
FR_RW_ERROR
Thefunctionfailedduetoadiskerrororaninternalerror.
FR_NOT_READY
Thediskdrivecannotworkduetonomediuminthedriveoranyotherreason.
FR_INVALID_OBJECT
Thefileobjectisinvalid.
Description
Thefilepointerinthefileobjectincreasesinnumberofbytesread.TheByteReadwillbecomelessthanByteToReadwhenthereadpointerreachedtoendofthefileoranyerroroccuredduringthereadoperation.
References
f_open,f_write,f_close,FIL
Return
f_write
Thef_writewritesdatatoafile.
FRESULTf_write(
FIL*FileObject,/*Pointertothefileobjectstructure*/
constvoid*Buffer,/*Pointertothedatatobewritten*/
WORDByteToWrite,/*Numberofbytestowrite*/
WORD*ByteWritten/*Pointertothevariabletoreturnnumberofbyteswritten*/
);
Parameter
FileObject
Pointertotheopenfileobjectstructure.
Buffer
Pointertothedatatobewritten.
ByteToWrite
Specifiesnumberofbytestowrite.
ByteWritten
PointertotheWORDvariabletoreturnnumberofbyteswritten.
ReturnValues
FR_OK(0)
Thefunctionsucceeded.
FR_DENIED
Thefunctiondeniedduetothefilehasbeenopenedinreadonlymode.
FR_RW_ERROR
Thefunctionfailedduetoadiskerrororaninternalerror.
FR_NOT_READY
Thediskdrivecannotworkduetonomediuminthedriveoranyotherreason.
FR_INVALID_OBJECT
Thefileobjectisinvalid.
Description
Theread/writepointerinthefileobjectisincreasedinnumberofbyteswritten.TheByteWrittenwillbecomelessthanByteToWritewhendiskgetsfullduringwritefunction.Thisfunctionisnotsupportedinread-onlyconfiguration.
References
f_open,f_read,f_close,FIL
Return
f_lseek
Thef_lseekfunctionemovesthefileread/writepointerofanopenfileobject.
FRESULTf_lseek(
FIL*FileObject,/*Pointertothefileobjectstructure*
DWORDOffset/*File