STM32固件库,Nand_if.c文件下的NAND_BuildLUT函数调试中出现问题,
即函数最后的那个do{}while ( pFreeBlock < MAX_PHY_BLOCKS_PER_ZONE );
循环中pFreeBlock 会导致LUT 数组溢出,但是我看了好久没明白这段代码的意思;
无从修改。
不知道有没有谁移植过这段程序,望指点一二。
/*******************************************************************************
* Function Name : NAND_BuildLUT
* Description : Build the look up table
* Input : None
* Output : None
* Return : Status
* !!!! NOTE : THIS ALGORITHM IS A SUBJECT OF PATENT FOR STMICROELECTRONICS !!!!!
*******************************************************************************/
static uint16_t NAND_BuildLUT (uint8_t ZoneNbr)
{
uint16_t pBadBlock, pCurrentBlock, pFreeBlock;
SPARE_AREA SpareArea;
/*****************************************************************************
1st step : Init.
*****************************************************************************/
/*Init the LUT (assume all blocks free) */
for (pCurrentBlock = 0 ; pCurrentBlock < MAX_PHY_BLOCKS_PER_ZONE ; pCurrentBlock++)
{
LUT[pCurrentBlock] = FREE_BLOCK; /* 12th bit is set to 1 */
}
/* Init Pointers */
pBadBlock = MAX_PHY_BLOCKS_PER_ZONE - 1;
pCurrentBlock = 0;
/*****************************************************************************
2nd step : locate used and bad blocks
*****************************************************************************/
while (pCurrentBlock < MAX_PHY_BLOCKS_PER_ZONE)
{
SpareArea = ReadSpareArea(pCurrentBlock * NAND_BLOCK_SIZE + (ZoneNbr * NAND_BLOCK_SIZE * MAX_PHY_BLOCKS_PER_ZONE));
if ((SpareArea.DataStatus == 0) || (SpareArea.BlockStatus == 0))
{
LUT[pBadBlock--] |= pCurrentBlock | (uint16_t)BAD_BLOCK ;
LUT[pCurrentBlock] &= (uint16_t)~FREE_BLOCK;
if (pBadBlock == MAX_LOG_BLOCKS_PER_ZONE)
{
return NAND_FAIL;
}
}
else if (SpareArea.LogicalIndex != 0xFFFF)
{
LUT[SpareArea.LogicalIndex & 0x3FF] |= pCurrentBlock | VALID_BLOCK | USED_BLOCK;
LUT[pCurrentBlock] &= (uint16_t)( ~FREE_BLOCK);
}
pCurrentBlock++ ;
}
printf("2nd step\r\n");
/*****************************************************************************
3rd step : locate Free Blocks by scanning the LUT already built partially
*****************************************************************************/
pFreeBlock = 0;
for (pCurrentBlock = 0 ; pCurrentBlock < MAX_PHY_BLOCKS_PER_ZONE ; pCurrentBlock++ )
{
if ( !(LUT[pCurrentBlock]& USED_BLOCK))
{
do
{
if (LUT[pFreeBlock] & FREE_BLOCK)
{
LUT [pCurrentBlock] |= pFreeBlock;
LUT [pFreeBlock] &= ~FREE_BLOCK;
break;
}
pFreeBlock++;
}
while ( pFreeBlock < MAX_PHY_BLOCKS_PER_ZONE );
}
}
return NAND_OK;
} |