Wince NAND Flash-Fal

Source: Internet
Author: User

Wince NAND Flash-Fal

From esslabwiki

1. Introduction

Flash is different from the common disk, and its feature is that it does not have to perform write operations on the same memory volume location, the write operation can be performed only when the location of the remember entity of erase is required. Therefore, the general file system, such as fat16, FAT32, nfts ..., It cannot be used directly on flash memory. If you want to use these file systems, to apply the logical block address to the actual flash memory location, through some mechanisms, the system can treat flash memory as a general hard drive. On the Win CE platform, we assume this feature is FTL (Flash Translation Layer ).

FTL is the most primitive application for nor flash. However, in the current market layout, the cost of large-capacity nor flash is higher than that of NAND flash with the same capacity, therefore, nftl (NAND Flash Translation Layer) was born. nftl is similar to FTL, mainly because it is used in NAND Flash.

 

Figure 1. Architecture

This document describes how wince6.0 works as a garbage collection, wear-leveling, finally, we will explain how to install a flash device on the basic system of Win CE.

2. System Architecture

In Windows CE

 

Figure 2. Flash Architecture

The focus of this article: FAL and FMD are as follows:

FMD (Flash Media driver) can perform driven, read, write, and erase actions on the flash of a specific vendor.

Fal (flash upload action layer): file system must perform this operation on the flash drive. In this case, the interface provided by FMD is followed by the Flash program.

Test Figure 1 again to see the behavior of this tutorial; file system must perform FMD (driver) operations through FAL for flash operations. Therefore, for various Flash files, you only need to modify the FMD. Instead of modifying the FAL interface, you can perform basic operations such as read, write, and erase on Flash files. Therefore, we can get a conclusion that if flash in one product is converted into another vendor, because Flash is consistent with FAL interfaces, as long as you pull the FMD, other programs do not need to be modified.

Then, the interfaces provided by FMD for FAL and their functions will be further clarified, so that they will not be described here.

FAL has two tables (dlut and secondary table) to provide the translation action. There are also two columns listing one free sector, A dirty block is used to provide free sector and help garbage collection. The detailed procedures and usage will be explained later.

3. Terminology

The following describes some basic terms and definitions that will appear in this article.

There may be multiple region in flash, and the actual region will be as follows:

 

Figure 3. region at flash

Region contains multiple blocks, which indicate the following:

 

Figure 4. Block at region

There are also multiple sector in the block. The example is as follows:

 

Figure 5. Sector at Block

However, it should be noted that Win CE uses the sector ing method in this case. Therefore, when the block operation is performed, it must be calculated based on the given sector address as the block.

Here we will explain its sentiment in each term:

For region, it only checks how many blocks it has and how many sector a block has.

For a block, its starting position is the first sector (in figure 5, it is sector 1 ). But here we need to explain how the block calculates the position of its first sector. If a block has 10 sector s, the first sector of the next block is Sector 11.

For file system, all the seen sector addresses are logical addresses. When file system wants to obtain a token on flash, the Logical Address of the FAL resource must be given. to retrieve the required information, the FAL must parse the corresponding logical address at the position of the mapping table, then the physical address is retrieved Based on the location in the External table, and then the data in Flash is retrieved by FMD. FAL then delivers the obtained data to the system that sends the request. In this example, the subsequent chapter will be resolved again.

4. Address Translation

How to quickly locate a logical address depends on the mapping table. With this, you can easily and quickly find the desired address (sector ). This section describes how to create a mapping table and mapping.

4.1 In-ram Data Structures

Puchar m_pdynamiclut [master_table_size]; DWORD m_cbphysicaladdr;

Bool m_bisnumsectorspersectablelog2;

DWORD m_dwnumsectorspersectable;

DWORD m_dwsecondarytablesize;

DWORD m_dwstartlogsector;

DWORD m_dwnumlogsectors;

DWORD m_dwstartphyssector;

DWORD m_dwnumphyssectors;

The main actions here are processing and processing the dlut table and secondary table. The purpose is to quickly find the physical address from the logical address; translate is an important table.

M_pdynamiclut [master_table_size]:

Here, master_table_size is 256. Therefore, the table size can be determined from the beginning. This variable is a dynamic look-up table (dlut), that is, a mapping table.

M_bisnumsectorspersectablelog2:

Determines whether the data of the eclipsector is power of 2 in the secondary table, which affects the calculation of table size and secondary ID. If it is a factor of 2, you can directly shift the value to save the time required for division.

M_dwnumsectorspersectable:

How many sector can be added to the secondary table?

M_dwsecondarytablesize:

The size of the secondary table. The length of the root shard's logical sector and physical address determines the size.

M_dwstartlogsector:

Location of the Start sector of the Logical Block

M_dwnumlogsectors:

Number of logical sector

M_dwstartphyssector:

Location of the starting sector of the physical block

M_dwnumphyssectors:

Number of sector of physical block

4.2 On-flash data structures

Typedef struct _ flashinfoex

{

DWORD cbsize;

Flash_type flashtype;

DWORD dwnumblocks;

DWORD dwdatabytespersector;

DWORD dwnumregions;

Flashregion region [1];

} Flashinfoex, * pflashinfoex;

Typedef struct _ flashinfo

{

Flash_type flashtype;

DWORD dwnumblocks;

DWORD dwbytesperblock;

Word wsectorsperblock;

Word wdatabytespersector;

} Flashinfo, * pflashinfo;

Flashtype: After init, whether it is nor or NAND to obtain the Flash type.

Dwnumblocks: Check the number of blocks in the flash.

Dwbytesperblock: the number of bytes in a block

Wsectorsperblock: Number of sector s in a block

Wdatabytespersector: the number of bytes per sector

Here is the basic info for obtaining the flash, and then calculate the number of blocks in the flash. The difference between flashinfoex and flashinfo is the region part. As a result, only one region is used for the NAND Flash part. Therefore, the following describes a region.

The block has the following statuses. different statuses are used to determine which list the sector will be placed in.

Block_status_unknown: other conditions that cannot be determined

Block_status_bad: This block is a bad block.

Block_status_readonly: This block is read only.

Block_status_reserved: The block is reserved.

In addition, FAL uses block ID and FMD to get the block status while FMD obtains the block ID, calculate that the first sector of the block in Flash is the second sector, and then extract its status to FAL.

For example, if a block has 10 sector s, the first sector of Block 2 is Sector 11. Therefore, FMD returns the status of Sector 11 to FAL.

 

Figure 6. Get block status

4.3 how to create mapping table

This describes the win ing table used by Win CE. It is to scan the flash info, first retrieve the region, flash type. A region contains multiple blocks, each of which has a separate table and multiple blocks.

The data structure of region is as follows:

Typedef struct _ flashregion

{

Region_type regiontype;

DWORD dwstartphysblock;

DWORD dwnumphysblocks;

DWORD dwnumlogicalblocks;

DWORD dwsectorsperblock;

DWORD dwbytesperblock;

DWORD dwcompactblocks;

} Flashregion, * pflashregion;

Dwstartphysblock: the index value of physical block in this region. The initial value here is 0. That is to say, if there are two region in this flash, and each region has 10 blocks, the start physical block of the first region is 0, the start physical block of the second region is 10.

Dwnumphysblocks: number of blocks in this region

Dwsectorsperblock: Number of Sector in the block

Dwcompactblocks: In compactor, it is set to 2, and then it will be seen in compaction.

Here is the ctor ing of sector. Therefore, table is built based on a single sector. Obtain the block first, and then the info of all sector in the block (only the spare area information is retrieved here ); depending on the current sector status, it is determined that it is put into the General List (here there are also points free, read only) or dirty list. Other sector with info are used to perform ing table operations. It should be noted that, since write is in the form of sector, the Free List is stored in the form of sector, while erase is in the form of block, therefore, the dirty list is stored as a block.

The psuedo-code for table creation is as follows,

For (each block)

{

For (each sector)

{

Read sector Info (spare Area)

If (sector is free)

{

Add into m_list (free)

}

If (Sector is dirty)

{

Add into dirty_list

}

If (Sector is mapped)

{

If (block is read_only & sector is first sector on block)

{

Add into m_list (read_only );

For (each sector)

Mapping the table: l2p

Break;

}

Mapping the table: l2p

}

} // For (each sector)

} // For (each block)

Next we will explain the mapping table process:

Dlut (Dynamic look up table) refers to the master table, which has 256 entries. One entry reflects one secondary table. The unused sector (free sector) will not be created in the table, and the on demand method will be used for the effect.

An informative sector will have the sector logical address that represents the position in the system. If you want to find the secondary tables and do not create it, the system will create its table, and the root physical logical address calculates its location in the secondary table, and then stores its physical address in the table.

 

Figure 7. dlut and secondary table

The following example shows the process of creating a table for the primary partition:

1. the logical sector given by the root user first calculates the secondary table ID (ID = 1 calculated by this parameter), and then checks whether the secondary table already exists. If the table does not exist, the system will allocate a new secondary table at this time.

2. Create a secondary table based on the data of the previously generated sector's spare area. In the future, it is the location of the computation in the secondary table.

3. Save physical sector address to this location after removal:

 

Figure 8. Create logical to physical sector address

4.4 read

The process can be illustrated in figure 4 to solve the problem ,. The root segment sector's logical address calculates that dlut is the first entry, and then the secondary table corresponding to the corresponding entry can be obtained, use the logical address to calculate the location in the secondary table, and then obtain the physical address. FMD can extract the flash-based information based on the address.

4.5 write

Before writing, you must obtain an available sector in the Free List. if the sector is insufficient, the compaction mechanism will be triggered to get enough sector to complete the write.

Before writing, mark the sector info in the processing process, and then sort the sector info. After configuring info, confirm that the sector can be merged, and then mark the sector info as handled, merge with data. Finally, update the physical address mapped to the ing table and logical address.

The previous response sets its update sector info to dirty. In addition, it calculates the block where the sector is located, add the number of the sector of the block dirty to 1. In this case, users should note that it is an SLC action, and it can allow this action at a time, while MLC does not allow this value to change to a more recent one.

5. Garbage Collection (compaction)

Garbage collection, named compaction in wince. Simply put, the purpose is to reclaim the space allocated by the invalid information, and the more recyclable at a time, the better.

5.1 What's garbage collection?

Because flash cannot restore the same location when updating the data (unless the location is erase first), the outplace method is used to restore the data. Outplace is the overhead of the action that must be erased every time the updated item is updated.

In the past, it was impossible to directly recover the database to become a sector with no metadata, so we need to have a policy to convert it into an available sector. This policy is the garbage collection.

5.2 garbage collection policy

There will be two lists in this FAL, except for the free sector and dirty blocks. The sizes of the partitions in the two lists are different because the separator is a single-bit de-partitioning, while the erase is a block.

The Direct selection of dirty block mentioned above is to directly retrieve the most dirty sector block in the dirty list and use it as the victim block. The random block selection method is to use the first physical block as the base and use 1 ~ 32. Run the random command to remove the physical block and set the base value to the block ID, which is the victim block.

Compaction is performed when the free sector is insufficient and the compaction is enabled only when the compaction is completed. In this case, the compaction is synchronous compaction. For example, if a block has 20 sector S, a minimum of 40 free sector s are required. Another issue is that when the number of data sector ers you want to allocate is more than the number of free sector currently, it will also compile the compaction row until there is enough free sector to complete the write data task.

Another time frame is that the current dirty sector of Flash has many free sector (but this condition is still because the number of free sector is more than the lower limit ), however, its priority is low. In this example, It is asynchronous compaction. Otherwise, the compaction must be terminated before it can be opened.

6. Wear-Leveling

6.1 what is wear-leveling?

Because each block in Flash has a limit on erase times, when there are too many erase times in a block, the access speed of the block may slow down, when the block is heavy, it may even cause block loss. Therefore, in order to avoid excessive access in the same region, and to average the number of times of each block erase, this machine uses wear-leveling.

6.2 how WL is done in Fal

Here, critical refers to the issue when the free sector is insufficient, so the block in the dirty list is directly used for compact. In other cases, the random method is used. The following describes the two policies.

1. Critical = true

Obtain the most dirty sector block from the dirty list to earse

The bool value is not changed.

2. Critical = false

It is a random condition, and static bool value is used as an alternative. The initial value of bool value is false.

If it is selected in the random mode for the first time (bool value = true), and the bool value is changed

In this case, select (bool value = false) from the dirty list for the second time and change the bool value.

In addition, if the block selected by random is free or invalid, select from dirty list.

Random uses the following example:

Sequence (s)

1

2

3

4

5

Execute

Dirtiest

Random

Dirtiest

Random

Dirtiest

Free Block

N

N

N

Y

N

Re-Execute

Dirtiest

S 1: The first time is bool value = false. Therefore, it is obtained from the dirty list and the bool value is changed to true.

S 2: The second time is bool value = true, so take random and change bool value to false

S 3: the third time is bool value = false. It is obtained from the dirty list and the bool value is changed to true.

S 4: the fourth time is bool value = true. Therefore, use random and change bool value to false. But the retrieved block is free, so the dirty list is changed.

S 5: the fifth time is bool value = false. It is obtained from the dirty list and the bool value is changed to true.

For example:

C = critical, D = dirty block, r = random block,

B = brandom-Indicates T. Next time you want to select random or retrieve victim from Dirty List

(Please refer to the example and description of critical = false)

If the sequence of C is as follows:

Tfftfff B = f (init)

The process of selecting a block is as follows:

Critical

Choose Block

B (change)

C = T

D

B = f (no change)

C = f

D

B = T

C = f

R → if the selected block is free

Then select from D.

B = f

C = T

D

B = f (no change)

C = f

D

B = T

C = f

R

B = f

C = f

D

B = T

7. FMD/FAL Interface

7.1 FMD-to-Fal Interface

Typedef struct _ fmdinterface

{

DWORD cbsize;

Pfn_init pinit;

Pfn_deinit pdeinit;

Pfn_getinfo pgetinfo;

Pfn_getblockstatus pgetblockstatus;

Pfn_setblockstatus psetblockstatus;

Pfn_readsector preadsector;

Pfn_writesector pwritesector;

Pfn_eraseblock peraseblock;

Pfn_powerup ppowerup;

Pfn_powerdown ppowerdown;

Pfn_getphyssectoraddr pgetphyssectoraddr;

Pfn_oemiocontrol poemiocontrol;

} Fmdinterface, * pfmdinterface;

In the above FMD function, FMD _ is used as the prefix, and pgetblockstatus is used as an example. the naming of CPP is fmd_getblockstauts, but for some usage tips, We need to wrap it into a structure so that it can be used in the function point method. The following describes the purpose of the function:

Pinit:

Initialize the device when the flash device is required. Find the supported chip for initialization, and return a FMD handle.

Pdeinit:

Obtain the FMD handle to put some resources used by the supervisor and disable the chip controller.

Pgetinfo:

This function is used to obtain the flash information. Pflashinfo is a structure that contains flash resources.

Pflashinfo-> flashtype: type of flash.

Pflashinfo-> wdatabytespersector: the number of bytes of a sector.

Pflashinfo-> dwnumblocks: Total number of blocks in flash.

Pflashinfo-> wsectorsperblock: the number of sector contained in each block.

Pflashinfo-> dwbytesperblock: the number of bytes contained in each block.

Pgetblockstatus:

To obtain the signature of a block. Block address. Because there may be bad blocks in flash, we will first check whether the current block is bad. This is to obtain the status of the first sector. If the generated block is a bad block, block_status_bad should be returned. If it is not a bad block, you need to retrieve the sector info of the starting sector of this shard. If the sector info parameter is returned, block_status_unknown is returned.

Psetblockstatus:

Set the block parameter. The first parameter is the block address, and the second parameter is the parameter to be set. In this function, check whether dwstatus is block_status_bad. If so, mark the bad block and return false. If not, run the dwstatus command to the info of the first sector of the block.

Preadsector:

Used as a sector on Alibaba flash. The following table lists the values of the input values:

Startsectoraddr:

The starting address of the sector, which is the starting address of the sector.

Psectorbuff:

The output data of each sector is stored in this buffer.

Psectorinfobuff:

Generally, the information of each sector is stored in the flash data. From the flash data, the sector's related information is stored in this buffer. These resources are called spare areas.

Dwnumsectors:

Number of sector.

Pwritesector:

The meaning is the same as above. This is for write.

Peraseblock:

It is an erase block, and the number of bytes is the second block.

Ppowerup:

Recover the power source from flash

Ppowerdown:

Power off flash Power Source

Pgetphyssectoraddr:

Obtain its physical address in flash.

Poemiocontrol:

Just like many iocontrol functions, different cases have different functions. For NAND Flash, implement is not always required here. In fact, if there is no implement, it does not affect the usage.

7.2 FAL the function interface for File System

Dsk_init:

In this case, initial FMD is used to obtain basic flash information. Then initial FAL to obtain the interface for the flash operation, and create a translation table.

Dsk_deinit: Free FAL object and free FMD again

Dsk_open: There is no implement, Only debug message.

Dsk_close: There is no implement, Only debug message, return true.

Dsk_read: not used.

Dsk_write: not used.

Dsk_seek: not support.

Dsk_powerdown: directly call the powerdown function of FMD.

Dsk_powerup: directly call the powerup function of FMD.

Dsk_iocontrol:

All flash operations pass through this function. First, the control code obtained by the root handler is used to check the corresponding response. For example, to obtain the information (getinfo) of flash, check whether the buffer and size obtained by the Handler are correct, and then perform corresponding actions.

Therefore, the preceding open, close, read, and write operations are handled by this function.

8. Connect a device to the System

8.1 FAL, FMD

In wince, flash driven is constructed by FAL and FMD (figure 1). FAL is in the library type, its prefix for external interfaces is DSK _ start (Chapter 1 describes Introduction), while FMD is a dynamic link type, its prefix is FMD _ start (Chapter 1 describes the prefix ). Msflash. Lib is required to be triggered on WinCE. This requires FAL. lib and FMD. DLL to be used in wince. FAL also requires an FMD to run a device on WinCE. Otherwise, it only provides an interface without a usable device.

FAL is composed of the following files (in this example column. cpp)

1. compactor-Provides compactor Functions

2. fal-build mapping table and actions for read, write, compactor, and format under FMD layer, read, write, erase or FMD layer for flash (see Figure 1)

3. falmain-interface provided to File System

4. Update log2physmap-mapping table and obtain the physical sector address.

5. sectormgr-manages the sector, such as the free-list and dirty-list handling (management), and provides functions for the sector operation.

The following operations extract Fal (Library) from the system, and add the modified flash FMD (driver ), it can be displayed in the environment of Wince on the simulator.

8.2 how to configure a storage with the System

Step 1.

When creating an osdesign, please build the entire environment (about 20 to 40 minutes) first. If you build the environment with the following actions first, an error occurs.

Step 2.

Add the project to your clone emulator (clone from device emulator: armv4i here. Clone name: arvv4ibase)

Src-> drivers (Path: C:/wince600/platform/armv4ibase/src/drivers)

The following is how Visual Studio works.

 

Modify the following file

Dirs: C:/wince600/platform/your clone emulator/src/drives, add the newly added project name (driver name ).

Example :( # -- Annotation)

# @ Cesysgen endif ce_modules_showfal

Falshow/

Fmdshow/

Platfrom. bib: The purpose is to define the image to be included in the OS, so Add the new FMD.

Example:

If bsp_fmdshow1

Fmdshow1.dll $ (_ flatreleasedir)/fmdshow1.dll NK Shk

Endif

Platfrom. Reg: defines the registry key and correlation value of the system starting from the cold start. It is worth mentioning that the storage iclass can be retained.

Example:

[HKEY_LOCAL_MACHINE/Drivers/builtin/fmdshow1]

"Prefix" = "DSK"

"DLL" = "fmdshow1.dll"

"Iclass" = multi_sz: "{A4E7EDDA-E575-4252-9D6B-4195D48BB865 }"

If you modify the value in platfrom. Reg, you only need to build-> advanced buildcommands-> build current BSP and subprojects.

Source: the library to be linked or used. This is available for every project.

Catalog item: This FMD can be viewed in device drivers under catalog items view.

Step 3. Remember to select the following options

 

Step 4. After all the above actions are completed, select your new FMD and build the entire project. After attach device, we can see the plane of Win CE emulator, and click my device in the colored box.

 

Step 5. Click the control panel in the colored box.

 

Step 6. Retrieve the storage manger in the highlighted box.

 

Step 7. The following is the newly added FMD.

Press new button next to partitions

 

The following dialog box appears, and a name is named "OK" in the upper right corner.

 

Will return to the previous response and select Properties

 

Step 8. Select dismount first.

 

Other buttons enable

 

Select format button

Step 9. Choose start button

 

Then, a response message box is displayed, and the format starts after the selection. Click OK

 

Step 10. Return to the partition of the partition properties, select the Mount button, and click OK in the upper right corner.

 

Return to the storage properties folder and click OK in the upper-right corner.

 

Step 11. Return to the my device interface and you will see a new storage.

 

The above is the workflow for creating a new flash device.

The above analysis is very clear, but it is relatively scattered. The general class exercises are shown at will:

 

A fal instance targets a region. Some concepts need to be clear. The sector of Wince is the size of the page returned by the underlying layer, so do not confuse it.

For large NAND Flash FAL + FMD, it is definitely not a good implementation method. The process of creating a ing table on power-on is too slow. If every page information is placed in a centralized place, it will inevitably lead to excessive use of the service.

It may be better to use MDD + PDD. If no experiment is available, make a comparison later.

 

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.