📄 nf.c
字号:
/* Write first part of LUT */
Nf_send_command(NF_READ_A_AREA_CMD); /* First half array */
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 */
/* Write 256 bytes from the buffer */
nf_download_buffer();
/* Valid the page programmation */
Nf_send_command(NF_PAGE_PROGRAM_CMD);
nf_init_buffer(); /* reinitialize the global buffer */
start += 0x80; /* process next 128 logical block */
end += 0x80;
block = (Uint16)(nf_zone) << 10;
temp_address = (Uint32)(nf_zone) << 15; /* restart from the begin of zone */
Nf_wait_busy();
for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--) /* for each block */
{
Nf_send_command (NF_READ_C_AREA_CMD); /* Read Spare data value */
Nf_send_address ( 0x05 );
Nf_send_address ( ((Byte*)&temp_address)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)&temp_address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ? */
Nf_send_address ( ((Byte*)&temp_address)[1] );/* 4th address cycle */
Nf_wait_busy();
byte_5 = Nf_rd_byte(); /* Block status byte */
if (byte_5 == 0xFF) /* If not a bad block */
{
block2.b[0] = Nf_rd_byte(); /* Read logical block address */
block2.b[1] = Nf_rd_byte();
if ( (block2.b[0] & 0xF8) == 0x10)
{
block2.w = (block2.w & 0x0FFF) >> 1;
if ( (block2.w < end) && (block2.w >= start))
{ /* Save logical block value in the buffer */
gl_buffer[2 * block2.b[1]] = block >> 8;
gl_buffer[2 * block2.b[1] + 1] = block;
}
}
}
temp_address += 32;
block++;
}
temp_address = (Uint32)(free_bloc_pos) << 5;
for (i = 0; i <= 0xFE; i += 2)
{
if (gl_buffer[i] == 0xFF)
{
do
{
temp_address += 32;
if (temp_address >= ((Uint32)(nf_zone + 1) << 15) )
temp_address = (Uint32)(nf_zone) << 15;
Nf_send_command(NF_READ_C_AREA_CMD);
Nf_send_address(0x05);
Nf_send_address ( ((Byte*)&temp_address)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)&temp_address)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ? */
Nf_send_address ( ((Byte*)&temp_address)[1] );/* 4th address cycle */
Nf_wait_busy();
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 >> 5;
gl_buffer[i] = (free_bloc_pos >> 8) + 0x80;
gl_buffer[i + 1] = free_bloc_pos;
}
}
/* Write second part of LUT */
Nf_send_command(NF_READ_B_AREA_CMD); /* Second half array */
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();
/* 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);
start += 0x80;
end += 0x80;
/* Increase the address of the conversion table */
address++;
}
while (start < NF_BLOCK_PER_ZONE);
}
}
Nf_wait_busy();
}
/*F**************************************************************************
* NAME: nf_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* NF initialisation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void nf_init (void)
{
P2 = NF_IDLE_STATE_ADD;
Nf_CS_ON();
Nf_send_command(NF_RESET_CMD);
/* Read ID code */
Nf_wait_busy();
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 (lut_modified)
{
nf_reassign_block();
lut_modified = FALSE;
}
gl_ptr_mem = pos;
gl_cpt_page = 0;
/* Determine the logical block value */
logical_block = (gl_ptr_mem >> 5);
/* Determinate zone */
nf_zone = logical_block / 1000;
/* Each zone have 1000 data blocks */
logical_block = logical_block - (1000 * (Uint16)(nf_zone));
/* Calculate the address where are the physical block value */
address = ((Uint32)(lut_block[nf_zone])<<5) + ((Uint32)(logical_block) >> 8);
address += ((Uint32)(lut_index[nf_zone])<<2);
/* Open the look-up table */
Nf_wait_busy();
if (logical_block & 0x80)
{
Nf_send_command(NF_READ_B_AREA_CMD); /* 2nd half page */
Nf_send_address( (logical_block << 1) - 256);
}
else
{
Nf_send_command(NF_READ_A_AREA_CMD); /* first half page */
Nf_send_address(logical_block << 1);
}
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();
/* Read the physical block number */
((Byte*)&physical_block)[0] = Nf_rd_byte();
((Byte*)&physical_block)[1] = Nf_rd_byte();
/* Calculate the physical sector address */
current_physical_sector_addr = ((Uint32)(physical_block) << 5) + (gl_ptr_mem & 0x1F);
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;
Nf_wait_busy();
if (!gl_cpt_page)
{
Nf_CS_ON();
Nf_send_command (NF_READ_A_AREA_CMD);
Nf_send_address ( 0x00 ); /* 1st address cycle */
Nf_send_address ( ((Byte*)¤t_physical_sector_addr)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)¤t_physical_sector_addr)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address ( ((Byte*)¤t_physical_sector_addr)[1] );/* 4th address cycle */
Nf_wait_busy();
b = Nf_rd_byte();
Nf_CS_OFF();
}
else
{
b = Nf_rd_byte();
}
gl_cpt_page++;
/* Detection of the end of data page */
if (gl_cpt_page == NF_DATA_SIZE)
{
/* read spare data bytes */
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
Nf_rd_byte();
gl_ptr_mem++; /* new page */
gl_cpt_page=0; /* start at column 0 */
if ( !(gl_ptr_mem & 0x1F) ) /* New block ? */
{
nf_read_open(gl_ptr_mem);
}
else
{
current_physical_sector_addr++;
}
}
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;
do
{
Nf_CS_ON();
Nf_wait_busy();
Nf_send_command (NF_READ_A_AREA_CMD);
Nf_send_address ( 0x00 ); /* 1st address cycle */
Nf_send_address ( ((Byte*)¤t_physical_sector_addr)[3] ); /* 2nd address cycle */
Nf_send_address ( ((Byte*)¤t_physical_sector_addr)[2] ); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address ( ((Byte*)¤t_physical_sector_addr)[1] );/* 4th address cycle */
Nf_wait_busy();
for (i = 8; i != 0; i--)
{
Usb_write_byte(Nf_rd_byte()); /* read 64 bytes from card */
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -