Flash file system implementation principle

Source: Internet
Author: User
Tags constant definition

Flash file system implementation principle
Http://www.dpj.com.cn/web site

Abstract: In the in-depth analysis, Ti provides the RTS for DSP development. lib (RTS. SRC is the source code), this paper introduces the operation methods for custom files and devices, designs a simple FLASH file system

It facilitates application programming.

Keywords: dsp cc/CCS flash File System

1 Overview

Some data files are often processed during DSP application development. These data files can be a collection of data actually collected, or a collection of data generated by simulation software.

Stored as files on the host disk. The general development environment (such as TI's CCS and CC) provides the ansi c standard operating file format, such as opening a file fopen ("drive letter:/path/file name ",

"Open Mode "). Embedded systems generally use flash plug-ins. We hope to be able to perform flash read/write timing like reading and writing host disk files, so that application programmers can focus on solving practical problems.

To provide a good programming interface. In addition, in systems that require keyboards, serial ports, and other devices, you also want to provide a simple API interface. For example, to obtain a key from the keyboard, you only need to do the following:

After fopen ("keyboard", "read") is executed, you can use the fread function to read a character.

Based on the DSP development environment CC/CCS provided by Ti (CC for 3x series, CCS for 5x and 6X series) and actual development experience, the solution to the above problems is provided, and successfully applied to our products.

2 cc/CCS File Operation Mechanism

Ti provides a Development Environment Code Composer for its tms320c3x series DSP. The C language compiler provides standard file operations. In the debug environment

The operations of parts are completed by communicating with the host in the Standard ANSI file operation format. Ansi c I/O operations are divided into three levels: high level, low level, and device level. In high level

, The standard interface is fopen, fwrite, and other functions, while the low level is open, write, and other functions. These three levels are implemented using three tables-file table, stream table (essentially memory buffer index), and

Device table. The basic attributes such as opening and closing of a file are reflected in the file table. When a file is opened, an information unit describing the file is added to the file table. Similarly, when a file is closed

The information unit of the file is deleted from the file table. The stream table provides buffer operations on files, and the buffer location and size are all recorded in the stream table. A file corresponds to a stream, that is, a buffer. For

Read/write refers to the read/write to the buffer zone. When the buffer zone is filled up, it is written to flash and other devices at a time. This avoids frequent flash operations and prolongs the service life of flash. Devices include Flash, hard disks,

The keyboard is displayed in the device table. Multiple Streams can correspond to one device. For example, multiple files can be opened in flash, but one device cannot correspond to multiple streams. Stream operations are closely related to device operations.

. When you open a file, it also shows the device on which the file is operated and distributes a stream. Subsequent operations on the file will be completed through the driver function of the specific device corresponding to the stream. Host

Any peripherals of target can be added to the device table.

Code Composer performs operations on the host disk file by integrating with the host development environment. The RTS. Lib provided by TI provides two functions to communicate with the host, and the writemsg () function sends

Data and parameters are sent to the host. The readmsg () function reads data from the host to the target machine. Code Composer interacts with the host. With the support of the host file system, the Read and Write requests of specific physical addresses are blocked.

Question. In the debugging phase, to create files, read files, and store data on the host, you only need to use standard ansi c functions to perform operations, which greatly facilitates programming debugging.

3. Implementation of the flash File System

Embedded file systems generally have a centralized file system. Storage space usage information is stored somewhere in the memory, such as dos fat and Unix inode tables. Linear file system, also known as continuous files

System. All information related to each file is stored in the memory continuously. Compared with a centralized file system, it provides simpler implementation and faster reading and writing, especially storing key file systems. Log File System

Write modifications to the file system in a unified order, like logging, to accelerate file writing and crash repair. A unique log structure is used. A log contains index information, names, and data. Embedded Systems cannot work hard.

Disks are generally based on flash memory.

3.1 flash features and corresponding processing

The read operation of Flash is the same as that of normal Ram, but the write and erase operations have their own characteristics. The same address cannot be written twice at the same time, and time-consuming erasure is required. There are three ways to execute Erasure:

: One is the part erasure, that is, all the content is erased at a time (this is equivalent to the formatting function, which can be used for the first time); the other is the block erasure; the third is the sector erasure. Take sst39vf400a

For example, the block size is 32 KB, the slice size is 2 kb, and the block is erased once. The slice is similar. If the content of a file is modified and the content is less than one slice

All content of this slice must be overwritten when a new file is created. All content of this slice must be erased Before rewriting. Therefore, flash-based file systems cannot fully use existing file systems

. The smaller the flash can erase, the smaller the file changes, and the smaller the I/O operations executed, thus reducing the I/O time, provides real-time file system performance. We use

The slice size of sst39vf400a is 2 kb, that is, 2048b (1 k = 1024 ). Define with constants, # define fileunit 2048.

3.2 hierarchy of FLASH file systems

In contrast to the ansi c standard, Flash file systems are divided into three layers. Level 1: API layer. The API layer is the interface between the file system and the user application. It contains a letter related to the file function.

Number library, such as fs_fopen and fs_fwrite, is also equivalent to the High Level layer. Level 2: file system layer, that is, low level layer. Whether the processing file in this layer exists, opens, closes, and assigns a phase for the file

Cache. This layer calls the underlying driver. The device level layer is the device driver layer. The actual read/write operations of Flash are performed at this layer. A specific flash memory corresponds to a specific read

Write a program.

3.3 design of the Flash file information table

This table stores the attributes of existing files in flash. The flash size and file attributes are all reflected in this table. The table is updated synchronously with the content in flash, that is, when the minimum block of a file is updated

In flash.

Flash space allocation:

① Flash space, in the unit of cluster, reading and writing are both a cluster, that is, a Sector Unit;

② 0 clusters allocate tables to files, which are not occupied by application files;

③ Each time the file system is initialized, the content of the 0 clusters in Flash is read to the memory and saved in the array fat16.

Constant Definition

# Define cluster_block_size 2048 // number of bytes per cluster

# Define number_of_cluster_in_fat16 25

// The total number of clusters in the file allocation table

# Define number_of_file_buf 10

// A total of several file Buffers

# Define mode_open_file_read 0x01 // read (File opening Mode)

# Define mode_open_file_write 0x02 // write (File opening Mode)

# Define max_size_of_fiel 2048 // Maximum File Size

File structure:

Typedef struct {

Unsigned int islock: 1; // whether the file is locked, = 0 is not opened; = 1 is opened. This flag is only used in the first cluster of the file

Unsigned int status: 7; // cluster status, = 0. This cluster is color, not used; = 1, this cluster is the first cluster; = 2, this cluster is not the first cluster

Char filename [8]; // file name, valid in the first cluster

Char fileexname [3]; // file extension, valid in the first cluster

Unsigned int sizeoffile; // The number of bytes in the file, which is valid in the first cluster.

Unsigned int nextcluster; // cluster number of the next cluster. If it is 0xffffffff, it indicates that this is the last cluster of the current file.

} Flashfat;

File handle struct:

Typedef struct {

Unsigned int buffer [cluster_block_size]; // File Buffer

Unsigned int fileblock; // location of the current file cluster

Unsigned int filemode; // enable the supported mode.

Unsigned int filebufnum; // number of bytes that have been/written in the File Buffer

Unsigned int filecurpos; // The current location where the file is read and written.

Unsigned int filesize; // File Size

} Flashfile;

3.4 device level driver Function

The sst39vf400a standard device-level driver function is as follows:

Void program_one_word (word srcword, word far DST) {/* write a word */

Word far * temp; word far * sourcebuf; word far * destbuf;

Int index; destbuf = DST;

Temp = (word far *) 0xc0005555;/* set the address to c000: 555 H */

* Temp = 0 xaaaa;/* write data 0xaaaa to this address */

Temp = (word far *) 0xc0002aaa;/* set the address to c000: 2aaah */

* Temp = 0x5555;/* write data 0x5555 to this address */

Temp = (word far *) 0xc0005555;/* set the address to c000: 5555 H */

* Temp = 0xa0a0;/* write data 0xa0a0 to this address */

* Destbuf = srcword;/* Transfer byte to destination address */

Check_toggle_ready (destbuf);/* Wait For togglf bit to be ready */

}

For the source code, see www.dpj.com.cn.

3.5 workflow of FLASH file system

Before using the Flash file system, add the Flash ROM device to the device table (assuming that there are no files in Flash) and read the Flash file table. The following describes the system workflow.

(1) Add a flashrom Device

Add_device ("flashrom", _ MSA, flash_open, flash_close, flash_read, flash_write, flash_lseek,
Flash_unlink, flash_rename );

Among them, flash_open, flash_close, flash_read, flsh_write, flash_lseek, flash_unlink, and flash_rename are the lowest layers.

The name of the flash drive function. Different driver functions are required for different flash.

Int flash_open (char * path, unsigned flags, int fno );

Int flash_close (INT fno );

Int flash_read (INT fno, char * buffer, unsigned count );

Int flash_write (INT fno, char * buffer, unsigned count );

(2) initialize the File System

Before using flash, you must initialize it. Initialize the Temporary File Buffer and read various Flash information into the system, such as the flash size, name, size, and creation date of the existing file.

To use flash correctly.

Init_efs ();/* initialize the file system function */

(3) execute various file operations

To open a file in flash, run fopen ("flashrom: // path/file name", "open mode. When opening a file, check whether the file exists in the file table. If no

If yes, check whether the file exists in the Flash file table. If yes, open the file. If no, create a new file and open the file at the same time. Then you can read, write, and append files,

Attribute Modification and other operations.

Key Technical Points of the Flash file system:

① Use the advanced layer file operation function of the RTS. Lib (TI comes with the source code of RTS. SRC. This library has handled high-level file application issues according to ansi c standards. We can make

Use various file operation functions. The difference is to change the drive letter to the Flash ROM drive letter. For example, change fopen ("C:/read.txt", "R") to fopen ("flashrom:/read.txt", "R "). Operate in this mode

Flash, tedious timing processing, fan-area erasure, and other repetitive issues, you can focus on application programming.

② The self-designed low-level code is used to take over the lower-layer processing of RTS. Lib. The aforementioned flash file information table is the core. Only through this table can we know what is in flash and where to operate it. When

When operating a file at the API layer, the high-level function calls the corresponding number of underlying processing types to determine whether the file is opened, whether the file can be read or written, and other attributes at the low level. At the same time, an internal buffer is allocated to the file.

File Operations first operate the buffer, that is, stream operations. When the buffer zone is full, the called Operation first operates the buffer zone, that is, the stream operation. When the buffer is full, call the device level function to write data to flash.

. Similarly, when reading a slice, read the content of a slice first, and then read and remove the content of a slice.

Operating on the keyboard and other peripherals is much simpler than Flash, without the need to design a file information table. You can execute two steps. First, add the device and call add_device (......) Function. Enter the device name.

Is to write a device driver function and pass the corresponding function name as a parameter to add_device. It should be noted that the actual meanings of different devices and the same operation names are different. For example

It means to read one character, so it can be flexibly processed in practice.

Conclusion

The Flash file system implements basic file read/write functions, but there are still some shortcomings: file sharing is not solved, and file loss may occur when the power is down. Because we developed this flash

The purpose of the system is to facilitate programming and debugging. In our application field (Power System relay protection), the probability of power loss is very low, the stored files are mainly set values, control words (not many modifications), and

Fault filtering records. Even if the data is lost, it will not cause catastrophic consequences. Therefore, the system can meet our application requirements as a whole.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/nogodoss/archive/2009/06/05/4243490.aspx

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.