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

📄 nf.c

📁 ATMEL MP3 源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
        byte_6 = Nf_rd_byte_even();
      }
      while ((byte_6 == 0xE8) && ((gl_address & 0x3F) != 0)); 
      if ((byte_6 == 0xE8) && ((gl_address & 0x3F) == 0))
      {
        nf_lut_index[i]--;
        gl_address-=NF_PAGE_BY_LUT;
      }
      else
      {
        nf_lut_index[i]-=2;
        gl_address-=(NF_PAGE_BY_LUT << 1);
      }      

      Nf_read_open_spare_area_even(gl_address, 0x00);
      if (Nf_rd_byte_even() != 0xFF)                       /* LUT have been modified */
      {
        nf_block_erase(gl_address);
        lut_is_present &= ~((0x01) << i);             /* Reset bit */
        nf_lut_index[i] = 0;
        Nf_wait_busy_even();
      }

    }
  }
  /*****************************************************************/
  /*          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_even[i];
      start = (Uint16)(i) << NF_SHIFT_BLOCK_ZONE;                   /* starting value for each zone */
      j = 0;
      if (block == start)                                           /* starting block for scan      */
      {
        block = start + 1023;
      }
      block_valid = FALSE;                                          /* init flag block valid */
      do
      {
        gl_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
        Nf_read_open_spare_area_even(gl_address, 0x05);
        byte_5 = Nf_rd_byte_even();
        byte_6 = Nf_rd_byte_even();
  
        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 */

  {
    if ( !(lut_is_present & (0x01 << nf_zone)))                   /* If not LUT */
    {
      start         = 0x00;
      end           = 0x40;                                               
      free_bloc_pos_even = (Uint16)(last_physical_used_block_even[nf_zone]);
      free_bloc_pos_odd  = (Uint16)(last_physical_used_block_odd[nf_zone]);
      gl_address         = (Uint32)(nf_lut_block[nf_zone]) << NF_SHIFT_SECTOR_BLOCK;        
      page               = 0;
      do
      {
        Nf_CS_EVEN();
        nf_init_buffer();                         
        temp_address  = (Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE;   
        block         = (Uint16)(nf_zone) << NF_SHIFT_BLOCK_ZONE;
        Nf_wait_busy_even();
        for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--)                   /* for each block                     */
        { 
          Nf_read_open_spare_area_even(temp_address, 0x05);
          if (Nf_rd_byte_even() == 0xFF)                                 /* If not a bad block                 */
          {
            block2.b[0] = Nf_rd_byte_even();                             /* Read logical block address         */
            block2.b[1] = Nf_rd_byte_even();
            byte_6 = 2 * (block2.b[1] & 0xFE);
            if ( (block2.b[0] & 0xF8) == 0x10)
            {
              block2.w = (block2.w & 0x0FFF) >> 1;          
              if ( (block2.w < end) && (block2.w >= start)) 
              { 
#if 0
                if (gl_buffer[byte_6] != 0xFF)
                {
                  /* This physical block is already affected */
                  /* We have to find block to be deleted */
                  i = block;
                  Nf_read_open_spare_area_even(temp_address, 0x11);     /* Byte addr 11 -> block have to be deleted */
                  if (Nf_rd_byte_even() == 0x00)                        /* This is the block to be deleted */
                  {
                    nf_block_to_be_deleted_even = block;
                    block = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
                  }
                  else
                  {
                    nf_block_to_be_deleted_even = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
                    gl_buffer[byte_6]      = block >> 8;
                    gl_buffer[byte_6 + 1]  = block;
                  }
                  /* for the last block, find the last sector used */
                  temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
                  do
                  {
                    Nf_read_open_spare_area_even(temp_address, 0x06);
                    (((Byte*)&temp_address)[3])++;
                    byte_6 = Nf_rd_byte_even();
                  }
                  while ((byte_6 != 0xFF) && ( ((Byte*)&temp_address)[3] & 0x3F));

                  ((byte_6 != 0xFF) && !( ((Byte*)&temp_address)[3] & 0x3F))
                  {
                    (((Byte*)&temp_address)[3])--;
                  }
                  else
                  {
                    (((Byte*)&temp_address)[3])-=2;
                  }

                  Nf_read_open_spare_area_even(temp_address, 0x10);    /* Byte addr 10 -> internal counter page */

                  /* 0 <= block2 < 1024 */
                  gl_ptr_mem =  ((Uint32)(block2.w) << NF_SHIFT_SECTOR_BLOCK) + (temp_address & 0x3F) + 
                                ((Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE);
                  /* 0 <= block < 1024 * zone_max */
                  nf_current_physical_sector_addr_even = ((Uint32)(block) << NF_SHIFT_SECTOR_BLOCK) + (temp_address & 0x3F); 
                  gl_cpt_page = 0;
                  nf_block_used = TRUE;

                  byte_5 = ~(Nf_rd_byte_even());

                  while (byte_5 != 0)
                  {
                    ((Byte*)&gl_cpt_page)[0] += 2;
                    byte_5 = byte_5 >> 1;
                  }
                  if (gl_cpt_page)
                    ((Byte*)&gl_cpt_page)[0] -= 2;
                  if (((Byte*)&gl_cpt_page)[0] == 0x08)
                  {
                    gl_ptr_mem++;
                    nf_current_physical_sector_addr_even++;
                    gl_cpt_page = 0;
                  }
                  block = i;
                  temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
                  nf_close_write_session = TRUE;
                }
                else
#endif
                { /* New affected physical block */
                  gl_buffer[byte_6]      = block >> 8;
                  gl_buffer[byte_6 + 1]  = block;
                }
              }
            }
          }
          temp_address += NF_PAGE_PER_BLOCK; 
          block++;
        }
          
        /* affect to the free physical block a fictive logical block */
        /* free physical block => gl_buffer[x] = 0xFF */
        temp_address = (Uint32)(free_bloc_pos_even) << NF_SHIFT_SECTOR_BLOCK;
        for (i = 0; i <= 0xFE; i+=4)
        {
          if (gl_buffer[i] == 0xFF)
          {
            do                                                /* Search free physical block */
            {
              temp_address += NF_PAGE_PER_BLOCK;
              if (temp_address >= ((Uint32)(nf_zone + 1) << NF_SHIFT_SECTOR_ZONE) )
                  temp_address = (Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE;
    
              Nf_read_open_spare_area_even(temp_address, 0x05);
              
              byte_5 = Nf_rd_byte_even();                          /* Invalid/Valid block */
              byte_6 = Nf_rd_byte_even();                          /* Used/Unused block */
            }
            while ( ( (byte_6 != 0xFF) && (byte_6 != 0xE8) ) || (byte_5 != 0xFF) );
  
            free_bloc_pos_even     = (temp_address  >> NF_SHIFT_SECTOR_BLOCK);
            gl_buffer[i]           = ((Byte*)&free_bloc_pos_even)[0] | 0x80;
            gl_buffer[i + 1]       = ((Byte*)&free_bloc_pos_even)[1];
          }
        }

        Nf_CS_ODD();
        temp_address  = (Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE;   
        block         = (Uint16)(nf_zone) << NF_SHIFT_BLOCK_ZONE;
        Nf_wait_busy_odd();
        for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--)                   /* for each block                     */
        { 
          Nf_read_open_spare_area_odd(temp_address, 0x05);
          if (Nf_rd_byte_odd() == 0xFF)                                 /* If not a bad block                 */
          {
            block2.b[0] = Nf_rd_byte_odd();                             /* Read logical block address         */
            block2.b[1] = Nf_rd_byte_odd();
            byte_6 = (2 * (block2.b[1] & 0xFE)) + 2;
            if ( (block2.b[0] & 0xF8) == 0x10)
            {
              block2.w = (block2.w & 0x0FFF) >> 1;          
              if ( (block2.w < end) && (block2.w >= start)) 
              { 
#if 0
                if (gl_buffer[byte_6] != 0xFF)
                {
                  /* This physical block is already affected */
                  /* We have to find block to be deleted */
                  i = block;
                  Nf_read_open_spare_area_odd(temp_address, 0x11);     /* Byte addr 11 -> block have to be deleted */
                  if (Nf_rd_byte_even() == 0x00)                        /* This is the block to be deleted */
                  {
                    nf_block_to_be_deleted_odd = block;
                    block = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
                  }
                  else
                  {
                    nf_block_to_be_deleted_odd = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
                    gl_buffer[byte_6]      = block >> 8;
                    gl_buffer[byte_6 + 1]  = block;
                  }
                  /* for the last block, find the last sector used */
                  temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
                  do
                  {
                    Nf_read_open_spare_area_odd(temp_address, 0x06);
                    (((Byte*)&temp_address)[3])++;
                    byte_6 = Nf_rd_byte_odd();
                  }
                  while ((byte_6 != 0xFF) && ( ((Byte*)&temp_address)[3] & 0x3F));

                  ((byte_6 != 0xFF) && !( ((Byte*)&temp_address)[3] & 0x3F))
                  {
                    (((Byte*)&temp_address)[3])--;
                  }
                  else
                  {
                    (((Byte*)&temp_address)[3])-=2;
                  }

                  Nf_read_open_spare_area_odd(temp_address, 0x10);    /* Byte addr 10 -> internal counter page */

                  /* 0 <= block < 1024 * zone_max */
                  nf_current_physical_sector_addr_odd = ((Uint32)(block) << NF_SHIFT_SECTOR_BLOCK) + (temp_address & 0x3F); 
                  gl_cpt_page = 0;
                  nf_block_used = TRUE;

                  byte_5 = ~(Nf_rd_byte_even());

                  while (byte_5 != 0)
                  {
                    ((Byte*)&gl_cpt_page)[0] += 2;
                    byte_5 = byte_5 >> 1;
                  }
                  if (gl_cpt_page)
                    ((Byte*)&gl_cpt_page)[0] -= 2;
                  if (((Byte*)&gl_cpt_page)[0] == 0x08)
                  {
                    gl_ptr_mem++;
                    nf_current_physical_sector_addr_odd++;
                    gl_cpt_page = 0;
                  }
                  block = i;
                  temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
                  nf_close_write_session = TRUE;
                }
                else
#endif
                { /* New affected physical block */
                  gl_buffer[byte_6]      = block >> 8;
                  gl_buffer[byte_6 + 1]  = block;
                }
              }
            }
          }
          temp_address += NF_PAGE_PER_BLOCK; 
          block++;
        }
          
        /* affect to the free physical block a fictive logical block */
        /* free physical block => gl_buffer[x] = 0xFF */
        temp_address = (Uint32)(free_bloc_pos_odd) << NF_SHIFT_SECTOR_BLOCK;

⌨️ 快捷键说明

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