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

📄 drv_nf.c

📁 The combined demo is dedicated for S1C33L05, so DMT33L05 should be used to load and run the demo. F
💻 C
📖 第 1 页 / 共 4 页
字号:
//      Parameters
//         Input  		:unsigned long 	ulAddress  		page address
//         Output 		:unsigned char* pReadBuf		buf for store read data
//         I/O    		:void
//      
//      Return value 		:unsigned char	FLASH_SUCCESS		read Success
//						FLASH_ECC_ERROR		ecc check error
//						FLASH_PAR_ERR		parameter error
//	Global variable 	:void	
//---------------------------------------------------------------------------------
unsigned char fnSMC_PageRead_DMA(unsigned long ulAddress, unsigned char* pReadData){

	unsigned short colAddr,pageAddr,blockAddrL,blockAddrH;
	unsigned char* getRedt;
	unsigned char* ECCRet;
	int i, Cnt;
	unsigned char* pReadBuf;
	
	#ifdef	DEBUG
		/* check parameter */
		if  (ulAddress >=(MAX_BLK_NUM * MAX_PG_NUM * PG_SIZE))
			return FLASH_PAR_ERR;
	#endif

  	pReadBuf = pReadData;
  
  	/* DMA initialize */
  	/* Set DMA transfer count */
  	*(volatile unsigned short*)0x48240 = 0x0200; /* 512 byte */
  
  	/*Set dual address DMA mode*/
  	DMA_DUAL;
  
  	/*Set source address (Nand flash address), byte/word transfer*/
  	*(volatile unsigned long*)0x48244 = 0x04000000; /* fix source address 4000000h, byte transfer */
 
  	/*Set destination address, successive,addr_inc mode*/
  	*(volatile unsigned long*) 0x48248 = (unsigned long)pReadBuf;
  	*(volatile unsigned short*) 0x4824a &= 0x7fff;
  	*(volatile unsigned short*) 0x4824a |= 0x7000; 
  
  	/* CLE, ALE latch*/
  	colAddr=(unsigned short)(ulAddress & 0x000000ff);           /* A7-0	*/
  	pageAddr=(unsigned short)((ulAddress & 0x0001fe00)>>9);     /* A16-9	*/
	blockAddrL=(unsigned short)((ulAddress & 0x01fe0000)>>17);  /* A24-17	*/
	blockAddrH=(unsigned short)((ulAddress & 0x0e000000)>>25);  /* A27-25	*/

  	SET_CLE_H;		/*  set CLE H*/
  
 	SET_SMC_CE_L;		/* set SMC CE L*/ 	
	
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=READ_MODE1_CMD;	/*  read mode 1 */
	
	SET_CLE_L;		/*  set CLE low*/
	SET_ALE_H;		/*  set ALE h*/
	
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=colAddr;   	/*  send col address*/
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=pageAddr;	/*  send page address*/
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=blockAddrL;	/*  send block l address*/
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=blockAddrH;  	/*  send block h address*/
	
	SET_ALE_L;		/* set ALE L*/

  
  	asm("nop");
  	asm("nop");  
  	asm("nop"); 
  	asm("nop");
  	asm("nop");   
  	asm("nop"); 
  	asm("nop");
 	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop");  
 	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
 	asm("nop"); 
  	do{}	
 	while (!CHECK_BUSY);		/*wait R/#B to high*/
 	
 	#ifdef ECC
  		RESET_ECC;
		EN_ECC;	
	#endif

  	/*  DMA transfer start*/
  
  	DMA_EN;
  	DMA_TRIG;  
  
  	/* Wait till DMA_En bit equal to "0"*/
	do{}	
	while (CHECK_DMA_END);
  
  	#ifdef ECC
  		DIS_ECC;
		/*  read redundant data	*/
		pReadBuf = pReadBuf + 0x200;  /* end of 512 byte data*/
	
	  	for (Cnt=0; Cnt< 6; Cnt++){
	  		* pReadBuf++ = *(volatile unsigned char*)NAND_Flash_BASE_ADDRESS;
	  	}
	#endif
 	 
 	SET_SMC_CE_H;		/* set SMC CE H */	
 	
	#ifdef ECC
  		do{}
		while (!CHECK_ECC_READY);
	
		/* Compare ECC data*/
		ECCRet = ECC_AREA0_COL_ADDR;
	
		pReadBuf = pReadBuf -6;
	
		for (i=0; i<6; i++){
    			if (* ECCRet++ != * pReadBuf++)
      				return FLASH_ECC_ERROR;
		}
	#endif
	
	return FLASH_SUCCESS;

}

//---------------------------------------------------------------------------------
//  	Function name 		: fnFlash_PageRead_DMA	
// 	Function description	: read page data from nand flash by DMA
//		 
//      Parameters
//         Input  		:unsigned long 	ulAddress  		page address
//         Output 		:unsigned char* pReadBuf		buf for store read data
//         I/O    		:void
//      
//      Return value 		:unsigned char	FLASH_SUCCESS		read Success
//						FLASH_ECC_ERROR		ecc check error
//						FLASH_PAR_ERR		parameter error
//	Global variable 	:void	
//---------------------------------------------------------------------------------    

unsigned char fnFlash_PageRead_DMA(unsigned long ulAddress, unsigned char* pReadData){

	unsigned short colAddr,pageAddr,blockAddrL,blockAddrH;
	unsigned char* getRedt;
	unsigned char* ECCRet;
	int i, Cnt;
	unsigned char* pReadBuf;
	
	#ifdef	DEBUG
		/* check parameter */
		if  (ulAddress >=(MAX_BLK_NUM * MAX_PG_NUM * PG_SIZE))
			return FLASH_PAR_ERR;
	#endif

  	pReadBuf = pReadData;
  
 	/* DMA initialize */
  	/* Set DMA transfer count*/
  	*(volatile unsigned short*)0x48240 = 0x0100; 	/*256 word*/
  
  	/* Set dual address DMA mode */
  	DMA_DUAL;
  
  	/* Set source address (Nand flash address), byte/word transfer*/
  	*(volatile unsigned long*)0x48244 = 0x44000000; /*  fix source address 4000000h, half word transfer*/
 
  	/* Set destination address, successive,addr_inc mode*/
  	*(volatile unsigned long*) 0x48248 = (unsigned long)pReadBuf;
  	*(volatile unsigned short*) 0x4824a &= 0x7fff;
  	*(volatile unsigned short*) 0x4824a |= 0x7000; 
  
  	/*  CLE, ALE latch*/
  	
  	colAddr=(unsigned short)(ulAddress & 0x000000ff);           /* A7-0 */
  	pageAddr=(unsigned short)((ulAddress & 0x0001fe00)>>9);     /* A16-9*/
	blockAddrL=(unsigned short)((ulAddress & 0x01fe0000)>>17);  /* A24-17*/
	blockAddrH=(unsigned short)((ulAddress & 0x0e000000)>>25);  /* A27-25*/

  	SET_CLE_H;		/*  set CLE H*/
  
	*(volatile unsigned short*)NAND_Flash_BASE_ADDRESS=READ_MODE1_CMD;	/*  read mode 1*/
	
	SET_CLE_L;		/*  set CLE low*/
	SET_ALE_H;		/*  set ALE h*/
	
	*(volatile unsigned short*)NAND_Flash_BASE_ADDRESS=colAddr;   	/*  send col address */
	*(volatile unsigned short*)NAND_Flash_BASE_ADDRESS=pageAddr;	  /*  send page address*/
	*(volatile unsigned short*)NAND_Flash_BASE_ADDRESS=blockAddrL;	/*  send block l address*/
	*(volatile unsigned short*)NAND_Flash_BASE_ADDRESS=blockAddrH;  /*  send block h address*/
	
	SET_ALE_L;		/* set ALE L*/

  	asm("nop");
  	asm("nop");  
  	asm("nop"); 
  	asm("nop");
  	asm("nop");   
  	asm("nop"); 
  	asm("nop");
 	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop");  
 	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
 	asm("nop"); 
  	do{}	
 	while (!CHECK_BUSY);		/*wait R/#B to high*/
	
	#ifdef ECC
  		RESET_ECC;
		EN_ECC;	
	#endif
  	/*  DMA transfer start*/
  
  	DMA_EN;
  	DMA_TRIG;  
  
  	/* Wait till DMA_En bit equal to "0"*/
	do{}	
	while (CHECK_DMA_END);
  
  	#ifdef ECC
  		DIS_ECC;
  
		/*  read redundant data*/
		pReadBuf = pReadBuf + 0x200;  /* end of 512 byte data*/
	
	  	for (Cnt=0; Cnt< 3; Cnt++){
	  		*(volatile unsigned short*)pReadBuf=*(volatile unsigned short*)NAND_Flash_BASE_ADDRESS;
	  		pReadBuf +=2;
	 	}
	
		do{}
		while (!CHECK_ECC_READY);
	
		/* Compare ECC data*/
		ECCRet = ECC_AREA0_COL_ADDR;
	
		pReadBuf = pReadBuf -6;
	
		for (i=0; i<6; i++){
    			if (* ECCRet++ != * pReadBuf++)
      				return FLASH_ECC_ERROR;
		}
	#endif
	return FLASH_SUCCESS;

}
//---------------------------------------------------------------------------------
//  	Function name 		: fnSMC_PageWrite_DMA	
// 	Function description	: write page data to smc by DMA
//		 
//      Parameters
//         Input  		:unsigned long 	ulAddress  		page address
//         Output 		:unsigned char* pWriteBuf		buf for store write data
//         I/O    		:void
//      
//      Return value 		:unsigned char	FLASH_SUCCESS		write Success
//						FLASH_ECC_ERROR		ecc check error
//						FLASH_PAR_ERR		parameter error
//						FLASH_WRITE_ERR		write error
//	Global variable 	:void	
//---------------------------------------------------------------------------------   
unsigned char fnSMC_PageWrite_DMA( unsigned long ulAddress, unsigned char* pWriteData){
	
	unsigned char  RetVal;
	unsigned char  ReadStatus;
	unsigned short colAddr,pageAddr,blockAddrL,blockAddrH;
	unsigned short Cnt;
	unsigned char* ECCRet;
	
	RetVal=FLASH_SUCCESS;
		
	#ifdef	DEBUG
		/* check parameter */
		if  (ulAddress >=(MAX_BLK_NUM * MAX_PG_NUM * PG_SIZE))
			return FLASH_PAR_ERR;
	#endif

	 /*DMA initialize */
  	 /*Set DMA transfer count */
  	*(volatile unsigned short*)0x48240 = 0x0200; /* 512 byte */
  
  	/*Set dual address DMA mode*/
  	DMA_DUAL;
  
  	/*Set source address (Nand flash address), byte/word transfer, increase address*/
  	*(volatile unsigned long*) 0x48244 = (unsigned long) pWriteData;
 	*(volatile unsigned char*) 0x48247 &= 0x3f; 	/*byte transfer*/
  	*(volatile unsigned char*) 0x48247 |= 0x30;   
  
  	/*Set fixed destination address*/
  	*(volatile unsigned long*)0x48248 = 0x44000000; /* fix destination address: 4000000h*/
  
 	 /* CLE, ALE set*/

  	colAddr=(unsigned short)(ulAddress & 0x000000ff);           /* A7-0*/
  	pageAddr=(unsigned short)((ulAddress & 0x0001fe00)>>9);     /* A16-9*/
	blockAddrL=(unsigned short)((ulAddress & 0x01fe0000)>>17);  /* A24-17*/
	  blockAddrH=(unsigned short)((ulAddress & 0x0e000000)>>25);  /* A27-25*/
	
	SET_CLE_H;	/*  set CLE high*/

  	SET_SMC_CE_L;		/* set SMC CE L */	
 		
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=AUTO_WRITE_CMD;/*  write command*/
	
	SET_CLE_L;		/*  set CLE low*/
	SET_ALE_H;		/*  set ALE h*/
	
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=colAddr;	    /*  send col address*/
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=pageAddr;	  /*  send page address*/
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=blockAddrL;	/*  send block l address*/
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=blockAddrH;  /*  send block h address*/
	
	SET_ALE_L;		/* set ALE L*/
	#ifdef ECC
  		RESET_ECC;
		EN_ECC;	
	#endif
 	
 	/*  DMA transfer start*/
  
  	DMA_EN;
  	DMA_TRIG;  
  
  	/* Wait till DMA_En bit equal to "0"*/
	do{}	
	while (CHECK_DMA_END);
  
	#ifdef ECC
  		DIS_ECC;

		do{}
		while (!CHECK_ECC_READY);
				
  		/* Write ECC data to redundant area (first 6byte/3word)*/
  		ECCRet = ECC_AREA0_COL_ADDR;

  		pWriteData = pWriteData + 0x200;  /* write ECC data to the end of pWriteData buffer*/
  
  		for (Cnt=0; Cnt<6; Cnt++){
    			*pWriteData ++ = * ECCRet ++;
  		}  
  
  		pWriteData = pWriteData - 6;

	  	for (Cnt=0; Cnt< 6; Cnt++){
	  		*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS = * pWriteData++;
	  	}
	#endif
	
	
	SET_CLE_H;	/*  set CLE high*/
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=PAGE_WRITE_CMD;
	SET_CLE_L;	/* set CLE l*/
  
  	asm("nop");
  	asm("nop");  
  	asm("nop"); 
  	asm("nop");
  	asm("nop");   
  	asm("nop"); 
  	asm("nop");
 	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop");  
 	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
  	asm("nop"); 
  	asm("nop");
  	asm("nop"); 
 	asm("nop"); 
  	do{}	
 	while (!CHECK_BUSY);		/*wait R/#B to high*/
  
	SET_CLE_H;	/*  set CLE high*/
	*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS=READ_STATUS_CMD;
	SET_CLE_L;	/* set CLE l	*/
	
	
	ReadStatus=*(volatile unsigned char*)NAND_Flash_BASE_ADDRESS;
	
	if (ReadStatus & 0x1)
		return FLASH_WRITE_ERR;
 			
 	 SET_SMC_CE_H;		/* set SMC CE H */	

	return RetVal;
}


//---------------------------------------------------------------------------------
//  	Function name 		: fnFlash_PageWrite_DMA	
// 	Function description	: write page data to nand flash by DMA
//		 
//      Parameters
//         Input  		:unsigned long 	ulAddress  		page address
//         Output 		:unsigned char* pWriteBuf		buf for store write data
//         I/O    		:void
//      
//      Return value 		:unsigned char	FLASH_SUCCESS		write Success
//						FLASH_ECC_ERROR		ecc check error
//						FLASH_PAR_ERR		parameter error
//						FLASH_WRITE_ERR		write error
//	Global variable 	:void	
//---------------------------------------------------------------------------------   
unsigned char fnFlash_PageWrite_DMA( unsigned long ulAddress, unsigned char* pWriteData){
	
	unsigned char  RetVal;
	unsigned char  ReadStatus;
	unsigned short colAddr,pageAddr,blockAddrL,blockAddrH;
	unsigned short Cnt;
	unsigned char* ECCRet;
	
	RetVal=FLASH_SUCCESS;
		
	#ifdef	DEBUG
		/* check parameter */
		if  (ulAddress >=(MAX_BLK_NUM * MAX_PG_NUM * PG_SIZE))
			return FLASH_PAR_ERR;
	#endif

  
  	/* DMA initialize */
  	/* Set DMA transfer count*/
  	*(volatile unsigned short*)0x48240 = 0x0100; /*   256 word*/
  
  	/* Set dual address DMA mode*/
  	DMA_DUAL;
  
  	/* Set source address (Nand flash address), byte/word transfer, increase address*/
  	*(volatile unsigned long*) 0x48244 = (unsigned long) pWriteData;
  	*(volatile unsigned char*) 0x48247 &= 0x7f; /* half word transfer*/
  	*(volatile unsigned char*) 0x48247 |= 0x70;  
  
  	/* Set fixed destination address*/
  	*(volatile unsigned long*)0x48248 = 0x44000000; /*  fix destination address: 4000000h*/

⌨️ 快捷键说明

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