A file system FatFs Moule that is easy to transplant and use

Source: Internet
Author: User

A file system FatFs Moule that is easy to transplant and use
[Date: 2008-10-23] Source: single chip microcomputer and Embedded System Application Author: Shenzhen University, Hong Yue Wei Wang baiming Xie chaoying [Font:Large Medium Small]

 

 

Introduction

With the development of information technology, the amount of information in today's society is growing, in the past, the system composed of single-chip microcomputer simply implemented address-based and byte-based reading/writing for storage media, which is not enough for people's practical application, therefore, using the file system to manage the storage media becomes a future development direction of the single chip microcomputer system. Currently, common file systems include Microsoft's FATl2, FATl6, FAT32, NTFS, and EXT2 and EXT3 in Linux. Due to the wide application of Microsoft Windows, among the current consumer electronic products, the most widely used are FAT file systems, such as USB flash drives, MP3, MP4, and digital cameras, therefore, it is very important for single-chip microcomputer system designers to find a FAT open source file system that is easy to transplant and use, occupies relatively small hardware resources, and has powerful functions.

FatFs Module is a completely free and open-source FAT file system Module designed specifically for small embedded systems. It is fully written in standard C language, so it has good hardware platform independence, it can be transplanted to the 8051, PIC, AVR, SH, Z80, H8, ARM and other series of single-chip microcomputer, with only a simple modification. It supports FATl2, FATl6, and FAT32 and supports multiple storage media. It has an independent buffer and can read/write multiple files, in addition, the 8-bit single-chip microcomputer and 16-bit single-chip microcomputer are specially optimized. FatFs Module has a simplified version of Tiny-FatFs. It differs from the full version of FatFs in two aspects:

① Less memory, as long as 1 kb ram;

② Only one storage medium is supported once.

The usage of FatFs is the same as that of Tiny-FatFs. It is very convenient to include different header files. This article mainly introduces Tiny-FatFs.

1 tiny-fatfs

1.1 preparations before transplantation

Fatfs module was designed to be used on different single-chip microcomputer at the beginning, so it has a good hierarchy, as shown in 1. The top layer is the application layer. The user does not need to care about the internal structure of the fatfs module and the complex fat Protocol. Instead, he only needs to call a series of application interface functions provided to the user by the fatfs module, such as f_open, f_read, f_write and f_close can be as simple as reading/writing files on a PC.

The middle layer fatfs module implements the FAT file read/write protocol. The full version of fatfs module provides ff. C and ff. h. The simplified version of tiny-fatfs provides TFF. C and TFF. h. Unless necessary, you generally do not need to modify it. You can directly include the header file of the required version in use.

The underlying interface provided by fatfs module is required for writing and porting code. It includes the storage media read/write interface diskio and the real-time clock for the creation and modification time of the supplied file.

The hardware platform for this transplantation uses the AVR Microcontroller and SD card of the ATmegal28 model. ATmegal28 is an 8-bit MCU with up to 4 kB RAM, 128 kb Internal flash and rich peripherals. The software platform is winavr, with strong code optimization capabilities and completely free of charge.

1.2 migration steps
1.2.1 write SPI and SD card interface code

This article uses the SD card SPI communication mode. The SD card Di is connected to MoSi, do is connected to miso, and CS is connected to SS. This requires ATmegal28 to provide the SPI read/write interface code, including initialization, read, and write. SPI initialization includes the initialization of SPI-related registers and related I/O Ports. Configure the SPI of Atmega 128 to host mode, first transmit high data, and set the clock rate to 1/2 system clock. The Code is as follows:

Spcr = (O <Spie) |
(1 <SPE) |
(O <DORD) |
(1 <MSTR) |
(O <CPOL) |
(O <CPHA) |
(O <SPR1) |
(O <SPRO );
SPSR | = (1 <SPI2X );

Configure the input/output of the I/O port. Configure the MOSI and Ss scripts as output, and the MISO script as input. Then, you can read/write data.

Code for reading 1 byte SPI interface:

Static BYTE rcvr_spi (void ){
SPDR = OxFF;
Loop_until_bit_is_set (SPSR, SPIF );
Return SPDR;
}
Write 1 byte SPI interface code:
Static void xmit_spi (BYTE dat ){
SPDR = dat;
Loop_until_bit_is_set (SPSR, SPIF)
}

Three basic interface functions are required to compile the SD card interface code based on the SPI read/write interface:

① Send one command to the SD card:
Static BYTE send-cmd (BYTE cmd, DWORD arg );
② Send one data packet to the SD card:
Static BOOL xmit-datablock (const BYTE * buff, BYTE token );
③ Receive 1 data packet from the SD card:
Static BCK] L rcvr-datablock (BYTE * buff, UINT btr );

1.2.2 write diskio
After compiling the interface code of the storage media, you can write DiskIO, as shown in DiskIO structure 2.

To port Tiny-FatFs, you need to write six interface functions:

① DSTATUS disk_initialize (BYTE drv );

Storage media initialization function. Because the storage medium is the SD card, it is actually the initialization of the SD card. DRV is the storage media number. Because tinv-fatfs only supports one storage media, DRV should always be o. 0 is returned for execution without error, and non-O is returned for error.
② Dstatus disk_status (byte DRV );

Status detection function. Checks whether the current storage media is supported. For tinv-fatfs, if DRV is 0, it is considered to be supported and then returns O.
③ Dresult disk_read (byte DRV, byte * buff, DWORD sector, byte. Count );

Read sector function. Based on the SD card read interface function, * buff stores the read data. The sector is the start sector of the Start read, and the count is the number of sectors to be read. One slice is 512 bytes long. If the execution is correct, the system returns O. if the error is not 0, the system returns 0.

④ Dresult disk_write (byte DRV, const byte * buff, DWORD sector, byte count );

Write sector functions. Based on the SD card writing interface function, * buff stores the data to be written. Sector is the start sector of the write operation. Count is the number of sectors to be written. One slice is 512 bytes long. If the execution is correct, the system returns O. if the error is not 0, the system returns 0.

⑤ Dresult disk_ioctl (byte DRV, byte Ctrl, voii * buff );

Storage media control functions. CTRL is the control code. * buff stores or receives control data. You can compile the function code you need in this function, such as obtaining the size of the storage media and checking whether the storage media is powered on or not. If it is a simple application, you do not need to write it. Return O.

⑥ DWORD get_fattime (Void );

Real-time clock functions. Returns a 32-bit unsigned integer. The clock information is contained in the 32-bit integer, as shown below:
Bit31: 25 years (O .. 127) from 1980 to the present
Bit24: 21 months (1... 12)
Bit20: 16 (1 .. 31)
Bitl5.1] (O .. 23)
Bitl0: 5 points (O .. 59)
Bit4: 0 s/2 (0 .. 29)

If you do not use a real-time clock, you can simply return a number. After correct writing of DiskIO, the porting is basically completed. The next step is to configure Tiny-FatFs.

2 tiny-fatfs Configuration

Tiny-FatFs is a configurable file system. You can select the desired function. Tiny-FatFs has five files, including tff. c, tff. h, diskio. c, diskio. h, and integer. h. Tff_c and integer. h generally do not need to be changed. The previous porting work mainly changed diskio. c, while the configuration of Tiny-FatFs mainly changed tff. h and diskio. h.

In diskio. h, you can enable disk-write or disk_ioetl as needed. Run the following code to enable disk_write and disk_ioctl:
# Define-R 'eadonly 0
# Define-USE_IOCTL 1

In tff. h, you can configure the entire file system as needed:

① # Define_MCU_ENDIAN. Values 1 and 2 are configurable. By default, values 1 are set to achieve better system performance. If the single-chip microcomputer is in the big-end mode or the system runs abnormally when it is set to 1, it must be set to 2.
② # Define_FS_READONLY. If it is set to 1, the read-only operation will be enabled, and all write operations involved in the file system will be removed during program compilation to save space.

③ # Define_FS_MINIMIZE. The options 0, 1, 2, and 3 are available. If 0 is set, all user functions provided by Tiny-FatFs can be used. If set to 1, f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, and f_rename are disabled; set 2 to disable f_opendir and f_readdir on the basis of 1; Set 3 to disable f_lseek on the basis of 1 and 2. Users can cut down as needed to save space.

3 tiny-fatfs read/write Test

Tiny-FatFs is powerful and provides rich and easy-to-use user interface functions, as shown in 3.

Tiny-FatFs is fully functional. This article only tests the f_mount, f_open, f_read, f_write, and f_close functions to read a 3.4 MB file and write a 1 MB file, the file names are testl. dat and test2.dat. The main code is as follows:

According to actual tests, it takes about 20 s to read a 11.059 MB file at a clock of 3.4 2 MHz, with an average of about 170 KB/S. It takes about 6 s to write a 1 MB file, the average speed is about 166 KB/s, which is quite satisfactory in the resource-limited single-chip microcomputer system. To sum up, the fatfs module has the advantages of being easy to transplant, powerful, and easy to use, and is suitable for small embedded systems. It is completely free and open-source and can also be used for educational and scientific research and its commercial purposes.

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.