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

📄 nf.c

📁 这是用C语言写的USB源程序,相信对大家有所帮助
💻 C
📖 第 1 页 / 共 3 页
字号:
			nf_64 = 0;
			break;
		case 0x75 :                         /* 32 Mbyte  */
			nf_device_type = NF_SIZE_32MB;
			nf_zone_max = 2;
			nb_reserved_sector=3;
			nf_disk_size =  NF_SECTOR_SIZE_32MB;
			nf_64 = 0;
			break;
		case 0x76 :                         /* 64 Mbyte  */
			nf_device_type = NF_SIZE_64MB;
			nf_zone_max = 4;
			nb_reserved_sector=1;
			nf_disk_size =  NF_SECTOR_SIZE_64MB;
			nf_64 = 1;        /* 4 address cycles */
			break;
		case 0x79 :                         /* 128 Mbyte */
			nf_device_type = NF_SIZE_128MB;
			nf_zone_max = 8;
			nb_reserved_sector=1;
			nf_disk_size =  NF_SECTOR_SIZE_128MB;
			nf_64 = 1;        /* 4 address cycles */
			break;
		default:break;
	}

  read_spare_byte();
  index_block_erased = 0;
  index_reassign = 0;
  Nf_CS_OFF();
}

/*F**************************************************************************
* NAME:     nf_read_open

*----------------------------------------------------------------------------
* PARAMS:   pos:   address of the logic sector to read (size 512 bytes)
*
* RETURN:   Update memory for reading
*
*----------------------------------------------------------------------------
* PURPOSE:  Low level memory read update

*****************************************************************************/
bit nf_read_open (Uint32 pos)
{
  Uint16	logical_block;
  Uint16	physical_block;
  Byte		nf_zone;

  Nf_CS_ON();
  gl_ptr_mem = pos;
  gl_cpt_page = 0;

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

  /* Determinate zone */
  nf_zone = (Byte)(logical_block / 992);

  /* Each zone have 1000 data blocks */
  logical_block %=(Uint16)992;// logical_block - (992 * nf_zone);

  /* Calculate the address where the physical block value */
  address = address_look_up_table + ((Uint32)(logical_block) >> 8) 
                  + ((Uint32)(nf_zone) << 2);

  /* 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_64)                                        /* Size of card >= 64Mbytes ?*/
    Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle         */


  Nf_wait_busy();

  /* Read the physical block number */
  ((Byte*)&physical_block)[0] = Nf_rd_byte();
  ((Byte*)&physical_block)[1] = Nf_rd_byte();

  /* Calculate the physical sector address */
  current_physical_sector_addr = ((Uint32)(physical_block) << 5) + (gl_ptr_mem & 0x1F); 

  nf_send_ra_cmd();
/*
  Nf_send_command (NF_READ_A_AREA_CMD);
  Nf_send_address ( 0x00 );                                       
  Nf_send_address ( ((Byte*)&current_physical_sector_addr)[3] );  
  Nf_send_address ( ((Byte*)&current_physical_sector_addr)[2] );  
  if (nf_64)                                                      
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[1] );
*/
  Nf_wait_busy();
  return OK;
}

/*F**************************************************************************
* NAME:     nf_read_close
*----------------------------------------------------------------------------
* PARAMS:
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE:  Low level memory read close
*
*****************************************************************************
* NOTE:
*
*****************************************************************************/
void nf_read_close (void)
{
  Nf_CS_OFF();
}


/*F*************************************************************************
* NAME:     nf_read_byte
*---------------------------------------------------------------------------
* AUTHOR:
*---------------------------------------------------------------------------
* PARAMS:
*
* RETURN:   Data read from memory
*
*---------------------------------------------------------------------------
* PURPOSE:  Low level memory read function
*
****************************************************************************
* NOTE:    
*
****************************************************************************/
Byte nf_read_byte (void)
{
  Byte b;
  Byte i;

  if (gl_cpt_page == 0x00)
    Nf_wait_busy();

  b = Nf_rd_byte();
  gl_cpt_page++;
  
  /* Detection of the end of data page */
  if (gl_cpt_page == NF_DATA_SIZE)                 
  {
    /* read spare data bytes */
	for(i=0;i<16;i++)ACC=Nf_rd_byte();

    gl_ptr_mem++;                           /* new page          */

    gl_cpt_page=0;                          /* start at column 0 */

    if ( !(gl_ptr_mem & 0x1F) )   /* New block ? */
    {
      nf_read_open(gl_ptr_mem);
    }
    else
    {
      current_physical_sector_addr++;
      Nf_wait_busy();

	  nf_send_ra_cmd();

    }
  }
  return b;
}




/*F**************************************************************************
* NAME: nf_read_sector
*----------------------------------------------------------------------------
* PARAMS:
*   global: gl_ptr_mem
*
* return: OK read done
*         KO read failure
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function is an optimized function that writes 512 bytes from NF
*   card to USB controller 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_read_sector(void)
{
  Byte i;
  Byte j;

  Nf_wait_busy();

  for (i = 8; i != 0; i--)
  {
	
	for(j=0;j<8;j++)
	{
		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_set_TXRDY();                        /* start usb transfer */
    while (!Usb_tx_complete());             /* wait end of transfer */
    Usb_clear_TXCMPL();                     /* ack transfer */
  }

  /* read spare data bytes */
  for(i=0;i<4;i++)
  {
  	ACC=Nf_rd_byte();
  	ACC=Nf_rd_byte();
	ACC=Nf_rd_byte();
  	ACC=Nf_rd_byte();
  }

  gl_ptr_mem++;                       /* new page          */
  
  if ( !(gl_ptr_mem & 0x1F) )   /* New block ? */
  {
    nf_read_open(gl_ptr_mem);
  }
  else
  {
    current_physical_sector_addr++;
    Nf_wait_busy();
	nf_send_ra_cmd();
  }
  return OK;  
}

/*F**************************************************************************
* NAME:     nf_write_open
*----------------------------------------------------------------------------
* AUTHOR:
*----------------------------------------------------------------------------
* PARAMS:   pos:   address of the the next write data
*

*----------------------------------------------------------------------------
* PURPOSE:  Low level memory write update

*****************************************************************************/
extern xdata  Byte mode_state;

bit nf_write_open (Uint32 pos)
{
  Uint16 logical_block;
  Uint16 physical_block;
  Uint32 look_up_table;
  Byte	nf_zone;

	if((mode_state == MODE_DOWNLOAD) && (!Nf_WP))
	{
		return KO;
	}

  Nf_CS_ON();
  gl_ptr_mem = pos;
  gl_cpt_page = 0;

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

  /* Determinate zone */
  nf_zone = (Byte)(logical_block / 992);

  /* Each zone have 1000 data blocks */
  logical_block =logical_block - ((Uint16)nf_zone * 992);

  /* Calculate the address where the physical block value */
  look_up_table = address_look_up_table + ((Uint32)(logical_block) >> 8) + ((Uint32)(nf_zone) << 2);

  nf_calc_logical_block(logical_block);

  Nf_wait_busy();
  /* Open the look-up table */
  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*)&look_up_table)[3] );   /* 2nd address cycle         */
  Nf_send_address ( ((Byte*)&look_up_table)[2] );   /* 3rd address cycle         */
  if (nf_64)                                        /* Size of card >= 64Mbytes ?*/
    Nf_send_address ( ((Byte*)&look_up_table)[1] ); /* 4th address cycle         */

  Nf_wait_busy();

  /* Read the physical block number                          */
  ((Byte*)&physical_block)[0] = Nf_rd_byte();
  ((Byte*)&physical_block)[1] = Nf_rd_byte();

	if(record_open_bit)
	{
		record_open_bit=0;
		physical_block &= 0x7FFF;
		block_used=FALSE;
	

		current_physical_sector_addr = (Uint32)(physical_block) << 5;

		if((pos&0x1f) == 0)
		{
			nf_block_erase(current_physical_sector_addr);	
			Nf_wait_busy();
		}
		nf_init_spare();
		index_block_erased=0;
	}
	else
	{
		index_free_block[0] += 2;
		if (index_free_block[0] >= nf_spare_block_number[0])
		{
			index_free_block[0] = 0;
		}
  
		index_look_up_table += 2;
		if (index_look_up_table >= nf_spare_block_number[0])
		{
			index_look_up_table = 0;
		}

		block_used = ( !(physical_block & 0x8000) ) ? TRUE : FALSE;
		physical_block &= 0x7FFF;

		if ( block_used )     /* Already used block ?  */
		{
			if (nf_zone != 0)
			{
				index_free_block[nf_zone] += 2;          
				if (index_free_block[nf_zone] >= nf_spare_block_number[nf_zone])
				{
					index_free_block[nf_zone] = 0;
				}
			}
 
			/* Find the new physical block */
			address = address_look_up_table + 3 + ((Uint32)(nf_zone) << 2);
			Nf_send_command(NF_READ_B_AREA_CMD);        
			Nf_send_address( index_free_block[nf_zone] + 192);
			Nf_send_address ( ((Byte*)&address)[3] );   
			Nf_send_address ( ((Byte*)&address)[2] );   
			if (nf_64)                                  
			{
      		Nf_send_address ( ((Byte*)&address)[1] ); 
			}

			/* Save physical block value to be erased at the end of the write operation */
			block_to_be_erased[index_block_erased] = physical_block;
			/* save index */
			index_block_used[index_block_erased] = index_free_block[nf_zone];          

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

			physical_block &= 0x7FFF;

			/* Update the reassign block structure : logical block <-> physical block */
			reassign_block[index_reassign].zone = nf_zone;
			reassign_block[index_reassign].logical_block =  logical_block;
			reassign_block[index_reassign].physical_block = physical_block;
			index_reassign++;
			current_physical_sector_addr = (Uint32)(physical_block) << 5;
			/* Copy first part of the block                          */
			nf_copy_block_head();
			index_block_erased++;
		}
		else                                 /* new logical block  */
		{ 
			/* Update the reassign block structure : logical block <-> physical block */
			reassign_block[index_reassign].zone = nf_zone;
			reassign_block[index_reassign].logical_block =  logical_block;
			reassign_block[index_reassign].physical_block = physical_block;
			index_reassign++;
			current_physical_sector_addr = (Uint32)(physical_block) << 5;
			nf_init_spare();                   
		}
	}

	Nf_wait_busy();
	nf_busy = FALSE;
	Nf_CS_OFF();
	return OK; 
}

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

  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_w_cmd(MODE_CMDA,1);

      /* 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);
    Nf_send_address ( 0x00 );
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[3] );
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[2] );
    if (nf_64)   
      Nf_send_address ( ((Byte*)&current_physical_sector_addr)[1] );

    /* Copy the buffer */
    nf_download_buffer();
    nf_update_spare_data();
    /* Program the device */
    Nf_send_command(NF_PAGE_PROGRAM_CMD);
    gl_cpt_page = 0;
    gl_ptr_mem++;
  }

  /* Copy last part of a block                  */
  if ( (block_used) && ( gl_ptr_mem & 0x1F ) )
    nf_copy_block_tail();
	//	nf_copy_block_ljl();

  Nf_wait_busy();

⌨️ 快捷键说明

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