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

📄 nand_flash.c

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

    /* Write the data to the internal buffer */ 
	GPIO_BitWrite(GPIO2,5,0); 						//set CL low
	GPIO_BitWrite(GPIO2,6,0);						//set AL low
	for(i=0; i<5; i++)
	{;}
    for (udIndex=0;udIndex<udlength;udIndex++)
         NANDF_CS = Buffer[udIndex];				//write data
	
	#endif

	/* 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_PageProgram *******************************/


/******************************************************************************
                  NAND_ReadElectronicSignature

Function:         void NAND_ReadElectronicSignature(ubyte *Buffer)

Arguments:        Buffer:contains the byte returned from ReadElectronicSignature

Return Value:     na

Description:      The NAND_ReadElectronicSignature operation issue a
                  ReadElectronicSignature Command as explained in the datasheet
                  of the 528 byte/264 word page family.
******************************************************************************/
void NAND_ReadElectronicSignature(dataWidth *Buffer) { 
  
   ubyte ubIndex;
   // int i;
   NAND_Open();
  
   /* Set the address pointer */                          
   NAND_CommandInput((ubyte)0x90);  /* Spare Area*/                                                                                       

   /* Issue the address bytes*/                          
   NAND_AddressInput((ubyte)0x00); /* first address byte */
  
   for (ubIndex=0;ubIndex<2;ubIndex++)
    Buffer[ubIndex]= NAND_DataOutput();
    
   NAND_Close();
       
}               
/**********************  NAND_ReadElectronicSignature *************************/


/******************************************************************************
                    NAND_ReadStatusRegister

Function:         ubyte NAND_ReadStatusRegister()

Arguments:        na

Return Value:     the status register readed 			

Description:      The PageProgram operation issue a Read Status Register  Command 
                  as explained in the datasheet of the 528 byte/264 word page
                  family.

*******************************************************************************/
ubyte NAND_ReadStatusRegister()  {

   ubyte ubStatus;
   
   //NAND_Open();
   /* Issue Read Status Register command */
   NAND_CommandInput((ubyte)0x70);
   ubStatus = (ubyte)NAND_DataOutput();

   //NAND_Close();
   /* Return the Status Register */
   return ubStatus;
}                        

/************************  NAND_ReadStatusRegister ****************************/


/******************************************************************************
                      NAND_Reset

Function:         void NAND_Reset(void)

Arguments:        na

Return Value:     na 			

Description:      The Reset operation issue a Reset Command as explained in the
                  datasheet of the 528 byte/264 word page family.

******************************************************************************/
void NAND_Reset(void) {

   NAND_Open();
   /* Issue the Reset Command*/
   NAND_CommandInput((ubyte)0xFF); 
     
   NAND_Close();  

}  
/******************************  NAND_Reset ***********************************/



/*******************************************************************************
                  NAND_SpareProgram

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

Arguments:      udAddress:  The address of the page to program
                            (Note A0-A3 contains the offset in the spare area).
                Buffer: Contains the source buffer with the data to program.
                udlength: Lunghezza del buffer or 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 Spare Program operation issue a Spare Program Command as
                explained in the datasheet of the 528 byte/264 word page family.
                The only value of the pointer is 50h. That means the Spare
                Program operation is used to Program only the spare area.

*******************************************************************************/
NAND_Ret NAND_SpareProgram(udword udAddress, dataWidth *Buffer, udword udlength){

  
   ubyte ubStatus;
   udword ubIndex;
   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_SPARE_SIZE) +  udlength ) > PAGE_SPARE_SIZE )
    return NAND_PAGE_OVERFLOW;
    
   NAND_Open();      

   /* Set the address pointer to the Spare Area*/
   NAND_CommandInput((ubyte)0x50);     /* Spare Area */
                                
   /* Issue Sequential data input command */                          
   NAND_CommandInput((ubyte)0x80);
                                      
                                  
  InsertAddress(udAddress);
                                
                                  
#ifdef DMA_ENABLE

   /* to do DMA */

#endif

#ifndef DMA_ENABLE
   /* Write the data to the internal buffer */ 

   GPIO_BitWrite(GPIO2,5,0); 						//set CL low
   GPIO_BitWrite(GPIO2,6,0);						//set AL low
   for(i=0; i<5; i++)
   {;}                   
   for (ubIndex=0;ubIndex<udlength;ubIndex++)
    NANDF_CS = Buffer[ubIndex];						//write data
#endif
                                  
                                  
   /* 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_SpareProgram *******************************/


/******************************************************************************
                  NAND_SpareRead

Function:       NAND_Ret NAND_SpareRead(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:  Lunghezza del buffer or 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 Sapre Read operation issue a Spare Read Command as explained
                in the datasheet of the 528 byte/264 word page family.
                The only value of the pointer is 50h. That means the Spare Read
                operation starts to read directly in the spare area.
                The Random Read, the serial read and the sequentially row read
                operations are possible.

******************************************************************************/
NAND_Ret NAND_SpareRead(udword udAddress, dataWidth *Buffer, udword udlength){
   

   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 */                          
   NAND_CommandInput((ubyte)0x50);  /* Spare Area*/
                                                                                             
   InsertAddress(udAddress);

    /* Wait for ready */
   ubStatus=waitForReady();
    
     /* Leave Read Status Register mode */
   NAND_CommandInput((ubyte)0x50);

 #ifdef DMA_ENABLE

  /* to do DMA */

 #endif
 #ifndef DMA_ENABLE

   /* Calculate the distance for the page edge*/                          
   uwFarToPage = (PAGE_SPARE_SIZE  - (udAddress % PAGE_SPARE_SIZE));
                                    
   /* Read data to the internal buffer */
   GPIO_BitWrite(GPIO2,5,0); 						//set CL low
   GPIO_BitWrite(GPIO2,6,0);						//set AL low
   for(i=0; i<5; i++)
   {;}
   udIndex=0;
   while((udIndex<udlength) && (uwFarToPage != 0))
   {
   		Buffer[udIndex++] = NANDF_CS;				//read data
      	uwFarToPage--;
   }   	
   if ( uwFarToPage == 0 ) {
        ubStatus=waitForReady(); /* Wait for ready*/
        uwFarToPage = PAGE_SPARE_SIZE;

       /* Leave Read Status Register mode */
       NAND_CommandInput((ubyte)0x50);
    }
    
    /*
    else {
      Buffer[udIndex++] = NAND_DataOutput();
      uwFarToPage--;
    }*/

#endif

  ubStatus = NAND_ReadStatusRegister();

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

}
/********************************  NAND_SpareRead *****************************/

void NAND_Init(void)
{	

  //********************************* NAND FLASH PORT Initial *********************************************
  EMI_Config(3,EMI_ENABLE | EMI_WAITSTATE(1) | EMI_SIZE_8);
  //GPIO_Config(GPIO2,1<<3,GPIO_OUT_PP);								//config P2.3 as /CS3 GPIO_AF_PP
  GPIO_Config(GPIO2,1<<3,GPIO_OUT_PP);							//config P2.3 as NANF_/CS
  GPIO_Config(GPIO2,1<<4,GPIO_OUT_PP);							//config P2.4 as NANF_/WP
  GPIO_Config(GPIO2,1<<5,GPIO_OUT_PP);							//config P2.5 as NANF_CL
  GPIO_Config(GPIO2,1<<6,GPIO_OUT_PP);							//config P2.6 as NANF_AL   
  GPIO_Config(GPIO2,1<<7,GPIO_IN_TRI_CMOS);						//config P2.7 as NANF_R/B  
  GPIO_BitWrite(GPIO2,4,0);											//clear NANF_/WP
  //********************************* NAND FLASH PORT Initial END *****************************************
	
  NAND_Reset(); 
  
  
  
}

⌨️ 快捷键说明

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