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

📄 nf.c

📁 ATMEL 89c51sndc mp3外接硬盘源码
💻 C
📖 第 1 页 / 共 5 页
字号:
    for (nf_zone = 0; nf_zone < NF_ZONE_MAX; nf_zone++)   /* for each zone */
    {
      if (lut_is_present[nf_zone] == FALSE)
      {
        start = 0x00;
        end   = 0x40;     /* 256 bytes for gl_buffer <-> 64 blocks */                            
        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)(lut_block[nf_zone]) << 5; 
        
        do
        {
          /* Even Part */
          Nf_CS_EVEN();
          nf_init_buffer();                         /* Reinitialize the buffer            */
          temp_address = ((Uint32)(nf_zone)) << 5;   /* We start at the beginning          */
          block    = (Uint16)(nf_zone);
          Nf_wait_busy_even();
          for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--) /* for each block                     */
          { 
            Nf_read_open_C_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();
              if ( (block2.b[0] & 0xF8) == 0x10)
              {
                
                block2.w = (block2.w & 0x0FFF) >> 1;          
                if ( (block2.w < end) && (block2.w >= start)) 
                { /* Save logical block value in the buffer */
                  gl_buffer[(Byte)(4 * block2.b[1])] = block >> 8;
                  gl_buffer[(Byte)(4 * block2.b[1] + 1)] = block & 0xFF;
            
            
                }
              }
            }
            temp_address += (NF_ZONE_MAX << 5); /* 4*32 */
            block+= NF_ZONE_MAX;
          }
            
          /* affect to the free physical block a fictive logical block */
          /* free physical block => gl_buffer[x] = 0xFF */
          temp_address = (Uint32)(free_bloc_pos_even) << 5;
          for (i = 0; i <= 0xFC; i+=4)
          {
            if (gl_buffer[i] == 0xFF)
            {
              do                                                /* Search free physical block */
              {
                temp_address += (NF_ZONE_MAX << 5);
                if (temp_address >= ((Uint32)(NF_ZONE_MAX) << 15) )
                    temp_address = (Uint32)(nf_zone) << 5;
                Nf_read_open_C_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 >> 5;
              gl_buffer[i] = (free_bloc_pos_even >> 8) + 0x80;
              gl_buffer[i + 1] = free_bloc_pos_even & 0xFF;
            }
          }

          /* Odd Part */  
          Nf_CS_ODD();
          Nf_wait_busy_odd();
          
          temp_address  = (Uint32)(nf_zone) << 5;   /* We start at the beginning          */
          block         = (Uint16)(nf_zone);
          for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--) /* for each block                     */
          { 
            Nf_read_open_C_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();
              if ( (block2.b[0] & 0xF8) == 0x10)
              {
                
                block2.w = (block2.w & 0x0FFF) >> 1;          
                if ( (block2.w < end) && (block2.w >= start)) 
                { /* Save logical block value in the buffer */
                  gl_buffer[(Byte)(4 * block2.b[1] + 2)] = block >> 8;
                  gl_buffer[(Byte)(4 * block2.b[1] + 1 + 2)] = block & 0xFF;
          
                }
              }
            }
            temp_address += (NF_ZONE_MAX << 5); 
            block += NF_ZONE_MAX;
          }
          
          /* affect to the free physical block a fictive logical block */
          /* free physical block => gl_buffer[x] = 0xFF */
          temp_address = (Uint32)(free_bloc_pos_odd) << 5;
          for (i = 0; i <= 0xFC; i+=4)
          {
            if (gl_buffer[i + 2] == 0xFF)
            {
              do                                                /* Search free physical block */
              {
                temp_address += (NF_ZONE_MAX << 5);
                if (temp_address >= ((Uint32)(NF_ZONE_MAX) << 15) )
                    temp_address = (Uint32)(nf_zone) << 5;
                Nf_read_open_C_area_odd(temp_address, 0x05);
                byte_5 = Nf_rd_byte_odd();                          /* Invalid/Valid block */
                byte_6 = Nf_rd_byte_odd();                          /* Used/Unused block */
              }
              while ( ( (byte_6 != 0xFF) && (byte_6 != 0xE8) ) || (byte_5 != 0xFF) );
    
              free_bloc_pos_odd = temp_address >> 5;
              gl_buffer[i + 2] = (free_bloc_pos_odd >> 8) + 0x80;
              gl_buffer[i + 1 + 2] = free_bloc_pos_odd & 0xFF;
            }
          }

          /* Write first part of LUT */
          Nf_CS_EVEN();
          Nf_wait_busy_even();  
          Nf_write_open_A_area_even(gl_address, 0x00);
          nf_download_buffer();
          Nf_send_command_even(NF_PAGE_PROGRAM_CMD);
          
          start    += 0x40;                                 /* process next 64 logical block   */
          end      += 0x40;
          nf_init_buffer();                                 /* reinitialize the global buffer   */
          
          temp_address  = (Uint32)(nf_zone) << 5;           /* restart from the begin of zone   */
          block         = (Uint16)(nf_zone);
          Nf_wait_busy_even();  

          for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--)         /* for each block             */
          {
            Nf_read_open_C_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();
              if ( (block2.b[0] & 0xF8) == 0x10)
              {
                block2.w = (block2.w & 0x0FFF) >> 1;          
                if ( (block2.w < end) && (block2.w >= start)) 
                { /* Save logical block value in the buffer */
                  gl_buffer[(Byte)(4 * block2.b[1])] = block >> 8;
                  gl_buffer[(Byte)(4 * block2.b[1] + 1)] = block & 0xFF;
            
                }
              }
            }
            temp_address += (NF_ZONE_MAX << 5); 
            block+=NF_ZONE_MAX;
          }
   
          temp_address = (Uint32)(free_bloc_pos_even) << 5;
          for (i = 0; i <= 0xFC; i += 4)
          {
            if (gl_buffer[i] == 0xFF)
            {
              do   
              {
                temp_address += (NF_ZONE_MAX << 5);
                if (temp_address >= ((Uint32)(NF_ZONE_MAX) << 15) )
                    temp_address = (Uint32)(nf_zone) << 5;
                Nf_read_open_C_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 >> 5;
              gl_buffer[i] = (free_bloc_pos_even >> 8) + 0x80;
              gl_buffer[i + 1] = free_bloc_pos_even & 0xFF;
            }
          }

          /* Odd part */
          Nf_CS_ODD();
          temp_address = (Uint32)(nf_zone) << 5;           /* restart from the begin of zone   */
          block    = (Uint16)(nf_zone);
          Nf_wait_busy_odd();          
          for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--)         /* for each block             */
          {
            Nf_read_open_C_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();
              if ( (block2.b[0] & 0xF8) == 0x10)
              {
                
                block2.w = (block2.w & 0x0FFF) >> 1;          
                if ( (block2.w < end) && (block2.w >= start)) 
                { /* Save logical block value in the buffer */
                  gl_buffer[(Byte)(4 * block2.b[1] + 2)] = block >> 8;
                  gl_buffer[(Byte)(4 * block2.b[1] + 1 + 2)] = block & 0xFF;
                }
              }
            }
            temp_address += (NF_ZONE_MAX << 5); 
            block+=NF_ZONE_MAX;
          }
          
          temp_address = (Uint32)(free_bloc_pos_odd) << 5;
          for (i = 0; i <= 0xFC; i += 4)
          {
            if (gl_buffer[i + 2] == 0xFF)
            {
              do   
              {
                temp_address += (NF_ZONE_MAX << 5);
                if (temp_address >= ((Uint32)(NF_ZONE_MAX) << 15) )
                    temp_address = (Uint32)(nf_zone) << 5;

                Nf_read_open_C_area_odd(temp_address, 0x05);
                byte_5 = Nf_rd_byte_odd();                          /* Invalid/Valid block        */
                byte_6 = Nf_rd_byte_odd();                          /* Used/Unused block          */
              }
              while ( ( (byte_6 != 0xFF) && (byte_6 != 0xE8) ) || (byte_5 != 0xFF) );
          
              free_bloc_pos_odd = temp_address >> 5;
              gl_buffer[i + 2] = (free_bloc_pos_odd >> 8) + 0x80;
              gl_buffer[i + 1 + 2] = free_bloc_pos_odd & 0xFF;
            }
          }

          /* Write second part of LUT */     
          Nf_CS_EVEN();
          Nf_wait_busy_even();
          Nf_write_open_B_area_even(gl_address, 0x00);
          nf_download_buffer();
          
          /* Write redundant data */
          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);                   /* Logical block value */
          Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); /* ECC area 2 */
          Nf_wr_byte_even(0xE8); Nf_wr_byte_even(0xFF);                   /* Logical block value */
          Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); Nf_wr_byte_even(0xFF); /* ECC area 1 */
           
          Nf_send_command_even(NF_PAGE_PROGRAM_CMD);
          
          start += 0x40;
          end   += 0x40;
          /* Increase the address of the conversion table */
          gl_address++;
        }
        while (start < NF_BLOCK_PER_ZONE);
      }
  }
  Nf_wait_busy_even();
}


/*F**************************************************************************
* NAME: nf_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   NF initialisation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void nf_init (void)
{   
  P2 = NF_IDLE_STATE_ADD;
  Nf_CS_EVEN();
  Nf_wait_busy_even();
  Nf_send_command_even(NF_RESET_CMD);
  Nf_CS_ODD();
  Nf_wait_busy_odd();
  Nf_send_command_odd(NF_RESET_CMD);
  Nf_wait_busy_odd();

  read_spare_byte();
}

/*F**************************************************************************
* NAME: nf_read_open
*----------------------------------------------------------------------------
* PARAMS:
*   pos: address of the logic sector to read (size 512 bytes)
*
* return:
*   Update memory for reading
*----------------------------------------------------------------------------
* PURPOSE:
*   Low level memory read update
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
bit nf_read_open (Uint32 pos)
{
  Uint16 physical_block;

  if (nf_lut_modified)
  {
    nf_reassign_block();
    nf_lut_modified = 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++;
  }

  /* Calculate the address where are the physical block value */
  gl_address  = ((Uint32)(lut_block[nf_zone])<<5);     /* 32*lut block                                 */
  gl_address += ((Uint32)(nf_logical_block) >> 7);        /* logical block / 128 = page number of the lut */
  gl_address += ((Uint32)(lut_index[nf_zone])<<3);     /* lut index * 8 = offset for beginning of lut  */

  /* Open the look-up table */
  Nf_CS_EVEN();       
  Nf_wait_busy_even();
  if (nf_logical_block & 0x40)                         /* 64 logical blocks by part of lut page        */

⌨️ 快捷键说明

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