In batch data transmission with the host, firstly, the CBW command sent by the host is parsed, so that the firmware can obtain the LBA requested by the host for data transmission, and then the LBA address is mapped to obtain the corresponding PBA, and then the host command is executed. Specific methods: First, read the current total number of bad blocks FlashBadBlocksCount. If it is 0, it means that there are no bad blocks, and LBA will return directly without mapping; If the last access address and LBA belong to the same block, then address mapping is not needed; If the current number of bad blocks is 1, it is judged whether the LBA block address is the same as the bad block address in the BBI table, so as to decide whether to adopt address mapping; If the current number of bad blocks is not 1, you need to look up the BBI table to determine whether it contains the block address of LBA. If it exists, you need to remap the address. If it does not exist, you don’t need to remap it. You can use the dichotomy lookup table to realize the quick comparison of two addresses [4].
3.4 Bad Block Handling
The processing method of finding that the current block is bad during writing or erasing the mapped address: first, look for the block status [flash _ bad _ blocks _ remap] in the mapping area to find the available mapping blocks. If all 50 remapped blocks are marked as bad or used, the program enters an infinite loop; If an available mapping block is found, the firmware scatters three situations in which a bad block may occur: (1) Error in erasing the current block: Write the address of the current block and the address of the available remapped block in the BBI table, and arrange them according to the address size, which is beneficial to binary table lookup and return the address of the remapped block. (2) Failed to copy a page from the swap area to the operation address: firstly, get the block address of the currently used swap block, and then judge whether the operation address is in the data storage area or the remapping area. If it is in the remapping area, it means that the block in the data storage area corresponding to the current operation address is already a bad block, and the remapped block is also bad in this operation. At this time, it is necessary to mark the current mapped block as bad, find the next available remapped block in the remapping area, write the original block address in the data storage area and the updated mapped block address into the BBI table, and return the new mapped address; If it is in the data storage area, remap it for the first time, update the bad block table, and return the mapping address. Then complete the copy work. (3) Error in writing a page of data to the operation address: copy the previous page in the block where the current address is located from the corresponding address in the exchange block to the remapping block,Then copy the unwritten data in the current page where the operation address is located from the swap block, rewrite the data in the buffer to the remapped address, and return the remapped address. The function is implemented as follows:
DWORD FlashDealBadBlock(DWORD Addr,DWORD Type)
{
DWORD i;
DWORD RemapBlockAddr;
DWORD SwapBlockAddr;
while(1)
{
RemapBlockAddr=FlashGetNewRemapBlock();
if(RemapBlockAddr==-1)
return Addr;
switch(Type)
{ case 1:
goto Exit;
break;
case 2:
SwapBlockAddr=FlashGetCurrentSwapBlock();
for(i=0; i《(Addr&(FLASH_BLOCK_SIZE-1))/
FLASH_PAGE_SIZE+1; i++)
{
if(0x00==(FlashCopyPage(SwapBlockAddr+
i*FLASH_PAGE_SIZE,RemapBlockAddr+
i*FLASH_PAGE_SIZE)))
goto BadRemapBlock;
}
goto Exit;
break;
case 3:
SwapBlockAddr=FlashGetCurrentSwapBlock();
for(i=0; i《(Addr&(FLASH_BLOCK_SIZE-1))/
FLASH_PAGE_SIZE; i++)
{
if(0x00==(FlashCopyPage(SwapBlockAddr+
i*FLASH_PAGE_SIZE,RemapBlockAddr+
i*FLASH_PAGE_SIZE)))
goto BadRemapBlock;
}
if(0x00==(FlashCopyPage(Addr,RemapBlockAddr+
i*FLASH_PAGE_SIZE)))
goto BadRemapBlock;
goto Exit;
break;
default:
break;
}
BadRemapBlock: FlashMarkRemapBlockBad
(RemapBlockAddr);
}
Exit:FlashUpdateBadBlockTable(Addr,RemapBlockAddr);
return RemapBlockAddr+(Addr&(FLASH_BLOCK_SIZE-1));
}
3.5 Continuous read and write operations
When the host establishes a batch transmission data connection with the device, the firmware obtains the initial LBA by parsing the CBW packet. The address is mapped and bad block management is carried out to obtain PBA. Set a variable. When this variable is valid, it means that the host still needs to read or write data to Flash until this variable becomes invalid. At the same time, the address of the last sector read and written by the host is stored in another variable, which has little effect in continuous reading, but in continuous writing, it is judged whether it belongs to the same block by comparing it with the block where the address of the last writing operation is located, so as to decide whether it is necessary to carry out address cross-block processing.
In this paper, a flash translation layer for NAND type is designed, which enables NFTL to complete address mapping, bad block management and continuous data reading and writing operations. The partition design of NAND Flash makes the block management structure clear, which is beneficial to the development of firmware. In this paper, the ECC check of Flash is not designed too much, because in practical application, NAND Flash is mainly used to store multimedia data (pictures, voice files, etc.), and it will not be frequently written or erased, and multimedia file data is not sensitive to the integrity of data [5], so it is not necessary to carry out strict ECC check on every bit of data stored in it, and a simple check method can be designed to replace ECC check.