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

📄 nf.c

📁 ATMEL 89c51sndc mp3外接硬盘源码
💻 C
📖 第 1 页 / 共 5 页
字号:
}


/*F**************************************************************************
* NAME: nf_write_byte
*----------------------------------------------------------------------------
* PARAMS:
*   b: data to write
*
* RETURN:
*   write status: OK: write done
*                 KO: write not done
*
*----------------------------------------------------------------------------
* PURPOSE:
*   Low level memory write function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_byte (Byte b)
{
Byte pdata *ptr = gl_buffer;

  if (nf_busy)
  {
    nf_busy = FALSE;
    if (!gl_cpt_page) /* if we are at the beginning of a new page */
    {
      /* if there is a block change */
      if ( !(gl_ptr_mem & 0x3F) )                            
      { 
        /* If previous block have to de deleted */
        if (nf_block_used)       
        {
          Nf_CS_EVEN();
          nf_block_erase((Uint32)(nf_block_to_be_deleted_even) << 5);
          Nf_CS_ODD();
          nf_block_erase((Uint32)(nf_block_to_be_deleted_odd) << 5);
        }
        /* increase the main buffer index */
        nf_gl_buf_idx++;
        /* if zone change */
        if (nf_gl_buf_idx >= block_max)
        {
          nf_write_open(gl_ptr_mem);
        }
        else
        {
          /* if the block in the buffer is already assign, then invert it with a spare block */
          if ( !(buf[nf_gl_buf_idx].even.w & 0x8000))                 
          {                                                 
            if ((buf_free[nf_gl_buf_free_idx].even & 0x7FFF) == lut_block[nf_zone])
            {
              nf_gl_buf_free_idx++;
              if (nf_gl_buf_free_idx >= nf_spare_block)
              {
                nf_gl_buf_free_idx = 0;
              }      
            }        
            /* invert the block and assign the next block to be deleted */
            nf_block_to_be_deleted_even = buf[nf_gl_buf_idx].even.w;
            nf_block_to_be_deleted_odd = buf[nf_gl_buf_idx].odd.w;
            buf[nf_gl_buf_idx].even.w = buf_free[nf_gl_buf_free_idx].even & 0x7FFF;
            buf[nf_gl_buf_idx].odd.w = buf_free[nf_gl_buf_free_idx].odd & 0x7FFF;
            buf_free[nf_gl_buf_free_idx].even = nf_block_to_be_deleted_even | 0x8000;
            buf_free[nf_gl_buf_free_idx].odd = nf_block_to_be_deleted_odd | 0x8000;
            /* block have to be deleted */
            nf_block_used = TRUE;
            /* increase the spare buffer index */
            nf_gl_buf_free_idx++;
            if ((nf_gl_buf_free_idx++) >= nf_spare_block )
            {
              nf_gl_buf_free_idx = 0;
            }
    
          }
          else /* The block is not assigned. Nothing to do */
          {
            buf[nf_gl_buf_idx].even.w &= 0x7FFF;
            nf_block_used = FALSE;
          }
          /* update the max index buffer */
          if (nf_gl_buf_idx > nf_gl_buf_idx_max)
            nf_gl_buf_idx_max = nf_gl_buf_idx;
    
          /* Update current physical sector */
          nf_current_physical_sector_addr_even = (Uint32)(buf[nf_gl_buf_idx].even.w) << 5;
          nf_current_physical_sector_addr_odd = (Uint32)(buf[nf_gl_buf_idx].odd.w) << 5;
          /* increase the logical block */
          nf_logical_block++;
          /* calculate the redundant block address */
          nf_calc_logical_block();
        }
        nf_parity_bit = NF_EVEN;
      }
    }
  
  }

  gl_buffer[(gl_cpt_page & 0xFF)] = b;        /* Save data in gl_buffer                                 */  
  gl_cpt_page++;                              /* Increase internal page counter                         */
  if (gl_cpt_page == 256)                     /* First 256 bytes                                        */
  {
    if (nf_parity_bit == NF_ODD)                          /* Select Nand Flash 1/2                                  */
    {
      Nf_active_ODD();
      Nf_wait_busy_odd();  
      Nf_write_open_A_area_odd(nf_current_physical_sector_addr_odd, 0x00);
      nf_download_buffer();                                                   /* Download the buffer        */
      Nf_send_command_odd(NF_PAGE_PROGRAM_CMD);                                   /* Send program command       */
    }
    else
    {
      Nf_active_EVEN();
      Nf_wait_busy_even();  
      Nf_write_open_A_area_even(nf_current_physical_sector_addr_even, 0x00);
      nf_download_buffer();                                                   /* Download the buffer        */
      Nf_send_command_even(NF_PAGE_PROGRAM_CMD);                                   /* Send program command       */
    }
    nf_busy = TRUE;                                                         /* Set busy flag              */    

  }
  else
  {
    if (gl_cpt_page == NF_DATA_SIZE)                                        /* last 256 bytes             */
    {
      if (nf_parity_bit == NF_ODD)
      {
        Nf_active_ODD();
        Nf_wait_busy_odd();
        Nf_write_open_B_area_odd(nf_current_physical_sector_addr_odd, 0x00);
        nf_download_buffer();
        nf_update_spare_data();
        Nf_send_command_odd(NF_PAGE_PROGRAM_CMD);

        nf_current_physical_sector_addr_odd++;
      }
      else
      {
        Nf_active_EVEN();
        Nf_wait_busy_even();
        Nf_write_open_B_area_even(nf_current_physical_sector_addr_even, 0x00);
        nf_download_buffer();
        nf_update_spare_data();
        Nf_send_command_even(NF_PAGE_PROGRAM_CMD);

        nf_current_physical_sector_addr_even++;
      }
      nf_busy = TRUE;
      gl_ptr_mem++;
      nf_parity_bit = ~nf_parity_bit;
      gl_cpt_page = 0;      
    }
  }
  return OK;
}


/*F**************************************************************************
* NAME: nf_write_sector
*----------------------------------------------------------------------------
* PARAMS:
*   global: gl_ptr_mem
*
* return:
*   write status: OK: write done
*                 KO: write not done
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function is an optimized function that writes 512 bytes from USB
*   controller to NF card
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_sector (Uint16 nb_sector)
{

  Byte j;

  do
  {
    if (nf_busy)
    {
      nf_busy = FALSE;
      if ( !(gl_ptr_mem & 0x3F) )       /* if there is a block change           */                           
      { 
        if (nf_block_used)                 /* if previous block have to de deleted */      
        {
          Nf_CS_EVEN();
          nf_block_erase((Uint32)(nf_block_to_be_deleted_even) << 5);
          Nf_CS_ODD();
          nf_block_erase((Uint32)(nf_block_to_be_deleted_odd) << 5);
        }

        nf_gl_buf_idx++;                 /* increase the main buffer index */
        if (nf_gl_buf_idx >= block_max)  /* if zone change                 */
        {
          nf_write_open(gl_ptr_mem);  /* update the main buffer         */
        }
        else                          /* don't change the buffer        */
        {
          /* if the block in the buffer is already assign, then invert it with a spare block */
          if ( !(buf[nf_gl_buf_idx].even.w & 0x8000))                 
          {                          
            if ((buf_free[nf_gl_buf_free_idx].even & 0x7FFF) == lut_block[nf_zone])
            {
              nf_gl_buf_free_idx++;
              if (nf_gl_buf_free_idx >= nf_spare_block)
              {
                nf_gl_buf_free_idx = 0;
              }      
            }        
            /* invert the block and assign the next block to be deleted */
            nf_block_to_be_deleted_even        = buf[nf_gl_buf_idx].even.w;
            nf_block_to_be_deleted_odd         = buf[nf_gl_buf_idx].odd.w;
            buf[nf_gl_buf_idx].even.w          = buf_free[nf_gl_buf_free_idx].even  & 0x7FFF;
            buf[nf_gl_buf_idx].odd.w           = buf_free[nf_gl_buf_free_idx].odd   & 0x7FFF;
            buf_free[nf_gl_buf_free_idx].even  = nf_block_to_be_deleted_even  | 0x8000;
            buf_free[nf_gl_buf_free_idx].odd   = nf_block_to_be_deleted_odd   | 0x8000;
            nf_block_used = TRUE;              /* block have to be deleted */
            nf_gl_buf_free_idx++;              /* increase the spare buffer index */
            if ((nf_gl_buf_free_idx++) >= nf_spare_block )
            {
              nf_gl_buf_free_idx = 0;
            }
          }
          else /* The block is not assigned. Nothing to do */
          {
            buf[nf_gl_buf_idx].even.w  &= 0x7FFF;
            buf[nf_gl_buf_idx].odd.w   &= 0x7FFF;
            nf_block_used = FALSE;
          }

          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)(buf[nf_gl_buf_idx].even.w)  << 5;
          nf_current_physical_sector_addr_odd  = (Uint32)(buf[nf_gl_buf_idx].odd.w)   << 5;
          nf_logical_block++;                    /* increase the logical block             */
          nf_calc_logical_block();            /* calculate the redundant block address  */
        }
        nf_parity_bit = NF_EVEN;
      }
    }
    
    if (nf_parity_bit == NF_ODD)
    {
      Nf_active_ODD();
      Nf_wait_busy_odd();  
      Nf_write_open_A_area_odd(nf_current_physical_sector_addr_odd, 0x00);            
      for (j = 8; j != 0; j--)
      {
        while (!Usb_rx_complete());             /* wait end of reception */
        Nf_wr_byte_odd(Usb_read_byte());            /* write 64 bytes to card */
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Nf_wr_byte_odd(Usb_read_byte());
        Usb_clear_RXOUT_PP();                      /* usb read acknowledgement */
      }
      nf_current_physical_sector_addr_odd++;
      nf_update_spare_data();
      Nf_send_command_odd (NF_PAGE_PROGRAM_CMD);    
    }
    else
    {
      Nf_active_EVEN();

⌨️ 快捷键说明

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