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

📄 nf.c

📁 nand型flash的读、写、擦除程序
💻 C
📖 第 1 页 / 共 4 页
字号:
          				Nf_write_open_A_area(gl_address, 0x00); /* Write first part of LUT */
          				nf_download_buffer();                   /* Write buffer */
          				Nf_send_command(NF_PAGE_PROGRAM_CMD);   /* Launch programmation */
        			}
        			else
        			{
          				Nf_write_open_B_area(gl_address, 0x00); /* Write second part of LUT */ 
          				nf_download_buffer();                   /* Write buffer */
          				Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF);/* Write redundant data */
          				Nf_wr_byte(0xFF); Nf_wr_byte(0xFF);
          				Nf_wr_byte(0xFF);
         				Nf_wr_byte(0xE8); Nf_wr_byte(0xFF);                   /* Logical block value */
          				Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* ECC area 2 */
          				Nf_wr_byte(0xE8); Nf_wr_byte(0xFF);                   /* Logical block value */
          				Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* ECC area 1 */
          				Nf_send_command(NF_PAGE_PROGRAM_CMD);
          				gl_address++;
        			}
        			half_page = ~half_page;
        			start    += 0x80; /* process next 128 logical block */
        			end      += 0x80;
      			}
      			while (start < NF_BLOCK_PER_ZONE);
    		}
  	}
	#undef half_page
	#undef temp_address
	#undef i
	#undef start
	#undef end 
	#undef free_bloc_pos
  	return OK;
}


/*F**************************************************************************
* NAME: nf_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   OK : init complete
*   KO : - NF not supported or not recognise
*----------------------------------------------------------------------------
* PURPOSE:
*   NF initialisation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_init (void)
{
	cs1n_as_iocs();
  	Nf_CS_ON();
  	Nf_send_command(NF_RESET_CMD);
  	Nf_wait_busy();
  #if (NF_CAPACITY_AUTO_DETECT == TRUE)               			/* Auto Detect the type of nand-flash  */
    	Nf_send_command(NF_READ_ID_CMD);
    	Nf_send_address(0x00);
    #pragma option -w-eff
    	Nf_rd_byte();                                     		/* Maker  code */
    #pragma option -w.
    	switch (Nf_rd_byte())                             		/* Device code */
    	{
      /***************************************************************************/
      		case 0x73 :                                     	/*------- 16 Mbyte --------*/
        		nf_device_type = NF_SIZE_16MB;
        		nf_zone_max = 1;                              	/* 1 zone : 1024 blocks    */
        		nf_mem_size =  NF_SECTOR_SIZE_16MB - MEM_RESERVED_SIZE;
        		nf_4_cycle_address = 0;                       	/* 3 address cycles        */
      			break;
      /***************************************************************************/
      		case 0x75 :                                     	/*------- 32 Mbyte --------*/
        		nf_device_type = NF_SIZE_32MB;
        		nf_zone_max = 2;                              	/* 2 zones : 2048 blocks   */
        		nf_mem_size =  NF_SECTOR_SIZE_32MB - MEM_RESERVED_SIZE;
        		nf_4_cycle_address = 0;                       	/* 3 address cycles        */
      			break;
      /***************************************************************************/
      		case 0x76 :                                     	/*------- 64 Mbyte --------*/
        		nf_device_type = NF_SIZE_64MB;
        		nf_zone_max = 4;                              	/* 4 zones : 4096 blocks   */
        		nf_mem_size =  NF_SECTOR_SIZE_64MB - MEM_RESERVED_SIZE;
        		nf_4_cycle_address = 1;                       	/* 4 address cycles        */
        		break;
      /***************************************************************************/
      		case 0x79 :                                     	/*------ 128 Mbyte --------*/
        		nf_device_type = NF_SIZE_128MB;
        		nf_zone_max = 8;                              	/* 8 zones : 8192 blocks   */
        		nf_mem_size =  NF_SECTOR_SIZE_128MB - MEM_RESERVED_SIZE;
        		nf_4_cycle_address = 1;                       	/* 4 address cycles        */
        		break;
      		default:	
        		return KO;
    	}
  #else
    	nf_mem_size = NF_DISK_SIZE - MEM_RESERVED_SIZE;                     
  #endif

  	nf_reserved_space_start = nf_mem_size + 1;
  	return OK;
}


/*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
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
bit nf_read_open (Uint32 pos)
{
	Uint16 physical_block;

  	Nf_CS_ON();

  	if (nf_lut_modified)
  	{
    		nf_reassign_block();
    		nf_lut_modified = FALSE;
  	}

  	gl_ptr_mem = pos;
  	gl_cpt_page = 0;

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

  	/* Determinate zone */
  	nf_zone = nf_logical_block / 1000;

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

  	/* Calculate the address where are the physical block value */
  	gl_address  = ((Uint32)(nf_lut_block[nf_zone])<<5) + ((Uint32)(nf_logical_block) >> 8);
  	gl_address += ((Uint32)(nf_lut_index[nf_zone])<<2);
  	/* Open the look-up table */
  	Nf_wait_busy();
  	if (nf_logical_block & 0x80)
  	{
    		Nf_send_command(NF_READ_B_AREA_CMD);            /* 2nd half page */
    		Nf_send_address((nf_logical_block << 1) - 256);
  	}
  	else
  	{
    		Nf_send_command(NF_READ_A_AREA_CMD);            /* first half page */
    		Nf_send_address(nf_logical_block << 1);
  	}
  	Nf_send_address (((Byte*)&gl_address)[0]);   /* 2nd address cycle         */
  	Nf_send_address (((Byte*)&gl_address)[1]);   /* 3rd address cycle         */
  	if (NF_4_CYCLE_ADDRESS_BIT)                  /* Size of card >= 64Mbytes ?*/
    		Nf_send_address (((Byte*)&gl_address)[2]); /* 4th address cycle         */
  
  	Nf_wait_busy();

  	/* Read the physical block number */
  	((Byte*)&physical_block)[1] = Nf_rd_byte();
  	((Byte*)&physical_block)[0] = Nf_rd_byte();
  	/* Calculate the physical sector address */
  	nf_current_physical_sector_addr = ((Uint32)(physical_block) << 5) + (gl_ptr_mem & NF_BLOCK_MASK); 
  	Nf_CS_OFF();
  	return OK;
}


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


/*F*************************************************************************
* NAME: nf_read_byte
*---------------------------------------------------------------------------
* PARAMS:
*
* return:
*   Data read from memory
*---------------------------------------------------------------------------
* PURPOSE:
*   Low level memory read function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: 
*----------------------------------------------------------------------------
* REQUIREMENTS: 
****************************************************************************/
Byte nf_read_byte (void)
{
	Byte b;
  	if (!gl_cpt_page)
  	{
    		Nf_CS_ON();
    		Nf_wait_busy();
    		Nf_read_open_A_area(nf_current_physical_sector_addr, 0x00);
    		b = Nf_rd_byte();
  	}
  	else
  	{
    		b = Nf_rd_byte();
  	}
  
  	gl_cpt_page++;
  
  	/* Detection of the end of data page */
  	if (((Byte*)&gl_cpt_page)[1] == NF_DATA_SIZE_H)                 
  	{
  #pragma option -w-eff
    	/* read spare data bytes */
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    		Nf_rd_byte();
    #pragma option -w.
    		gl_ptr_mem++;                           /* new page */
    		gl_cpt_page=0;                          /* start at column 0 */
		Nf_CS_OFF();
		if (!(((Byte*)&gl_ptr_mem)[0] & NF_BLOCK_MASK))               /* New block ? */
    		{
      			nf_read_open(gl_ptr_mem);
    		}
    		else
    		{
      			nf_current_physical_sector_addr++;
    		}
  	}
  	return b;
}


/*F**************************************************************************
* NAME: nf_read_sector
*----------------------------------------------------------------------------
* PARAMS:
*   nb_sector:  number of contiguous sector to read 
*   global:     gl_ptr_mem
*
* return: OK read done
*         KO read failure
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function is an optimized function that writes nb-sector * 512 bytes
*   from NF card to USB controller 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   nb_sector always >= 1, can not be zero
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_read_sector(Uint16 nb_sector)
{
	Byte i;
	bit   begin_ping_pong;

  	begin_ping_pong = TRUE;

  	do
  	{
    		Nf_CS_ON();
    		Nf_wait_busy();
    		Nf_read_open_A_area(nf_current_physical_sector_addr, 0x00);
  
    		for (i = 8; i != 0; i--)
    		{
    		   #if 0
      			Usb_write_byte(Nf_rd_byte());         /* read 64 bytes from card */
      			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_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_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());

⌨️ 快捷键说明

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