Implementing YAFFS file system on C51 system

Publisher:omega34Latest update time:2007-01-18 Source: 单片机及嵌入式系统应用Keywords:NAND Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
With the popularity of NAND Flash memory as a large-capacity data storage medium, the NAND flash memory-based file system YAFFS (Yet Another Flash File System) is gradually being applied to various embedded systems. This article will elaborate on the implementation process of YAFFS file system on C51 system.

1 Characteristics of NAND Flash

Non-volatile flash memory Flash has the characteristics of fast speed, low cost and high density, and is widely used in embedded systems. There are two main types of Flash memory: NOR and NAND. The NOR type is more suitable for storing program code; the NAND type can be used for large-capacity data storage. The storage units of NAND flash memory are blocks and pages. The Samsung K9F5608 used in this article includes 2,048 blocks, each block includes 32 pages, and the page size is 528 bytes, which is divided into two 256-byte data areas, and finally a 16-byte spare space.

K9F5608 has the following characteristics: Read/write operations are performed in page units, while erase operations are performed in block units. Read, write, and erase operations are all completed through commands; byte erasure is not possible and needs to be performed before each rewrite operation. Erase a whole block; there is a certain proportion of bad blocks when leaving the factory; the number of erases for each block is limited, about 100,000 times [1].

2 Introduction to YAFFS file system

YAFFS is the first embedded file system specifically designed for NAND Flash memory, suitable for large-capacity storage devices; and is released under the GPL (General Public License) agreement, and the source code is available for free on its website.

In YAFFS, files are stored in fixed-size data blocks, and the block size can be 512 bytes, 1024 bytes, or 2048 bytes. This implementation relies on its ability to associate a data block header with each data block. Each file (including directories) has a data block header corresponding to it. The data block header stores ECC (Error Correction Code) and file system organization information for error detection and bad block processing. Taking full consideration of the characteristics of NAND Flash, YAFFS stores this data block header in the 16-byte spare space of Flash. When the file system is mounted, the file system information can be read into the memory by simply scanning the spare space of the memory, and resides in the memory. This not only speeds up the loading speed of the file system, but also increases the speed of file access. Increased memory consumption.

In order to save memory while improving the search speed of file data blocks, YAFFS uses a more efficient mapping structure to map file locations to physical locations. The data segments of the file are organized into a tree structure. This tree structure has 32-byte nodes. Each internal node includes 8 pointers to other nodes. The leaf nodes include 16 2-byte pointers to physical addresses. . When YAFFS rewrites a file, it always writes new data blocks first, and then deletes the old data blocks from the file. In this way, even if the power fails unexpectedly when modifying the file, only the smallest writing unit of the modified data will be lost, thereby achieving power failure protection and ensuring data integrity.

Combining the efficiency of the greedy algorithm and the averageness of random selection, YAFFS achieves the purpose of balancing loss average and reducing system overhead. When certain small probability conditions are met, an attempt is made to randomly select a recyclable page; in other cases, a greedy algorithm is used to recycle the "dirties" blocks [2].

The YAFFS file system is designed according to the hierarchical structure and is divided into the following four parts: yaffs_guts.c, the main algorithm of the file system. This part of the code is completely written in portable C language; yaffs_fs.c, the interface of the Linux VFS layer; NAND Interface, packaging layer between yaffs_guts and NAND memory access functions, such as calling the Linux mtd layer or RAM simulation layer; portable functions, packaging functions of services. The most important point is that in order to obtain better portability, YAFFS provides a direct call mode, which gives us the opportunity to transplant the YAFFS file system on the C51 system.

3 Transplantation process

  The direct source code can be obtained at http://www.aleph1.co.uk/cgi-bin/viewcvs.cgi/ , including the following files and their header files.

  ◆ yaffscfg.c: Set various device parameters and system parameters.
  ◆ yaffsfs.c: Mainly implements directly called interface functions, such as opening files, writing files, and closing files, etc. Just include its header file in the application when using it.
  ◆ yaffs_flashif.c: NAND Flash operation function interface, which is the underlying function that directly operates the memory. For testing, this file uses RAMDISK simulation method to realize the operation of Flash memory. In practical applications, it is necessary to modify the definition of Flash hardware operation functions, including yflash_EraseBlockInNAND(), yflash_WriteChunkToNAND(), yflash_ReadChunkFromNAND() and yflash_InitialiseNAND().
  ◆ yaffs_guts.c: The main implementation algorithm of YAFFS file system.
  ◆ nand_ecc.c:: ECC algorithm.
  ◆ yaffs_ramdisk.c:: RAMDISK support code.
  ◆ yaffs_fileem.c: Use a file on the host to simulate Flash memory, only for testing.
  ◆ dtest.c:: Directly call the test function of the file system.

After obtaining the source code, the transplantation process can be divided into two steps: ①? Cut according to your own needs; ②? Convert the code to C51 style.

3.1 Downsizing

YAFFS is a powerful file system. Considering that the program code memory and RAM resources of the C51 system are very limited, and certain file operation functions may not be needed in the application, it is necessary to reduce the size of this file system. Cutting includes code cutting and data structure modifications.

First, remove the files yaffs_ramdisk.c, yaffs_ramdisk.h, yaffs_fileem.c and interface.h used for testing, and add #include yaffs_flashif.h to yaffscfg.c.

In this system, only three database files in K9F5608 are read/written. The first-level directory is sufficient. There is no operation permission problem for a single user. Simple file storage does not involve connection (relationship between Linux-like operating system files) issues. Therefore, operation functions related to directory operations, operation permissions, and file connections can be deleted from the system.

Include in yaffsfs.c and its header files (omitting the yaffs_ prefix): readlink(), DumpDir(), readdir(), opendir(), lstat(), stat(), freespace(), chmod(), mkdir (), rename(), link(), closedir(), FollowLink(), fstat(), listclear(), fchmod(), sylink() and mknod().

Include in yaffs_guts.c and its header file (omitting the yaffs_ prefix): Renameobject(), mknodedirectoty(), mknodSymLink(), mknodSpecial(), Link(), GetAttributes(), GetSymLinkAlias(), root(), LostNFound (), GutsTest(), DumpObject(), GetNumberofFreeChunk(), GetObjectLInkCount() and GetEquivalentObject().

Then modify the data structure according to your own needs. The data structures related to the directory operations, operation permissions and file connections mentioned above (such as Uid, Gid, nlink, etc.) are meaningless to us, so we need to modify the relevant data structure. In order to save memory, some macro-defined data constants need to be modified, such as the number of handles running at the same time and the maximum length of the file name.

The cutting work is best carried out on a machine equipped with a Linux operating system. You can use simulation while cutting to check whether the functions you need can be achieved.

3.2 Conversion to C51 style

The YAFFS file system is developed in the Linux environment using the C language for developing user programs. It has some differences from C51, mainly including:

  ◇ C51 does not support the __inline__ function modifier, and its macro can be defined as empty;
  ◇ u8, u16, and u32 all need to be re-macro-defined as C51 data types unsigned char, unsigned int, and unsigned long;
  ◇ off_t is defined as long.

In the YAFFS source code, data and bit are used as variables, but in C51 these are keywords and must be replaced.

In the definition of the yaffs_Device structure in the YAFFS source code, a function pointer with parameters is used to operate the Flash hardware by calling the function pointed to by the pointer. However, in C51, calling the function through the register function pointer cannot pass the actual parameters unless the obtained Parameters can be passed between registers. Therefore, the function pointer in the yaffs_Device structure definition is removed here, and the Flash interface function in yaffs_if.c is directly called.

When converting to C51 style, it is best to modify and compile in the Keil integrated development environment, and then modify after errors are found. When the compiler prompts that there are multiple errors, start correcting the first error; it may be that after the previous error is corrected, the following errors will no longer be errors.

4 YAFFS usage examples under C51 system

The following program code is the key code for operating DBF database files in the design.

yaffs_StartUp(); //Set some parameters, including the addresses of the start block and end block of each partition in Flash, the number of reserved blocks, etc.
yaffs_mount("/flash"); //YAFFS supports multiple partitions, choose to mount here Load the /flash partition and create a new file db1.dbf
f=yaffs_open("/flash/db1.dbf", O_CREAT,S_IREAD | S_IWRITE);
yaffs_close(f);//Close the file db1.dbf
f=yaffs_open("/flash/db1 .dbf", O_RDWR,0);//Open the file db1.dbf in read/write mode
r=yaffs_write(f,"hello",5);//Write data to the file
yaffs_lseek(f,2,SEEK_SET); //Move the file read/write pointer
r=yaffs_write(f,"world",5);
r=yaffs_lseek(f,0,SEEK_SET);
r=yaffs_read(f,buffer,10);//Read data from the file
r=yaffs_close(f);//Close the file db1.dbf
r=yaffs_unlink("/flash/db1.dbf");//Delete the file db1.dbf

It can be seen that the use of the YAFFS interface function is very similar to the file operation function in the standard C language, which is simple and easy to use.

5 Summary

The YAFFS file system is the first embedded file system written specifically for NAND Flash memory. It implements power-down protection, fatigue balancing and effective garbage collection. Compared with JFFS, it takes up less resources and runs faster. Compared with FAT, it is more suitable for managing NAND Flash data memory. If you need to implement a Flash file system in a C51 system, transplanting YAFFS is a good choice, but after all, it is developed under Linux on a 32-bit machine. To make it perfectly combine with the C51 style of an 8-bit machine, it is also a good choice. Further efforts are needed.

References
[1] Samsung Corporation.? K9F5608DataSheet. 20031217.
[2] Mao Yongqiang, Huang Guangming. Implementation of YAFFS file system on embedded Linux. Electronic Design Applications, 2006(3).

Keywords:NAND Reference address:Implementing YAFFS file system on C51 system

Previous article:Implementing YAFFS file system on C51 system
Next article:Application of AT89C51 microcontroller in wireless data transmission

Recommended ReadingLatest update time:2024-11-16 16:43

Migrate code from Nand Flash to memory on FS2410 development board
First, the purpose    I have done an experiment before, moving the first 4k code in Nand Flash to the specified location in memory. The 4K code starting from 0x40000000 in SRAM is copied to the specified location of SDRAM, and the Nand The reason is that after the development board is powered on, the first 4K data
[Microcontroller]
Pure Storage’s Portworx Named a Leader in GigaOm Kubernetes Storage for the Third Consecutive Year
Pure Storage and Portworx Secure Leadership in Enterprise and Cloud-Native Kubernetes Data Storage August 22, 2022, China - Pure Storage®, a global IT pioneer that provides storage as a service for multi-cloud environments, announced that it has been named a leader in two GigaOm reports on Kubernetes
[Industrial Control]
Pure Storage’s Portworx Named a Leader in GigaOm Kubernetes Storage for the Third Consecutive Year
SAM4E MCU Tour - 16. NAND Flash Read and Write
This time, we briefly introduced NAND Flash and how to use it in ASF.   1. Wiring This development board is equipped with a 256 MB, 8-bit NAND Flash (MT29F2G08ABAEA). The pin wiring is as follows: I'll be lazy and directly show the pin multiplexing diagram. PC14 indicates that the NAND FLASH needs to be used as per
[Microcontroller]
SAM4E MCU Tour - 16. NAND Flash Read and Write
Western Digital plans to mass produce 162-layer NAND by the end of the year and aims to exceed 200 layers by 2024
Western Digital plans to start mass production of 162-layer BiCS6 3D NAND, which could be used in PCIe 5-compliant SSDs by the end of this year. The company is also working on 200-layer NAND for data center storage, PLC, and a way to bond multiple 3D NAND wafers together to increase the number of layers.
[Mobile phone portable]
U-boot-2014.04 ported to MINI2440 (9) nor flash boot and nand flash boot
On mini2440, NOR flash boot and NAND flash boot can meet different needs. Here we transplant. NOR boot transplant is relatively simple, because there is no need to redirect the code. NAND boot is relatively complex. Here we start with NOR and get to the point:        1. Modify to support NOR flash boot        Step
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号