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

📄 nf.c

📁 c51snd1c硬盘播放器全部资料.源码.线路图.protel99se的pcb图
💻 C
📖 第 1 页 / 共 4 页
字号:
/*C**************************************************************************
* NAME:         nf.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE:      snd1c-refd-nf-4_0_3      
* REVISION:     1.26     
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains the high level NF routines
*****************************************************************************/
 
/*_____ 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_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 ________________________________________________*/

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[];
extern          bit reserved_disk_space;

data  Uint32  nf_current_physical_sector_addr; /* address of current phys. sect. */  
data  Uint32  gl_address;                      /* general address variable */
data  Uint16  nf_look_up_table_block;          /* lut address (block) */
data  Byte    nf_zone;                         /* current zone number */
data  Byte    nf_gl_buf_idx;                   /* idx for cache */
data  Byte    gl_buf_free_idx ;                /* idx for cache for free blocks */
data  Uint16  gl_cpt_page;                     /* internal page counter */

bdata bit     nf_block_used;                    /* set when a block was used */
bdata bit     nf_busy;                          /* nand flash busy flag */
bdata bit     nf_lut_modified;                  /* LUT modified flag */
bdata bit     nf_reserved_space = FALSE;

idata Uint16  nf_logical_block;                /* current logical block */
idata Byte    nf_gl_buf_idx_max;               /* max index for updating lut */

xdata Byte    nf_spare_block;                  /* number of free block */
xdata Uint16  nf_lut_block[NF_ZONE_MAX];       /* lut address / zone (block) */
xdata Uint16  nf_buf_free[24];                 /* contain free physical block address */
xdata Uint16  nf_block_to_be_deleted;          /* give the address of the next deleted block */
xdata Uint16  nf_block_min;                    /* first address block in the buffer */
xdata Uint16  nf_block_max;                    /* last address block in the buffer */
xdata Union16 nf_buf[NF_BUFFER_SIZE];          /* cache 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 */

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

#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 */
#endif


xdata Uint32 nf_mem_size;
xdata Uint32 nf_reserved_space_start;
/*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;
Byte byte_6;
Byte lut_is_present;
Uint16 block;
Uint16 j;                       /* counter */
Union16 block2;
bit   block_valid;
xdata Uint16 last_physical_used_block[NF_ZONE_MAX]; /* Last physical block used */

#define half_page block_valid
#define temp_address gl_ptr_mem
#define i nf_zone
#define start gl_cpt_page
#define end nf_look_up_table_block
#define free_bloc_pos nf_logical_block

  Nf_CS_ON();
  Nf_wait_busy();
  /* Global media initialization */
  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 */
  gl_buf_free_idx = 0;                      /* Free physical buffer idx */
  nf_old_zone = 0xFF;                       /* Previous zone number */
  for (i = 0; i < NF_ZONE_MAX_CPT; i++)
  {
    nf_lut_index[i] = 0;                    /* LUT index */
  }
  lut_is_present = 0x00;

  /***************************************************/
  /* For each zone, this following lines :           */
  /*   Search bad block                              */
  /*   Determine if a LUT is present                 */
  /*   Determine the last physical block             */ 
  /***************************************************/
  gl_address = 0;                           /* Start from physical sector 0 */
  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) << 10;/* Initialize the last used physical block value */

    for (j = NF_BLOCK_PER_ZONE; j != 0; j--)/* for each block */ 
    {
      Nf_read_open_C_area(gl_address, 0x05);
      if ( Nf_rd_byte() != 0xFF )           /* block status data : valid/invalid block */
      {
        nf_spare_block_number[i]--;         /* Defect block */
      }
      else                                  /* Block is valid */
      {
                                            /* Determine if the block is a specific block */
        byte_6 = Nf_rd_byte();
        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 |= (0x01) << i;
              nf_lut_block[i] = gl_address >> 5;
 
          }
          /* Determine the last physical used block */
          if ((byte_6 & 0xF8) == 0x10)         /* Used block */
          {
            last_physical_used_block[i] = gl_address >> 5;
          }
        }
      }
      gl_address += 32;
    }
  }


  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]) << 5;
      do
      {
        Nf_wait_busy();
        Nf_read_open_C_area(gl_address, 0x06);
        nf_lut_index[i]++;
        gl_address+=4;
      }
      while (Nf_rd_byte() == 0xE8);
      nf_lut_index[i]-=2;
      gl_address = gl_address - 8;
      Nf_read_open_C_area(gl_address, 0x00);
      if (Nf_rd_byte() != 0xFF)             /* LUT has been modified */
      {
        nf_block_erase(gl_address);
        lut_is_present &= ~((0x01) << i);   /* Reset bit */
        nf_lut_index[i] = 0;
      }
    }
  }
  /*****************************************************************/
  /*          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) << 10;            /* starting value for each zone */
      j = 0;
      if (block == start)                   /* starting block for scan */
      {
        block = start + 1023;
      }
      block_valid = FALSE;                  /* init flag block valid */
      Nf_wait_busy();
      do
      {
        gl_address = (Uint32)(block) << 5;
        Nf_read_open_C_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 */
  {
    half_page = 0;
    if (!(lut_is_present & (0x01 << nf_zone)))  /* If not LUT */
    {
      start = 0x00;
      end   = 0x80;     /* 256 bytes for gl_buffer <-> 128 blocks */                            
      free_bloc_pos = (Uint16)(last_physical_used_block[nf_zone]);
      gl_address    = (Uint32)(nf_lut_block[nf_zone]) << 5;        
      do
      {
        nf_init_buffer();                         /* Reinitialize the buffer */
        temp_address = (Uint32)(nf_zone) << 15;   /* We start at the beginning */
        block        = (Uint16)(nf_zone) << 10;
        Nf_wait_busy();
        for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--) /* for each block */
        { 
          Nf_read_open_C_area(temp_address, 0x05);
          byte_5 = Nf_rd_byte();            /* Block status byte */
          if (byte_5 == 0xFF)               /* If not a bad block */
          {
            block2.b[0] = Nf_rd_byte();     /* Read logical block address */
            block2.b[1] = Nf_rd_byte();
            if ((block2.b[0] & 0xF8) == 0x10)
            {
              
              block2.w = (block2.w & 0x0FFF) >> 1;          
              if ((block2.w < end) && (block2.w >= start)) 
              { /* Save logical block value in the buffer */
                gl_buffer[2 * block2.b[1]] = block >> 8;
                gl_buffer[2 * block2.b[1] + 1] = block;
              }
            }
          }
          temp_address += 32; 
          block++;
        }
          
        /* affect to the free physical block a fictive logical block */
        /* free physical block => gl_buffer[x] = 0xFF */
        temp_address = (Uint32)(free_bloc_pos) << 5;
        for (j = 0; j <= 0xFE; j += 2)
        {
          if (gl_buffer[j] == 0xFF)
          {
            do                              /* Search free physical block */
            {
              temp_address += 32;
              if (temp_address >= ((Uint32)(nf_zone + 1) << 15))
                  temp_address = (Uint32)(nf_zone) << 15;
    
              Nf_read_open_C_area(temp_address, 0x05);
              
              byte_5 = Nf_rd_byte();        /* Invalid/Valid block */
              byte_6 = Nf_rd_byte();        /* Used/Unused block */
            }
            while (((byte_6 != 0xFF) && (byte_6 != 0xE8)) || (byte_5 != 0xFF));
  
            free_bloc_pos = temp_address >> 5;
            gl_buffer[j] = (free_bloc_pos >> 8) | 0x80;
            gl_buffer[j + 1] = free_bloc_pos;
          }
        }
        
        if (half_page == 0)
        {
          Nf_write_open_A_area(gl_address, 0x00); /* Write first part of LUT */
          nf_download_buffer();                   /* Write buffer */
          Nf_send_command(NF_PAGE_PROGRAM_CMD);   /* Launch programmation */

⌨️ 快捷键说明

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