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

📄 nf.c

📁 nand型flash的读、写、擦除程序
💻 C
📖 第 1 页 / 共 4 页
字号:
    		}
   		else
    		{
      		/* if the block in the buffer is already assign, then invert it with a spare block */
      			if (!(nf_buf[nf_gl_buf_idx].w & 0x8000))                 
      			{                                                 
        			if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_lut_block[nf_zone])
        			{
          				gl_buf_free_idx++;
          				if (gl_buf_free_idx >= nf_spare_block)
          				{
            					gl_buf_free_idx = 0;
          				}      
        			}        
        			/* invert the block and assign the next block to be deleted */
        			nf_block_to_be_deleted       = nf_buf[nf_gl_buf_idx].w;
        			nf_buf[nf_gl_buf_idx].w      = nf_buf_free[gl_buf_free_idx] & 0x7FFF;
        			nf_buf_free[gl_buf_free_idx] = nf_block_to_be_deleted | 0x8000;
        			/* block have to be deleted */
        			nf_block_used = TRUE;
        			/* increase the spare buffer index */
        			if ((gl_buf_free_idx++) >= nf_spare_block)
        			{
          				gl_buf_free_idx = 0;
        			}

      			}
      			else /* The block is not assigned. Nothing to do */
      			{
        			nf_buf[nf_gl_buf_idx].w &= 0x7FFF;
        			nf_block_used = FALSE;
      			}
      			/* update the max index buffer */
      			if (nf_gl_buf_idx > nf_gl_buf_idx_max)
        			nf_gl_buf_idx_max = nf_gl_buf_idx;

      			/* Update current physical sector */
      			nf_current_physical_sector_addr = (Uint32)(nf_buf[nf_gl_buf_idx].w) << 5;
      			/* increase the logical block */
      			nf_logical_block++;
      			/* calculate the redundant block address */
      			nf_calc_logical_block();
    		}
  	}
  	
  	Nf_CS_ON();
  	if (!gl_cpt_page)
  	{
    		Nf_wait_busy();
    		Nf_write_open_A_area(nf_current_physical_sector_addr, 0x00);
  	}
  	Nf_wr_byte(b);
  	/* Increase internal page counter */
  	gl_cpt_page++;
  	if (((Byte*)&gl_cpt_page)[1] == NF_DATA_SIZE_H) /* check if end of data page */
  	{
    		nf_update_spare_data();
    		Nf_send_command(NF_PAGE_PROGRAM_CMD);
    		((Byte*)&gl_cpt_page)[1] = 0;
    		gl_ptr_mem++;
    		if (!(((Byte*)&gl_ptr_mem)[0] & NF_BLOCK_MASK)) 
    		{
      			nf_busy = TRUE;
    		}
    		else
    		{
      			((Byte*)&nf_current_physical_sector_addr)[0]++;
    		}
  	}
  	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;

  	do
  	{   
    		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) << 5);
      			}
      			/* 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);
      			}
      			else
      			{
        			/* if the block in the buffer is already assign, then invert it with a spare block */
        			if (!(nf_buf[nf_gl_buf_idx].w & 0x8000))                 
        			{                                                 
          				if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_lut_block[nf_zone])
          				{
            					gl_buf_free_idx++;
            					if (gl_buf_free_idx >= nf_spare_block)
            					{
              						gl_buf_free_idx = 0;
            					}      
          				}        
          				/* invert the block and assign the next block to be deleted */
          				nf_block_to_be_deleted       = nf_buf[nf_gl_buf_idx].w;
          				nf_buf[nf_gl_buf_idx].w      = nf_buf_free[gl_buf_free_idx] & 0x7FFF;
          				nf_buf_free[gl_buf_free_idx] = nf_block_to_be_deleted | 0x8000;
          				/* block have to be deleted */
          				nf_block_used = TRUE;
          				/* increase the spare buffer index */
          				if ((gl_buf_free_idx++) >= nf_spare_block)
          				{
            					gl_buf_free_idx = 0;
          				}
        			}
        			else /* The block is not assigned. Nothing to do */
        			{
          				nf_buf[nf_gl_buf_idx].w &= 0x7FFF;
          				nf_block_used = FALSE;
        			}
        			/* update the max index buffer */
        			if (nf_gl_buf_idx > nf_gl_buf_idx_max)
          				nf_gl_buf_idx_max = nf_gl_buf_idx;
  
        			/* Update current physical sector */
        			nf_current_physical_sector_addr = (Uint32)(nf_buf[nf_gl_buf_idx].w) << 5;
        			/* increase the logical block */
        			nf_logical_block++;
        			/* calculate the redundant block address */
        			nf_calc_logical_block();
      			}
    		}

    		Nf_CS_ON();
  
    		Nf_wait_busy();
    		Nf_write_open_A_area(nf_current_physical_sector_addr, 0x00);

    		for (j = 8; j != 0; j--)
    		{
    		   #if 0
      			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 */
      		    #endif 
    		}
  
    		/* Update spare data */
    		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(((Byte*)&nf_redundant_logical_block_value)[1]);
    		Nf_wr_byte(((Byte*)&nf_redundant_logical_block_value)[0]);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(0xFF);
    		Nf_wr_byte(((Byte*)&nf_redundant_logical_block_value)[1]);
    		Nf_wr_byte(((Byte*)&nf_redundant_logical_block_value)[0]);
  
    		Nf_send_command (NF_PAGE_PROGRAM_CMD);
    		/* increase gl_ptr_mem : next page */
    		gl_ptr_mem++;              
    		/* set flag busy */
    		if (!(((Byte*)&gl_ptr_mem)[0] & NF_BLOCK_MASK)) 
    		{
      			nf_busy = TRUE;
    		}
    		else
    		{
      			((Byte*)&nf_current_physical_sector_addr)[0]++;
    		}
    		nb_sector--;
  	}
  	while (nb_sector != 0);
  	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:
*****************************************************************************/
#define CYL_16  (Uint16)((Uint16)(500) - (MEM_RESERVED_SIZE / (4 * 16)))
#define CYL_32  (Uint16)((Uint16)(500) - (MEM_RESERVED_SIZE / (8 * 16)))
#define CYL_64  (Uint16)((Uint16)(500) - (MEM_RESERVED_SIZE / (8 * 32)))
#define CYL_128 (Uint16)((Uint16)(500) - (MEM_RESERVED_SIZE / (16 * 32)))

	const s_format  nf_tab_format[]=
  	{
   		/* nb_cylinder, nb_head,  nb_sector, nb_hidden, nb_sector_per_cluster */
    		{ CYL_16, (Byte)4,  (Byte)16,  (Byte)57,  (Byte)32 }, /* 16MB */
    		{ CYL_32, (Byte)8,  (Byte)16,  (Byte)51,  (Byte)32 }, /* 32MB */
    		{ CYL_64, (Byte)8,  (Byte)32,  (Byte)39,  (Byte)32 }, /* 64MB */
    		{ CYL_128, (Byte)16, (Byte)32,  (Byte)63,  (Byte)32 }, /* 128MB */
  	};

	const  s_format  nf_tab_format_reserved[]=
  	{
   		/* nb_cylinder, nb_head,  nb_sector, nb_hidden, nb_sector_per_cluster */
    		{ (Uint16)500, (Byte)4,  (Byte)16,  (Byte)57,  (Byte)32 }, /* 16MB */
    		{ (Uint16)500, (Byte)8,  (Byte)16,  (Byte)51,  (Byte)32 }, /* 32MB */
    		{ (Uint16)500, (Byte)8,  (Byte)32,  (Byte)39,  (Byte)32 }, /* 64MB */
    		{ (Uint16)500, (Byte)16, (Byte)32,  (Byte)63,  (Byte)32 }, /* 128MB */
  	};
  	
s_format * nf_format (void)
{
  	/* 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 (s_format *)(&nf_tab_format[NF_TYPE]);
  	}
  	else
  	{
    		if (nf_reserved_space == FALSE)
    		{
      			nf_reserved_space = TRUE;
      			nf_mem_size += MEM_RESERVED_SIZE;
    		}
    		return (s_format *)(&nf_tab_format_reserved[NF_TYPE]);
  	}
}

⌨️ 快捷键说明

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