📄 nf_drv.c
字号:
Nf_send_command (NF_PAGE_PROGRAM_CMD);
Nf_wait_busy();
/* 2nd half page */
Nf_read_open_B_area(gl_address, 0x00); /* Read Open */
nf_upload_buffer();
Nf_write_open_B_area(nf_current_physical_sector_addr, 0x00); /* Write open */
nf_download_buffer();
nf_update_spare_data(); /* Update spare data */
Nf_send_command (NF_PAGE_PROGRAM_CMD);
nf_current_physical_sector_addr++;
gl_address++;
}
Nf_wait_busy();
}
/*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 dummy;
Byte j, k;
Uint16 start;
Uint16 block_start;
Uint32 address2; /* LUT Source address */
char pdata *ptr = gl_buffer; /* gl_buffer pointer */
if (nf_lut_index[nf_zone] == 7)
{
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 /*(nf_spare_block_number[nf_zone]>>1)*/ )
{
gl_buf_free_idx = 0;
}
}
/* Source address */
address2 = ((Uint32)(nf_look_up_table_block)<<5) + 28;
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 /*(nf_spare_block_number[nf_zone]>>1)*/ )
{
gl_buf_free_idx = 0;
}
/* Target address : recopy the last 2Kb */
gl_address = ((Uint32)(nf_look_up_table_block) << 5);
nf_lut_index[nf_zone] = 0;
}
else
{
/* Source address */
address2 = ((Uint32)(nf_look_up_table_block)<<5) + ((Uint32)(nf_lut_index[nf_zone])<<2);
nf_lut_index[nf_zone]++;
/* Target address */
//address = (Uint32)(nf_look_up_table_block) << 5 + (Uint32)(lut_index[nf_zone]<<2);
gl_address = address2 + 4;
}
j = 0;
k = 0;
start = 0;
block_start = nf_block_min;
do
{
Nf_wait_busy();
Nf_read_open_A_area(address2, 0x00); /* Open look-up table in read mode */
nf_upload_buffer();
while ((k <= nf_gl_buf_idx_max) && (start <= block_start) && ((start + 128) > block_start))
{
gl_buffer[(2 * (block_start & 0x7F))] = nf_buf[k].b[0];
gl_buffer[(2 * (block_start & 0x7F))+1] = nf_buf[k].b[1];
k++;
block_start++;
}
Nf_write_open_A_area(gl_address, 0x00);
nf_download_buffer(); /* Write 256 bytes from the buffer */
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* Valid the page programmation */
start += 128;
Nf_wait_busy(); /* Wait for R/B signal */
Nf_read_open_B_area(address2, 0x00); /* Open look-up table in read mode */
nf_upload_buffer();
while ((k <= nf_gl_buf_idx_max) && (start <= block_start) && ((start + 128) > block_start))
{
gl_buffer[(2 * (block_start & 0x7F))] = nf_buf[k].b[0];
gl_buffer[(2 * (block_start & 0x7F))+1] = nf_buf[k].b[1];
k++;
block_start++;
}
if (start >= 896) /* This part update the free physical block table */
{
dummy = 208;
for (j = 0; j <= 23; j++)
{
gl_buffer[dummy] = nf_buf_free[j]>>8;
gl_buffer[dummy+1] = nf_buf_free[j];
dummy += 2;
}
}
Nf_write_open_B_area(gl_address, 0x00);
nf_download_buffer(); /* Write 256 bytes from the buffer */
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(0xE8);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
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);
/* Valid the page programmation */
Nf_send_command(NF_PAGE_PROGRAM_CMD);
start += 128;
address2++;
gl_address++;
}
while (start < 1024);
nf_gl_buf_idx_max = 0;
if (nf_lut_index[nf_zone] == 0)
{
address2 = (Uint32)(nf_lut_block[nf_zone])<<5;
nf_lut_block[nf_zone] = nf_look_up_table_block; /* Update address of look up table */
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*)&address2)[3] ); /* 2nd address cycle*/
Nf_send_address ( ((Byte*)&address2)[2] ); /* 3rd address cycle*/
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address ( ((Byte*)&address2)[1] ); /* 4th address cycle*/
Nf_send_command(NF_BLOCK_ERASE_CONFIRM_CMD); /* Erase command */
}
Nf_wait_busy();
}
/*F**************************************************************************
* NAME: nf_mark_bad_block
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Write 0x00 on block status byte (Byte 5 of spare data)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void nf_mark_bad_block (void)
{
Nf_wait_busy();
Nf_write_open_C_area(gl_address, 0x00);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
Nf_wr_byte(0x00);
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(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_send_command (NF_PAGE_PROGRAM_CMD); /* Send program command */
Nf_wait_busy();
}
/*F**************************************************************************
* NAME: nf_block_erase
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* OK : erase done
* KO : erase not done
*----------------------------------------------------------------------------
* PURPOSE: Erase a block on Nand Flash Media
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
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:
* OK : erase done
* KO : erase not done
*----------------------------------------------------------------------------
* PURPOSE:
* This function erase all blocks on a NF card and write CIS information
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
bit nf_erase_all_block (void)
{
char pdata *ptr = gl_buffer;
bit bad_block_detect;
Uint16 i;
Nf_CS_ON();
/* Erase all block */
for (gl_address = (Uint32)(NF_ZONE_MAX) * 1024 * 32 - 32; gl_address != 0; gl_address-=32)
{
Nf_wait_busy();
Nf_read_open_C_area(gl_address, 0x05); /* Read block status byte */
if (Nf_rd_byte() == 0xFF) /* Not a bad block */
{
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_wait_busy();
Nf_write_open_C_area(gl_address, 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);
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 */
}
Nf_wait_busy();
Nf_read_open_C_area(gl_address, 0x00); /* Read 16 bytes */
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
{
nf_block_erase(gl_address); /* Finally, erase the block */
if ( nf_check_status() == KO)
{ /* Failure on erase operation */
nf_mark_bad_block();
}
}
}
}
}
/* Reconstruct the CIS */
nf_block_erase(0x00);
Nf_wait_busy();
gl_address = 0x00;
Nf_write_open_A_area(gl_address, 0x00); /* Open in write mode at the address 0x00 */
for (i = 0; i < 256; i++)
{
(*ptr++) = nf_cis_table[i]; /* Save CIS table in gl_buffer */
}
nf_download_buffer(); /* write buffer 2 times */
nf_download_buffer();
Nf_wr_byte(0xFF); /* 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(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);
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* Valid the page programmation */
Nf_wait_busy();
read_spare_byte();
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -