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

📄 nand_flash.c

📁 通过建立逻辑映射表读写NAND_FLASH,可以延长NAND_FLASH的使用寿命,排除坏块的干扰!其中包括NAND_FLASH读写驱动以及逻辑映射表的建立!根据自己的要求修改读写驱动即可使用
💻 C
📖 第 1 页 / 共 3 页
字号:

/******************************************************************************
                  NAND_BlockErase                                                           `

Function:        NAND_Ret NAND_BlockErase(udword Blocknumber)

Arguments:       Blocknumber: The block number to erase,12bit length.

Return Value: 	 NAND_FLASH_SIZE_OVERFLOW: The address is not within the flash
                 NAND_PASS: The operation is successfully executed
                 NAND_FAIL: The operation is not successfully executed,

Description:     This function issue a Block Erase Command as explained in the
                 datasheet of the 528 byte/264 word page family.

******************************************************************************/
NAND_Ret NAND_BlockErase(udword Blocknumber) {
                
  ubyte  ubStatus;
  udword udAddress; 
    
  udAddress = (Blocknumber & 0xFFF) << 14;
  if ( udAddress >= FLASH_SIZE )
      return NAND_FLASH_SIZE_OVERFLOW;
   
  NAND_Open();
   
  /* Issue Sequential data input command */
  NAND_CommandInput((ubyte)0x60);

  InsertColumnAddress(udAddress);

   /* Issue confirm code command */
  NAND_CommandInput((ubyte)0xD0);

   /* wait for ready*/
  ubStatus=waitForReady();
  //ubStatus = NAND_ReadStatusRegister();

  
  NAND_Close();
  return ubStatus&(0x01);

}
/*********************************  NAND_BlockErase ***************************/




/******************************************************************************
                  NAND_CopyBack(该函数有问题,不能实现预期功能)

Function:       NAND_Ret NAND_CopyBack(udword udSourceAddr, udword udDestinationAddr)

Arguments:      udSourceAddr: The address of the source page to copy.
                udDestinationAddr: The address of the destination page.

Return Value: 	NAND_FLASH_SIZE_OVERFLOW: The address is not within the flash
                NAND_PASS: The operation are successfully executed
                NAND_FAIL: The operation are not successfully executed.
                NAND_WRONG_ADDRESS : The pages are not in the same half of the
                                     flash.

Description:    This function issue a CopyBack Command as explained in the
                datasheet of the 528 byte/264 word page family. 
                The Copy Back Program operation is used to copy the data stored 
                in one page and reprogram it in another page. The operation does 
                not require external memory, so it is faster and more efficient
                than the standard operations because the time consuming phases 
                of reading out the data and then reloading the data to be programmed
                are not required. The operation is particularly useful in garbage
                collection routine .

*******************************************************************************/
NAND_Ret NAND_CopyBack(udword udSourceAddr, udword udDestinationAddr){
   
 ubyte ubStatus;
   
   NAND_Open();

   /* Control if the address is within the flash*/
   if ((udSourceAddr  >= FLASH_SIZE) ||(udDestinationAddr  >= FLASH_SIZE))
      return NAND_FLASH_SIZE_OVERFLOW;
   
   #ifdef NAND512RW3A
      /* Check if A25 is the same for the two addresses */
   if ( (udSourceAddr >> (24 + HALF_PAGE_POINTER)) !=
       (udDestinationAddr >> (24 + HALF_PAGE_POINTER)) )
       return NAND_WRONG_ADDRESS;
   #endif
  
  #ifdef NAND256W3A
    /* Check if A24 is the same for the two addresses */
   if((udSourceAddr&(0x01000000))!=(udDestinationAddr&(0x01000000)))
   	return NAND_WRONG_ADDRESS;
   #endif

   #ifdef NAND256W4A
   /* Check if A23 is the same for the two addresses */
   if((udSourceAddr&(0x800000))!=(udDestinationAddr&(0x800000)))
   	return NAND_WRONG_ADDRESS;
   #endif

   #ifdef NAND128W3A
   /* Check if A23 is the same for the two addresses */
   if((udSourceAddr&(0x800000))!=(udDestinationAddr&(0x800000)))
   	return NAND_WRONG_ADDRESS;
   #endif

   #ifdef NAND128W4A
   /* Check if A22 is the same for the two addresses */
   if((udSourceAddr&(0x400000))!=(udDestinationAddr&(0x400000)))
   	return NAND_WRONG_ADDRESS;
   #endif


   #ifdef NAND01GRW3A
   /* Check if A25 A26 is the same for the two addresses */
   if((udSourceAddr&(0x06000000))!=(udDestinationAddr&(0x06000000)))
   	return NAND_WRONG_ADDRESS;
   #endif
   
   #ifdef NAND512Stacked
   /* Check if A24 A25 is the same for the two addresses */
   if((udSourceAddr&(0x03000000))!=(udDestinationAddr&(0x03000000)))
   	return NAND_WRONG_ADDRESS;
   #endif            
   
   /* Set the address pointer to 1st half page*/                          
   NAND_CommandInput((ubyte)0x00);         
   
   /*insert the source address*/                                                                                  
   InsertAddress(udSourceAddr);
   
   /* Wait for ready */
   ubStatus = waitForReady();

   /* Issue Copy-Back Data Input Command*/
   NAND_CommandInput((ubyte)0x8A);

   /*send the address*/
   InsertAddress(udDestinationAddr);
                                         
   /* Issue the Program Command*/
   NAND_CommandInput((ubyte)0x10);
                                                                               
   /* Wait for ready */
   ubStatus = waitForReady();
     
   //ubStatus = NAND_ReadStatusRegister();
  
   NAND_Close();
   
   /* Return Pass or Fail */
   return (ubStatus&(0x01)); 
}
/*****************************  NAND_CopyBack *********************************/
                                                                   

/******************************************************************************
                  NAND_PageRead

Function:       NAND_Ret NAND_PageRead(udword udAddress, dataWidth *Buffer,
                                              udword udlength)
Arguments:      udAddress: The address of the page to read.
                Buffer: Contains the destination buffer to store the data.
                udlength: number of data to read.

Return Value:   NAND_FLASH_SIZE_OVERFLOW: The address is not within the flash
                NAND_PASS: The operation are successfully executed
                NAND_FAIL: The operation are not successfully execute

Description:    The PageRead operation issue a PageRead Command as explained in
                the datasheet of the 528 byte/264 word page family.
                The only value of the pointer are 00h and 01h. That means the Page
                Read operation starts in any part of the data area of the page 
                except the spare area. So, the SpareRead operations must be call
                to read a page directly in the spare area.
                The Random Read, the serial read and the sequentially row read
                operations are possible.
*******************************************************************************/
NAND_Ret NAND_PageRead(udword udAddress, dataWidth *Buffer, udword udlength) {

   volatile udword udIndex;
   ubyte ubStatus;
   uword uwFarToPage;
   ubyte i;
   
   /* Control if the address is within the flash*/
   if ( udAddress >= FLASH_SIZE )
    return NAND_FLASH_SIZE_OVERFLOW;

   
   NAND_Open();
                             
   /* Set the address pointer*/                          
   if ( (udAddress % PAGE_DATA_SIZE) >= PAGE_DATA_SIZE/2 )/* A8 =1 */
    NAND_CommandInput(HALF_PAGE_POINTER);   /* 2nd half page*/
   else 
    NAND_CommandInput((ubyte)0x00);         /* 1st half page*/
   
   /*send the address*/
   InsertAddress(udAddress);
   
   /* Wait for ready */
   ubStatus=waitForReady();
   
   /* Leave Read Status Register mode */
   NAND_CommandInput((ubyte)0x00);

   
   /* Calculate the distance for the page edge*/                          
   uwFarToPage = (PAGE_SIZE  - (udAddress % PAGE_DATA_SIZE));
   /*DMA management*/
   #ifdef DMA_ENABLE
     /* to do DMA */
   #else                            
	   /* Read data to the internal buffer */
	   udIndex=0;
	   
	   GPIO_BitWrite(GPIO2,5,0); 						//set CL low
	   GPIO_BitWrite(GPIO2,6,0);						//set AL low
	   for(i=0; i<5; i++)
	   {;}
	   while((udIndex<udlength) && (uwFarToPage != 0))
	   {
	   		Buffer[udIndex] = NANDF_CS; 
			uwFarToPage--;	udIndex++;
	   }
	   
	   if ( uwFarToPage == 0 )
	   {
			//Wait fo Ready
			ubStatus=waitForReady();
			// Leave Read Status Register mode 
			NAND_CommandInput((ubyte)0x00);
			uwFarToPage=PAGE_SIZE;
	   }
	   
	   		/*
			if ( uwFarToPage == 0 ) {
				    //Wait fo Ready
			    ubStatus=waitForReady();
			  
			    // Leave Read Status Register mode 
			    NAND_CommandInput((ubyte)0x00);
			    uwFarToPage=PAGE_SIZE;
			}
			else
			{
			 Buffer[udIndex++] = NAND_DataOutput(); 
			 uwFarToPage--;
			}*/
   #endif

   ubStatus = NAND_ReadStatusRegister();
     
   NAND_Close();
	/* Return Pass or Fail */   
   return (ubStatus&(0x01));
}

/******************************  NAND_PageRead ********************************/



/******************************************************************************
                NAND_PageProgram function

Function:       NAND_Ret NAND_PageProgram(udword udAddress, dataWidth *Buffer,
                                                 udword udlength)

Arguments:      udAddress: The address of the page to program.
                Buffer:    Contains the source buffer with the data to program.
                udlength:  number of data to program.

Return Value: 	NAND_FLASH_SIZE_OVERFLOW: The address is not within the flash
                NAND_PAGE_OVERFLOW: The size exceedes the page size
                NAND_PASS: The operation are successfully executed
                NAND_FAIL: The operation are not successfully execute

Description:    The PageProgram operation issue a PageProgram Command as
                explained in the datasheet of the 528 byte/264 word page family.
                The only values of the pointer are 00h and 01h. That means the Page
                Program operation starts in any part of the data area of the page 
                except the spare area. So, the SpareProgram operations must be
                call to Program only the spare area.
******************************************************************************/
NAND_Ret NAND_PageProgram(udword udAddress, dataWidth *Buffer, udword udlength) {

	register udword udIndex;    
	ubyte ubStatus;
    ubyte i;


	/* Control if the address is within the flash*/
	if ( udAddress >= FLASH_SIZE )
		return NAND_FLASH_SIZE_OVERFLOW;

	/* Control if the address is the buffer size is within the page */
	if ( ( (udAddress % PAGE_DATA_SIZE) +  udlength ) > PAGE_SIZE )
		return NAND_PAGE_OVERFLOW;

	NAND_Open();
	                         
	/* Check for A8 - Set the address pointer */            
	/* A8 =1*/
	if ( (udAddress % PAGE_DATA_SIZE) >= PAGE_DATA_SIZE/2 )
		/* 2nd half page*/
		NAND_CommandInput(HALF_PAGE_POINTER); 
	else
		/* 1st half page*/
		NAND_CommandInput((ubyte)0x00);       
	                         
	/* Issue Sequential data input command */
	NAND_CommandInput((ubyte)0x80);
	                         
	/*send the address*/                          
	InsertAddress(udAddress);

⌨️ 快捷键说明

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