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

📄 nf.c

📁 MP3播放器详细设计方案
💻 C
📖 第 1 页 / 共 4 页
字号:
      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 */    
    }
  
    gl_ptr_mem++;                             /* new page          */
    Nf_CS_OFF();
    if ( !(gl_ptr_mem & 0x1F) )               /* New block ? */
    {
      nf_read_open(gl_ptr_mem);
    }
    else
    {
      current_physical_sector_addr++;
    }
    nb_sector--;
  }
  while (nb_sector != 0);

  while (!Usb_tx_complete());             /* wait end of last transfer */
  Usb_clear_TXCMPL();                     /* ack transfer */

  return OK;
}


/*F**************************************************************************
* NAME: nf_write_open
*----------------------------------------------------------------------------
* AUTHOR:
*----------------------------------------------------------------------------
* 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;
Byte j;
bit change_zone = FALSE;

  gl_ptr_mem = pos;
  gl_cpt_page = 0;

  /* Determine the logical block value */
  logical_block = (gl_ptr_mem >> 5);
  nf_zone = 0;

  while (logical_block > 999)
  {
    logical_block -= 1000;
    nf_zone++;
  }
  Nf_CS_ON();

  if (nf_zone != nf_old_zone)
  {
    change_zone = TRUE;
    if (lut_modified)
    { /* save old lut */
      i = nf_old_zone;
      nf_old_zone = nf_zone;
      nf_zone = i;
      nf_reassign_block();
      lut_modified = FALSE;
      nf_zone = nf_old_zone;
    }
    else
    { /* update old zone value */
      nf_old_zone = nf_zone;
    }
    block_min = 0xFFFF;
    gl_buf_free_idx = 0;
  }

  if ( (logical_block >= block_min) && (logical_block < (block_min + NF_BUFFER_SIZE - 5)))
  { /* we don't have to update the buffer */
    nf_calc_logical_block();
    gl_buf_idx = logical_block - block_min;
  }
  else 
  { /* we have to update the buffer */
    /* Store the look up table block address */
    look_up_table_block =  lut_block[nf_zone];
    /* If LUT have been modified */
    if (lut_modified)           
    {
      nf_reassign_block();      /* update the LUT */
    }
    /* Calculate the address for LUT access */
    address = ((Uint32)(look_up_table_block)<<5) + ((Uint32)(logical_block) >> 8);
    address += (Uint32)(lut_index[nf_zone])<<2;
    /* Calculate the redundant block address value */
    nf_calc_logical_block();
    i = logical_block & 0xFF;
    /* For the current zone, initialize the number of spare block */
    spare_block = nf_spare_block_number[nf_zone];
    /* Calculate the low and the high block stored in the buffer */
    block_min = logical_block;
    block_max = ( (logical_block + NF_BUFFER_SIZE) > 999) ? (1000 - logical_block) : NF_BUFFER_SIZE;

    /* Open the look-up table */
    Nf_wait_busy();     
    if (logical_block & 0x80)
    {
      Nf_send_command(NF_READ_B_AREA_CMD);            /* 2nd half page */
      Nf_send_address( (logical_block << 1) - 256);
    
    }
    else
    {
      Nf_send_command(NF_READ_A_AREA_CMD);            /* first half page */
      Nf_send_address(logical_block << 1);
    }

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

    for (j = 0; j < block_max ; j++)
    {
      /* Read conversion table : 64 datas */
      buf[j].b[0] = Nf_rd_byte();
      buf[j].b[1] = Nf_rd_byte();
      i++;
      if (i == 0)
      {
        address++;
        Nf_send_command(NF_READ_A_AREA_CMD);
        Nf_send_address(0x00);
        Nf_send_address ( ((Byte*)&address)[3] );   /* 2nd address cycle         */
        Nf_send_address ( ((Byte*)&address)[2] );   /* 3rd address cycle         */
        if (NF_4_CYCLE_ADDRESS)                                        /* Size of card >= 64Mbytes ?*/
          Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle         */
        Nf_wait_busy();
      }
    }
  
    if (change_zone)        /* If it is a new zone, then load the spare buffer */
    {
      /* Check for used block : first, read in the LUT the free physical block */
      address = ((Uint32)(look_up_table_block)<<5) + 3;
      address += (Uint32)(lut_index[nf_zone])<<2;
  
      Nf_send_command(NF_READ_B_AREA_CMD);        /* Second half array          */
      Nf_send_address( 208);
      Nf_send_address ( ((Byte*)&address)[3] );   /* 2nd address cycle          */
      Nf_send_address ( ((Byte*)&address)[2] );   /* 3rd address cycle          */
      if (NF_4_CYCLE_ADDRESS)                                        /* Size of card >= 64Mbytes ?*/
        Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle          */
      
      Nf_wait_busy();
    
      for (i = 0; i <= (spare_block); i++)
      {
        buf_free[i] = (Uint16)(Nf_rd_byte()<<8);
        buf_free[i] +=  Nf_rd_byte();
      }
    }
    gl_buf_idx = 0;           /* initialize index for main buffer         */
    gl_buf_idx_max = 0;       /* initialize the max index for the buffer  */

  }
  
  /* if block is already assigned, then invert with a spare block */
  block_used = ( !(buf[gl_buf_idx].w & 0x8000) ) ? TRUE : FALSE;
  if (block_used)
  {
    if ((buf_free[gl_buf_free_idx] & 0x7FFF) == look_up_table_block)
    {
      gl_buf_free_idx++;
      if (gl_buf_free_idx >= spare_block  )
      {
        gl_buf_free_idx = 0;
      }      
    }
    /* assign block to be deleted */
    block_to_be_deleted = buf[gl_buf_idx].w;
    buf[gl_buf_idx].w = buf_free[gl_buf_free_idx];
    buf_free[gl_buf_free_idx++] = block_to_be_deleted | 0x8000;
    /* increase the index for spare block buffer */
    if (gl_buf_free_idx >= spare_block  )
    {
      gl_buf_free_idx = 0;
    }

  }

  /* Mark block as assigned */
  buf[gl_buf_idx].w &= 0x7FFF;       

  if (gl_buf_idx > gl_buf_idx_max)
    gl_buf_idx_max = gl_buf_idx;

  /* Update the current physical sector address */
  current_physical_sector_addr = ((Uint32)(buf[gl_buf_idx].w) << 5);

  if ( block_used )                   /* Fisrt block already used block ?  */
  { 
    nf_copy_block_head();             /* Copy the first part of the block  */
  }
  else                                 
  { 
    nf_init_spare();                   /* else init spare data for new logical block  */
  }

  nf_busy = FALSE;                     /* Clear flag busy */

  lut_modified = TRUE;
  Nf_wait_busy();
  /* Set LUT as modified */
  address = ((Uint32)(look_up_table_block)<<5);
  address += (Uint32)(lut_index[nf_zone])<<2;
  
  Nf_send_command(NF_READ_C_AREA_CMD);           /* Data spare area          */
  Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);/* Sequential data input Type Command */

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

  Nf_wr_byte(0x00);                           /* Reset first byte */
  Nf_send_command(NF_PAGE_PROGRAM_CMD);      /* Send program command to the device */
  
  Nf_wait_busy();



  return OK; 
}


/*F**************************************************************************
* NAME: nf_write_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Low level memory write close: release NF 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_close (void)
{ 
  Nf_CS_ON();

  if (gl_cpt_page != 0)       /* uncomplete write */
  {
    if (gl_cpt_page < 256)    /* First half page */
    {
      for ( ; gl_cpt_page < 256; gl_cpt_page++)
      {
        gl_buffer[gl_cpt_page] = 0x00;
      }
      Nf_wait_busy();
      Nf_send_command (NF_READ_A_AREA_CMD);
      Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);                 /* Sequential data input Type Command */
      Nf_send_address ( 0x00 );                                       /* 1st address cycle*/
      Nf_send_address ( ((Byte*)&current_physical_sector_addr)[3] );  /* 2nd address cycle*/
      Nf_send_address ( ((Byte*)&current_physical_sector_addr)[2] );  /* 3rd address cycle*/
      if (NF_4_CYCLE_ADDRESS)                                                      /* Size of card >= 64Mbytes ?*/
        Nf_send_address ( ((Byte*)&current_physical_sector_addr)[1] );/* 4th address cycle*/
      /* Copy the buffer */
      nf_download_buffer();
      /* Program the device */
      Nf_send_command(NF_PAGE_PROGRAM_CMD);
    }
    /* 2nd half page */
    for ( ; gl_cpt_page < 512; gl_cpt_page++)
    {
     gl_buffer[gl_cpt_page] = 0x00;
    }
    Nf_wait_busy();
    Nf_send_command (NF_READ_B_AREA_CMD);
    Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);                 /* Sequential data input Type Command */
    Nf_send_address ( 0x00 );                                       /* 1st address cycle*/
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[3] );  /* 2nd address cycle*/
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[2] );  /* 3rd address cycle*/
    if (NF_4_CYCLE_ADDRESS)                                                      /* Size of card >= 64Mbytes ?*/
      Nf_send_address ( ((Byte*)&current_physical_sector_addr)[1] );/* 4th address cycle*/
    /* Copy the buffer */
    nf_download_buffer();
    nf_update_spare_data();
    Nf_send_command(NF_PAGE_PROGRAM_CMD);   /* Send program command to the device */
    gl_cpt_page = 0;
    gl_ptr_mem++;
  }

  /* Copy last part of a block if the block was assigned */
  if (block_used)
  {
    if ( gl_ptr_mem & 0x1F ) 
      nf_copy_block_tail();
    nf_block_erase((Uint32)(block_to_be_deleted) << 5);    /* Erase old block */
  }
  Nf_wait_busy();

  Nf_CS_OFF();
  return OK;
}


⌨️ 快捷键说明

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