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

📄 nf.c

📁 ATMEL MP3 源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
        if (gl_buf_free_idx >= nf_spare_block)
        {
          gl_buf_free_idx = 0;
        }      
      }                                                  
      nf_block_to_be_deleted_even = nf_buf[nf_gl_buf_idx].even.w;            /* assign block to be deleted */
      nf_block_to_be_deleted_odd  = nf_buf[nf_gl_buf_idx].odd.w;             /* assign block to be deleted */
      nf_buf[nf_gl_buf_idx].even.w = nf_buf_free[gl_buf_free_idx].even;
      nf_buf[nf_gl_buf_idx].odd.w  = nf_buf_free[gl_buf_free_idx].odd;
    
      nf_buf_free[gl_buf_free_idx].even   = nf_block_to_be_deleted_even | 0x8000;
      nf_buf_free[gl_buf_free_idx].odd    = nf_block_to_be_deleted_odd  | 0x8000;
      gl_buf_free_idx++;
      if (gl_buf_free_idx >= nf_spare_block  )
      {
        gl_buf_free_idx = 0;
      }
      nf_block_used = TRUE;
    }
    else /* The block is not assigned. Nothing to do */
    {
      nf_block_used = FALSE;
    }
      
    nf_buf[nf_gl_buf_idx].even.w &= 0x7FFF;
    nf_buf[nf_gl_buf_idx].odd.w  &= 0x7FFF;

    if (nf_gl_buf_idx > nf_gl_buf_idx_max)      /* update the max index buffer */
      nf_gl_buf_idx_max = nf_gl_buf_idx;
  
    /* Update current physical sector */
    nf_current_physical_sector_addr_even = (Uint32)(nf_buf[nf_gl_buf_idx].even.w) << NF_SHIFT_SECTOR_BLOCK;
    nf_current_physical_sector_addr_odd  = (Uint32)(nf_buf[nf_gl_buf_idx].odd.w)  << NF_SHIFT_SECTOR_BLOCK;
    nf_logical_block++;                         /* increase the logical block */
    nf_calc_logical_block();                    /* calculate the redundant block address */
  }

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



}

/*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)
{
Byte i;
Uint16 j;
bit change_zone = FALSE;

  if (nf_write_advanced)        /* Previous operation was a write mass storage session */
  {
    nf_write_advanced = FALSE;  /* nf_write_advanced will be set only if there is a mass storage session */
    if (pos == save_gl_ptr_mem) /* if this sector is the previous "next sector" */
    {
      nf_current_physical_sector_addr_odd = save_physical_sector_odd;     /* set odd sector       */
      nf_current_physical_sector_addr_even = save_physical_sector_even;   /* set even sector      */
      gl_ptr_mem = pos >> 2;
      gl_cpt_page = (pos & 0x03) << 9;
      nf_parity_bit = ((Byte*)&gl_ptr_mem)[3] & 0x01;   /* set nf parity           */
      nf_busy = FALSE;                                  /* clear flag busy         */
      nf_wr_open = TRUE;                                /* send write open command */
      return OK;
    }
    else
    {
      nf_force_write_close();
    }
  }
  else
  {
    if (nf_close_write_session)
    {
      nf_close_write_session = FALSE;
      nf_force_write_close();
    }
  }

  gl_ptr_mem = pos >> 2;
  gl_cpt_page = (pos & 0x03) << 9;


  /* Determine the logical block value and logical zone value */
  nf_logical_block = (gl_ptr_mem >> NF_SHIFT_SECTOR_BLOCK) >> 1;
  nf_zone = 0;
  while (nf_logical_block > 999)
  {
    nf_logical_block -= 1000;
    nf_zone++;
  }
  Nf_CS_EVEN();

  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;
    }
    nf_block_min = 0xFFFF;
    gl_buf_free_idx = 0;
  }

  if ( (nf_logical_block >= nf_block_min) && (nf_logical_block < (nf_block_min + NF_BUFFER_SIZE - 5)))
  { /* we don't have to update the buffer */
    nf_calc_logical_block();
    nf_gl_buf_idx = nf_logical_block - nf_block_min;
  }
  else 
  { 
    nf_look_up_table_block =  nf_lut_block[nf_zone];          /* we have to update the buffer */
    if (nf_lut_modified)                                      /* if lut have been modified */
    {
      nf_reassign_block();                                    /* then update lut */
    }
    gl_address      = ((Uint32)(nf_look_up_table_block)<<NF_SHIFT_SECTOR_BLOCK) 
                      + (Uint32)(nf_lut_index[nf_zone] <<1)
                      + (((Byte*)&nf_logical_block)[0] >> 1);
                

    nf_spare_block  = nf_spare_block_number[nf_zone];         /* for the current zone, initialize the number of spare block */
    nf_block_min    = nf_logical_block;                       /* starting block value */
    nf_block_max    = ((nf_logical_block + NF_BUFFER_SIZE) > 999) ? (1000 - nf_logical_block) : NF_BUFFER_SIZE;
    nf_calc_logical_block();                                  /* calculate the redundant block address value */

    Nf_wait_busy_even();     
    Nf_send_command_even (NF_READ_CMD);                       /* open the look-up table       */
    Nf_send_address_even (nf_logical_block << 2);             /* column address Byte 0        */
    Nf_send_address_even ((nf_logical_block >> 6) & 0x07);    /* 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 >= 128Mbytes ?    */
      Nf_send_address_even ( ((Byte*)&gl_address)[1] );       /* row address Byte 2           */

    Nf_send_command_even (NF_READ_CMD2);
    Nf_wait_busy_even();
  
    j = nf_logical_block & 0x1FF;
    for (i = 0; i < nf_block_max ; i++)                       /* fill lut buffer              */
    {
      nf_buf[i].even.b[0] = Nf_rd_byte_even();
      nf_buf[i].even.b[1] = Nf_rd_byte_even();
      nf_buf[i].odd.b[0] = Nf_rd_byte_even(); 
      nf_buf[i].odd.b[1] = Nf_rd_byte_even();
      j++;
      if ( j >= 512)
      {
        j = 0;
        gl_address++;
        Nf_send_command_even(NF_READ_CMD);
        Nf_send_address_even( 0x00);
        Nf_send_address_even( 0x00);
        Nf_send_address_even( ((Byte*)&gl_address)[3]);
        Nf_send_address_even( ((Byte*)&gl_address)[2]);
        if (NF_5_CYCLE_ADDRESS_BIT)                           /* Size of nf >= 128Mbytes ?    */
          Nf_send_address_even ( ((Byte*)&gl_address)[1] );   /* Row address Byte 2           */
        Nf_send_command_even(NF_READ_CMD2);
        Nf_wait_busy_even();
      }
    }
  
    if (change_zone)                                          /* If it is a new zone          */
    {                                                         /* then load the spare buffer   */
      gl_address      = ((Uint32)(nf_look_up_table_block)<<NF_SHIFT_SECTOR_BLOCK) 
                      + (Uint32)(nf_lut_index[nf_zone]<<1) + 1;
        
      Nf_send_command_even(NF_READ_CMD);
      Nf_send_address_even(0xA0);                    
      Nf_send_address_even(0x07);
      Nf_send_address_even( ((Byte*)&gl_address)[3]);
      Nf_send_address_even( ((Byte*)&gl_address)[2]);
      if (NF_5_CYCLE_ADDRESS_BIT)                             /* Size of nf >= 128Mbytes ?    */
        Nf_send_address_even ( ((Byte*)&gl_address)[1] );     /* Row address Byte 2           */
      Nf_send_command_even(NF_READ_CMD2);
      Nf_wait_busy_even();              
      for (i = 0; i < (nf_spare_block); i++)
      {
        nf_buf_free[i].even  =   (Uint16)(Nf_rd_byte_even()<<8);
        nf_buf_free[i].even +=   Nf_rd_byte_even();
        nf_buf_free[i].odd   =   (Uint16)(Nf_rd_byte_even()<<8);
        nf_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  */

  }

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

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

  if (nf_gl_buf_idx > nf_gl_buf_idx_max)
    nf_gl_buf_idx_max = nf_gl_buf_idx;
  
  nf_current_physical_sector_addr_even = ((Uint32)(nf_buf[nf_gl_buf_idx].even.w) << NF_SHIFT_SECTOR_BLOCK);/* Update the current physical sector address */
  nf_current_physical_sector_addr_odd =  ((Uint32)(nf_buf[nf_gl_buf_idx].odd.w)  << NF_SHIFT_SECTOR_BLOCK);/* Update the current physical sector address */

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

  gl_address = ((Uint32)(nf_look_up_table_block) << NF_SHIFT_SECTOR_BLOCK) +
                (Uint32)(nf_lut_index[nf_zone]   << 1);

  Nf_active_EVEN();
  Nf_wait_busy_even();
  Nf_send_command_even(NF_SEQUENTIAL_DATA_INPUT_CMD);   /* write lut as modified                */
  Nf_send_address_even(0x00);                           /* column address byte 0                */
  Nf_send_address_even(0x08);                           /* 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 >= 128Mbytes ?            */
    Nf_send_address_even ( ((Byte*)&gl_address)[1] );   /* row address Byte 2                   */

  Nf_wr_byte_even(0x00);                                /* reset first byte                     */
  Nf_send_command_even(NF_PAGE_PROGRAM_CMD);            /* send program command to the device   */
  nf_busy = FALSE;                                      /* clear flag busy                      */
  nf_lut_modified = TRUE;                               /* set lut flag modified                */
  nf_wr_open = TRUE;                                    /* set this flag to send write command  */
  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 & 0x1FF) != 0)   

⌨️ 快捷键说明

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