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

📄 nf.c

📁 ATMEL公司AT89C51SND1为主控制器MP3源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/*C**************************************************************************
* NAME:         nf.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE:      snd1c-refd-nf-4_0_3      
* REVISION:     1.5     
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains the high level NF routines for 2 NF slots
*****************************************************************************/

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

#include "config.h"                           /* system configuration */
#include "board.h"                            /* board definition  */
#include "lib_mcu\usb\usb_drv.h"              /* usb driver definition */
#include "nf.h"                               /* NF 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_512B
  #error NF_CONFIG must be defined with NF_2X_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 ________________________________________________*/

extern  data    Uint32  gl_ptr_mem;         /* memory data pointer */
extern  pdata   Byte gl_buffer[];
extern  bdata   bit rx_bank;
extern          bit reserved_disk_space;



xdata Byte nf_send_cmd_odd At(NF_CMD_LATCH_ENABLE_ADD_ODD);       /* Command */
xdata Byte nf_send_add_odd At(NF_ADD_LATCH_ENABLE_ADD_ODD);       /* Address */
xdata Byte volatile nf_data_odd At(NF_ADDRESS_CMD_DATA_ODD);      /* Data    */

xdata Byte nf_send_cmd_even At(NF_CMD_LATCH_ENABLE_ADD_EVEN);     /* Command */
xdata Byte nf_send_add_even At(NF_ADD_LATCH_ENABLE_ADD_EVEN);     /* Address */
xdata Byte volatile nf_data_even At(NF_ADDRESS_CMD_DATA_EVEN);    /* Data    */

/*_____ D E C L A R A T I O N ______________________________________________*/

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_zone_change;                           /* indicate that we have to change zone       */
bdata bit nf_parity_bit;
bdata bit nf_reserved_space = FALSE;


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

idata Byte    nf_zone;                              /* current zone number                        */
idata Byte    nf_gl_buf_free_idx ;
idata Byte    nf_gl_buf_idx_max;                    /* max index for updating LUT                 */
idata Byte    nf_spare_block;                       /* number of free block                       */
xdata Byte    nf_old_zone;                          /* for zone change                            */  
idata Uint16  nf_logical_block;                     /* Current logical block value                */
idata Uint16  nf_look_up_table_block;               /* look up table address (block)              */
xdata Uint16  nf_block_to_be_deleted_even;          /* give the address of the next deleted block */
xdata Uint16  nf_block_to_be_deleted_odd;           /* give the address of the next deleted block */
idata Uint16  nf_block_min;                         /* address of the first block in the buffer   */
idata Byte    block_max;                            /* address of the last block in the buffer    */

xdata t_free_blocks buf_free[24];                   /* contain free physical block address        */
xdata Byte lut_index[NF_ZONE_MAX];                  /* give the index for updating LUT            */
xdata Uint16 lut_block[NF_ZONE_MAX];                /* LUT address (block value)                  */
xdata Byte nf_spare_block_number[NF_ZONE_MAX];      /* Number of free spare block in each zone    */
xdata t_lut   buf[NF_BUFFER_SIZE];                  /* Buffer for write operation                 */

xdata Uint32 nf_mem_size;
xdata Uint32 nf_reserved_space_start;

#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_4_cycle_address;                 /* nf_4_cycle_address definition                */
  bdata bit     nf_copy_back;                       /* copy back command available                  */

#endif


/**************************************************************************************
manufacturer code :
            0x98                Toshiba
            0xEC                Samsung


supported   |  device |  capacity |     |manufacturer | Copy back
by this     |   code  |           |     |S : Samsung  | supported
driver      |         |           |     |T : Toshiba  |
------------------------------------------------------------------------------------
  no        |   0x39  |    8MB    | 512 |S    (1,8V)  | no
  no        |   0xE6  |    8MB    | 512 |S - T(3,3V)  | no
  YES       |   0x73  |   16MB    | 512 |S - T(3,3V)  | no
  YES       |   0x33  |   16MB    | 512 |S    (1,8V)  | no
  YES       |   0x75  |   32MB    | 512 |S    (3,3V)  | yes
  YES       |   0x75  |   32MB    | 512 |T    (3,3V)  | no
  YES       |   0x76  |   64MB    | 512 |S    (3,3V)  | yes
  YES       |   0x76  |   64MB    | 512 |T    (3,3V)  | no
  YES       |   0x79  |  128MB    | 512 |S    (3,3V)  | yes
  YES       |   0x79  |  128MB    | 512 |T    (3,3V)  | no
  no        |   0xA1  |  128MB    |2048 |S    (1,8V)  | yes
  no        |   0xF1  |  128MB    |2048 |S    (3,3V)  | yes
  no        |   0xAA  |  256MB    |2048 |S    (1,8V)  | yes
  no        |   0xDA  |  256MB    |2048 |S    (3,3V)  | yes
**************************************************************************************/


/*F**************************************************************************
* NAME: nf_read_spare_byte
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   OK : LUT construction complete
*   KO : pb for LUT
*----------------------------------------------------------------------------
* PURPOSE:
*   read spare data byte and construct the look up table.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_read_spare_byte(void)
{
Byte    byte_5;         /* 5th byte of spare data */
Byte    byte_6;         /* 6th byte of spare data */
Byte    tmp_spare;      /* dummy 8 bits data      */
Byte    i;              /* counter                */
Uint16  j;              /* counter                */
Uint32  temp_address;   /* dummy 32 bits data     */              
Uint16  start;          /* start value for the construction of the LUT */
Uint16  end;            /* final value            */
Uint16  block;          /* 16 bits dummy data     */
Union16 block2;         /* 16 bits dummy data     */
bdata bit     block_valid;
bdata bit     lut_part;
xdata Uint16  free_bloc_pos_even;
xdata Uint16  free_bloc_pos_odd;
xdata Uint16  last_physical_used_block_even[NF_ZONE_MAX]; /* Last physical block used     */
xdata Uint16  last_physical_used_block_odd[NF_ZONE_MAX];  /* Last physical block used     */
xdata Byte    lut_is_present[NF_ZONE_MAX];


  /* Global media initialization */
  nf_old_zone        = 0xFF;
  nf_block_min       = 0xFFFF;          /* starting buffer value    */
  nf_gl_buf_idx_max  = 0;               /* max index in the buffer  */
  nf_lut_modified    = FALSE;           /* buffer change flag       */
  nf_gl_buf_idx      = 0;               /* main buffer index        */
  nf_gl_buf_free_idx = 0;               /* free physical buffer idx */
  for (i = 0; i < NF_ZONE_MAX_CPT; i++)
  {
    lut_index[i] = 0;                   /* LUT index                */
    lut_is_present[i] = FALSE;
  }

  /***************************************************/
  /* For each zone, this following lines :           */
  /*   Search bad block                              */
  /*   Determine if a LUT is present                 */
  /*   Determine the last physical block             */ 
  /***************************************************/

  Nf_CS_EVEN();
  Nf_wait_busy_even();

  for (i = 0; i < NF_ZONE_MAX_CPT; i++)                     /* for each zone */
  {
    gl_address = i << 5;   
    nf_spare_block_number[i] = 23;                          /* Initialize the spare block table */
    last_physical_used_block_even[i] = (Uint16)(i);         /* Initialize the last used physical block value */

    for (j = NF_BLOCK_PER_ZONE; j != 0; j--)                /* for each block */ 
    {
      Nf_read_open_C_area_even(gl_address, 0x05);
      if (Nf_rd_byte_even() != 0xFF )                       /* block status data : valid/invalid block */
      {
        nf_spare_block_number[i]--;                         /* Defect block */
        if (nf_spare_block_number[i] < 4)
          return KO;
      }
      else                                                  /* Block is valid */
      {
        byte_6 = Nf_rd_byte_even();                                 /* 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 */
          if (nf_spare_block_number[i] < 4)
            return KO;

        }
        else
        {
          /* Determine if the block is the look up table */ 
          if (byte_6 == 0xE8)                       /* look up table ? */
          {
            lut_is_present[i] = TRUE;
            lut_block[i] = gl_address >> 5;
          }
          /* Determine the last physical used block */
          if ( (byte_6 & 0xF8) == 0x10)             /* Used block  */
          {
            last_physical_used_block_even[i] = gl_address >> 5;
          }
        }

      }
      gl_address += (NF_ZONE_MAX_CPT << 5);
    }
  }



  Nf_CS_ODD();
  Nf_wait_busy_odd();

  for (i = 0; i < NF_ZONE_MAX_CPT; i++)                             /* for each zone */
  {
    gl_address =  i << 5;
    tmp_spare = 23;                                                 /* Initialize the spare block table */
    last_physical_used_block_odd[i] = (Uint16)(i);                  /* Initialize the last used physical block value */

    for (j = NF_BLOCK_PER_ZONE; j != 0; j--)                        /* for each block */ 
    {
      Nf_read_open_C_area_odd(gl_address, 0x05);
      byte_5 = Nf_rd_byte_odd();
      if ( byte_5 != 0xFF )                                         /* block status data : valid/invalid block */
      {
        tmp_spare--;                                                /* Defect block */
        if (tmp_spare < 4)
          return KO;

      }
      else                                                          /* Block is valid */
      {
                                                                    /* Determine is the block is a specific block */
        byte_6 = Nf_rd_byte_odd();
        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 */
           )
        { 
          tmp_spare--;                                              /* specific or invalid block */
          if (tmp_spare < 4)
            return KO;

        }
        else
        {
          if (byte_6 == 0xE8)                                       /* look up table ? */
          {
            Nf_wait_busy_odd();
            Nf_send_command_odd (NF_READ_A_AREA_CMD);
            Nf_send_command_odd (NF_BLOCK_ERASE_CMD);               /* Auto Block Erase Setup */
            Nf_send_address_odd ( ((Byte*)&gl_address)[3] );        /* 2nd address cycle*/
            Nf_send_address_odd ( ((Byte*)&gl_address)[2] );        /* 3rd address cycle*/
            if (NF_4_CYCLE_ADDRESS_BIT)                             /* Size of card >= 64Mbytes ?*/
              Nf_send_address_odd ( ((Byte*)&gl_address)[1] );      /* 4th address cycle*/
            
            Nf_send_command_odd(NF_BLOCK_ERASE_CONFIRM_CMD);        /* Erase command */
          }
          if ( (byte_6 & 0xF8) == 0x10)                             /* Used block  */
          {
            last_physical_used_block_odd[i] = gl_address >> 5;
          }
        }

      }
      gl_address += (NF_ZONE_MAX_CPT << 5);
    }

    if (nf_spare_block_number[i] > tmp_spare)
    {
      nf_spare_block_number[i] = tmp_spare;
    }
  }
  
  Nf_CS_EVEN();
  Nf_wait_busy_even();
  /* Determine index for each correct lut */
  for (i = 0; i < NF_ZONE_MAX_CPT; i++)
  { 
    if (lut_is_present[i] == TRUE)                                  
    {
      gl_address = ((Uint32)(lut_block[i]) << 5);
      do
      {
        Nf_read_open_C_area_even(gl_address, 0x06);
        lut_index[i]++;
        gl_address += NB_PAGE_BY_LUT;
        Nf_wait_busy_even();
        byte_6 = Nf_rd_byte_even();
      }
      while ((byte_6 == 0xE8) && ((gl_address & 0x1F) != 0));

⌨️ 快捷键说明

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