📄 nf_drv.c
字号:
* PARAMS:
*
*
* RETURN: OK : erase done
* KO : erase not done
*
*----------------------------------------------------------------------------
* PURPOSE: Erase a block on Nand Flash Media
*
*
*****************************************************************************
* NOTE:
*
*
*
*
*****************************************************************************/
bit nf_block_erase (Uint32 pos)
{
Nf_wait_busy();
Nf_send_command (NF_READ_A_AREA_CMD);
Nf_send_command (NF_BLOCK_ERASE_CMD); /* Auto Block Erase Setup */
Nf_send_address ( ((Byte*)&pos)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)&pos)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ? */
Nf_send_address ( ((Byte*)&pos)[1] ); /* 4th address cycle */
Nf_send_command(NF_BLOCK_ERASE_CONFIRM_CMD);/* Erase command */
Nf_wait_busy();
return OK;
}
/*F**************************************************************************
* NAME: nf_erase_all_block
*----------------------------------------------------------------------------
* PARAMS:
*
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE: This function erase all blocks on a NF card and write CIS
* information
*
*
*****************************************************************************
* NOTE:
*
*****************************************************************************/
bit nf_erase_all_block(void)
{
#if 0
char pdata *ptr = gl_buffer;
bit bad_block_detect;
Uint16 i, j, block;
Byte byte_5;
Byte byte_6;
for (i = 0; i < 256; i++)
gl_buffer[i] = 0x00;
Nf_CS_ON();
/* Erase all block */
for (block = 0; block < NF_BLOCK_PER_ZONE * NF_ZONE_MAX; block++)
{
address = (Uint32)(block) << 5;
/* Read block status byte */
Nf_wait_busy();
Nf_send_command(NF_READ_C_AREA_CMD);
Nf_send_address(0x05);
Nf_send_address ( ((Byte*)&address)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle */
Nf_wait_busy();
byte_5 = Nf_rd_byte();
byte_6 = Nf_rd_byte();
if (1 /*byte_5 == 0xFF*/) /* Not a bad block */
{
nf_block_erase(address);
if ( nf_check_status() == KO)
{ /* Failure on erase operation */
nf_mark_bad_block();
}
else
{
/* Check if all bit is 1 */
i = 0;
bad_block_detect = FALSE;
while ((i < 32) && (bad_block_detect == FALSE))
{
Nf_wait_busy();
Nf_send_command(NF_READ_A_AREA_CMD);
Nf_send_address(0x00);
Nf_send_address ( ((Byte*)&address)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle */
Nf_wait_busy();
bad_block_detect = FALSE;
nf_upload_buffer();
j = 0;
/* Check first half page */
while ((j < 256) && (gl_buffer[j] == 0xFF)) j++;
if (j != 256)
{
bad_block_detect = TRUE;
}
else /* Check second half page */
{
nf_upload_buffer();
j = 0;
while ((j < 256) && (gl_buffer[j] == 0xFF)) j++;
if (j != 256)
{
bad_block_detect = TRUE;
}
else /* Check redundant spare area */
{
for (j=0; j < 16; j++)
gl_buffer[j] = Nf_rd_byte();
j = 0;
while ((j < 16) && (gl_buffer[j] == 0xFF)) j++;
if (j != 16)
{
bad_block_detect = TRUE;
}
}
}
i++;
address++;
}
address = (Uint32)(block) << 5;
if (bad_block_detect)
{
i--;
nf_mark_bad_block();
}
else
{
for (j = 0; j < 256; j++)
gl_buffer[j] = 0x00;
for (j = 0; j < 32; j++)
{
/* Fill all pages with 0x00 */
Nf_wait_busy();
Nf_send_command(NF_READ_A_AREA_CMD);
Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);
Nf_send_address(0x00);
Nf_send_address ( ((Byte*)&address)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle */
nf_download_buffer();
nf_download_buffer();
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
/* Valid the page programmation */
Nf_send_command(NF_PAGE_PROGRAM_CMD);
if ( nf_check_status() == KO)
{ /* Failure on program operation */
nf_mark_bad_block();
}
Nf_wait_busy();
address++;
}
address = (Uint32)(block) << 5;
j = 0;
bad_block_detect = FALSE;
do
{
Nf_send_command(NF_READ_A_AREA_CMD);
Nf_send_address(0x00);
Nf_send_address( ((Byte*)&address)[3] ); /* 2nd address cycle */
Nf_send_address( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address( ((Byte*)&address)[1] ); /* 4th address cycle */
Nf_wait_busy();
for (i = 512 + 16; i != 0; i--)
{
if (Nf_rd_byte() != 0x00) bad_block_detect = TRUE;
}
j++;
address++;
}
while ( (bad_block_detect == FALSE) && (j < 32) );
address = (Uint32)(block) << 5;
if (bad_block_detect)
{
nf_mark_bad_block();
Nf_wait_busy();
}
else
{
/* Finally, erase the block */
nf_block_erase(address);
if ( nf_check_status() == KO)
{ /* Failure on erase operation */
nf_mark_bad_block();
}
}
}
}
}
}
#endif
char pdata *ptr = gl_buffer;
bit bad_block_detect;
Uint16 i;
Nf_CS_ON();
/* Erase all block */
for (address = (Uint32)(NF_ZONE_MAX) * 1024 * 32 - 32; address != 0; address-=32)
{
/* Read block status byte */
Nf_wait_busy();
Nf_send_command(NF_READ_C_AREA_CMD);
Nf_send_address(0x05);
Nf_send_address ( ((Byte*)&address)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle */
Nf_wait_busy();
if (Nf_rd_byte() == 0xFF) /* Not a bad block */
{
nf_block_erase(address);
if ( nf_check_status() == KO)
{ /* Failure on erase operation */
nf_mark_bad_block();
}
else
{ /* Fill redundant area with 0x00 */
Nf_wait_busy();
Nf_send_command(NF_READ_C_AREA_CMD);
Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);
Nf_send_address(0x00);
Nf_send_address ( ((Byte*)&address)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle */
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
/* Valid the page programmation */
Nf_send_command(NF_PAGE_PROGRAM_CMD);
if ( nf_check_status() == KO)
{ /* Failure on program operation */
nf_mark_bad_block();
}
/* Read 16 bytes */
Nf_wait_busy();
Nf_send_command(NF_READ_C_AREA_CMD);
Nf_send_address(0x00);
Nf_send_address( ((Byte*)&address)[3] ); /* 2nd address cycle */
Nf_send_address( ((Byte*)&address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address( ((Byte*)&address)[1] ); /* 4th address cycle */
Nf_wait_busy();
bad_block_detect = FALSE;
for (i = 16; i != 0; i--)
{
if (Nf_rd_byte() != 0x00) bad_block_detect = TRUE;
}
if (bad_block_detect)
{
nf_mark_bad_block();
}
else
{
/* Finally, erase the block */
nf_block_erase(address);
if ( nf_check_status() == KO)
{ /* Failure on erase operation */
nf_mark_bad_block();
}
}
}
}
}
/* Reconstruct the CIS */
/* Open in write mode at the address 0x00 */
Nf_wait_busy();
Nf_send_command(NF_READ_A_AREA_CMD);
Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);
Nf_send_address(0x00);
Nf_send_address(0x00);
Nf_send_address(0x00);
if (NF_4_CYCLE_ADDRESS)
Nf_send_address(0x00);
/* Save CIS table in gl_buffer */
for (i = 0; i < 256; i++)
{
(*ptr++) = nf_cis_table[i];
}
/* write buffer 2 times */
nf_download_buffer();
nf_download_buffer();
/* Write spare data */
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
/* Valid the page programmation */
Nf_send_command(NF_PAGE_PROGRAM_CMD);
Nf_wait_busy();
read_spare_byte();
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -