📄 nf.c
字号:
{
(((Byte*)&temp_address)[3])--;
}
else
{
(((Byte*)&temp_address)[3])-=2;
}
Nf_read_open_spare_area(temp_address, 0x10); /* Byte addr 10 -> internal counter page */
/* 0 <= block2 < 1024 */
gl_ptr_mem = ((Uint32)(block2.w) << NF_SHIFT_SECTOR_BLOCK) + (temp_address & 0x3F) +
((Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE);
/* 0 <= block < 1024 * zone_max */
nf_current_physical_sector_addr = ((Uint32)(block) << NF_SHIFT_SECTOR_BLOCK) + (temp_address & 0x3F);
gl_cpt_page = 0;
nf_block_used = TRUE;
byte_5 = ~(Nf_rd_byte());
while (byte_5 != 0)
{
((Byte*)&gl_cpt_page)[0] += 2;
byte_5 = byte_5 >> 1;
}
if (gl_cpt_page)
((Byte*)&gl_cpt_page)[0] -= 2;
if (((Byte*)&gl_cpt_page)[0] == 0x08)
{
gl_ptr_mem++;
nf_current_physical_sector_addr++;
gl_cpt_page = 0;
}
block = i;
temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
nf_close_write_session = TRUE;
}
else
{ /* New affected physical block */
gl_buffer[byte_6] = block >> 8;
gl_buffer[byte_6 + 1] = block;
}
}
}
}
temp_address += NF_PAGE_PER_BLOCK;
block++;
}
/* affect to the free physical block a fictive logical block */
/* free physical block => gl_buffer[x] = 0xFF */
temp_address = (Uint32)(free_bloc_pos) << NF_SHIFT_SECTOR_BLOCK;
for (i = 0; i <= 0xFE; i+=2)
{
if (gl_buffer[i] == 0xFF)
{
do /* Search free physical block */
{
temp_address += NF_PAGE_PER_BLOCK;
if (temp_address >= ((Uint32)(nf_zone + 1) << NF_SHIFT_SECTOR_ZONE) )
temp_address = (Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE;
Nf_read_open_spare_area(temp_address, 0x05);
byte_5 = Nf_rd_byte(); /* Invalid/Valid block */
byte_6 = Nf_rd_byte(); /* Used/Unused block */
}
while ( ( (byte_6 != 0xFF) && (byte_6 != 0xE8) ) || (byte_5 != 0xFF) );
free_bloc_pos = (temp_address >> NF_SHIFT_SECTOR_BLOCK);
gl_buffer[i] = ((Byte*)&free_bloc_pos)[0] | 0x80;
gl_buffer[i + 1] = ((Byte*)&free_bloc_pos)[1];
}
}
/* Update LUT */
Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);
Nf_send_address ( 0x00 ); /* Column address byte 0 */
Nf_send_address ( page ); /* 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 card >= 128Mbytes ? */
Nf_send_address ( ((Byte*)&gl_address)[1] ); /* Row address Byte 2 */
nf_download_buffer(); /* Write 256 bytes from the buffer */
page++;
if (page == 0x08)
{
/* Write redundant 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(0xE8); Nf_wr_byte(0xFF); /* Logical block value */
Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* ECC area 2 */
Nf_wr_byte(0xE8); Nf_wr_byte(0xFF); /* Logical block value */
Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* ECC area 1 */
}
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* Valid the page programmation */
start += 0x80; /* process next 128 logical block */
end += 0x80;
if (nf_close_write_session)
{
nf_close_write_session = FALSE;
nf_force_write_close();
gl_address = (Uint32)(nf_lut_block[nf_zone]) << NF_SHIFT_SECTOR_BLOCK;
}
}
while (start < NF_BLOCK_PER_ZONE);
}
}
/* Global initialization */
nf_block_min = 0xFFFF; /* Starting buffer value */
nf_gl_buf_idx_max = 0; /* Max index in the buffer */
nf_lut_modified = FALSE; /* Buffer change flag */
nf_gl_buf_idx = 0; /* Main buffer index */
gl_buf_free_idx = 0; /* Free physical buffer idx */
nf_old_zone = 0xFF; /* Previous zone number */
nf_close_write_session = FALSE;
nf_write_advanced = FALSE;
Nf_wait_busy();
return OK;
}
/*F**************************************************************************
* NAME: nf_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* NF initialisation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_init (void)
{
P2 = NF_IDLE_STATE_ADD;
Nf_CS_ON();
Nf_send_command(NF_RESET_CMD);
Nf_wait_busy();
#if (NF_CAPACITY_AUTO_DETECT == TRUE) /* Auto Detect the type of nand-flash */
Nf_send_command(NF_READ_ID_CMD);
Nf_send_address(0x00);
Nf_rd_byte(); /* Maker code */
switch (Nf_rd_byte()) /* Device code */
{
/***************************************************************************/
case NF_K9F1G08U0M : /*------- 128 Mbyte -------*/
case NF_K9F1K08Q0M : /*------- 128 Mbyte -------*/
nf_device_type = NF_SIZE_128MB;
nf_zone_max = 1; /* 1 zone : 1024 blocks */
nf_mem_size = (NF_SECTOR_SIZE_128MB + 1) * 4 - 1;
nf_5_cycle_address = 0; /* 4 address cycles */
break;
/***************************************************************************/
case NF_K9K2G08Q0M : /*------- 256 Mbyte -------*/
case NF_K9K2G08U0M : /*------- 256 Mbyte -------*/
nf_device_type = NF_SIZE_256MB;
nf_zone_max = 2; /* 2 zones : 2048 blocks */
nf_mem_size = (NF_SECTOR_SIZE_256MB + 1) * 4 - 1;
nf_5_cycle_address = 1; /* 5 address cycles */
break;
default:
return KO;
}
#else
nf_mem_size = (NF_SECTOR_SIZE + 1) * 4 - 1;
#endif
return (read_spare_byte());
}
/*F**************************************************************************
* NAME: nf_read_open
*----------------------------------------------------------------------------
* PARAMS:
* pos: address of the logic sector to read (size 512 bytes)
*
* return:
* Update memory for reading
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory read update
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_read_open (Uint32 pos)
{
Uint16 physical_block;
Nf_CS_ON();
if (nf_lut_modified)
{
if (nf_close_write_session)
{
nf_close_write_session = FALSE;
nf_force_write_close();
}
nf_reassign_block();
nf_lut_modified = FALSE;
}
gl_ptr_mem = pos >> 2;
gl_cpt_page = (pos & 0x03) << 9 ;
nf_logical_block = (gl_ptr_mem >> NF_SHIFT_SECTOR_BLOCK);
nf_zone = nf_logical_block / 1000; /* Determinate logical zone */
nf_logical_block = nf_logical_block - (1000 * (Uint16)(nf_zone)); /* Logical block value */
gl_address = ((Uint32)(nf_lut_block[nf_zone])<<NF_SHIFT_SECTOR_BLOCK)+ /* lut address calculation */
((Uint32)(nf_lut_index[nf_zone]));
Nf_wait_busy();
Nf_send_command(NF_READ_CMD); /* Open the look-up table */
Nf_send_address (nf_logical_block << 1); /* Column address Byte 0 */
Nf_send_address (nf_logical_block >> 7); /* 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 */
Nf_send_command(NF_READ_CMD2);
nf_busy = TRUE; /* Set busy flag */
nf_write_advanced = FALSE; /* Desactive write optimization */
Nf_wait_busy();
((Byte*)&physical_block)[0] = Nf_rd_byte(); /* Read the physical block number */
((Byte*)&physical_block)[1] = Nf_rd_byte();
/* Calculate the physical sector address */
nf_current_physical_sector_addr = ((Uint32)(physical_block) << NF_SHIFT_SECTOR_BLOCK) +
(((Byte*)&gl_ptr_mem)[3] & 0x3F);
Nf_CS_OFF();
return OK;
}
/*F**************************************************************************
* NAME: nf_read_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory read close
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void nf_read_close (void)
{
Nf_CS_OFF();
}
/*F*************************************************************************
* NAME: nf_read_byte
*---------------------------------------------------------------------------
* PARAMS:
*
* return:
* Data read from memory
*---------------------------------------------------------------------------
* PURPOSE:
* Low level memory read function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
****************************************************************************/
Byte nf_read_byte (void)
{
Byte b;
if (nf_busy)
{
nf_busy = FALSE;
Nf_CS_ON();
Nf_wait_busy();
Nf_send_command(NF_READ_CMD);
Nf_send_address( 0x00);
Nf_send_address( ((Byte*)&gl_cpt_page)[0]);
Nf_send_address( ((Byte*)&nf_current_physical_sector_addr)[3]);
Nf_send_address( ((Byte*)&nf_current_physical_sector_addr)[2]);
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_READ_CMD2);
Nf_wait_busy();
b = Nf_rd_byte();
Nf_CS_OFF();
}
else
{
b = Nf_rd_byte();
}
gl_cpt_page++;
if ( ((Byte*)&gl_cpt_page)[0] == 0x08) /* Detection of the end of data page */
{
gl_ptr_mem++; /* new page */
gl_cpt_page=0; /* reset column */
if ( !(((Byte*)&gl_ptr_mem)[3] & 0x3F) )/* New block ? */
{
nf_read_open(gl_ptr_mem << 2);
}
else
{
((Byte*)&nf_current_physical_sector_addr)[3]++;
nf_busy = TRUE; /* Force read open command */
}
}
return b;
}
/*F**************************************************************************
* NAME: nf_read_sector
*----------------------------------------------------------------------------
* PARAMS:
* nb_sector: number of contiguous sector to read
* global: gl_ptr_mem
*
* return: OK read done
* KO read failure
*----------------------------------------------------------------------------
* PURPOSE:
* This function is an optimized function that writes nb-sector * 512 bytes
* from NF card to USB controller
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* nb_sector always >= 1, can not be zero
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_read_sector(Uint16 nb_sector)
{
Byte i;
bit begin_ping_pong;
begin_ping_pong = TRUE;
/* Start a transfert of nb_sector * 512 bytes */
do
{
Nf_CS_ON();
if (nf_busy) /* send a read command */
{
Nf_send_command(NF_READ_CMD);
Nf_send_address( 0x00);
Nf_send_address( ((Byte*)&gl_cpt_page)[0]);
Nf_send_address( ((Byte*)&nf_current_physical_sector_addr)[3]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -