1. Overview
FAT32 is one of the Windows system hard disk partition formats, with a maximum single file size of 4GB.
The FAT32 consists of the following 3 parts:
Mbr:master Boot Record, 512KB, physical 0 address of the hard disk to 0X55AA end partition;
Fat:file Allocation table, 512*2kb, 32-bit file allocation table, maximum single file size 4GB, to 0X55AA end partition;
File and directory data: a directory area.
For example, a 42B file would occupy 2k=4 Sector (512KB).
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/8B/E7/wKioL1hb8tOCFTdlAADCIpodqWs929.jpg "title=" Example_2k.jpg "alt=" Wkiol1hb8tocftdlaadcipodqws929.jpg "/>
2. Introduction of Fatfs Source code
FATFS is a free, open-source FAT file system that is especially suitable for small embedded devices andFATFS supports fat12/fat16/fat32.
FATFS File system structure:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/EB/wKiom1hb83iC2wPBAAAV68bUFyA597.png "title=" Fat_ Arch.png "alt=" Wkiom1hb83ic2wpbaaav68bufya597.png "/>
Fatfs source files are as follows:
|
function |
|
ffconf.h |
Fatfs module profile |
need to configure parameters as required |
ff.h |
Fatfs and Application module Common header file |
not modified |
FF.C |
Fatfs module source code |
not modified |
diskio.h |
Fatfs and disk I/o module common header file |
not modified |
diskio.c |
Fatfs and disk I/o module interface Layer file |
platform-related code, required to write functions based on media |
Integer.h |
data type definition |
related to compiler |
Option folder |
Optional external features |
For example, to support kanji needs to be modified |
3. Fatfs Source Porting
Three steps for Fatfs transplantation
Data type: Define the type of data in Integer.h
Configuration: Configure Fatfs related functions via ffconf.h
function writing: In DISKIO.C to write the underlying driver, generally need to write 5 function disk_status, disk_initialize
Disk_read, Disk_write, Disk_ioctl, Get_fattime
In Ffconf.h, the main modifications support the full extent of functions, supported font formats, and so on.
#define _FS_MINIMIZE0/* This option defines minimization level to Remove some basic api functions.// 0: all basic functions are enabled./ 1: f_stat (), f_getfree (), f_unlink (), f_mkdir (), f_truncate () and f_rename ()/ are removed./ 2: f_opendir (), f_readdir () and f_closedir () are removed in Addition to 1./ 3: f_lseek () function is removed in addition to 2. */#define _CODE_PAGE1/* This option specifies the oem code page to be used on the target system./ Incorrect setting of the code page can cause a file open failure.// 1 - ascii (no extended character. non-lfn cfg. only)/ 437 - U.S./ 936 - Simplified Chinese (DBCS) */ #define_USE_LFN0 #define_max_lfn255#define_lfn_unicode0/* this option switches character encoding on the API. (0:ansi/oem or 1:utf-16)/ to use unicode string for the path name, enable lfn and set _ lfn_unicode = 1./ this option also affects behavior of string i/o functions. */
In diskio.c, modify the corresponding driver (SPI Low-level driver View Blog "NIOS2 essay--SD card SPI operation
"), the modified code is as follows:
filename: diskio.c//author: shugen.yin//date: 2016.12.22/ /function: fatfs lower layer api#include "Diskio.h"/* fatfs lower layer api */#include "sd_spi.h"//Initialize Disk dstatus disk_initialize (byte pdrv) {U8 res=0; res = sd_initialize ();//sd_initialize () if (res) return sta_noinit;else return 0; //initialization succeeded} //get disk status dstatus disk_status (BYTE pdrv/* physical drive nmuber (0..) */) { return 0;} DRESULT disk_read (byte pdrv,/* physical drive nmuber (0 ...) */BYTE *buff,/* Data buffer to store read data */DWORD sector,/* sector address (LBA) */UINT count/* Number of sectors to read (1..128) */) {U8 RES=0; &Nbsp; if (!count) Return res_parerr;//count cannot be equal to 0 or return parameter error res=sd_readdisk ( Buff,sector,count); //handles the return value, spi_sd_driver.c the return value to FF.C return value if (res== 0x00) return res_ok; else return res_error; } #if _USE_WRITEDRESULT disk_write (byte pdrv,/* physical drive nmuber (0 ...) */const BYTE *buff,/* Data to be written */DWORD sector,/* sector address (LBA) */uint count/* number of sectors to write (1..128) */) {u8 res=0; if (!count) return RES_ Parerr;//count cannot be equal to 0, otherwise the return parameter error res=sd_writedisk ((u8*) buff,sector,count); // Processes the return value, turning the return value of SPI_SD_DRIVER.C to the return value of FF.C if (res == 0x00) return res_ok; Else return res_error;} #endif #if _use_ioctldresult disk_ioctl (Byte pdrv,/* physical drive nmuber (0 ...) */byte cmd,/* control code */void *buff/* buffer to send/receive control data */) {dresult res; switch (cmd) { case ctrl_sync:sd_cs_set; if (SD_ Waitready () ==0) res = res_ok; else res = RES_ERROR; SD_CS_CLR; break; case get_sector_size: * (WORD*) Buff = 512; res = RES_OK; break; case get_block_size: * (word*) buff = 8; res = RES_OK; break; case get_sector_count: * (DWORD*) buff = SD_ Getsectorcount (); res = res_ok; break; default: res = RES_PARERR; break; } return res;} #endifDWORD get_fattime (void) { return 0;} //dynamically allocates Memory void *ff_memalloc (uint size) {return (void*) size;} Free memory void ff_memfree (VOID* MF) {}
4. Build the hardware and software environment
Here is the "NIOS2 essay--SD Card SPI operation" in the project, the TF card (2GB) borrowed SD card sleeve inserted into the SD card holder, as shown in:
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/8B/E7/wKioL1hb-KOwxx1gAAFxJFajvtI387.jpg "title=" Board_ Fat.jpg "width=" "height=" 338 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:450px;height:338px; "alt=" Wkiol1hb-kowxx1gaafxjfajvti387.jpg "/>
Write the main function and run the final compilation: The terminal displays the file write success
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/8B/E7/wKioL1hb-Q_ScwcDAAB5gO7b4VI482.jpg "title=" Fatfs_ Console.jpg "alt=" Wkiol1hb-q_scwcdaab5go7b4vi482.jpg "/>
5. Final results
Use the card reader to open the TF card, HELLO. TXT file is generated and the contents of the open file are written correctly.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/E7/wKioL1hb-c2Q2bm5AABvmf9o2T4889.png "title=" RESULT _fatfs.png "width=" "height=" 249 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:500PX;HEIGHT:249PX; "alt=" Wkiol1hb-c2q2bm5aabvmf9o2t4889.png "/>
This article is from the "Shugenyin blog" blog, make sure to keep this source http://shugenyin.blog.51cto.com/4259554/1885330
NIOS2 Essay--fat32 File system