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

📄 nf_drv.c

📁 ATMEL MP3 源代码
💻 C
📖 第 1 页 / 共 2 页
字号:

    Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD); 
    Nf_send_address ( 0x00 );
    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           */

    nf_download_buffer();

    Nf_send_command (NF_PAGE_PROGRAM_CMD);

    ((Byte*)&gl_cpt_page)[0]++;
    
    Nf_wait_busy();

    Nf_send_command (NF_READ_CMD);
    Nf_send_address ( 0x00);
    Nf_send_address ( ((Byte*)&gl_cpt_page)[0]);
    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_READ_CMD2);
    Nf_wait_busy();

    nf_upload_buffer();

    Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD); 
    Nf_send_address ( 0x00 );
    Nf_send_address ( ((Byte*)&gl_cpt_page)[0]);
    Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[3]);
    Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[2]);
    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_download_buffer();
    Nf_send_command (NF_PAGE_PROGRAM_CMD);

    ((Byte*)&gl_cpt_page)[0]++;

    if ( ((Byte*)&gl_cpt_page)[0] == 0x08)              /* end of sector */
    {
      gl_cpt_page = 0;
      gl_ptr_mem++;
      ((Byte*)&nf_current_physical_sector_addr)[3]++;
      ((Byte*)&gl_address)[3]++;
    }      
  }


  if (((Byte*)&gl_ptr_mem)[3] & 0x3F)
  {
    for (j = (((Byte*)&gl_ptr_mem)[3] & 0x3F); j < 0x40; 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);
      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);
      nf_current_physical_sector_addr++;
      gl_address++;
    }
  }
}


/*F**************************************************************************
* NAME: nf_reassign_block
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Reassign the block value in LUT (copy of LUT)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
void nf_reassign_block (void)
{
Byte i;           /* Counter            */
Uint16 start;
Uint32 address2;  /* LUT source address */

  if (nf_lut_index[nf_zone] == 63)
  {
    if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_look_up_table_block)
    {
      gl_buf_free_idx++;
      if (gl_buf_free_idx >= nf_spare_block)
      {
        gl_buf_free_idx = 0;
      }      
    }

    /* Source address */
    address2 = ((Uint32)(nf_look_up_table_block)<<NF_SHIFT_SECTOR_BLOCK) + 63;

    nf_look_up_table_block = nf_buf_free[gl_buf_free_idx] & 0x7FFF;
    
    gl_buf_free_idx++;
    if (gl_buf_free_idx >= nf_spare_block)
    {
      gl_buf_free_idx = 0;
    }      

    /* Target addres */
    gl_address = ((Uint32)(nf_look_up_table_block) << NF_SHIFT_SECTOR_BLOCK);

    nf_lut_index[nf_zone] = 0;

  }
  else
  {
    /* Source address */
    address2 = ((Uint32)(nf_look_up_table_block)<<NF_SHIFT_SECTOR_BLOCK) + nf_lut_index[nf_zone];
    nf_lut_index[nf_zone]++;
    /* Target address */
    gl_address = address2 + 1;
  }

  Nf_wait_busy();
  Nf_send_command (NF_READ_CMD);
  Nf_send_address (0x00);
  Nf_send_address (0x00);
  Nf_send_address (((Byte*)&address2)[3]);
  Nf_send_address (((Byte*)&address2)[2]);
  if (NF_5_CYCLE_ADDRESS_BIT)                         /* Size of card >= 128Mbytes ?  */
    Nf_send_address ( ((Byte*)&address2)[1] );  /* Row address Byte 2           */
  Nf_send_command (NF_COPY_BACK_CMD);

  start = nf_block_min << 1;

  Nf_wait_busy();
  Nf_send_command (NF_RANDOM_DATA_INPUT_CMD);
  Nf_send_address (((Byte*)&start)[1]);           /* Column address Byte 0        */
  Nf_send_address (((Byte*)&start)[0]);           /* Column address Byte 1        */
  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           */


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


  Nf_send_command (NF_RANDOM_DATA_INPUT_CMD);                             /* Spare block */
  Nf_send_address(0xD0);                                                  /* logical block > 1000 */
  Nf_send_address(0x07);
  
  for (i = 0; i <= 23; i++)
  {
    Nf_wr_byte (nf_buf_free[i]>>8);
    Nf_wr_byte (nf_buf_free[i]);
  }
  Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* Spare area */
  Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xE8); Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xE8);

  Nf_send_command(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();
    Nf_send_command (NF_BLOCK_ERASE_CMD);                     /* Auto Block Erase Setup     */
    Nf_send_address ( ((Byte*)&address2)[3] );                /* Row address Byte 1         */
    Nf_send_address ( ((Byte*)&address2)[2] );                /* Row address Byte 2         */
    if (NF_5_CYCLE_ADDRESS_BIT)                                   /* Size of card >= 64Mbytes ? */
      Nf_send_address ( ((Byte*)&address2)[1] );              /* Row address Byte 3         */
    Nf_send_command(NF_BLOCK_ERASE_CONFIRM_CMD);              /* Erase command              */
  }
}


/*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)
{
  Nf_wait_busy();
  Nf_write_open_spare_area(gl_address, 0x03);
  Nf_wr_byte(0x00);
  Nf_wr_byte(0x00);
  Nf_wr_byte(0x00);
  Nf_send_command (NF_PAGE_PROGRAM_CMD);  /* Send program command */
  Nf_wait_busy();
}


/*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)
{
  Nf_wait_busy();
  Nf_send_command (NF_BLOCK_ERASE_CMD);       /* Auto Block Erase Setup       */
  Nf_send_address ( ((Byte*)&pos)[3] );       /* Row address Byte 0           */
  Nf_send_address ( ((Byte*)&pos)[2] );       /* Row address Byte 1           */
  if (NF_5_CYCLE_ADDRESS_BIT)                     /* Size of card >= 128Mbytes ?  */
    Nf_send_address ( ((Byte*)&pos)[1] );     /* Row address Byte 2           */
  Nf_send_command(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 and write CIS information
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
bit nf_erase_all_block (void)
{
bit bad_block_detect;
Byte i;

  Nf_CS_ON();
  for (gl_address = 0; gl_address < (Uint32)(NF_ZONE_MAX_CPT) * NF_BLOCK_PER_ZONE * NF_PAGE_PER_BLOCK; gl_address+=NF_PAGE_PER_BLOCK)
  {
    Nf_wait_busy();
    Nf_read_open_spare_area(gl_address, 0x05);      /* Read block status byte */
    if ( Nf_rd_byte() == 0xFF)                       /* Not a bad block */
    {
      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_wait_busy();
        Nf_write_open_spare_area(gl_address, 0x00);
        for (i = 16; i != 0; i--)
          Nf_wr_byte(0x00);
        Nf_send_command(NF_PAGE_PROGRAM_CMD);             /* Valid the page programmation */
        if ( nf_check_status() == KO)
        { 
          nf_mark_bad_block();                            /* Failure on program operation */
        }

        Nf_wait_busy();
        Nf_read_open_spare_area(gl_address, 0x00);        /* Read 16 bytes */
        bad_block_detect = FALSE;
        for (i = 16; i != 0; i--)
        {
          if (Nf_rd_byte() != 0x00) bad_block_detect = TRUE;
        }

        if (bad_block_detect)
        {
          nf_mark_bad_block();
        }
        else
        {
          nf_block_erase(gl_address);          /* Finally, erase the block */      
          if ( nf_check_status() == KO)
          { 
            nf_mark_bad_block();      
          }
        }
      }
    }
  }
  read_spare_byte();
  return OK;
}




⌨️ 快捷键说明

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