📄 nf_drv.c
字号:
/*C**************************************************************************
* NAME: nf_drv.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE: snd1c-refd-nf-4_0_3
* REVISION: 1.17
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains the NF driver routines
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h" /* system configuration */
#include "board.h" /* board definition */
#include "nf_drv.h" /* nf driver definition */
/*_____ M A C R O S ________________________________________________________*/
#ifndef NF_CAPACITY_AUTO_DETECT
#error NF_CAPACITY_AUTO_DETECT must be defined in board.h
#endif
#if NF_CONFIG != NF_1X_512B
#error NF_CONFIG must be defined with NF_1X_512B in config.h
#endif
#ifndef NF_FULL_CHIP_ERASE
#error NF_FULL_CHIP_ERASE must be set to TRUE or FALSE in config.h
#endif
#ifndef MEM_RESERVED_SIZE
#error MEM_RESERVED_SIZE must be defined in config.h
#endif
/*_____ D E F I N I T I O N ________________________________________________*/
/*_____ D E C L A R A T I O N ______________________________________________*/
extern pdata Byte gl_buffer[]; /* global buffer of 256 bytes */
extern xdata Byte nf_send_cmd; /* Command */
extern xdata Byte nf_send_add; /* Address */
extern xdata Byte volatile nf_data; /* Data */
extern data Uint32 gl_ptr_mem; /* memory data pointer */
extern data Uint32 gl_address; /* general address variable */
extern data Uint16 nf_look_up_table_block; /* Look up table block address */
extern data Byte gl_buf_free_idx ;
extern data Byte nf_gl_buf_idx;
extern data Uint32 nf_current_physical_sector_addr; /* give the address of the current sector */
extern data Byte nf_zone;
idata Uint16 nf_redundant_logical_block_value;/* Logical block value in the redundant area */
extern idata Byte nf_gl_buf_idx_max;
extern idata Uint16 nf_logical_block;
extern xdata Byte nf_spare_block_number[NF_ZONE_MAX];/* Number of free spare block in each zone */
extern xdata Byte nf_lut_index[NF_ZONE_MAX];
extern xdata Uint16 nf_buf_free[24];
extern xdata Uint16 nf_block_to_be_deleted;
extern xdata Uint16 nf_block_min;
extern xdata Uint16 nf_lut_block[NF_ZONE_MAX]; /* LUT address in block value */
extern xdata Union16 nf_buf[NF_BUFFER_SIZE]; /* Buffer for write operation: contains physical block address */
extern xdata Byte nf_spare_block;
extern xdata Uint32 nf_reserved_space_start;
#if (NF_CAPACITY_AUTO_DETECT == TRUE) /* If autodetect capacity nand flash is active */
extern xdata Byte nf_zone_max; /* nf_zone_max definition */
extern xdata Byte nf_device_type; /* nf_device_type definition */
extern bdata bit nf_4_cycle_address; /* nf_4_cycle_address definition */
#endif
/*F**************************************************************************
* NAME: nf_check_status
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* NF status
*----------------------------------------------------------------------------
* PURPOSE:
* Check the status of the device after a program or an erase operation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
bit nf_check_status (void)
{
Nf_wait_busy();
/* Status Type Command */
Nf_send_command(NF_READ_STATUS_CMD);
if ( (Nf_rd_byte() & 0x01) == 0x00)
{
return OK;
}
else
{
return KO;
}
}
/*F**************************************************************************
* NAME: nf_calc_logical_block
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Calculate the logical block value (used in spare data area)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void nf_calc_logical_block (void)
{
volatile Byte dummy;
bdata bit parity_bit;
nf_redundant_logical_block_value = (nf_logical_block << 1) + 0x1000;
/* Parity bit calculation */
dummy = nf_redundant_logical_block_value;
parity_bit = P;
dummy = ((Byte*)&nf_redundant_logical_block_value)[0];
if (P) parity_bit = ~parity_bit;
if (parity_bit)
nf_redundant_logical_block_value++;
}
/*F**************************************************************************
* NAME: nf_update_spare_data
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Update the value of the logical block on the spare data area
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void nf_update_spare_data (void)
{
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(nf_redundant_logical_block_value >> 8);
Nf_wr_byte(nf_redundant_logical_block_value );
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(nf_redundant_logical_block_value >> 8 );
Nf_wr_byte(nf_redundant_logical_block_value );
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
Nf_wr_byte(0xFF);
}
/*F**************************************************************************
* NAME: nf_init_spare
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Initialize logical value in redundant data when a block is opened for
* the first time and the sector inside the block is not 0.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void nf_init_spare (void)
{
Byte j;
for (j = (((Byte*)&gl_ptr_mem)[3] & NF_BLOCK_MASK); j != 0 ; j--)
{
Nf_wait_busy();
/* Write open on spare data area */
Nf_write_open_C_area(nf_current_physical_sector_addr, 0x00);
/* Update spare data */
nf_update_spare_data();
/* Send program command */
Nf_send_command (NF_PAGE_PROGRAM_CMD);
((Byte*)&nf_current_physical_sector_addr)[3]++;
}
}
/*F**************************************************************************
* NAME: nf_copy_block_head
*----------------------------------------------------------------------------
* PARAMS:
* block : physical block number
* nb_sector : number of sector to be copied
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Copy the first part of a block that is not modified during a write
* operation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void nf_copy_block_head (void)
{
Byte j;
/* address of the source physical block */
gl_address = (Uint32)(nf_block_to_be_deleted) << 5;
for (j = (((Byte*)&gl_ptr_mem)[3] & NF_BLOCK_MASK); j != 0; j--)
{
/* 1st half page */
Nf_wait_busy();/* Read Open */
Nf_read_open_A_area(gl_address, 0x00);
nf_upload_buffer();
Nf_write_open_A_area(nf_current_physical_sector_addr, 0x00); /* Write open */
nf_download_buffer();
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();
/* Update spare data */
nf_update_spare_data();
Nf_send_command (NF_PAGE_PROGRAM_CMD);
/* increase counter */
((Byte*)&nf_current_physical_sector_addr)[3]++;
((Byte*)&gl_address)[3]++;
}
}
/*F**************************************************************************
* NAME: nf_copy_block_tail
*----------------------------------------------------------------------------
* PARAMS:
* block : physical block number
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Copy the last part of a block that is not modifiedd uring a write
* operation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void nf_copy_block_tail (void)
{
Byte j;
/* Address of the source block */
gl_address = ((Uint32)(nf_block_to_be_deleted) << 5) + (((Byte*)&gl_ptr_mem)[3] & NF_BLOCK_MASK);
for (j = (((Byte*)&gl_ptr_mem)[3] & NF_BLOCK_MASK); j < NF_PAGE_PER_BLOCK; j++)
{
/* 1st half page */
Nf_wait_busy();
Nf_read_open_A_area(gl_address, 0x00); /* Read Open */
nf_upload_buffer();
Nf_write_open_A_area(nf_current_physical_sector_addr, 0x00); /* Write open */
nf_download_buffer();
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);
((Byte*)&nf_current_physical_sector_addr)[3]++;
((Byte*)&gl_address)[3]++;
}
}
/*F**************************************************************************
* NAME: nf_reassign_block
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -