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

📄 nf_drv.c

📁 c51snd1c硬盘播放器全部资料.源码.线路图.protel99se的pcb图
💻 C
📖 第 1 页 / 共 3 页
字号:
    }
  }
  Nf_send_command_even (NF_RANDOM_DATA_INPUT_CMD);                             /* Spare block */
  Nf_send_address_even(0x00);                                                  /* logical block > 1000 */
  Nf_send_address_even(0x08);
  Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); /* Spare area */
  Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xE8); Nf_wr_byte_even(0xFF);
  Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xE8);
  Nf_send_command_even(NF_PAGE_PROGRAM_CMD);                                   /* Valid the page programmation */

  ((Byte*)&address2)[3]++;
  ((Byte*)&gl_address)[3]++;

  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]);
  Nf_send_address_even (((Byte*)&address2)[2]);
  if (NF_5_CYCLE_ADDRESS_BIT)                         /* Size of card >= 128Mbytes ?  */
    Nf_send_address_even ( ((Byte*)&address2)[1] );  /* Row address Byte 2           */
  Nf_send_command_even (NF_COPY_BACK_CMD);
  
  if (((Uint16)(nf_block_min) + i) >= 512)
    start = (nf_block_min + i - 512) << 2;
  else
    start = 0;


  Nf_wait_busy_even();
  Nf_send_command_even (NF_RANDOM_DATA_INPUT_CMD);
  Nf_send_address_even (((Byte*)&start)[1]);           /* Column address Byte 0        */
  Nf_send_address_even (((Byte*)&start)[0]);           /* Column address Byte 1        */
  Nf_send_address_even ( ((Byte*)&gl_address)[3] );    /* Row address Byte 0           */
  Nf_send_address_even ( ((Byte*)&gl_address)[2] );    /* Row address Byte 1           */
  if (NF_5_CYCLE_ADDRESS_BIT)                          /* Size of nf >= 128Mb?         */
    Nf_send_address_even ( ((Byte*)&gl_address)[1] );  /* Row address Byte 2           */

  for (; i <= nf_gl_buf_idx_max; i++)
  {
    Nf_wr_byte_even (nf_buf[i].even.b[0]); 
    Nf_wr_byte_even (nf_buf[i].even.b[1]);
    Nf_wr_byte_even (nf_buf[i].odd.b[0]); 
    Nf_wr_byte_even (nf_buf[i].odd.b[1]);
  }

  Nf_send_command_even (NF_RANDOM_DATA_INPUT_CMD);      /* Spare block          */
  Nf_send_address_even(0xA0);                           /* logical block > 1000 */
  Nf_send_address_even(0x07);
  for (i = 0; i <= 23; i++)
  {
    Nf_wr_byte_even (nf_buf_free[i].even>>8);
    Nf_wr_byte_even (nf_buf_free[i].even);
    Nf_wr_byte_even (nf_buf_free[i].odd>>8);
    Nf_wr_byte_even (nf_buf_free[i].odd);
  }
  /* write spare area */
  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(0xE8); Nf_wr_byte_even(0xFF);
  Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xE8);
  Nf_send_command_even(NF_PAGE_PROGRAM_CMD);                        /* Valid the page programmation */


  nf_gl_buf_idx_max = 0;

  if (nf_lut_index[nf_zone] == 0)                                   /* Erase old lut                   */
  {
    address2 = (Uint32)(nf_lut_block[nf_zone])<<NF_SHIFT_SECTOR_BLOCK;
    nf_lut_block[nf_zone] = nf_look_up_table_block;                 /* Update address of look up table */
    Nf_wait_busy_even();
    Nf_send_command_even (NF_BLOCK_ERASE_CMD);                      /* Auto Block Erase Setup     */
    Nf_send_address_even ( ((Byte*)&address2)[3] );                 /* Row address Byte 1         */
    Nf_send_address_even ( ((Byte*)&address2)[2] );                 /* Row address Byte 2         */
    if (NF_5_CYCLE_ADDRESS_BIT)                                     /* Size of nf >= 128Mbytes?   */
      Nf_send_address_even ( ((Byte*)&address2)[1] );               /* Row address Byte 3         */
    Nf_send_command_even(NF_BLOCK_ERASE_CONFIRM_CMD);               /* Erase command              */
  }
  Nf_wait_busy_even();

}


/*F**************************************************************************
* NAME: nf_mark_bad_block
*----------------------------------------------------------------------------
* PARAMS:   
*           
* return:   
*----------------------------------------------------------------------------
* PURPOSE:
*   Write 0x00 on block status byte (Byte 5 of spare data)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
void nf_mark_bad_block (void)
{
  if (nf_parity_bit == NF_ODD)
  {
    Nf_wait_busy_odd();
    Nf_write_open_spare_area_odd(gl_address, 0x03);
    Nf_wr_byte_odd(0x00);
    Nf_wr_byte_odd(0x00);
    Nf_wr_byte_odd(0x00);
    Nf_send_command_odd (NF_PAGE_PROGRAM_CMD);  /* Send program command */
  }
  else
  {
    Nf_wait_busy_even();
    Nf_write_open_spare_area_even(gl_address, 0x03);
    Nf_wr_byte_even(0x00);
    Nf_wr_byte_even(0x00);
    Nf_wr_byte_even(0x00);
    Nf_send_command_even (NF_PAGE_PROGRAM_CMD);  /* Send program command */
  }
}


/*F**************************************************************************
* NAME: nf_block_erase
*----------------------------------------------------------------------------
* PARAMS:   
*
* return:
*   OK : erase done
*   KO : erase not done
*----------------------------------------------------------------------------
* PURPOSE: Erase a block on Nand Flash Media
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
bit nf_block_erase (Uint32 pos)
{
  if (nf_parity_bit == NF_ODD)
  {
    Nf_active_ODD();
    Nf_wait_busy_odd();
    Nf_send_command_odd (NF_BLOCK_ERASE_CMD);       /* Auto Block Erase Setup       */
    Nf_send_address_odd ( ((Byte*)&pos)[3] );       /* Row address Byte 0           */
    Nf_send_address_odd ( ((Byte*)&pos)[2] );       /* Row address Byte 1           */
    if (NF_5_CYCLE_ADDRESS_BIT)                     /* Size of card >= 128Mbytes ?  */
      Nf_send_address_odd ( ((Byte*)&pos)[1] );     /* Row address Byte 2           */
    Nf_send_command_odd(NF_BLOCK_ERASE_CONFIRM_CMD);/* Erase command                */
  }
  else
  {
    Nf_active_EVEN();
    Nf_wait_busy_even();
    Nf_send_command_even (NF_BLOCK_ERASE_CMD);       /* Auto Block Erase Setup       */
    Nf_send_address_even ( ((Byte*)&pos)[3] );       /* Row address Byte 0           */
    Nf_send_address_even ( ((Byte*)&pos)[2] );       /* Row address Byte 1           */
    if (NF_5_CYCLE_ADDRESS_BIT)                      /* Size of card >= 128Mbytes ?  */
      Nf_send_address_even ( ((Byte*)&pos)[1] );     /* Row address Byte 2           */
    Nf_send_command_even(NF_BLOCK_ERASE_CONFIRM_CMD);/* Erase command                */
  }
  return OK;
}


/*F**************************************************************************
* NAME: nf_erase_all_block
*----------------------------------------------------------------------------
* PARAMS:   
*           
* return:   
*   OK : erase done
*   KO : erase not done
*----------------------------------------------------------------------------
* PURPOSE:
*   This function erase all blocks on a NF card
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 gl_address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
bit nf_erase_all_block (void)
{
Byte i;
Uint16 j;
Uint16 block;
Uint16 block_reserved_space;
bit erase;
  /* Erase all block */
  block_reserved_space = (Uint16) (((nf_reserved_space_start >> (NF_SHIFT_SECTOR_BLOCK + 3)) % 1000));
  gl_address = 0;
  for (nf_zone = 0; nf_zone < NF_ZONE_MAX_CPT; nf_zone++)
  {
    for (j = 1024; j != 0; j--)
    {
      Nf_CS_ODD();
      Nf_wait_busy_odd();
      Nf_read_open_spare_area_odd(gl_address, 0x05); /* Read block status byte */
      erase = TRUE;
      if (Nf_rd_byte_odd() != 0xFF) /* if bad block */
      {
        erase = FALSE;          /* don't erase block */
      }
      else
      {
        ((Byte*)&block)[0] = Nf_rd_byte_odd(); /* Read logical block address */
        ((Byte*)&block)[1] = Nf_rd_byte_odd();
        if ( (MEM_RESERVED_SIZE != 0) && (nf_zone == (NF_ZONE_MAX_CPT - 1)))
        {
          if ((((Byte*)&block)[0] & 0xF8) == 0x10)
          {
            block = (block & 0x0FFF) >> 1;
            if (block >= block_reserved_space)      /* If it is a reserved block */
            {
              erase = FALSE;                        /* Don't erase block */
            }
          }
        }
      }

      if (NF_FULL_CHIP_ERASE == FALSE)
      {
        if (((Byte*)&block)[0] != 0xE8)
        {
          erase = FALSE;
        }
      }
      else
      {
        if (((Byte*)&block)[0] == 0x00)
        {
          erase = FALSE;
        }
      }

      if (erase)
      {
        nf_block_erase(gl_address);
        if (nf_check_status() == KO)
        { 
          nf_mark_bad_block(); /* Failure on erase operation */     
        }
        else
        { /* Fill redundant area with 0x00 */
          Nf_write_open_spare_area_odd(gl_address, 0x00);
          for (i = 64; i != 0; i--)
            Nf_wr_byte_odd(0x00);
          Nf_send_command_odd(NF_PAGE_PROGRAM_CMD);        /* Valid the page programmation */
          if ( nf_check_status() == KO)
          { 
            nf_mark_bad_block();                        /* Failure on program operation */
          }
          else
          {
            Nf_read_open_spare_area_odd(gl_address, 0x00);        /* Read 64 bytes */
            i = 64;
            while ((i != 0) && (Nf_rd_byte_odd() == 0x00)) i--;

            if (i)
            {
              nf_mark_bad_block();
            }
            else
            {
              nf_block_erase(gl_address);          /* Finally, erase the block */      
              if ( nf_check_status() == KO)
              { /* Failure on erase operation */
                nf_mark_bad_block();      
              }
            }
          }
        }
      }
      Nf_CS_EVEN();
      Nf_wait_busy_even();
      Nf_read_open_spare_area_even(gl_address, 0x05); /* Read block status byte */
      erase = TRUE;
      if (Nf_rd_byte_even() != 0xFF) /* if bad block */
      {
        erase = FALSE;          /* don't erase block */
      }
      else
      {
        ((Byte*)&block)[0] = Nf_rd_byte_even(); /* Read logical block address */
        ((Byte*)&block)[1] = Nf_rd_byte_even();
        if ( (MEM_RESERVED_SIZE != 0) && (nf_zone == (NF_ZONE_MAX_CPT - 1)))
        {
          if ((((Byte*)&block)[0] & 0xF8) == 0x10)
          {
            block = (block & 0x0FFF) >> 1;
            if (block >= block_reserved_space)      /* If it is a reserved block */
            {
              erase = FALSE;                        /* Don't erase block */
            }
          }
        }
      }

      if (NF_FULL_CHIP_ERASE == FALSE)
      {
        if (((Byte*)&block)[0] != 0xE8)
        {
          erase = FALSE;
        }
      }
      else
      {
        if (((Byte*)&block)[0] == 0x00)
        {
          erase = FALSE;
        }
      }

      if (erase)
      {
        nf_block_erase(gl_address);
        if (nf_check_status() == KO)
        { 
          nf_mark_bad_block(); /* Failure on erase operation */     
        }
        else
        { /* Fill redundant area with 0x00 */
          Nf_write_open_spare_area_even(gl_address, 0x00);
          for (i = 64; i != 0; i--)
            Nf_wr_byte_even(0x00);
          Nf_send_command_even(NF_PAGE_PROGRAM_CMD);        /* Valid the page programmation */
          if ( nf_check_status() == KO)
          { 
            nf_mark_bad_block();                        /* Failure on program operation */
          }
          else
          {
            Nf_read_open_spare_area_even(gl_address, 0x00);        /* Read 64 bytes */
            i = 64;
            while ((i != 0) && (Nf_rd_byte_even() == 0x00)) i--;

            if (i)
            {
              nf_mark_bad_block();
            }
            else
            {
              nf_block_erase(gl_address);          /* Finally, erase the block */      
              if ( nf_check_status() == KO)
              { /* Failure on erase operation */
                nf_mark_bad_block();      
              }
            }
          }
        }
      }

      gl_address += 64;
    }
  }



  return (nf_read_spare_byte());
}




⌨️ 快捷键说明

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