⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nf_drv.c

📁 c51snd1c硬盘播放器全部资料.源码.线路图.protel99se的pcb图
💻 C
📖 第 1 页 / 共 3 页
字号:
/*C**************************************************************************
* NAME:         nf_drv.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE:      snd1c-refd-nf-4_0_3      
* REVISION:     1.6     
*----------------------------------------------------------------------------
* PURPOSE: 
* This file contains the NF driver routines for 2Kb page size NF and with 2
* parallel NF
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/

#include "config.h"                             /* system configuration       */
#include "board.h"                              /* board definition           */
#include "nf_drv.h"                             /* nf 2k low 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_2X_2KB
  #error NF_CONFIG must be defined with NF_2X_2KB 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  xdata Byte volatile nf_data_even;                   /* Data                                     */
extern  xdata Byte nf_send_cmd_even;                        /* Command                                  */
extern  xdata Byte nf_send_add_even;                        /* Address                                  */
extern  xdata Byte volatile nf_data_odd;                    /* Data                                     */
extern  xdata Byte nf_send_cmd_odd;                         /* Command                                  */
extern  xdata Byte nf_send_add_odd;                         /* Address                                  */

extern  pdata Byte gl_buffer[];                             /* global buffer of 256 bytes               */
extern  data  Uint32  gl_ptr_mem;                           /* memory data pointer                      */

extern  data  Uint32 gl_address;                            /* general address variable                 */
extern  data  Uint32 nf_current_physical_sector_addr_even;  /* give the address of the current sector   */ 
extern  data  Uint32 nf_current_physical_sector_addr_odd;   /* give the address of the current sector   */ 
extern  data  Uint16  gl_cpt_page;                          /* internal page counter (byte access)      */
extern  data  Byte gl_buf_free_idx ;
extern  data  Byte nf_gl_buf_idx;
extern  data  Byte nf_zone;

extern  idata  Uint16 nf_look_up_table_block;               /* Look up table block address              */
extern  idata Byte nf_gl_buf_idx_max;                       /* maximum position index                   */


extern  bdata bit nf_busy;                                  /* general busy flag                        */
extern  bdata bit nf_parity_bit;

extern  xdata Uint32 nf_disk_size;                          /* Size of the disk in sector - 1           */ 
extern  xdata Uint16 nf_block_to_be_deleted_even;           /* address of block to be deleted           */
extern  xdata Uint16 nf_block_min;                          /* position of first block in the buffer    */
extern  xdata Uint16 nf_block_to_be_deleted_odd;            /* address of block to be deleted           */
extern  xdata Uint16 nf_lut_block[NF_ZONE_MAX];             /* LUT address in block value               */
extern  xdata Uint16 nf_logical_block;                      /* Logical block value (0 < x <1023)        */
extern  xdata Uint16 nf_spare_block;                        /* Number of free block                     */
extern  xdata t_lut  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 t_free_blocks nf_buf_free[24];                /* free block buffer                        */
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


idata Uint16 nf_redundant_logical_block_value;      /* Logical block value in the redundant area*/


/*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)
{
  if (nf_parity_bit == NF_ODD)
  {
    Nf_wait_busy_odd();
    Nf_send_command_odd(NF_READ_STATUS_CMD);  /* Status Type Command */
    if ( (Nf_rd_byte_odd() & 0x01) == 0x00)
    {  
      return OK;
    }
    }
  else
  {
    Nf_wait_busy_even();
    Nf_send_command_even(NF_READ_STATUS_CMD);  /* Status Type Command */
    if ( (Nf_rd_byte_even() & 0x01) == 0x00)
    {  
      return OK;
    }
  }
  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;
  if (nf_parity_bit == NF_ODD)
  {
    Nf_wr_byte_odd(0xFF);    Nf_wr_byte_odd(0xFF);
    Nf_wr_byte_odd(0xFF);    Nf_wr_byte_odd(0xFF);
    Nf_wr_byte_odd(0xFF);    Nf_wr_byte_odd(0xFF);
    Nf_wr_byte_odd(nf_redundant_logical_block_value >> 8);
    Nf_wr_byte_odd(nf_redundant_logical_block_value );
    Nf_wr_byte_odd(0xFF);    Nf_wr_byte_odd(0xFF);
    Nf_wr_byte_odd(0xFF);    
    Nf_wr_byte_odd(nf_redundant_logical_block_value >> 8 );
    Nf_wr_byte_odd(nf_redundant_logical_block_value );
    Nf_wr_byte_odd(0xFF);    Nf_wr_byte_odd(0xFF);
    Nf_wr_byte_odd(0xFF);
    if (gl_cpt_page != 0)
    {
      temp = (gl_cpt_page) >> 9;
      temp = (0x01) << temp;
      temp = ~temp;
      Nf_wr_byte_odd(temp);
    }
    else
    {
      Nf_wr_byte_odd(0xFF);
    }
  }
  else
  {
    Nf_wr_byte_even(0xFF);    Nf_wr_byte_even(0xFF);
    Nf_wr_byte_even(0xFF);    Nf_wr_byte_even(0xFF);
    Nf_wr_byte_even(0xFF);    Nf_wr_byte_even(0xFF);
    Nf_wr_byte_even(nf_redundant_logical_block_value >> 8);
    Nf_wr_byte_even(nf_redundant_logical_block_value );
    Nf_wr_byte_even(0xFF);    Nf_wr_byte_even(0xFF);
    Nf_wr_byte_even(0xFF);
    Nf_wr_byte_even(nf_redundant_logical_block_value >> 8 );
    Nf_wr_byte_even(nf_redundant_logical_block_value );
    Nf_wr_byte_even(0xFF);    Nf_wr_byte_even(0xFF);
    Nf_wr_byte_even(0xFF);
    if (gl_cpt_page != 0)
    {
      temp = (gl_cpt_page) >> 9;
      temp = (0x01) << temp;
      temp = ~temp;
      Nf_wr_byte_even(temp);
    }
    else
    {
      Nf_wr_byte_even(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;
  nf_parity_bit = NF_EVEN;
  for (j = ((Byte*)&gl_ptr_mem)[3] & 0x7F; j != 0 ; j--)
  {  
    if (nf_parity_bit == NF_ODD)
    {   
      Nf_active_ODD(); 
      Nf_wait_busy_odd();
      Nf_write_open_spare_area_odd (nf_current_physical_sector_addr_odd, 0x00);
      nf_update_spare_data ();                              /* update spare data     */
      Nf_send_command_odd (NF_PAGE_PROGRAM_CMD);            /* send program command  */ 
      ((Byte*)&nf_current_physical_sector_addr_odd)[3]++;
    }
    else
    {
      Nf_active_EVEN();
      Nf_wait_busy_even();
      Nf_write_open_spare_area_even (nf_current_physical_sector_addr_even, 0x00);
      nf_update_spare_data ();                   
      Nf_send_command_even (NF_PAGE_PROGRAM_CMD);      
      ((Byte*)&nf_current_physical_sector_addr_even)[3]++;;
    }
    nf_parity_bit = ~nf_parity_bit;
  }

}


/*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;
Uint32 address2;
  nf_parity_bit = NF_EVEN;

  gl_address = (Uint32)(nf_block_to_be_deleted_odd) << NF_SHIFT_SECTOR_BLOCK;    /* address of the source physical block */
  address2 = (Uint32)(nf_block_to_be_deleted_even)  << NF_SHIFT_SECTOR_BLOCK;    /* address of the source physical block */

  for (j = (((Byte*)&gl_ptr_mem)[3] & 0x7F); j != 0; j--)
  { 
    if (nf_parity_bit == NF_ODD)
    {
      Nf_active_ODD();
      Nf_wait_busy_odd();
      Nf_send_command_odd (NF_READ_CMD);
      Nf_send_address_odd (0x00);
      Nf_send_address_odd (0x00);
      Nf_send_address_odd ( ((Byte*)&gl_address)[3] );      /* Row address Byte 0           */
      Nf_send_address_odd ( ((Byte*)&gl_address)[2] );      /* Row address Byte 1           */
      if (NF_5_CYCLE_ADDRESS_BIT)                           /* Size of nf >= 128Mbytes ?    */
        Nf_send_address_odd ( ((Byte*)&gl_address)[1] );    /* Row address Byte 2           */

      Nf_send_command_odd (NF_COPY_BACK_CMD);               /* Send copy back command       */
      Nf_wait_busy_odd();
      Nf_send_command_odd (NF_RANDOM_DATA_INPUT_CMD);
      Nf_send_address_odd (0x00);
      Nf_send_address_odd (0x00);
      Nf_send_address_odd ( ((Byte*)&nf_current_physical_sector_addr_odd)[3] );     /* Row address Byte 0         */
      Nf_send_address_odd ( ((Byte*)&nf_current_physical_sector_addr_odd)[2] );     /* Row address Byte 1         */
      if (NF_5_CYCLE_ADDRESS_BIT)                                                   /* Size of nf >= 128Mbytes?   */
        Nf_send_address_odd ( ((Byte*)&nf_current_physical_sector_addr_odd)[1] );   /* Row address Byte 2         */

      Nf_send_command_odd(NF_RANDOM_DATA_INPUT_CMD);
      Nf_send_address_odd(0x10);
      Nf_send_address_odd(0x08);
      Nf_wr_byte_odd(0xEF);/* set internal page counter to 2048 */
      Nf_wr_byte_odd(0xFF);/* reset delete block marker */


      Nf_send_command_odd (NF_PAGE_PROGRAM_CMD);
      ((Byte*)&nf_current_physical_sector_addr_odd)[3]++;                           /* increase addresses         */
      ((Byte*)&gl_address)[3]++;
    }
    else
    {
      Nf_active_EVEN();
      Nf_wait_busy_even();
      Nf_send_command_even (NF_READ_CMD);
      Nf_send_address_even (0x00);
      Nf_send_address_even (0x00);
      Nf_send_address_even ( ((Byte*)&address2)[3] );       /* Row address Byte 0           */
      Nf_send_address_even ( ((Byte*)&address2)[2] );       /* Row address Byte 1           */
      if (NF_5_CYCLE_ADDRESS_BIT)                           /* Size of nf >= 128Mbytes ?    */
        Nf_send_address_even ( ((Byte*)&address2)[1] );     /* Row address Byte 2           */

      Nf_send_command_even (NF_COPY_BACK_CMD);              /* Send copy back command       */
      Nf_wait_busy_even();
      Nf_send_command_even (NF_RANDOM_DATA_INPUT_CMD);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -