Detailed description of fatfs File System-attached with porting suggestions

Source: Internet
Author: User

Recently developed SPI flash, I was planning to create a file system. Since jffs, yaffs, and trueffs have been used before,CodeThe amount is quite large. This time I want to find the amount of code that is not so scary. After learning about it, I heard that the configuration will be relatively complicated. Finally, fatfs is selected. The code size is small enough. The latest r0.09 version has only one. c file (of course, there is also a bottom-layer file to be written by yourself, the option folder is ignored), the old version will be smaller. In addition, if the number of users is large enough, you can select it. Although it cannot be transplanted to VxWorks due to hardware or project reasons, it must be recorded.

HereHttp://elm-chan.org/fsw/ff/00index_e.htmlDownload the source code, only over 800 K, poor, you can also download the exampleProgramHas implemented solutions on multiple platforms such as AVR, Win32, and LPC. Open the SRC folder. An optionfolder contains 00readme.txt, diskio. H, FF. C, FF. H, ffconf. H, and interger. h. The files to be modified during transplantation mainly include ffconf. h and interger. h. The latter is modified when its definition conflicts with that on the target platform or is used improperly.

Before making specific changes, read the fatfsSource codeYou can read integer first. h. Understand the data type, and then ff. h. Understand the data structure and various function declarations used by the file system, and then diskio. h. Learn about media-related data structures and operation functions. The ff. c file is relatively large. You can roughly scan the implemented functions at the end, and then read the relevant code carefully based on the order in which the application layer program calls the functions. You can use NotePad to open all files, which is very convenient. Several structs in ff. h are very important. They are listed as follows:

/*  File System Object Structure (fatfs)  */  Typedef  Struct  {Byte fs_type;  /*  Fat subtype, which is generally used for mounting. If it is set to 0, it indicates that the object is not mounted.  */  Byte DRV;  /*  Physical drive number, generally 0  */  Byte csize;  /* Number of sectors of each cluster (128, 4)  */  Byte n_fats;  /*  Number of file allocation tables (1, 2)  */      /*  The fat file system is followed by the Boot Sector, two file allocation tables, the root directory area and the data area.  */  Byte wflag;  /*  Indicates whether the file has been changed. If it is set to 1, it must be written back.  */  Byte fsi_flag;  /* Indicates whether the file system information has been changed. If it is set to 1, it must be written back.  */  Word ID;  /*  File System Mount ID  */  Word n_rootdir;  /*  Number of entries in the root directory (for fat12/16)  */  # If _ Max_ss! = 512 Word ssize;  /*  The number of bytes per slice (used for flash with a sector greater than bytes)  */ # Endif  # If _ Fs_reentrant _ Sync_t sobj;  /*  Allows re-entry, that is, defining synchronization objects, used in tiny  */  # Endif  # If ! _ Fs_readonly DWORD last_clust;  /*  Last allocated Cluster  */  DWORD free_clust;  /*  Number of idle Clusters */  DWORD fsi_sector;  /*  Sector for storing fsinfo (for FAT32)  */  # Endif  # If _ Fs_rpath DWORD cdir;  /*  Allows relative paths to store the starting cluster of the current directory (0: Root)  */  # Endif  DWORD n_fatent;  /*  Number of fat entries (number of clusters + 2) */  DWORD fsize;  /*  Sector occupied by each fat  */  DWORD fatbase;  /*  Fat start sector  */  DWORD dirbase;  /*  Root directory start sector (FAT32: Cluster #)  */  DWORD database;  /* Start sector of the Data Directory  */  DWORD winsect;  /*  Fan ID stored in the Current Buffer  */  Byte win [_ max_ss];  /*  Cache for a single sector  */  } Fatfs; 

 Then there is the relevant file and folder structure, with a specific note:

 /*  File object structure (FIL)  */  Typedef Struct  {Fatfs * FS; /*  FS pointer  */  Word ID;  /*  FS Mount ID  */  Byte flag;  /*  File status  */  Byte pad1;  /* I do not know the meaning, nor have I seen the program use  */  DWORD fptr;  /*  File read/write pointer  */  DWORD fsize;  /*  Size  */  DWORD sclust;  /*  File start cluster (0 when fsize = 0)  */  DWORD clust;  /* Current Cluster  */  DWORD dsect;  /*  Current data sector  */  # If ! _ Fs_readonly DWORD dir_sect;  /*  Sector that contains directory items  */  Byte * Dir_ptr; /*  Ponter to the directory entry in the window  */ # Endif  # If _ Use_fastseek DWORD * Cltbl; /*  Pointer to the cluster link ing table  */  # Endif  # If _ Fs_share Uint lockid;  /*  File lock ID (index of file semaphore table)  */  # Endif  # If ! _ Fs_tinyByte Buf [_ max_ss];  /*  File data read/write buffer  */  # Endif  } Fil; 

The following is the directory:

 /*  Directory object structure (DIR)  */  Typedef  Struct  {Fatfs * FS; /*  Same as above  */ Word ID; word index;  /*  Current read/write index number  */  DWORD sclust;  /*  File data zone start Cluster  */  DWORD clust;  /*  Current Cluster  */  DWORD sect;  /*  Current Sector  */ Byte * Dir; /*  The current SFN entry pointer in the slice cache. SFN has unknown meaning. It is similar to LFN and is related to the file name.  */  Byte * FN; /*  Pointer to the SFN (in/out) {file [8], ext [3], status [1]}  */  # If _ Use_lfn Wchar * LFN; /*  Pointer to the LFN working Buffer  */ Word lfn_idx;  /*  Last matched LFN index number (0 xFFFF: No LFN)  */  # Endif  } Dir; 

Other interfaces such as f_mount and f_open are not described in detail. The chk_mounted function is actually used during mounting. Here, the information about the Mount partition is allocated to the fatfs struct; there is also a get_fat function, which is also important. It is useful in f_open and many directory operation functions, and the expression of the fat entry is also very obscure, and it calls a move_window function, it is also very obscure, probably because my English is too bad. In fact, move_window is used to change the current working sector of the file system. If it is to be migrated to the current sector, it will be directly returned. If not, it will write back the original sector. If it is a fat table, it must also be written into the backup area.

Now that you are familiar with the code structure, you can modify the file system features related to hardware in the ffconf. h file configuration. Then, you can add a set of underlying operations by yourself. Take a look at ffconf. H, which defines many macros. You can configure them one by one as needed:

First look at the function Configuration:

_ Fs_tiny: whether the file system is standard or micro. The default value is standard (0 );

_ Fs_readonly: whether the file system is read-only. The default value is read/write (0). If read-only, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename, f_truncate, and f_getfree are unavailable;

_ Fs_minimize: Specifies the file system cropping function. The default value is all functions (0). If the value is 1 or 2, most of the links and directories are removed;

_ Use_strfunc: whether to allow string operations. The default value is not allowed (0). Depending on your needs, it is generally set to 1. If you work in windows, we recommend that you set this parameter to 2 to ensure file compatibility (for example, line breaks '\ n' and carriage returns' \ R;

_ Use_mkfs: whether to allow the f_mkfs function. The default value is 0. It is used to create folders. We recommend that you enable this function;

_ Use_forward: used to allow the f_forward function. This function is used only when the tiny file system is enabled. It is used to immediately transfer read and write data to the data stream to save Ram space;

_ Use_fastseek: whether to enable the quick index. The default value is 0. When enabled, the cltbl element in the fil struct is used to accelerate the search;

_ Code_page: Specifies the OEM code page used by the target system. The default value is Japanese (932), which is changed to simplified 936 Chinese. What does OEM mean? In OS encoding, Unicode is a double-byte character encoding, Which is unified to 2 bytes in both Chinese and English, or other languages. It is consistent with any existing encoding (ASCII, GB, etc) are not compatible. The WindowsNT (2000) kernel uses this encoding. All data is converted to Unicode before entering the kernel. After exiting the kernel, it is converted to version-related encoding (usually called OEM, in the Simplified Chinese version, it is GB );

_ Use_len, _ max_len, and _ lfn_unicode: these three parameters are not clear, but are definitely related to long file names. We do not recommend that you enable them. Otherwise, you need to add more functions, which is troublesome;

_ Fs_rpath: Indicates whether to allow relative paths. If this option is selected, it will not be enabled. Otherwise, the logic will become complicated and the amount of code will increase;

Check the hardware configuration again:

_ Volumes: Number of logical volumes on the disk (flash). The default value is 1. It is not recommended to modify it;

_ Max_ss: slice size. The default value is 512 bytes. The maximum value is 4096 bytes;

_ Multi_partition: partition option. The default value is 0, which is a partition. You can set it if you want multiple partitions;

_ Use_erase: whether to allow sector erasure. The default value is 0. If this parameter is enabled, add the erasure command code to the disk_ioctl function;

Finally, the File System Configuration:

_ Word_access: indicates the progressive data format. The default value is 0, that is, the progressive data is in bytes, which is more compatible. If your system's latest unit is word (2 bytes), it can be set to 1;

_ Fs_reentrant, _ fs_timeout, and _ sync_t: these three options are related to whether the file system allows re-entry. Simply put, they are whether they can be simultaneously accessed by multiple threads, such as in RTOs, it is generally recommended to enable this function. _ sync_t can be defined as the operation object in the corresponding OS, handle in windows, OS _event in UCOS, and semaphore in VxWorks. In addition, you need to add the ff_req_grant, ff_rel_grant, and ff_del_syncobj functions after enabling them. In fact, the function is to apply for mutex, release mutex, and delete mutex, you can define the OS encapsulation;

_ Fs_share: similar to the above, it indicates the maximum number of files allowed to be opened at the same time in the file system. The default value is 0, that is, only one file can be opened.

Contains the format of the underlying function interface and description of each parameter. As for the underlying driver, I only used SPI flash. For details, refer to my previous article.Article. Note that the parameter sector in the underlying read/write function refers to the serial number of the slice and needs to be converted into the byte location in the driver interface.

At this point, the transplantation is basically completed. If ld_word (PTR) (Word) (* (word *) (byte *) (PTR) appears in your file system )) if there is a problem (Data Exception terminates data abort exception and so on), please search"To learn more about fatfs file system migrationYou can solve the problem. There are detailed solutions.

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.