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

📄 nf.c

📁 这是用C语言写的USB源程序,相信对大家有所帮助
💻 C
📖 第 1 页 / 共 3 页
字号:
  // Select A area for erasing block process   
 	erase_free_block();

  /* Search next free block for look up table */
  address = address_look_up_table + 3;

  Nf_wait_busy();

  Nf_send_command(NF_READ_B_AREA_CMD);       
  Nf_send_address(index_look_up_table + 192);
  Nf_send_address( ((Byte*)&address)[3] );   
  Nf_send_address( ((Byte*)&address)[2] ); 
  if (nf_64)                               
    Nf_send_address( ((Byte*)&address)[1] );
  Nf_wait_busy();

  ((Byte*)&look_up_table_block)[0] = Nf_rd_byte();
  ((Byte*)&look_up_table_block)[1] = Nf_rd_byte();

  look_up_table_block = look_up_table_block & 0x7FFF;

  /* reassign block value on the look up table */
  nf_reassign_block();

  /* Initialize index_block_erased and index_reassign */
  for (i = 0; i < nf_zone_max; i++)
  {
    index_block_used[i] = 0;
  }
  index_block_erased = 0;
  index_reassign = 0;
  Nf_CS_OFF();
  return OK;
}

/*F**************************************************************************
* NAME:     nf_writeA_close
*----------------------------------------------------------------------------
* AUTHOR:
*----------------------------------------------------------------------------
* PARAMS:
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE:  Low level memory write close: release NF 
*
*****************************************************************************
* NOTE:
*
*****************************************************************************/
bit nf_writeA_close (void)
{ 
Byte i;

  Nf_CS_ON();

  Nf_wait_busy();
  /* Initialize index_block_erased and index_reassign */
  for (i = 0; i < nf_zone_max; i++)
  {
    index_block_used[i] = 0;
  }
  index_block_erased = 0;
  index_reassign = 0;
  Nf_CS_OFF();
  return OK;
}



/*F**************************************************************************
* NAME:     nf_write_byte
*----------------------------------------------------------------------------
* PARAMS:   b: data to write
*           global: gl_ptr_mem
*
* RETURN:   write status: OK: write done
*                         KO: write not done
*
*----------------------------------------------------------------------------
* PURPOSE:  Low level memory write function
*
*****************************************************************************/
bit nf_write_byte (Byte b)
{
Byte pdata *ptr = &gl_buffer[0];

  if ( nf_busy  )
  {
    nf_busy = FALSE;
    if ( !gl_cpt_page )   // If we are at the beginning of a new page 
    {
      if ( !(gl_ptr_mem & 0x1F))    
      { // New block : we need to determine if this new block is already used 
        nf_write_open (gl_ptr_mem);
      }
      else
      { // We just need to continue the write operation on the next physical sector
      	current_physical_sector_addr++;
      }
    }
 } 

  Nf_CS_ON();

  // Save data in gl_buffer   
  gl_buffer[(gl_cpt_page & 0xFF)] = b;
  // Increase internal page counter 
  gl_cpt_page++;                          
  if (gl_cpt_page == 256)                 // First 256 bytes 
  {
    // Open in write mode in A Area 
    Nf_wait_busy();
    nf_send_w_cmd(MODE_CMDA,1);

    // Copy the buffer 
    nf_download_buffer();
    Nf_send_command(NF_PAGE_PROGRAM_CMD);
    nf_busy = TRUE;    

  }
  else
    if (gl_cpt_page == NF_DATA_SIZE)        // check if end of data page      
    {
      // Open in write mode in B Area 
      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_64)                                       // Size of card >= 64Mbytes ?
        Nf_send_address ( ((Byte*)&current_physical_sector_addr)[1] );// 4th address cycle
      // Copy the buffer 
      nf_download_buffer();
      // Update spare data 
      nf_update_spare_data();
      Nf_send_command(NF_PAGE_PROGRAM_CMD);
      nf_busy = TRUE;
      gl_ptr_mem++;
      gl_cpt_page = 0;      
    }

  Nf_CS_OFF();
  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 (void)
{
  Byte i,j;

  if (nf_busy)
  {
    nf_busy = FALSE;
    if ( !(gl_ptr_mem & 0x1F) )    
    { /* New block : we need to determine if this new block is already used */
      nf_write_open (gl_ptr_mem);
    }
    else
    { /* We just need to continue the write operation on the next physical sector */
      /* Write open for the next sector */
      current_physical_sector_addr++;
    }

  }

  Nf_CS_ON();


  Nf_wait_busy();
  nf_send_w_cmd(MODE_CMDA,1);

  /* If no ECC */
  for (j = 8; j != 0; j--)
  {
    while (!Usb_rx_complete());             /* wait end of reception */

	for(i=0;i<4;i++)
	{
    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 */
  }
  /* Update spare data */
  nf_update_spare_data();

  Nf_send_command (NF_PAGE_PROGRAM_CMD);
  gl_ptr_mem++;              /* increase gl_ptr_mem : next page */
  nf_busy = TRUE;
  Nf_CS_OFF();
  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.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   MMC FORMAT PARAMETERS
*   CAPACITY  LBAs      CYL   HDs   S/T   CLUSTs  S/C   S/F   FAT   HID
*   16MB      31360     490   2     32    3908    8     12    12    32
*   28MB      54784     428   4     32    13652   4     54    16    32
*   32MB      62720     490   4     32    15632   4     62    16    32
*   64MB      125440    490   8     32    31282   4     123   16    32
*   128MB     250880    980   8     32    62581   4     245   16    32
*   256MB     501760    980   16    32    62720   8     245   16    32
*----------------------------------------------------------------------------
* 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)496, (Byte)4,  (Byte)16,  (Byte)57,  (Byte)32 }, // 16MB 
    { (Uint16)496, (Byte)8,  (Byte)16,  (Byte)49,  (Byte)32 }, // 32MB 
    { (Uint16)496, (Byte)8,  (Byte)32,  (Byte)39,  (Byte)32 }, // 64MB 
    { (Uint16)496, (Byte)16, (Byte)32,  (Byte)63,  (Byte)32 }, // 128MB 
  };

  // Erase all block 
  nf_erase_all_block();

  // -- NF Type Selection -- 
  if (nf_device_type == NF_SIZE_128MB) return &nf_tab_format[NF_128MB];
  if (nf_device_type == NF_SIZE_64MB)  return &nf_tab_format[NF_64MB];
  if (nf_device_type == NF_SIZE_32MB)  return &nf_tab_format[NF_32MB];
  if (nf_device_type == NF_SIZE_16MB)  return &nf_tab_format[NF_16MB];
}

//****************************************************************//
// Create By lijinlu 2002.11.25
// save parameter in flash
//----------------------------------------------------------------//
void store_read(void)
{
	Uint32  offset; 
	Byte    temp;
	Byte    buf0,buf1,buf2;

	store_ptr=0;
	do
	{	
		offset = ((Uint32)store_ptr * 64) + (Uint32)STORE_BEGIN_PAGE * 0x200;

		Nf_CS_ON();
		nf_read_init(offset);
		temp = Nf_rd_byte();
		if (temp != STORE_SIGN)
		{
			if (temp != 0xff)
			{
				Nf_CS_ON();
				nf_block_erase((Uint32)STORE_BEGIN_PAGE);
				address = STORE_BEGIN_PAGE;
				nf_mark_bad_block();
				Nf_CS_OFF();			
			}
			Nf_CS_OFF();return;
		}
		buf0 = Nf_rd_byte();
		buf1 = Nf_rd_byte();
		buf2 = Nf_rd_byte();
		PlayVolume = Nf_rd_byte();
		PlayStatusSign = Nf_rd_byte();
		sound_mode = Nf_rd_byte();
		light_mode = Nf_rd_byte();
		
		if (Nf_rd_byte() == 0X1E)
		{
			Nf_WP = 0;
		}
		else
		{
			Nf_WP = 1;
		}

		if (Nf_rd_byte() == 0XE1)
		{
			EnglishPage=10;
		}
		else
		{
			EnglishPage = 0;
		}

		if(Nf_rd_byte() == 0X1E)
		{
			NormalPlay = 0;
		}
		else
		{
			NormalPlay = 1;
		}

		if (buf0 > 0x1F)
		{
			buf0 = 0X10;
		}
		if (buf1 > 0X1F)
		{
			buf1 = 0X10;
		}
		if (buf2 > 0X1F)
		{
			buf2 = 0X10;
		}
		if (PlayVolume > 45)
		{
			PlayVolume = 15;
		}

		MP3BAS = buf0;
		MP3MED = buf1;
		MP3TRE = buf2;

		if (light_mode & 0x80)
		{
			light_bit = 1;
		}
		else
		{
			light_bit = 0;
		}

		store_ptr++;
		Nf_CS_OFF();	

	}while (store_ptr != 0);
}
//---------------------------------------------------------------
void store_write(void)
{
Uint32  offset;
Uint16  addr0;

	if (store_ptr == 0)
	{
		Nf_CS_ON();
		nf_block_erase((Uint32)STORE_BEGIN_PAGE);
		address = STORE_BEGIN_PAGE;
		nf_mark_bad_block();
		Nf_CS_OFF();
	}

	addr0 = (Uint16)store_ptr * 64;
	offset = (addr0 / 0x200) + STORE_BEGIN_PAGE;

	Nf_CS_ON();
	Nf_wait_busy();
	if ((addr0&0x100)==0)
	{
		Nf_send_command (NF_READ_A_AREA_CMD);
	}
	else
	{
		Nf_send_command (NF_READ_B_AREA_CMD);
	}
	Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);
	Nf_send_address((Byte)addr0);
	Nf_send_address(((Byte*)&offset)[3]);
	Nf_send_address(((Byte*)&offset)[2]);
	if (nf_64)
	{
		Nf_send_address(((Byte*)&offset)[1]);
	}
	
	Nf_wr_byte(STORE_SIGN);

	Nf_wr_byte(MP3BAS);
	Nf_wr_byte(MP3MED);
	Nf_wr_byte(MP3TRE);
	Nf_wr_byte(PlayVolume);
	Nf_wr_byte(PlayStatusSign);
	Nf_wr_byte(sound_mode);
	Nf_wr_byte(light_mode);
	if (!Nf_WP)
	{
		Nf_wr_byte(0x1E);
	}
	else
	{
		Nf_wr_byte(0xff);
	}
	if (EnglishPage == 10)
	{
		Nf_wr_byte(0xE1);
	}
	else
	{
		Nf_wr_byte(0);
	}
	if (NormalPlay)
	{
		Nf_wr_byte(0);
	}
	else
	{
		Nf_wr_byte(0X1E);
	}

   Nf_send_command(NF_PAGE_PROGRAM_CMD);
	Nf_CS_OFF();
}

//-------------------------
void	nf_write_sector_open(Uint16 addr)
{
	Nf_wait_busy();
	Nf_send_command(NF_READ_A_AREA_CMD);
	Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);
	Nf_send_address(0x00);
	Nf_send_address (((Byte *)&addr)[1]);				// 2nd address cycle         
	Nf_send_address (((Byte *)&addr)[0]);				// 3rd address cycle         
	if (nf_64)
	{
		Nf_send_address(0);									// 4th address cycle         
	}
	Nf_wait_busy();
}
//-------------------------
void 	erase_free_block(void)
{
Byte i;
	Nf_send_command (NF_READ_A_AREA_CMD);
	// Erase unused block                         
	for (i = 0; i < index_block_erased; i++)
	{
		address = (Uint32)(block_to_be_erased[i]) << 5;
		nf_block_erase(address);	
	}
}
//-------------------------

⌨️ 快捷键说明

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