📄 nf_drv.c
字号:
/*C**************************************************************************
* NAME: nf_drv.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE: snd1c-refd-nf-4_0_3
* REVISION: 1.7
*----------------------------------------------------------------------------
* 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 low driver definition */
/*_____ M A C R O S ________________________________________________________*/
#if NF_CONFIG != NF_1X_2KB
#error NF_CONFIG must be defined with NF_1X_2KB in config.h
#endif
#ifndef NF_CAPACITY_AUTO_DETECT
#error NF_CAPACITY_AUTO_DETECT must be defined in board.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 xdata Byte volatile nf_data; /* Data */
extern xdata Byte nf_send_cmd; /* Command */
extern xdata Byte nf_send_add; /* Address */
extern pdata Byte gl_buffer[]; /* global buffer of 256 bytes */
extern data Uint32 gl_ptr_mem; /* memory data pointer */
extern data Byte nf_zone;
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 Uint16 gl_cpt_page; /* internal page counter (byte access) */
extern idata Byte nf_gl_buf_idx_max; /* maximum position index */
extern idata Uint16 nf_logical_block; /* Logical block value (0 < x <1023) */
extern bdata bit nf_busy; /* general busy flag */
extern xdata Uint32 nf_disk_size; /* Size of the disk in sector - 1 */
extern xdata Uint16 nf_buf_free[24]; /* free block buffer */
extern xdata Uint16 nf_block_to_be_deleted; /* address of block to be deleted */
extern xdata Uint16 nf_block_min; /* position of first block in the buffer */
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_number[NF_ZONE_MAX];/* Number of free spare block in each zone */
extern xdata Byte nf_lut_index[NF_ZONE_MAX]; /* Index of the last valid LUT */
extern xdata Byte nf_spare_block; /* Number of free block */
idata Uint16 nf_redundant_logical_block_value;/* Logical block value in the redundant area*/
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_5_CYCLE_ADDRESS_BIT; /* NF_5_CYCLE_ADDRESS_BIT 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();
Nf_send_command(NF_READ_STATUS_CMD); /* Status Type Command */
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)
{
Byte temp;
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);
if (gl_cpt_page != 0)
{
temp = (gl_cpt_page) >> 9;
temp = (0x01) << temp;
temp = ~temp;
Nf_wr_byte(temp);
}
else
{
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;
/* Calculate the current physical sector */
for (j = (((Byte*)&gl_ptr_mem)[3] & 0x3F); j != 0 ; j--)
{
Nf_wait_busy();
Nf_write_open_spare_area (nf_current_physical_sector_addr, 0x00);
nf_update_spare_data (); /* Update spare data */
Nf_send_command (NF_PAGE_PROGRAM_CMD); /* Send program command */
((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;
gl_address = (Uint32)(nf_block_to_be_deleted) << NF_SHIFT_SECTOR_BLOCK; /* address of the source physical block */
for (j = (((Byte*)&gl_ptr_mem)[3] & 0x3F); j != 0; j--)
{
Nf_wait_busy();
Nf_send_command (NF_READ_CMD);
Nf_send_address (0x00);
Nf_send_address (0x00);
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_send_command (NF_COPY_BACK_CMD); /* Send copy back command */
Nf_wait_busy();
Nf_send_command (NF_RANDOM_DATA_INPUT_CMD);
Nf_send_address (0x00);
Nf_send_address (0x00);
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[3] ); /* Row address Byte 0 */
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[2] ); /* Row address Byte 1 */
if (NF_5_CYCLE_ADDRESS_BIT) /* Size of card >= 128Mbytes ?*/
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* Row address Byte 2 */
Nf_send_command (NF_PAGE_PROGRAM_CMD);
((Byte*)&nf_current_physical_sector_addr)[3]++; /* increase addresses */
((Byte*)&gl_address)[3]++;
}
if (gl_cpt_page != 0)
{
nf_init_buffer();
Nf_wait_busy();
Nf_send_command (NF_READ_CMD);
Nf_send_address (0x00);
Nf_send_address (0x00);
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_send_command (NF_COPY_BACK_CMD);
Nf_wait_busy();
Nf_send_command (NF_RANDOM_DATA_INPUT_CMD);
Nf_send_address ( ((Byte*)&gl_cpt_page)[1] );
Nf_send_address ( ((Byte*)&gl_cpt_page)[0] );
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[3] ); /* Row address Byte 0 */
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[2] ); /* Row address Byte 1 */
if (NF_5_CYCLE_ADDRESS_BIT) /* Size of card >= 128Mbytes ? */
Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* Row address Byte 2 */
for (j = (gl_cpt_page / 512); j < 4; j++)
{
nf_download_buffer();
nf_download_buffer();
}
nf_update_spare_data();
Nf_send_command (NF_PAGE_PROGRAM_CMD);
}
}
/*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) << NF_SHIFT_SECTOR_BLOCK) +
(((Byte*)&gl_ptr_mem)[3] & 0x3F);
if (gl_cpt_page != 0)
{
nf_init_buffer();
Nf_wait_busy();
Nf_send_command (NF_READ_CMD);
Nf_send_address (0x00);
Nf_send_address (0x00);
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_COPY_BACK_CMD); /* send copy back command */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -