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

📄 nf.c

📁 c51snd1c硬盘播放器全部资料.源码.线路图.protel99se的pcb图
💻 C
📖 第 1 页 / 共 5 页
字号:
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        Usb_write_byte(Nf_rd_byte_even());
        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 */
    {
      nf_busy = TRUE;
      gl_ptr_mem++;                               /* new page          */
     
      if ( !(((Byte*)&gl_ptr_mem)[3] & 0x7F) )    /* New block ? */
      {
        nf_read_open(gl_ptr_mem << 2);
      }
      else
      {
        if (nf_parity_bit == NF_ODD)
        {
          ((Byte*)&nf_current_physical_sector_addr_odd)[3]++;        
        }
        else
        {
          ((Byte*)&nf_current_physical_sector_addr_even)[3]++;
        }        
        nf_parity_bit = ~nf_parity_bit;
        ((Byte*)&gl_cpt_page)[0] = 0;
      }
    }
    nb_sector--;
  }
  while (nb_sector != 0);
  while (!Usb_tx_complete());                   /* wait end of last transfer */
  Usb_clear_TXCMPL();                           /* ack transfer */
  return OK;
}


/*F**************************************************************************
* NAME: invert_nf_block
*----------------------------------------------------------------------------
* PARAMS:
*   
*
* return: 
*         
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function invert assigned and free physical block 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void invert_nf_block(void)
{
  nf_busy = FALSE;
  
  if (nf_block_used)                      /* If previous block have to de deleted */  
  {
    nf_parity_bit = NF_EVEN;
    nf_block_erase((Uint32)(nf_block_to_be_deleted_even) << NF_SHIFT_SECTOR_BLOCK);
    nf_parity_bit = NF_ODD;
    nf_block_erase((Uint32)(nf_block_to_be_deleted_odd)  << NF_SHIFT_SECTOR_BLOCK);
  }
  nf_gl_buf_idx++;                        /* increase the main buffer index */
  if (nf_gl_buf_idx >= nf_block_max)      /* if zone change */
  {
    nf_write_advanced = FALSE;
    nf_write_open(gl_ptr_mem << 2);
  }
  else
  {
    if ( !(nf_buf[nf_gl_buf_idx].even.w & 0x8000))  /* if block already assigned */               
    {     
      if ((nf_buf_free[gl_buf_free_idx].even & 0x7FFF) == nf_lut_block[nf_zone])
      {
        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_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 */
  }
}

/*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;
    }
  }

  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 - 1)))
  { /* 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 buff

⌨️ 快捷键说明

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