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

📄 nf.c

📁 MP3播放器详细设计方案
💻 C
📖 第 1 页 / 共 4 页
字号:
        for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--)         /* for each block             */
        {
          Nf_read_open_C_area(temp_address, 0x05);
          byte_5 = Nf_rd_byte();                          /* Block status byte          */
          if (byte_5 == 0xFF)                             /* If not a bad block         */
          {
            block2.b[0] = Nf_rd_byte();                   /* Read logical block address */
            block2.b[1] = Nf_rd_byte();
            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[2 * block2.b[1]] = block >> 8;
                gl_buffer[2 * block2.b[1] + 1] = block;
              }
            }
          }
          temp_address += 32; 
          block++;
        }
    
        temp_address = (Uint32)(free_bloc_pos) << 5;
        for (i = 0; i <= 0xFE; i += 2)
        {
          if (gl_buffer[i] == 0xFF)
          {
            do   
            {
              temp_address += 32;
              if (temp_address >= ((Uint32)(nf_zone + 1) << 15))
                  temp_address = (Uint32)(nf_zone) << 15;
              Nf_read_open_C_area(temp_address, 0x05);
             
              byte_5 = Nf_rd_byte();                          /* Invalid/Valid block        */
              byte_6 = Nf_rd_byte();                          /* Used/Unused block          */
            }
            while (((byte_6 != 0xFF) && (byte_6 != 0xE8)) || (byte_5 != 0xFF));
  
            free_bloc_pos = temp_address >> 5;
            gl_buffer[i] = (free_bloc_pos >> 8) + 0x80;
            gl_buffer[i + 1] = free_bloc_pos;
          }
        }
  
        /* Write second part of LUT */     
        Nf_write_open_B_area(gl_address, 0x00);
        nf_download_buffer();
  
        /* Write redundant data */
        Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF);
        Nf_wr_byte(0xFF); Nf_wr_byte(0xFF);
        Nf_wr_byte(0xFF);
        Nf_wr_byte(0xE8); Nf_wr_byte(0xFF);                   /* Logical block value */
        Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* ECC area 2 */
        Nf_wr_byte(0xE8); Nf_wr_byte(0xFF);                   /* Logical block value */
        Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* ECC area 1 */
         
        Nf_send_command(NF_PAGE_PROGRAM_CMD);
        start += 0x80;
        end   += 0x80;
        /* Increase the address of the conversion table */
        gl_address++;
      }
      while (start < NF_BLOCK_PER_ZONE);
    }
  }
  Nf_wait_busy();
}


/*F**************************************************************************
* NAME: nf_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   NF initialisation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void nf_init (void)
{
  P2 = NF_IDLE_STATE_ADD;
  Nf_CS_ON();

  Nf_send_command(NF_RESET_CMD);
  Nf_wait_busy();
  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;

  Nf_CS_ON();

  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 >> 5);

  /* Determinate zone */
  nf_zone = nf_logical_block / 1000;

  /* Each zone have 1000 data blocks */
  nf_logical_block = nf_logical_block - (1000 * (Uint16)(nf_zone));

  /* Calculate the address where are the physical block value */
  gl_address  = ((Uint32)(nf_lut_block[nf_zone])<<5) + ((Uint32)(nf_logical_block) >> 8);
  gl_address += ((Uint32)(nf_lut_index[nf_zone])<<2);
  /* Open the look-up table */
  Nf_wait_busy();
  if (nf_logical_block & 0x80)
  {
    Nf_send_command(NF_READ_B_AREA_CMD);            /* 2nd half page */
    Nf_send_address((nf_logical_block << 1) - 256);
  }
  else
  {
    Nf_send_command(NF_READ_A_AREA_CMD);            /* first half page */
    Nf_send_address(nf_logical_block << 1);
  }

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

  /* Read the physical block number */
  ((Byte*)&physical_block)[0] = Nf_rd_byte();
  ((Byte*)&physical_block)[1] = Nf_rd_byte();

  /* Calculate the physical sector address */
  nf_current_physical_sector_addr = ((Uint32)(physical_block) << 5) + (gl_ptr_mem & 0x1F); 
  Nf_CS_OFF();
  return OK;
}


/*F**************************************************************************
* NAME: nf_read_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Low level memory read close
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void nf_read_close (void)
{
  Nf_CS_OFF();
}


/*F*************************************************************************
* NAME: nf_read_byte
*---------------------------------------------------------------------------
* PARAMS:
*
* return:
*   Data read from memory
*---------------------------------------------------------------------------
* PURPOSE:
*   Low level memory read function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
****************************************************************************/
Byte nf_read_byte (void)
{
Byte b;
  
  if (!gl_cpt_page)
  {
    Nf_CS_ON();
    Nf_wait_busy();
    Nf_read_open_A_area(nf_current_physical_sector_addr, 0x00);
    b = Nf_rd_byte();
    Nf_CS_OFF();
  }
  else
  {
    b = Nf_rd_byte();
  }
  
  gl_cpt_page++;
  
  /* Detection of the end of data page */
  if (gl_cpt_page == NF_DATA_SIZE)                 
  {
    /* read spare data bytes */
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();
    Nf_rd_byte();

    gl_ptr_mem++;                           /* new page */
    gl_cpt_page=0;                          /* start at column 0 */

    if (!(gl_ptr_mem & 0x1F))               /* New block ? */
    {
      nf_read_open(gl_ptr_mem);
    }
    else
    {
      nf_current_physical_sector_addr++;
    }
  }
  return b;
}


/*F**************************************************************************
* NAME: nf_read_sector
*----------------------------------------------------------------------------
* PARAMS:
*   nb_sector:  number of contiguous sector to read 
*   global:     gl_ptr_mem
*
* return: OK read done
*         KO read failure
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function is an optimized function that writes nb-sector * 512 bytes
*   from NF card to USB controller 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   nb_sector always >= 1, can not be zero
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_read_sector(Uint16 nb_sector)
{
Byte i;
bit   begin_ping_pong;

  begin_ping_pong = TRUE;

  do
  {
    Nf_CS_ON();
    Nf_wait_busy();
    Nf_read_open_A_area(nf_current_physical_sector_addr, 0x00);
  
    for (i = 8; i != 0; i--)
    {
      Usb_write_byte(Nf_rd_byte());         /* read 64 bytes from card */
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());

⌨️ 快捷键说明

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