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

📄 nf.c

📁 ATMEL 89c51sndc mp3外接硬盘源码
💻 C
📖 第 1 页 / 共 5 页
字号:
        }
        else
        {
          while (!Usb_tx_complete());         /* wait end of transfer */
          Usb_clear_TXCMPL();                 /* ack transfer */
        }
        Usb_set_TXRDY();                      /* start usb transfer */    
      }
  
  
    }

      
    gl_ptr_mem++;                             /* new page          */
    nf_parity_bit = ~nf_parity_bit;
    if ( !(gl_ptr_mem & 0x3F) )               /* New block ? */
    {
      if (nb_sector != 1)
      {
        nf_read_open(gl_ptr_mem);
      }
    }
    nb_sector--;
  }
  while(nb_sector != 0);

  while (!Usb_tx_complete());             /* wait end of transfer */
  Usb_clear_TXCMPL();                     /* ack transfer */

  return OK;

}

/*F**************************************************************************
* NAME: nf_write_open
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS:
*   pos: address of the the next write data
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Low level memory write update
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_open (Uint32 pos)
{
Uint16 i;
Uint16 j;
bit change_zone = FALSE;


  gl_ptr_mem = pos;
  gl_cpt_page = 0;

  
  /* Determine the logical block value */
  nf_logical_block = (gl_ptr_mem >> 6);              /* 64 pages by block */
  nf_zone = 0;

  while (nf_logical_block > 999)
  {
    nf_logical_block -= 1000;
    nf_zone++;
  }

  if (nf_zone != nf_old_zone)
  {
    change_zone = TRUE;
    if (nf_lut_modified)
    { /* save old lut */
      i = nf_old_zone;
      nf_old_zone = nf_zone;
      nf_zone = i;
      nf_reassign_block();
      nf_lut_modified = FALSE;
      nf_zone = nf_old_zone;
    }
    else
    { /* update old zone value */
      nf_old_zone = nf_zone;
    }
    block_min = 0xFFFF;
    nf_gl_buf_free_idx = 0;
  }

  if ( (nf_logical_block >= block_min) && (nf_logical_block < (block_min + NF_BUFFER_SIZE - 3))) 
  /* 3 ??? the buffer must contain at any time the logical blocks for one bulk transfer = 64KB ==> 5 logical blocks */
  { /* we don't have to update the buffer */
    nf_calc_logical_block();
    nf_gl_buf_idx = nf_logical_block - block_min;
  }
  else 
  { /* we have to update the buffer */
    /* Store the look up table block address */
    nf_look_up_table_block =  lut_block[nf_zone];
    /* If LUT have been modified */
    if (nf_lut_modified)           
    {
      nf_reassign_block();      /* update the LUT */
    }
    /* Calculate the address for LUT access */
    gl_address = (((Uint32)(nf_look_up_table_block))<<5);
    gl_address += (((Uint32)(lut_index[nf_zone]))<<3);

    /* Calculate the address for right page of the LUT access */
    gl_address += ((Uint32)(nf_logical_block) >> 7);
    
    /* Calculate the redundant block address value */
    nf_calc_logical_block();
    i = nf_logical_block & 0x7F; /* i = logical % 128 because there are 128 log add in a page of lut */
    /* For the current zone, initialize the number of spare block */
    nf_spare_block = nf_spare_block_number[nf_zone];
    /* Calculate the low and the high block stored in the buffer */
    block_min = nf_logical_block;
    block_max = ( (nf_logical_block + NF_BUFFER_SIZE) > 999) ? (1000 - nf_logical_block) : NF_BUFFER_SIZE;
    
    /* Open the look-up table */
    Nf_CS_EVEN();
    Nf_wait_busy_even();     
    if (nf_logical_block & 0x40)
    {
      Nf_send_command_even(NF_READ_B_AREA_CMD);            /* 2nd half page */
      Nf_send_address_even((Byte)(nf_logical_block & 0x3F) << 2); 
    }
    else
    {
      Nf_send_command_even(NF_READ_A_AREA_CMD);            /* first half page */
      Nf_send_address_even((Byte)(nf_logical_block & 0x3F) << 2);
    }

    Nf_send_address_even ( ((Byte*)&gl_address)[3] );   /* 2nd address cycle         */
    Nf_send_address_even ( ((Byte*)&gl_address)[2] );   /* 3rd address cycle         */
    #if (NF_4_CYCLE_ADDRESS)                                        /* Size of card >= 64Mbytes ?*/
      Nf_send_address_even ( ((Byte*)&gl_address)[1] ); /* 4th address cycle         */
    #endif

    Nf_wait_busy_even();

    for (j = 0; j < block_max ; j++)
    {
      /* Read conversion table : 64 datas */
      buf[j].even.b[0] = Nf_rd_byte_even();
      buf[j].even.b[1] = Nf_rd_byte_even();
      buf[j].odd.b[0] = Nf_rd_byte_even();
      buf[j].odd.b[1] = Nf_rd_byte_even();
      i++;
      if (i >= 0x80)
      {
        i = 0;
        gl_address++;
        Nf_read_open_A_area_even(gl_address, 0x00);
      }
    }
  
    if (change_zone)        /* If it is a new zone, then load the spare buffer */
    {
      /* Check for used block : first, read in the LUT the free physical block */
      gl_address = ((Uint32)(nf_look_up_table_block)<<5);
      gl_address += (Uint32)(lut_index[nf_zone])<<3;
      gl_address += (NB_PAGE_BY_LUT - 1);            /*  = offset for page of free blocks */
      Nf_read_open_B_area_even(gl_address, 160);

      for (i = 0; i <= (nf_spare_block); i++)
      {
        buf_free[i].even = (Uint16)(Nf_rd_byte_even()<<8);
        buf_free[i].even +=  Nf_rd_byte_even();
        buf_free[i].odd = (Uint16)(Nf_rd_byte_even()<<8);
        buf_free[i].odd +=  Nf_rd_byte_even();
      }
    }
    nf_gl_buf_idx = 0;           /* initialize index for main buffer         */
    nf_gl_buf_idx_max = 0;       /* initialize the max index for the buffer  */
    nf_gl_buf_free_idx = 0;

  }
  
  /* if block is already assigned, then invert with a spare block */
  nf_block_used = ( !(buf[nf_gl_buf_idx].even.w & 0x8000) ) ? TRUE : FALSE;
  if (nf_block_used)
  {
    if ((buf_free[nf_gl_buf_free_idx].even & 0x7FFF) == lut_block[nf_zone])
    {
      nf_gl_buf_free_idx++;
      if (nf_gl_buf_free_idx >= nf_spare_block  )
      {
        nf_gl_buf_free_idx = 0;
      }      
    }
    /* assign block to be deleted */
    nf_block_to_be_deleted_even = buf[nf_gl_buf_idx].even.w;
    nf_block_to_be_deleted_odd = buf[nf_gl_buf_idx].odd.w;
    
    buf[nf_gl_buf_idx].even.w = buf_free[nf_gl_buf_free_idx].even;
    buf[nf_gl_buf_idx].odd.w = buf_free[nf_gl_buf_free_idx].odd;
    
    buf_free[nf_gl_buf_free_idx].even = nf_block_to_be_deleted_even | 0x8000;
    buf_free[nf_gl_buf_free_idx].odd = nf_block_to_be_deleted_odd | 0x8000;
    
    nf_gl_buf_free_idx++;

    /* increase the index for spare block buffer */
    if (nf_gl_buf_free_idx >= nf_spare_block  )
    {
      nf_gl_buf_free_idx = 0;
    }

  }
  
  /* Mark block as assigned */
  buf[nf_gl_buf_idx].even.w &= 0x7FFF;  
  buf[nf_gl_buf_idx].odd.w &= 0x7FFF;     

  if (nf_gl_buf_idx > nf_gl_buf_idx_max)
    nf_gl_buf_idx_max = nf_gl_buf_idx;

  /* Update the current physical sector address */
  nf_current_physical_sector_addr_even = ((Uint32)(buf[nf_gl_buf_idx].even.w) << 5);
  nf_current_physical_sector_addr_odd = ((Uint32)(buf[nf_gl_buf_idx].odd.w) << 5);


  if ( nf_block_used )                   /* Fisrt block already used block ?  */
  { 
    nf_copy_block_head();             /* Copy the first part of the block  */
  }
  else                                 
  { 
    nf_init_spare();                   /* else init spare data for new logical block  */
  }

  nf_busy = FALSE;                     /* Clear flag busy */

  nf_lut_modified = TRUE;

    /* Calculate the address for LUT access */
  gl_address = (((Uint32)(nf_look_up_table_block))<<5);
  gl_address += (((Uint32)(lut_index[nf_zone]))<<3);

  /* mark LUT as modified */
  Nf_CS_EVEN();
  Nf_wait_busy_even();    
  Nf_write_open_C_area_even(gl_address, 0x00);
  Nf_wr_byte_even(0x00);
  Nf_send_command_even(NF_PAGE_PROGRAM_CMD);

  if (gl_ptr_mem & 0x01)
    nf_parity_bit = NF_ODD;
  else
    nf_parity_bit = NF_EVEN;
  return OK; 
}


/*F**************************************************************************
* NAME: nf_write_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Low level memory write close: release NF 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_close (void)
{ 
  if (gl_cpt_page != 0)                                       /* uncomplete write page session  */
  {                                                           /* Fill page with 0x00 value      */
    if (gl_cpt_page < 256)                                    /* First half page                */
    {
      for ( ; gl_cpt_page < 256; gl_cpt_page++)
      {
        gl_buffer[gl_cpt_page] = 0x00;
      }

      if (nf_parity_bit == NF_ODD)                                      /* Open Nand Flash in write mode */
      {
        Nf_wait_busy_odd();  
        Nf_write_open_A_area_odd(nf_current_physical_sector_addr_odd, 0x00);
        nf_download_buffer();                                       /* Download the buffer */
        Nf_send_command_odd(NF_PAGE_PROGRAM_CMD);                       /* Send Program command */
      }
      else
      {
        Nf_wait_busy_even();  
        Nf_write_open_A_area_even(nf_current_physical_sector_addr_even, 0x00);
        nf_download_buffer();                                       /* Download the buffer */
        Nf_send_command_even(NF_PAGE_PROGRAM_CMD);                       /* Send Program command */
      }
    }                                                             /* End of first half page */
    

    for ( ; gl_cpt_page < 512; gl_cpt_page++)                     /* 2nd half page */
    {
     gl_buffer[gl_cpt_page] = 0x00;
    }
    if (nf_parity_bit == NF_ODD)
    {
      Nf_wait_busy_odd();  
      Nf_write_open_B_area_odd(nf_current_physical_sector_addr_odd, 0x00);
      nf_download_buffer();                                             /* Download the buffer */
      nf_update_spare_data();
      Nf_send_command_odd(NF_PAGE_PROGRAM_CMD);                             /* Send program command to the device */
    }
    else
    {
      Nf_wait_busy_even();  
      Nf_write_open_B_area_odd(nf_current_physical_sector_addr_even,0x00);
      nf_download_buffer();                                             /* Download the buffer */
      nf_update_spare_data();
      Nf_send_command_odd(NF_PAGE_PROGRAM_CMD);                             /* Send program command to the device */
    }
    gl_ptr_mem++;                                                     /* useless? */
  }

  if (nf_block_used)                           /* Copy last part of a block if the block was assigned */
  {
    if ( gl_ptr_mem & 0x3F )
      nf_copy_block_tail();
    Nf_CS_EVEN();
    nf_block_erase((Uint32)(nf_block_to_be_deleted_even) << 5);    /* Erase old block */
    Nf_CS_ODD();
    nf_block_erase((Uint32)(nf_block_to_be_deleted_odd) << 5);     /* Erase old block */
  }
  return OK;

⌨️ 快捷键说明

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