📄 nf_2k.c
字号:
/*C**************************************************************************
* NAME: nf_2k.c
*----------------------------------------------------------------------------
* Copyright (c) 2002 Atmel.
*----------------------------------------------------------------------------
* RELEASE: snd1c-demo-hdd-0_2_0
* REVISION: 1.2
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains the high level NF with 2Kb page routines
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "..\src\system\config.h" /* system configuration */
#include "lib_demob\board.h" /* board definition */
#include "lib\usb\usb_drv.h" /* usb driver definition */
#include "nf_2k.h" /* NF 2Kb definition */
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
xdata Byte nf_send_cmd At(NF_CMD_LATCH_ENABLE_ADD); /* Command */
xdata Byte nf_send_add At(NF_ADD_LATCH_ENABLE_ADD); /* Address */
xdata Byte volatile nf_data At(NF_ADDRESS_CMD_DATA); /* Data */
/*_____ D E C L A R A T I O N ______________________________________________*/
extern data Uint32 gl_ptr_mem; /* memory data pointer */
extern pdata Byte gl_buffer[];
data Uint32 nf_current_physical_sector_addr; /* give the address of the current sector */
data Uint32 gl_address; /* general address variable */
data Uint16 nf_look_up_table_block; /* look up table address (block) */
data Byte nf_zone; /* current zone number */
data Byte nf_gl_buf_idx;
data Byte gl_buf_free_idx ;
bdata bit nf_block_used; /* Set when a block was used */
bdata bit nf_busy; /* indicate that nand flash is busy */
bdata bit nf_lut_modified; /* indicate that LUT have been modified */
bdata bit nf_wr_open; /* indicate that a write command is required */
xdata Uint16 nf_logical_block; /* Current logical block value */
xdata Uint16 nf_lut_block[NF_ZONE_MAX]; /* LUT address (block value) */
xdata Uint16 nf_buf_free[24]; /* contain free physical block address */
xdata Uint16 nf_spare_block; /* number of free block */
xdata Uint16 nf_block_to_be_deleted; /* give the address of the next deleted block */
xdata Uint16 nf_block_min; /* address of the first block in the buffer */
xdata Uint16 nf_block_max; /* address of the last block in the buffer */
xdata Union16 nf_buf[NF_BUFFER_SIZE]; /* Buffer for write operation */
xdata Byte nf_old_zone; /* for zone change */
xdata Byte nf_lut_index[NF_ZONE_MAX]; /* give the index for updating LUT */
xdata Byte nf_spare_block_number[NF_ZONE_MAX]; /* Number of free spare block in each zone */
idata Byte nf_gl_buf_idx_max; /* max index for updating LUT */
idata Uint16 gl_cpt_page; /* internal page counter (byte access) */
xdata Uint32 nf_mem_size;
xdata Uint32 save_gl_ptr_mem;
xdata Uint32 save_physical_sector;
bit nf_write_advanced;
bit nf_close_write_session;
#if (NF_CAPACITY_AUTO_DETECT == TRUE) /* If autodetect capacity nand flash is active */
xdata Byte nf_zone_max; /* nf_zone_max definition */
xdata Byte nf_device_type; /* nf_device_type definition */
bdata bit NF_5_CYCLE_ADDRESS_BIT; /* NF_5_CYCLE_ADDRESS_BIT definition */
#endif
extern void fat_format (void);
/*F**************************************************************************
* NAME: nf_force_write_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* End of write close function.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void nf_force_write_close(void)
{
/* Copy last part of a block if the block was assigned */
if (nf_block_used)
{
if ( (gl_ptr_mem & 0x3F) || (gl_cpt_page != 0) )
nf_copy_block_tail();
nf_block_erase((Uint32)(nf_block_to_be_deleted) << NF_SHIFT_SECTOR_BLOCK); /* Erase old block */
Nf_wait_busy();
}
}
/*F**************************************************************************
* NAME: read_spare_byte
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* read spare data byte and construct the look up table.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit read_spare_byte(void)
{
Byte byte_5;
Byte byte_6;
Byte lut_is_present;
Byte page;
Uint16 block;
Uint16 j; /* counter */
Uint16 i; /* counter */
Uint16 start; /* start value for the construction of the LUT */
Uint16 end; /* final value */
Uint16 free_bloc_pos;
Union16 block2;
bit block_valid;
Uint32 temp_address;
xdata Uint16 last_physical_used_block[NF_ZONE_MAX]; /* Last physical block used */
Nf_CS_ON();
lut_is_present = 0x00;
for (i = 0; i < NF_ZONE_MAX; i++)
{
nf_lut_index[i] = 0; /* LUT index */
}
/***************************************************/
/* For each zone, this following lines : */
/* Search bad block */
/* Determine if a LUT is present */
/* Determine the last physical block */
/***************************************************/
gl_address = 0;
Nf_wait_busy();
for (i = 0; i < NF_ZONE_MAX_CPT; i++) /* for each zone */
{
nf_spare_block_number[i] = 23; /* Initialize the spare block table */
last_physical_used_block[i] = (Uint16)(i) << NF_SHIFT_BLOCK_ZONE; /* Initialize the last used physical block value */
for (j = NF_BLOCK_PER_ZONE; j != 0; j--)
{
Nf_read_open_spare_area(gl_address, 0x05);
if ( Nf_rd_byte() != 0xFF ) /* block status : valid/invalid */
{
nf_spare_block_number[i]--; /* Defect block */
}
else /* Block is valid */
{
byte_6 = Nf_rd_byte(); /* Determine is the block is a specific block */
if ( (byte_6 == 0x00) || /* 0x00 = specific block */
( (byte_6 != 0xFF) && ( (byte_6 & 0xF8) != 0x10 ) && /* Value in redundant spare area not correct */
(byte_6 != 0xE8) ) /* Don't care about LUT block */
)
{
nf_spare_block_number[i]--; /* specific or invalid block */
}
else
{
if (byte_6 == 0xE8) /* look up table ? */
{
lut_is_present |= (0x01) << i;
nf_lut_block[i] = gl_address >> NF_SHIFT_SECTOR_BLOCK;
}
if ( (byte_6 & 0xF8) == 0x10) /* Determine the last physical used block */
{
last_physical_used_block[i] = gl_address >> NF_SHIFT_SECTOR_BLOCK;
}
}
}
gl_address += NF_PAGE_PER_BLOCK;
}
}
for (i = 0; i < NF_ZONE_MAX_CPT; i++)
{ /* Determine the index for each correct lut */
if ((lut_is_present & (0x01 << i)))
{
gl_address = (Uint32)(nf_lut_block[i]) << NF_SHIFT_SECTOR_BLOCK;
do
{
Nf_read_open_spare_area(gl_address, 0x06);
nf_lut_index[i]++;
gl_address++;
}
while (Nf_rd_byte() == 0xE8); /* OK for this test */
nf_lut_index[i]-=2;
gl_address-=2;
Nf_read_open_spare_area(gl_address, 0x00);
if (Nf_rd_byte() != 0xFF) /* LUT have been modified */
{
nf_block_erase(gl_address);
lut_is_present &= ~((0x01) << i); /* Reset bit */
nf_lut_index[i] = 0;
Nf_wait_busy();
}
}
}
/*****************************************************************/
/* Find free physical block for LUT for each zone */
/*****************************************************************/
for (i = 0; i < NF_ZONE_MAX_CPT; i++)
{
if (!(lut_is_present & (0x01 << i)))
{
block = last_physical_used_block[i];
start = (Uint16)(i) << NF_SHIFT_BLOCK_ZONE; /* starting value for each zone */
j = 0;
if (block == start) /* starting block for scan */
{
block = start + 1023;
}
block_valid = FALSE; /* init flag block valid */
do
{
gl_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
Nf_read_open_spare_area(gl_address, 0x05);
byte_5 = Nf_rd_byte();
byte_6 = Nf_rd_byte();
if ( (byte_5 == 0xFF) && (byte_6 == 0xFF) ) /* not assigned and valid block */
{
block_valid = TRUE; /* find a correct block */
}
else /* else decrease block number */
{
if (block == start)
{
block = start + 1023;
}
else
{
block--;
}
}
j++;
}
while ( (!block_valid) && (j <= 1024) );
if (j == 1024)
{
return KO;
}
nf_lut_block[i] = block; /* Update look up table address */
}
}
/**********************************************************/
/* Create the look-up table */
/* Process is done zone per zone and by step of 256 blocks*/
/* ********************************************************/
for (nf_zone = 0; nf_zone < NF_ZONE_MAX_CPT; nf_zone++) /* for each zone */
{
if ( !(lut_is_present & (0x01 << nf_zone))) /* If not LUT */
{
start = 0x00;
end = 0x80;
free_bloc_pos = (Uint16)(last_physical_used_block[nf_zone]);
gl_address = (Uint32)(nf_lut_block[nf_zone]) << NF_SHIFT_SECTOR_BLOCK;
page = 0;
do
{
nf_init_buffer();
temp_address = (Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE;
block = (Uint16)(nf_zone) << NF_SHIFT_BLOCK_ZONE;
Nf_wait_busy();
for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--) /* for each block */
{
Nf_read_open_spare_area(temp_address, 0x05);
if (Nf_rd_byte() == 0xFF) /* If not a bad block */
{
block2.b[0] = Nf_rd_byte(); /* Read logical block address */
block2.b[1] = Nf_rd_byte();
byte_6 = block2.b[1] << 1;
if ( (block2.b[0] & 0xF8) == 0x10)
{
block2.w = (block2.w & 0x0FFF) >> 1;
if ( (block2.w < end) && (block2.w >= start))
{
if (gl_buffer[byte_6] != 0xFF)
{
/* This physical block is already affected */
/* We have to find block to be deleted */
Nf_read_open_spare_area(temp_address, 0x11); /* Byte addr 11 -> block have to be deleted */
if (Nf_rd_byte() == 0x00) /* This is the block to be deleted */
{
nf_block_to_be_deleted = block;
block = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
}
else
{
nf_block_to_be_deleted = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
gl_buffer[byte_6] = block >> 8;
gl_buffer[byte_6 + 1] = block;
}
/* for the last block, find the last sector used */
temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
do
{
Nf_read_open_spare_area(temp_address, 0x06);
(((Byte*)&temp_address)[3])++;
}
while (Nf_rd_byte() != 0xFF);
(((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;
}
nf_force_write_close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -