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

📄 nf.c

📁 ATMEL MP3 源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
    if (NF_5_CYCLE_ADDRESS_BIT)                        /* Size of card >= 128Mbytes ?  */
      Nf_send_address ( ((Byte*)&gl_address)[1] ); /* Row address Byte 2           */
    Nf_wr_byte(0x00);
    Nf_send_command (NF_PAGE_PROGRAM_CMD);
    Nf_wait_busy();
  }
  Nf_CS_OFF();
  return OK;
}




/*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)
{
  if (nf_busy)
  {
    nf_busy = FALSE;
    /* If previous block have to de deleted */
    if (nf_block_used)       
    {
      nf_block_erase((Uint32)(nf_block_to_be_deleted) << NF_SHIFT_SECTOR_BLOCK);
    }
    /* increase the main buffer index */
    nf_gl_buf_idx++;
    /* if zone change */
    if (nf_gl_buf_idx >= nf_block_max)
    {
      nf_write_open(gl_ptr_mem << 2);
    }
    else
    {
      /* if the block in the buffer is already assign, then invert it with a spare block */
      if ( !(nf_buf[nf_gl_buf_idx].w & 0x8000))                 
      {                                                 
        if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_lut_block[nf_zone])
        {
          gl_buf_free_idx++;
          if (gl_buf_free_idx >= nf_spare_block)
          {
            gl_buf_free_idx = 0;
          }      
        }        
        /* invert the block and assign the next block to be deleted */
        nf_block_to_be_deleted = nf_buf[nf_gl_buf_idx].w;
        nf_buf[nf_gl_buf_idx].w = nf_buf_free[gl_buf_free_idx] & 0x7FFF;
        nf_buf_free[gl_buf_free_idx] = nf_block_to_be_deleted | 0x8000;
        nf_block_used = TRUE;                               /* block have to be deleted         */
        gl_buf_free_idx++;
        if (gl_buf_free_idx >= nf_spare_block )         /* increase the spare buffer index  */
        {
          gl_buf_free_idx = 0;
        }
    
      }
      else /* The block is not assigned. Nothing to do */
      {
        nf_buf[nf_gl_buf_idx].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 = (Uint32)(nf_buf[nf_gl_buf_idx].w) << NF_SHIFT_SECTOR_BLOCK;
      nf_logical_block++;                         /* increase the logical block */
      nf_calc_logical_block();                    /* calculate the redundant block address */
    }
  }

  Nf_CS_ON();
  if (nf_wr_open)
  {
    Nf_wait_busy();
    Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);
    Nf_send_address ( ((Byte*)&gl_cpt_page)[1]);
    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 card >= 128Mbytes ?  */
        Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* Row address Byte 2           */
    nf_wr_open = FALSE;
  }

  Nf_wr_byte(b);
  gl_cpt_page++;

  if ( ((Byte*)&gl_cpt_page)[0] == 0x08)        /* end of sector */
  {
    nf_update_spare_data();
    Nf_send_command(NF_PAGE_PROGRAM_CMD);       /* valid the page programmation */  
    nf_wr_open = TRUE;                          /* next time send write open command */
    gl_ptr_mem++;
    gl_cpt_page = 0;      

    if (!(((Byte*)&gl_ptr_mem)[3] & 0x3F))      /* If there is a block change */
    {
      nf_busy = TRUE;                           /* then set busy flag */
    }
    else
    {
      nf_current_physical_sector_addr++;        /* else write next sector */
    }
    Nf_wait_busy();
  }
  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 nb-sector * 512 bytes
*   from USB controller to NF card
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   nb_sector always >= 1, can not be zero
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_sector (Uint16 nb_sector)
{
Byte j;
Byte i;
  for (i = nb_sector; i != 0; i--)
  {
    if (nf_busy)                              /* if there is a block change */
    {
      nf_busy = FALSE;

      if (nf_block_used)                      /* If previous block have to be deleted */
      {
        nf_block_erase((Uint32)(nf_block_to_be_deleted) << 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_open(gl_ptr_mem << 2);
      }
      else
      {
        /* if the block in the buffer is already assign, then invert it with a spare block */
        if ( !(nf_buf[nf_gl_buf_idx].w & 0x8000))                 
        {                                                 
          if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_lut_block[nf_zone])
          {
            gl_buf_free_idx++;
            if (gl_buf_free_idx >= nf_spare_block)
            {
              gl_buf_free_idx = 0;
            }      
          }        
          /* invert the block and assign the next block to be deleted */
          nf_block_to_be_deleted = nf_buf[nf_gl_buf_idx].w;
          nf_buf[nf_gl_buf_idx].w = nf_buf_free[gl_buf_free_idx] & 0x7FFF;
          nf_buf_free[gl_buf_free_idx] = nf_block_to_be_deleted | 0x8000;
          nf_block_used = TRUE;                                 /* block have to be deleted         */
          if ((gl_buf_free_idx++) >= nf_spare_block )           /* increase the spare buffer index  */
          {
            gl_buf_free_idx = 0;
          }
        }
        else /* The block is not assigned. Nothing to do */
        {
          nf_buf[nf_gl_buf_idx].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 = (Uint32)(nf_buf[nf_gl_buf_idx].w) << NF_SHIFT_SECTOR_BLOCK;
        nf_logical_block++;               /* increase the logical block             */
        nf_calc_logical_block();          /* calculate the redundant block address  */
      }
    }
  
    Nf_CS_ON();
  
    if (nf_wr_open)
    {
      nf_wr_open = FALSE;
      Nf_wait_busy();
      Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);
      Nf_send_address ( ((Byte*)&gl_cpt_page)[1]);
      Nf_send_address ( ((Byte*)&gl_cpt_page)[0]);
      Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[3]);    /* Row address Byte 0           */
      Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[2]);    /* Row address Byte 1           */
      if (NF_5_CYCLE_ADDRESS_BIT)                                             /* Size of card >= 128Mbytes ?  */
        Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* Row address Byte 2           */
    }

    for (j = 8; j != 0; j--)
    {
      while (!Usb_rx_complete());             /* wait end of reception */
      Nf_wr_byte(Usb_read_byte());            /* write 64 bytes to card */
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Nf_wr_byte(Usb_read_byte());
      Usb_clear_RXOUT_PP();                         /* usb read acknowledgement */
    }

    ((Byte*)&gl_cpt_page)[0] += 2;
    if ( ((Byte*)&gl_cpt_page)[0] == 0x08)            /* end of sector */
    {
      nf_update_spare_data();                         /* before, update spare data */
      gl_ptr_mem++;                                   /* increase gl_ptr_mem : next page */
      if (!(((Byte*)&gl_ptr_mem)[3] & 0x3F))                       /* if we change block */
      {
        Nf_send_command (NF_PAGE_PROGRAM_CMD);
        nf_busy = TRUE;                               /* then set busy flag -> find a new free block */
      }
      else
      {
        ((Byte*)&nf_current_physical_sector_addr)[3]++;/* else compute next sector */
        if (i == 1)                                   /* If last sector */
        {
          Nf_send_command (NF_PAGE_PROGRAM_CMD);      /* then send program command */
        }
        else
        {
          Nf_send_command (NF_CACHE_PROGRAM_CMD);     /* else send cache commmand */
        }
      }
      nf_wr_open = TRUE;                              /* Next time, send an open program command */
      gl_cpt_page = 0;                                /* Reset the global page counter */
    }
  }

  if (!nf_busy)
  {
    nf_write_advanced = TRUE;
  }

  return OK;
}



/*F**************************************************************************
* NAME: nf_format
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   Address of the format parameter structure in code
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function is called by the fat_format function and returns a pointer
*   to a table containing the format parameters after erasing the NF.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
s_format code * nf_format (void)
{

code  s_format  nf_tab_format[]=
  {
   /* nb_cylinder, nb_head,  nb_sector, nb_hidden, nb_sector_per_cluster */
    { (Uint16)500, (Byte)16,  (Byte)32,  (Byte)107,   (Byte)4 },   /* 128MB */
    { (Uint16)500, (Byte)16,  (Byte)64,  (Byte)107,   (Byte)8 },  /* 256MB */
  };

  /* Erase all block */
  nf_erase_all_block();

  /* -- NF Type Selection -- */
  return &nf_tab_format[NF_TYPE];
}

⌨️ 快捷键说明

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