📄 nf_drv.c
字号:
Nf_wait_busy();
Nf_send_command (NF_RANDOM_DATA_INPUT_CMD);
Nf_send_address (0x00);
Nf_send_address (0x00);
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[3] ); /* row address Byte 0 */
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[2] ); /* row address Byte 1 */
if (NF_5_CYCLE_ADDRESS_BIT) /* size of nf > 128Mbytes ? */
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* row address Byte 2 */
for (j = ((Byte*)&gl_cpt_page)[0] >> 1; j != 0; j--)
{
nf_download_buffer();
nf_download_buffer();
}
Nf_send_command (NF_RANDOM_DATA_INPUT_CMD); /* spare area */
((Byte*)&gl_cpt_page)[0] = 0x08;
Nf_send_address(0x00);
Nf_send_address(0x08);
nf_update_spare_data(); /* update spare data */
Nf_send_command (NF_PAGE_PROGRAM_CMD);
gl_cpt_page = 0;
gl_ptr_mem++;
((Byte*)&nf_current_physical_sector_addr)[3]++;
((Byte*)&gl_address)[3]++;
}
if (((Byte*)&gl_ptr_mem)[3] & 0x3F)
{
for (j = (((Byte*)&gl_ptr_mem)[3] & 0x3F); j < 0x40; j++)
{
Nf_wait_busy();
Nf_send_command (NF_READ_CMD);
Nf_send_address (0x00);
Nf_send_address (0x00);
Nf_send_address ( ((Byte*)&gl_address)[3] ); /* row address Byte 0 */
Nf_send_address ( ((Byte*)&gl_address)[2] ); /* row address Byte 1 */
if (NF_5_CYCLE_ADDRESS_BIT) /* size of nf > 128Mbytes ? */
Nf_send_address ( ((Byte*)&gl_address)[1] ); /* row address Byte 2 */
Nf_send_command (NF_COPY_BACK_CMD);
Nf_wait_busy();
Nf_send_command (NF_RANDOM_DATA_INPUT_CMD);
Nf_send_address (0x00);
Nf_send_address (0x00);
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[3] ); /* row address Byte 0 */
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[2] ); /* row address Byte 1 */
if (NF_5_CYCLE_ADDRESS_BIT) /* size of nf > 128Mbytes ? */
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* row address Byte 2 */
Nf_send_command (NF_PAGE_PROGRAM_CMD);
nf_current_physical_sector_addr++;
gl_address++;
}
}
}
/*F**************************************************************************
* NAME: nf_reassign_block
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Reassign the block value in LUT (copy of LUT)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void nf_reassign_block (void)
{
Byte i; /* Counter */
Uint16 start;
Uint32 address2; /* LUT source address */
if (nf_lut_index[nf_zone] == 63)
{
if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_look_up_table_block)
{
gl_buf_free_idx++;
if (gl_buf_free_idx > nf_spare_block)
{
gl_buf_free_idx = 0;
}
}
/* source address */
address2 = ((Uint32)(nf_look_up_table_block)<<NF_SHIFT_SECTOR_BLOCK) + 63;
nf_look_up_table_block = nf_buf_free[gl_buf_free_idx] & 0x7FFF;
gl_buf_free_idx++;
if (gl_buf_free_idx > nf_spare_block)
{
gl_buf_free_idx = 0;
}
/* target addres */
gl_address = ((Uint32)(nf_look_up_table_block) << NF_SHIFT_SECTOR_BLOCK);
nf_lut_index[nf_zone] = 0;
}
else
{
/* source address */
address2 = ((Uint32)(nf_look_up_table_block)<<NF_SHIFT_SECTOR_BLOCK) + nf_lut_index[nf_zone];
nf_lut_index[nf_zone]++;
/* target address */
gl_address = address2 + 1;
}
/* prepare a copy back command to update the lut */
Nf_wait_busy();
Nf_send_command (NF_READ_CMD);
Nf_send_address (0x00); /* column address Byte 0 */
Nf_send_address (0x00); /* column address Byte 1 */
Nf_send_address (((Byte*)&address2)[3]); /* row address Byte 0 */
Nf_send_address (((Byte*)&address2)[2]); /* row address Byte 1 */
if (NF_5_CYCLE_ADDRESS_BIT) /* size of nf > 128Mbytes ? */
Nf_send_address ( ((Byte*)&address2)[1] ); /* row address Byte 2 */
Nf_send_command (NF_COPY_BACK_CMD);
start = nf_block_min << 1;
Nf_wait_busy();
Nf_send_command (NF_RANDOM_DATA_INPUT_CMD);
Nf_send_address (((Byte*)&start)[1]); /* Column address Byte 0 */
Nf_send_address (((Byte*)&start)[0]); /* Column address Byte 1 */
Nf_send_address ( ((Byte*)&gl_address)[3] ); /* Row address Byte 0 */
Nf_send_address ( ((Byte*)&gl_address)[2] ); /* Row address Byte 1 */
if (NF_5_CYCLE_ADDRESS_BIT) /* Size of nf > 128Mbytes ? */
Nf_send_address ( ((Byte*)&gl_address)[1] ); /* Row address Byte 2 */
for (i = 0; i <= nf_gl_buf_idx_max; i++)
{
Nf_wr_byte (nf_buf[i].b[0]);
Nf_wr_byte (nf_buf[i].b[1]);
}
Nf_send_command (NF_RANDOM_DATA_INPUT_CMD); /* spare block */
Nf_send_address(0xD0); /* logical block >= 1000 */
Nf_send_address(0x07);
for (i = 0; i <= 23; i++)
{
Nf_wr_byte (nf_buf_free[i]>>8);
Nf_wr_byte (nf_buf_free[i]);
}
Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* spare area */
Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xE8); Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xE8);
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* valid the page programmation */
nf_gl_buf_idx_max = 0; /* reset the max buff index */
if (nf_lut_index[nf_zone] == 0) /* erase old lut */
{
address2 = (Uint32)(nf_lut_block[nf_zone])<<NF_SHIFT_SECTOR_BLOCK;
nf_lut_block[nf_zone] = nf_look_up_table_block; /* update address of look up table */
Nf_wait_busy();
Nf_send_command (NF_BLOCK_ERASE_CMD); /* Auto Block Erase Setup */
Nf_send_address ( ((Byte*)&address2)[3] ); /* row address Byte 1 */
Nf_send_address ( ((Byte*)&address2)[2] ); /* row address Byte 2 */
if (NF_5_CYCLE_ADDRESS_BIT) /* size of nf > 128Mbytes ? */
Nf_send_address ( ((Byte*)&address2)[1] ); /* row address Byte 3 */
Nf_send_command(NF_BLOCK_ERASE_CONFIRM_CMD); /* erase command */
}
}
/*F**************************************************************************
* NAME: nf_mark_bad_block
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Write 0x00 on block status byte (Byte 5 of spare data)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void nf_mark_bad_block (void)
{
Nf_wait_busy();
Nf_write_open_spare_area(gl_address, 0x03);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_send_command (NF_PAGE_PROGRAM_CMD); /* Send program command */
}
/*F**************************************************************************
* NAME: nf_block_erase
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* OK : erase done
* KO : erase not done
*----------------------------------------------------------------------------
* PURPOSE: Erase a block on Nand Flash Media
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
bit nf_block_erase (Uint32 pos)
{
Nf_wait_busy();
Nf_send_command (NF_BLOCK_ERASE_CMD); /* Auto Block Erase Setup */
Nf_send_address ( ((Byte*)&pos)[3] ); /* Row address Byte 0 */
Nf_send_address ( ((Byte*)&pos)[2] ); /* Row address Byte 1 */
if (NF_5_CYCLE_ADDRESS_BIT) /* Size of card >= 128Mbytes ? */
Nf_send_address ( ((Byte*)&pos)[1] ); /* Row address Byte 2 */
Nf_send_command(NF_BLOCK_ERASE_CONFIRM_CMD);/* Erase command */
return OK;
}
/*F**************************************************************************
* NAME: nf_erase_all_block
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* OK : erase done
* KO : erase not done
*----------------------------------------------------------------------------
* PURPOSE:
* This function erase all blocks on a NF and start the lut construction
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* This function use the global variable Uint32 gl_address
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
bit nf_erase_all_block (void)
{
Byte i;
Uint16 j;
Uint16 block;
Uint16 block_reserved_space;
bit erase;
Nf_CS_ON();
/* Erase all block */
block_reserved_space = (Uint16) (((nf_reserved_space_start >> NF_SHIFT_SECTOR_BLOCK) % 1000));
gl_address = 0;
for (nf_zone = 0; nf_zone < NF_ZONE_MAX_CPT; nf_zone++)
{
for (j = 1024; j != 0; j--)
{
Nf_wait_busy();
Nf_read_open_spare_area(gl_address, 0x05); /* Read block status byte */
erase = TRUE;
if (Nf_rd_byte() != 0xFF) /* if bad block */
{
erase = FALSE; /* don't erase block */
}
else
{
((Byte*)&block)[0] = Nf_rd_byte(); /* Read logical block address */
((Byte*)&block)[1] = Nf_rd_byte();
if ( (MEM_RESERVED_SIZE != 0) && (nf_zone == (NF_ZONE_MAX_CPT - 1)))
{
if ((((Byte*)&block)[0] & 0xF8) == 0x10)
{
block = (block & 0x0FFF) >> 1;
if (block >= block_reserved_space) /* If it is a reserved block */
{
erase = FALSE; /* Don't erase block */
}
}
}
}
if (NF_FULL_CHIP_ERASE == FALSE)
{
if (((Byte*)&block)[0] != 0xE8)
{
erase = FALSE;
}
}
else
{
if (((Byte*)&block)[0] == 0x00)
{
erase = FALSE;
}
}
if (erase)
{
nf_block_erase(gl_address);
if (nf_check_status() == KO)
{
nf_mark_bad_block(); /* Failure on erase operation */
}
else
{ /* Fill redundant area with 0x00 */
Nf_write_open_spare_area(gl_address, 0x00);
for (i = 16; i != 0; i--)
Nf_wr_byte(0x00);
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* Valid the page programmation */
if ( nf_check_status() == KO)
{
nf_mark_bad_block(); /* Failure on program operation */
}
else
{
Nf_read_open_spare_area(gl_address, 0x00); /* Read 16 bytes */
i = 16;
while ((i != 0) && (Nf_rd_byte() == 0x00)) i--;
if (i)
{
nf_mark_bad_block();
}
else
{
nf_block_erase(gl_address); /* Finally, erase the block */
if ( nf_check_status() == KO)
{ /* Failure on erase operation */
nf_mark_bad_block();
}
}
}
}
}
gl_address += NF_PAGE_PER_BLOCK;
}
}
Nf_CS_OFF();
return (nf_read_spare_byte());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -