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

📄 nf.c

📁 c51snd1c硬盘播放器全部资料.源码.线路图.protel99se的pcb图
💻 C
📖 第 1 页 / 共 5 页
字号:
    }
  #else
    nf_mem_size = (NF_SECTOR_SIZE + 1) * 4 - 1 - MEM_RESERVED_SIZE;
  #endif

  nf_reserved_space_start = nf_mem_size + 1;

  return OK;
}




/*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_close_write_session = FALSE;
    nf_force_write_close();
    nf_reassign_block();
    nf_lut_modified = FALSE;
  }

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


  nf_logical_block  = (gl_ptr_mem >> NF_SHIFT_SECTOR_BLOCK);     
  nf_zone           = nf_logical_block / 1000;                            /* Determinate logical zone */
  nf_logical_block  = nf_logical_block - (1000 * (Uint16)(nf_zone));      /* Logical block value      */

  gl_address        = ((Uint32)(nf_lut_block[nf_zone])<<NF_SHIFT_SECTOR_BLOCK)+ /* lut address calculation  */
                      ((Uint32)(nf_lut_index[nf_zone]));

  Nf_wait_busy();
  Nf_send_command(NF_READ_CMD);                   /* open the look-up table       */
  Nf_send_address (nf_logical_block << 1);        /* column address Byte 0        */
  Nf_send_address (nf_logical_block >> 7);        /* column address Byte 1        */
  Nf_send_address ( ((Byte*)&gl_address)[3] );    /* row address Byte 0           */
  Nf_send_address ( ((Byte*)&gl_address)[2] );    /* row address Byte 1           */
  if (NF_5_CYCLE_ADDRESS_BIT)                     /* size of nf > 128Mbytes ?     */
    Nf_send_address ( ((Byte*)&gl_address)[1] );  /* row address Byte 2           */
  Nf_send_command(NF_READ_CMD2);
  nf_busy = TRUE;                                 /* set busy flag                */
  nf_write_advanced = FALSE;                      /* desactive write optimization */

  Nf_wait_busy();

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

  /* Calculate the physical sector address */
  nf_current_physical_sector_addr = ((Uint32)(physical_block) << NF_SHIFT_SECTOR_BLOCK) + 
                                    (((Byte*)&gl_ptr_mem)[3] & 0x3F);   
  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 (nf_busy)
  {
    nf_busy = FALSE;
    Nf_CS_ON();
    Nf_wait_busy();
    Nf_send_command(NF_READ_CMD);
    Nf_send_address( 0x00);
    Nf_send_address( ((Byte*)&gl_cpt_page)[0]);
    Nf_send_address( ((Byte*)&nf_current_physical_sector_addr)[3]);
    Nf_send_address( ((Byte*)&nf_current_physical_sector_addr)[2]);
    if (NF_5_CYCLE_ADDRESS_BIT)                                         /* Size of nf >= 128Mbytes ?    */
      Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* Row address Byte 2           */
    Nf_send_command(NF_READ_CMD2);
    Nf_wait_busy();
    b = Nf_rd_byte();
    Nf_CS_OFF();
  }
  else
  {
    b = Nf_rd_byte();
  }
  gl_cpt_page++;
  
  if ( ((Byte*)&gl_cpt_page)[0] == 0x08)    /* Detection of the end of data page */
  {
    gl_ptr_mem++;                           /* new page          */
    gl_cpt_page=0;                          /* reset column      */
    if ( !(((Byte*)&gl_ptr_mem)[3] & 0x3F) )/* New block ?       */
    {
      nf_read_open(gl_ptr_mem << 2);
    }
    else
    {
      ((Byte*)&nf_current_physical_sector_addr)[3]++;
      nf_busy = TRUE;                       /* Force read open command */
    }
  }
  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;
  /* Start a transfert of nb_sector * 512 bytes */
  do  
  {
    Nf_CS_ON();
    if (nf_busy)                   /* send a read command */
    {
      Nf_send_command(NF_READ_CMD);
      Nf_send_address( 0x00);
      Nf_send_address( ((Byte*)&gl_cpt_page)[0]);
      Nf_send_address( ((Byte*)&nf_current_physical_sector_addr)[3]);
      Nf_send_address( ((Byte*)&nf_current_physical_sector_addr)[2]);
      if (NF_5_CYCLE_ADDRESS_BIT)                                         /* Size of nf >= 128Mbytes ?  */
        Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* Row address Byte 2         */
      Nf_send_command(NF_READ_CMD2);
      nf_busy = FALSE;
      Nf_wait_busy();
    }

    for (i = 8; i != 0; i--)                                /* 8 * 64 bytes = 512 bytes */
    {
      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());
      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());
      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());

      if (begin_ping_pong)
      {
        begin_ping_pong = FALSE;
      }
      else
      {
        while (!Usb_tx_complete());             /* wait end of transfer               */
        Usb_clear_TXCMPL();                     /* ack transfer                       */
      }
      Usb_set_TXRDY();                          /* start usb transfer                 */    
    }

    ((Byte*)&gl_cpt_page)[0] += 2;
    if ( ((Byte*)&gl_cpt_page)[0] == 0x08)      /* Detection of the end of data page  */
    {
      gl_ptr_mem++;                             /* new page                           */
      if ( !(((Byte*)&gl_ptr_mem)[3] & 0x3F) )  /* New block ?                        */
      {
        nf_read_open(gl_ptr_mem << 2);
      }
      else
      {
        ((Byte*)&nf_current_physical_sector_addr)[3]++;
        gl_cpt_page = 0;
        nf_busy = TRUE;
      }
    }
    nb_sector--;
    Nf_CS_OFF();
  }
  while (nb_sector != 0);
  while (!Usb_tx_complete());                   /* wait end of last transfer        */
  Usb_clear_TXCMPL();                           /* ack transfer                     */
  return OK;
}


/*F**************************************************************************

⌨️ 快捷键说明

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