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

📄 nf_drv.c

📁 nand型flash的读、写、擦除程序
💻 C
📖 第 1 页 / 共 2 页
字号:
* NAME: nf_reassign_block
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Reassign the block value in LUT (copy of LUT)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
void nf_reassign_block (void)
{
	Byte j, k;
	Uint16 start;
	Uint16 block_start;                     
	xdata Uint32 address2;                            /* LUT Source address */

  	if (nf_lut_index[nf_zone] == 7)
  	{
    		if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_look_up_table_block)
    		{
      			gl_buf_free_idx++;
      			if (gl_buf_free_idx >= nf_spare_block)
      			{
        			gl_buf_free_idx = 0;
      		}      
    	}

    	/* Source address */
    	address2 = ((Uint32)(nf_look_up_table_block)<<5) + 28;

    	nf_look_up_table_block =  nf_buf_free[gl_buf_free_idx] & 0x7FFF;
    
    	gl_buf_free_idx++;
    	if (gl_buf_free_idx >= nf_spare_block)
    	{
      		gl_buf_free_idx = 0;
    	}      

    	/* Target address : recopy the last 2Kb */
    	gl_address = ((Uint32)(nf_look_up_table_block) << 5);
	
    	nf_lut_index[nf_zone] = 0;

  	}
  	else
  	{
    		/* Source address */
    		address2 = ((Uint32)(nf_look_up_table_block)<<5) + ((Uint32)(nf_lut_index[nf_zone])<<2);

	    	nf_lut_index[nf_zone]++;

    		/* Target address */
    		gl_address = address2 + 4;
  	}

  	j = 0;
  	k = 0;
  	start = 0;
  	block_start = nf_block_min;

  	do
  	{
    		Nf_wait_busy();
    		Nf_read_open_A_area(address2, 0x00);      /* Open look-up table in read mode */
    		nf_upload_buffer();

    		while ((k <= nf_gl_buf_idx_max) && (start <= block_start) && ((start + 128) > block_start))
    		{
      			gl_buffer[(2 * (block_start & 0x7F))] = nf_buf[k].b[1];
      			gl_buffer[(2 * (block_start & 0x7F))+1] = nf_buf[k].b[0];
      			k++;
      			block_start++;
    		}

    		Nf_write_open_A_area(gl_address, 0x00);
    		nf_download_buffer();                     /* Write 256 bytes from the buffer */

    		Nf_send_command(NF_PAGE_PROGRAM_CMD);     /* Valid the page programmation */

    		start += 128;

    		Nf_wait_busy();                           /* Wait for R/B signal */
    		Nf_read_open_B_area(address2, 0x00);      /* Open look-up table in read mode */
    		nf_upload_buffer();

    		while ((k <= nf_gl_buf_idx_max) && (start <= block_start) && ((start + 128) > block_start))
    		{
      			gl_buffer[(2 * (block_start & 0x7F))] = nf_buf[k].b[1];
      			gl_buffer[(2 * (block_start & 0x7F))+1] = nf_buf[k].b[0];
      			k++;
      			block_start++;
    		}   
 
    		if (start >= 896)                       /* This part update the free physical block table */
    		{
      			k = 208;
      			for (j = 0; j <= 23; j++)
      			{           
        			gl_buffer[k] = nf_buf_free[j]>>8;
        			gl_buffer[k+1] = nf_buf_free[j];  
        			k += 2;
      			}
    		}

    		Nf_write_open_B_area(gl_address, 0x00);
    		nf_download_buffer();                   /* Write 256 bytes from the buffer */

    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xE8);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xE8);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);

    		/* Valid the page programmation */
    		Nf_send_command(NF_PAGE_PROGRAM_CMD);

    		start += 128;
    		((Byte*)&address2)[0]++;
    		((Byte*)&gl_address)[0]++;
  	}
  	while (start < 1024);

  	nf_gl_buf_idx_max = 0;

  	if (nf_lut_index[nf_zone] == 0)
  	{
    		address2 = (Uint32)(nf_lut_block[nf_zone])<<5;
    		nf_lut_block[nf_zone] = nf_look_up_table_block;           /* Update address of look up table */
    		Nf_wait_busy();
    		Nf_send_command (NF_READ_A_AREA_CMD);
    		Nf_send_command (NF_BLOCK_ERASE_CMD);                     /* Auto Block Erase Setup */
    		Nf_send_address ( ((Byte*)&address2)[0] );                /* 2nd address cycle*/
    		Nf_send_address ( ((Byte*)&address2)[1] );                /* 3rd address cycle*/
    		if (NF_4_CYCLE_ADDRESS_BIT)                               /* Size of card >= 64Mbytes ?*/
      			Nf_send_address ( ((Byte*)&address2)[2] );              /* 4th address cycle*/
    		Nf_send_command(NF_BLOCK_ERASE_CONFIRM_CMD);              /* Erase command */
  	}
}


/*F**************************************************************************
* NAME: nf_mark_bad_block
*----------------------------------------------------------------------------
* PARAMS:   
*           
* return:   
*----------------------------------------------------------------------------
* PURPOSE:
*   Write 0x00 on block status byte (Byte 5 of spare data)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
void nf_mark_bad_block (void)
{
  	Nf_wait_busy();
  	Nf_write_open_C_area(gl_address, 0x03);

  	Nf_wr_byte(0x00);
  	Nf_wr_byte(0x00);
  	Nf_wr_byte(0x00);

  	Nf_send_command (NF_PAGE_PROGRAM_CMD);  /* Send program command */
}


/*F**************************************************************************
* NAME: nf_block_erase
*----------------------------------------------------------------------------
* PARAMS:   
*
* return:
*   OK : erase done
*   KO : erase not done
*----------------------------------------------------------------------------
* PURPOSE: Erase a block on Nand Flash Media
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
bit nf_block_erase (Uint32 pos)
{
  	Nf_wait_busy();
  	Nf_send_command (NF_READ_A_AREA_CMD);
  	Nf_send_command (NF_BLOCK_ERASE_CMD);       /* Auto Block Erase Setup     */
  	Nf_send_address ( ((Byte*)&pos)[0] );       /* 2nd address cycle          */
  	Nf_send_address ( ((Byte*)&pos)[1] );       /* 3rd address cycle          */
  	if (NF_4_CYCLE_ADDRESS_BIT)                 /* Size of card >= 64Mbytes ? */
    		Nf_send_address ( ((Byte*)&pos)[2] );     /* 4th address cycle          */

  	Nf_send_command(NF_BLOCK_ERASE_CONFIRM_CMD);/* Erase command              */
  	return OK;
}


/*F**************************************************************************
* NAME: nf_erase_all_block
*----------------------------------------------------------------------------
* PARAMS:   
*           
* return:   
*   OK : erase done
*   KO : erase not done
*----------------------------------------------------------------------------
* PURPOSE:
*   This function erase all blocks on a NF card and write CIS information
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
bit nf_erase_all_block (void)
{
	Byte i;
	Uint16 j;
	Uint16 block;
	Uint16 block_reserved_space;
	bit erase;
	
	Nf_CS_ON();
  	/* Erase all block */
  	block_reserved_space = (Uint16) (((nf_reserved_space_start >> 5) % 1000));
  	gl_address = 0;
  	for (nf_zone = 0; nf_zone < NF_ZONE_MAX_CPT; nf_zone++)
  	{
    		for (j = 1024; j != 0; j--)
    		{
      			Nf_wait_busy();
      			Nf_read_open_C_area(gl_address, 0x05); /* Read block status byte */
      			erase = TRUE;
      			if (Nf_rd_byte() != 0xFF) /* if bad block */
      			{
        			erase = FALSE;          /* don't erase block */
      			}
      			else
      			{
        			((Byte*)&block)[1] = Nf_rd_byte(); /* Read logical block address 高位*/
        			((Byte*)&block)[0] = Nf_rd_byte(); /* 低位 */
        			if ( (MEM_RESERVED_SIZE != 0) && (nf_zone == (NF_ZONE_MAX_CPT - 1)))
        			{
          				if ((((Byte*)&block)[1] & 0xF8) == 0x10) /* 高位 */
          				{
            					block = (block & 0x0FFF) >> 1;
            					if (block >= block_reserved_space)      /* If it is a reserved block */
            					{
              						erase = FALSE;                        /* Don't erase block */
            					}
          				}
        			}
      			}
      			if (NF_FULL_CHIP_ERASE == FALSE)
      			{
        			if (((Byte*)&block)[1] != 0xE8) /* 高位 */
        			{
          				erase = FALSE;
        			}
      			}
      			else
      			{
        			if (((Byte*)&block)[1] == 0x00) /* 高位 */
        			{
          				erase = FALSE;
        			}
      			}
      			erase = 1;
      			if (erase)
      			{
        			nf_block_erase(gl_address);
        			if (nf_check_status() == KO)
        			{ 
          				nf_mark_bad_block(); /* Failure on erase operation */     
        			}
        			else
        			{ /* Fill redundant area with 0x00 */
          				Nf_write_open_C_area(gl_address, 0x00);
          				for (i = 16; i != 0; i--)
            					Nf_wr_byte(0x00);
          				Nf_send_command(NF_PAGE_PROGRAM_CMD);        /* Valid the page programmation */
          				if ( nf_check_status() == KO)
          				{ 
            					nf_mark_bad_block();                        /* Failure on program operation */
          				}
          				else
          				{
            					Nf_read_open_C_area(gl_address, 0x00);        /* Read 16 bytes */
            					i = 16;
            					while ((i != 0) && (Nf_rd_byte() == 0x00)) i--;
            					if (i)
            					{
              						nf_mark_bad_block();
            					}
            					else
            					{
              						nf_block_erase(gl_address);          /* Finally, erase the block */      
              						if ( nf_check_status() == KO)
              						{ /* Failure on erase operation */
                						nf_mark_bad_block();      
              						}
            					}
          				}
        			}
      			}
      			gl_address += 32;
    		}
  	}
  	return (nf_read_spare_byte());
}




⌨️ 快捷键说明

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