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

📄 nf.c

📁 ATMEL公司AT89C51SND1为主控制器MP3源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
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 */
      nf_invert_block();
      nf_logical_block++;         /* increase the logical block */
      nf_calc_logical_block();    /* calculate the redundant block address */
    }
  }

  if (nf_wr_open)
  {
    Nf_CS_ON();
    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)                                         /* nf > 128Mbytes ?   */
      Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* row address Byte 2 */
    nf_wr_open = FALSE;
    Nf_CS_OFF();
  }

  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 */
    }
  }
  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;
Uint16 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 or block is not in the cache */
      {
        nf_write_advanced = FALSE;            /* desactive optimized process to force cache reconstruction */
        nf_write_open(gl_ptr_mem << 2);
      }
      else
      {
        nf_invert_block();      /* invert block if necessary and update physical sector addr  */
        nf_logical_block++;     /* increase the logical block                                 */
        nf_calc_logical_block();/* calculate the redundant block address                      */
      }
    }
  
  
    if (nf_wr_open) 
    {
      Nf_CS_ON();
      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         */
      Nf_CS_OFF();
    }

    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);        /* launch programming */
        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;       /* active write optimization */
  }

  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 * nf_format (void)
{
#define CYL_128  (Uint16)((Uint16)(500) -  (MEM_RESERVED_SIZE / (16 * 32)))
#define CYL_256  (Uint16)((Uint16)(500) -  (MEM_RESERVED_SIZE / (16 * 64)))
#define CYL_512  (Uint16)((Uint16)(1000) - (MEM_RESERVED_SIZE / (16 * 64)))
#define CYL_1024 (Uint16)((Uint16)(2000) - (MEM_RESERVED_SIZE / (16 * 64)))
#define CYL_2048 (Uint16)((Uint16)(4000) - (MEM_RESERVED_SIZE / (16 * 64)))

code  s_format  nf_tab_format[]=
  {
   /* nb_cylinder, nb_head,   nb_sector, nb_hidden,   nb_sector_per_cluster  */
    { CYL_128,     (Byte)16,  (Byte)32,  (Byte)107,   (Byte)4 },   /* 128MB  */
    { CYL_256,     (Byte)16,  (Byte)64,  (Byte)107,   (Byte)8 },   /* 256MB  */
    { CYL_512,     (Byte)16,  (Byte)64,  (Byte)107,   (Byte)8 },   /* 512MB  */
    { CYL_1024,    (Byte)16,  (Byte)64,  (Byte)107,   (Byte)8 },   /* 1024MB */
    { CYL_2048,    (Byte)16,  (Byte)64,  (Byte)107,   (Byte)8 },   /* 2048MB */

  };

code  s_format  nf_tab_format_reserved[]=
  {
   /* 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  */
    { (Uint16)1000, (Byte)16,  (Byte)64,  (Byte)107,   (Byte)8 },  /* 512MB  */
    { (Uint16)2000, (Byte)16,  (Byte)64,  (Byte)107,   (Byte)8 },  /* 1024MB */
    { (Uint16)4000, (Byte)16,  (Byte)64,  (Byte)107,   (Byte)8 },  /* 2048MB */

  };
  /* Erase all block */
  nf_erase_all_block();

  /* -- NF Type Selection -- */
  if (reserved_disk_space == FALSE)
  {
    if (nf_reserved_space == TRUE)
    {
      nf_mem_size -= MEM_RESERVED_SIZE;
      nf_reserved_space = FALSE;
    }
    return &nf_tab_format[NF_TYPE];
  }
  else
  {
    if (nf_reserved_space == FALSE)
    {
      nf_reserved_space = TRUE;
      nf_mem_size += MEM_RESERVED_SIZE;
    }
    return &nf_tab_format_reserved[NF_TYPE];
  }
}

⌨️ 快捷键说明

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