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

📄 fmd.cpp

📁 此压缩包为杰得开发得z228的BSP的源代码,可以实现很多功能,尤其是视频解码有很好的效果.
💻 CPP
📖 第 1 页 / 共 3 页
字号:

  for(index = 0; index < readlen; index++)
   	oobbuf[index] = (BYTE)NF_DATA_R();    

	NF_CE_H();
	
  return 0;
}

int nand_writeoob(UINT32 blkpage,UINT32 offset,UINT8 *oobbuf,UINT32 writelen)
{
	int iRet;  
	UINT8 readcmd,coladdr1,coladdr2;
	UINT32 index,mode;

#ifdef S512
	int NewSpareAddr = 2048 + 16*(blkpage%4);
	blkpage = blkpage / 4;
#endif

	if((oobbuf == NULL) || (pChipInfo == NULL))
		return -1;

		  	
 	if(pChipInfo->sectorperpage == 4)
	{	
		readcmd = CMD_READ;
#ifdef S512
		coladdr1 = (NewSpareAddr + offset)&0xFF;
		coladdr2 = ((NewSpareAddr + offset)>>8)&0xFF;	
#else
		coladdr1 = (pChipInfo->pagesize + offset)&0xFF;
		coladdr2 = ((pChipInfo->pagesize + offset)>>8)&0xFF;				
#endif				
	}
	else
	{
		readcmd = CMD_READ2;	
		coladdr1 = offset&0xFF;
		coladdr2 = (offset>>8)&0xff;
	}
	
	mode = (pChipInfo->coladdrcycle&0xF) | ((pChipInfo->rowaddrcycle&0xF)<<4);

	nand_wp(FALSE);
	
	NF_CE_L();  	

	switch(mode)
	{
		case 0x21:
		{
			NF_CMD(readcmd);
			NF_CMD(CMD_WRITE);
  		NF_ADDR(coladdr1);
  		NF_ADDR(blkpage&0xFF);
    	NF_ADDR((blkpage>>8)&0xFF);
   	}
  	break;
  	case 0x22:
  	{
			NF_CMD(readcmd);
			NF_CMD(CMD_WRITE);
  		NF_ADDR(coladdr1);
  		NF_ADDR(coladdr2);
  		NF_ADDR(blkpage&0xFF);
    	NF_ADDR((blkpage>>8)&0xFF);
    }
  	break;
  	case 0x31:
		{
			NF_CMD(readcmd);
			NF_CMD(CMD_WRITE);
  		NF_ADDR(coladdr1);
  		NF_ADDR(blkpage&0xFF);
    	NF_ADDR((blkpage>>8)&0xFF);
    	NF_ADDR((blkpage>>16)&0xFF);
    }
  	break;
  	case 0x32:
  	{
			NF_CMD(readcmd);	//yz_add
			NF_CMD(CMD_WRITE);
  		NF_ADDR(coladdr1);
  		NF_ADDR(coladdr2);
  		NF_ADDR(blkpage&0xFF);
    	NF_ADDR((blkpage>>8)&0xFF);
    	NF_ADDR((blkpage>>16)&0xFF);
    }
    break;
	}

  for(index = 0; index < writelen; index++)
  	NF_DATA_W(oobbuf[index]);  

  NF_CMD(CMD_WRITE2);  	

	nand_wait();  

  NF_DETECT_RB();

  NF_CMD(CMD_STATUS);
  if(NF_DATA_R() & 0x01)  
   	iRet = -1;
  else     	
   	iRet = 0;  
   	
	NF_CE_H();

	nand_wp(TRUE);
  
	return iRet;	
}

int nandflash_eraseall(void)
{
	unsigned int index;
	
	RETAILMSG(1, (TEXT("NandFlash Erasing all...\r\n")));
	for(index = 870; index < pChipInfo->blocknum; index++)
	{
		if(nand_erase((unsigned int)(index * pChipInfo->pageperblock)) == -1)
		{
			RETAILMSG(1, (TEXT("Fail to erase(block: %d)!\r\n"),index));
			continue;
		}	
	}
	RETAILMSG(1, (TEXT("End!\r\n")));
	return 0;
}

#define NAND_TESTBLOCK 200
/*
void nandflash_test(void)
{
  int iRet;
  unsigned int  blkpage,index,index1,index2;
#ifdef S512
  unsigned char pagebuf[512],oobbuf[16],pagesize,oobsize;
  unsigned char cmppagebuf[512],cmpoobbuf[16];  
#else
  unsigned char pagebuf[2048],oobbuf[64],pagesize,oobsize;
  unsigned char cmppagebuf[2048],cmpoobbuf[64];  
#endif  

	FILE* fp;
	
	RETAILMSG(1, (TEXT("NandFlash Testing...\r\n")));
  if(pChipInfo == NULL)
  {
  	RETAILMSG(1, (TEXT("No nand flash is detected!\r\n")));    
    return;    		
  }
  
  fp = fopen("test.txt","wb");
  if(!fp)
  {
  	RETAILMSG(1, (TEXT("NandFlash Testing file open error...\r\n")));
  	return;
  }
  
	
	memset(pagebuf,0x55,sizeof(pagebuf));
	memset(oobbuf,0x55,sizeof(oobbuf));
	pagesize = pChipInfo->pagesize;
	oobsize = pChipInfo->oobsize;
	
	RETAILMSG(1, (TEXT("Writing...(block:%d Page:%d)!\r\n"),pChipInfo->blocknum,pChipInfo->pageperblock));   
	fprintf(fp,"Writing...(block:%d Page:%d)!\r\n",pChipInfo->blocknum,pChipInfo->pageperblock);
//	for(index = 0; index < pChipInfo->blocknum/2000; index++)	
	for(index = 0; index < pChipInfo->blocknum/NAND_TESTBLOCK; index++)	
	{		
		blkpage = (unsigned int)(index * pChipInfo->pageperblock);
		if(nand_erase(blkpage) == -1)
		//if(nand_erase(index) == -1)
		{
			RETAILMSG(1, (TEXT("Fail to erase(block: %d)!\r\n"),index));  
			fprintf(fp,"Fail to erase(block: %d)!\r\n",index);
			continue;
		}		
		for(index1 = 0; index1 < pChipInfo->pageperblock; index1++,blkpage++)
		{		
			iRet = nand_writepage(blkpage,pagebuf);
			if(iRet == -1)			
			{
				RETAILMSG(1, (TEXT("Fail to write page(block: %d page: %d)!\r\n"),index,index1)); 						
				fprintf(fp,"Fail to write page(block: %d page: %d)!\r\n",index,index1);
				break;
			}			
			iRet = nand_writeoob(blkpage,0,oobbuf,pChipInfo->oobsize);
			if(iRet == -1)
			{
				RETAILMSG(1, (TEXT("Fail to write oob(block: %d page: %d)!\r\n"),index,index1));
				fprintf(fp,"Fail to write oob(block: %d page: %d)!\r\n",index,index1);
				break;
			}
		}
	}
	RETAILMSG(1, (TEXT("Reading and checking...\r\n")));  
	fprintf(fp,"Reading and checking...\r\n");
//	for(index = 0; index < pChipInfo->blocknum/2000; index++)	
	for(index = 0; index < pChipInfo->blocknum/NAND_TESTBLOCK; index++)	
	{
		blkpage = (unsigned int)(index * pChipInfo->pageperblock);
		for(index1 = 0; index1 < pChipInfo->pageperblock; index1++,blkpage++)
		{			
			memset(cmppagebuf,0x00,sizeof(cmppagebuf));
			memset(cmpoobbuf,0x00,sizeof(cmpoobbuf));
			
			iRet = nand_readpage(blkpage,cmppagebuf);
			if(iRet == -1)
			{
				RETAILMSG(1, (TEXT("Fail to read page(block: %d page: %d)!\r\n"),index,index1));			 
				fprintf(fp,"Fail to read page(block: %d page: %d)!\r\n",index,index1);
			  break;;	
			}
			iRet = nand_readoob(blkpage,0,cmpoobbuf,pChipInfo->oobsize);
			if(iRet == -1)
			{
				RETAILMSG(1, (TEXT("Fail to read oob(block: %d page: %d)!\r\n"),index,index1));			 			 
			  break;
			}			
			for(index2 = 0; index2 < pagesize; index2++)
			{
				if(cmppagebuf[index2] != pagebuf[index2])
				{
					RETAILMSG(1, (TEXT("Different is found(block: %d page: %d Byte:%d)! WriteValue:%02x ReadValue:%02x!\r\n"),index,index1,index2,pagebuf[index2],cmppagebuf[index2]));					
					fprintf(fp,"Different is found(block: %d page: %d Byte:%d)! WriteValue:%02x ReadValue:%02x!\r\n",index,index1,index2,pagebuf[index2],cmppagebuf[index2]);
					//break;
				}
			}
			if(index2 < pagesize)
				break;			
			for(index2 = 0; index2 < oobsize; index2++)
			{
				if(cmpoobbuf[index2] != oobbuf[index2])
				{
					RETAILMSG(1, (TEXT("Different is found(block: %d oob: %d Byte:%d)! WriteValue:%02x ReadValue:%02x!\r\n"),index,index1,index2,pagebuf[index2],cmppagebuf[index2]));
					fprintf(fp,"Different is found(block: %d oob: %d Byte:%d)! WriteValue:%02x ReadValue:%02x!\r\n",index,index1,index2,oobbuf[index2],cmpoobbuf[index2]);
					//break;
				}
			}
			if(index2 < pagesize)
				break;
		}
	}	

	//yz_add
	
//	for(index = 0; index < pChipInfo->blocknum/2000; index++)	
	for(index = 0; index < pChipInfo->blocknum/NAND_TESTBLOCK; index++)	
	{		
		blkpage = (unsigned int)(index * pChipInfo->pageperblock);
		if(nand_erase(blkpage) == -1)
		//if(nand_erase(index) == -1)
		{
			RETAILMSG(1, (TEXT("Fail to erase(block: %d)!\r\n"),index));  
			fprintf(fp,"Fail to erase(block: %d)!\r\n",index);
			continue;
		}
	}	
	
	RETAILMSG(1, (TEXT("End of test!\r\n")));  	
	fprintf(fp,"End of test!\r\n");
	fclose(fp);
}
*/
/*
//qzsu add 2007-3-6
void nandflash_test2(void)
{
  int iRet;
  unsigned int  index,index2;
#ifdef S512
  unsigned char pagebuf[512],oobbuf[16],pagesize,oobsize;
  unsigned char cmppagebuf[512],cmpoobbuf[16];  
#else
  unsigned char pagebuf[2048],oobbuf[64],pagesize,oobsize;
  unsigned char cmppagebuf[2048],cmpoobbuf[64];  
#endif  

	FILE* fp;
	
	RETAILMSG(1, (TEXT("NandFlash Testing...\r\n")));
  if(pChipInfo == NULL)
  {
  	RETAILMSG(1, (TEXT("No nand flash is detected!\r\n")));    
    return;    		
  }
  
  fp = fopen("test.txt","wb");
  if(!fp)
  {
  	RETAILMSG(1, (TEXT("NandFlash Testing file open error...\r\n")));
  	return;
  }
	
	memset(pagebuf,0x55,sizeof(pagebuf));
	memset(oobbuf,0x55,sizeof(oobbuf));
	pagesize = pChipInfo->pagesize;
	oobsize = pChipInfo->oobsize;
	
	RETAILMSG(1, (TEXT("Writing...(block:%d Page:%d)!\r\n"),pChipInfo->blocknum,pChipInfo->pageperblock));   
	fprintf(fp,"Writing...(block:%d Page:%d)!\r\n",pChipInfo->blocknum,pChipInfo->pageperblock);
	
	for(index = 0; index < pChipInfo->blocknum*pChipInfo->pageperblock; index++)	
	{		
		if(nand_erase(index) == -1)
		{
			RETAILMSG(1, (TEXT("Fail to erase(page: %d)!\r\n"),index));
			fprintf(fp,"Fail to erase(page: %d)!\r\n",index);
			break;
		}		
		iRet = nand_writepage(index,pagebuf);
		if(iRet == -1)			
		{
			RETAILMSG(1, (TEXT("Fail to write page( page: %d)!\r\n"),index)); 
			fprintf(fp,"Fail to write page( page: %d)!\r\n",index);					
			break;
		}			
		iRet = nand_writeoob(index,0,oobbuf,pChipInfo->oobsize);
		if(iRet == -1)
		{
			RETAILMSG(1, (TEXT("Fail to write oob( page: %d)!\r\n"),index));
			fprintf(fp,"Fail to write oob( page: %d)!\r\n",index);
			break;
		}

		memset(cmppagebuf,0x00,sizeof(cmppagebuf));
		memset(cmpoobbuf,0x00,sizeof(cmpoobbuf));
			
		iRet = nand_readpage(index,cmppagebuf);
		if(iRet == -1)
		{
				RETAILMSG(1, (TEXT("Fail to read page( page: %d)!\r\n"),index));	
				fprintf(fp,"Fail to read page( page: %d)!\r\n",index);		 
			  break;;	
		}
		iRet = nand_readoob(index,0,cmpoobbuf,pChipInfo->oobsize);
		if(iRet == -1)
		{
				RETAILMSG(1, (TEXT("Fail to read oob( page: %d)!\r\n"),index));	
				fprintf(fp,"Fail to read oob( page: %d)!\r\n",index);		 			 
			  break;
		}			
		for(index2 = 0; index2 < pagesize; index2++)
		{
				if(cmppagebuf[index2] != pagebuf[index2])
				{
					RETAILMSG(1, (TEXT("Different is found( page: %d Byte:%d)! WriteValue:%02x ReadValue:%02x!\r\n"),index,index2,pagebuf[index2],cmppagebuf[index2]));					
					fprintf(fp,"Different is found( page: %d Byte:%d)! WriteValue:%02x ReadValue:%02x!\r\n",index,index2,pagebuf[index2],cmppagebuf[index2]);					
					break;
				}
		}
		for(index2 = 0; index2 < oobsize; index2++)
		{
				if(cmpoobbuf[index2] != oobbuf[index2])
				{
					RETAILMSG(1, (TEXT("Different is found( oob: %d Byte:%d)! WriteValue:%02x ReadValue:%02x!\r\n"),index,index2,oobbuf[index2],cmpoobbuf[index2]));
					fprintf(fp,"Different is found( oob: %d Byte:%d)! WriteValue:%02x ReadValue:%02x!\r\n",index,index2,oobbuf[index2],cmpoobbuf[index2]);
					break;
				}
		}
	}

	nandflash_eraseall();
		
	RETAILMSG(1, (TEXT("End of test!\r\n")));  
	fprintf(fp,"End of test!\r\n");
	
	fclose(fp);
}
*/


/*******************************************************
******************* FMD Driver Layer *******************
*******************************************************/
PVOID FMD_Init(LPCTSTR lpActiveReg, PPCI_REG_INFO pRegIn, PPCI_REG_INFO pRegOut)
{
  	UINT32 chipid,index;	
	PHYSICAL_ADDRESS PhysicalAddress;
	//unsigned int gpio4base;
	//BOOL bLastMode;// = SetKMode(TRUE);
	
	pChipInfo = NULL;    	

#ifdef S512
	RETAILMSG(1, (TEXT("FMD_Init +1.0 - 512 byte\r\n")));
#else
	RETAILMSG(1, (TEXT("FMD_Init +1.0 - 2048 byte\r\n")));
#endif

	PhysicalAddress.HighPart = 0;	
	PhysicalAddress.LowPart = BaseAddr;		//Nand flash base address
	//pRegBaseAddr = (unsigned int *)MmMapIoSpace(PhysicalAddress, 0x1000, FALSE);	
	BaseAddr = (unsigned int)MmMapIoSpace(PhysicalAddress, 0x900000, FALSE);

	RegBaseAddr2 = GpioBaseAddr[CS_GP_num];
	PhysicalAddress.HighPart = 0;	
	PhysicalAddress.LowPart = RegBaseAddr2;	// CS signal;
	//pRegBaseAddr2 = (unsigned int *)MmMapIoSpace(PhysicalAddress, 0x1000, FALSE);
	RegBaseAddr2 = (unsigned int)MmMapIoSpace(PhysicalAddress, 0x1000, FALSE);

	if(RB_GP_num == CS_GP_num)
	{
		RegBaseAddr = RegBaseAddr2;
	}
	else
	{
	RegBaseAddr = GpioBaseAddr[RB_GP_num];
	PhysicalAddress.HighPart = 0;	
	PhysicalAddress.LowPart = RegBaseAddr;		// R/B signal;
	//pRegBaseAddr = (unsigned int *)MmMapIoSpace(PhysicalAddress, 0x1000, FALSE);
	RegBaseAddr = (unsigned int)MmMapIoSpace(PhysicalAddress, 0x1000, FALSE);
	}

	if(WP_GP_num == CS_GP_num)
	{
		NandWpAddr = RegBaseAddr2;
	}
	else if(WP_GP_num == RB_GP_num)
	{
		NandWpAddr = RegBaseAddr;
	}
	else
	{
	NandWpAddr = GpioBaseAddr[WP_GP_num];
	PhysicalAddress.HighPart = 0;	
	PhysicalAddress.LowPart = NandWpAddr;		//WP signal
	NandWpAddr = (unsigned int)MmMapIoSpace(PhysicalAddress, 0x1000, FALSE);
	}

	RETAILMSG(1, (TEXT("virtual addr:  BaseAddr - 0x%x, CS - 0x%x, RB - 0x%x, WP - 0x%x\r\n"),BaseAddr,RegBaseAddr2,RegBaseAddr,NandWpAddr));

//only if you use the GPIO4_0 - 4_3, you should configure the GPIO reg as software function!!
#if 1
	if(CS_GP_num == 4)
	{
		*(volatile unsigned int*)(RegBaseAddr2 + 0x420 ) = 0x06;	//GP4_1,4_2 for UART_0
		RETAILMSG(1, (TEXT("CS# use GPIO4_x, configure the software function\r\n")));
	}
	else if(RB_GP_num == 4)
	{
		*(volatile unsigned int*)(RegBaseAddr + 0x420 ) = 0x06;	//GP4_1,4_2 for UART_0
		RETAILMSG(1, (TEXT("RB# use GPIO4_x, configure the software function\r\n")));
	}		
	else if(WP_GP_num == 4)
	{
		*(volatile unsigned int*)(NandWpAddr + 0x420 ) = 0x06;	//GP4_1,4_2 for UART_0
		RETAILMSG(1, (TEXT("WP# use GPIO4_x, configure the software function\r\n")));
	}
#endif	
	
	*(volatile unsigned int*)(RegBaseAddr2 + 0x400 ) |= (1<<CS_GP_pin);	//CS out
	*(volatile unsigned int*)(NandWpAddr + 0x400 ) |= (1<<WP_GP_pin);		//WP out
	*(volatile unsigned int*)(RegBaseAddr + 0x400 ) &= (~(1<<RB_GP_pin));		//RB IN

	*(volatile unsigned int*)(NandWpAddr + 0x3fc ) |= (1<<WP_GP_pin);		//WP = 1 to enable Nandflash write
	
	//yz_add
	DisableNandCE;
	//DisableNandCE2;	
	
	chipid = nand_getid();	
	RETAILMSG(1, (TEXT("nand_getid: %x!\r\n"),chipid));
	
  for(index = 0; ChipInfoList[index].chipid; index++)
  {
   	if(chipid == ChipInfoList[index].chipid)    
    {	
      pChipInfo = &ChipInfoList[index];
      break;
    }
  }    
  
  //SetKMode(bLastMode);
  
  if(pChipInfo == NULL)
  	RETAILMSG(1, (TEXT("Fail to FMD_Init!!!\r\n")));	
#ifdef CE_2  	
  else
  {
	UINT32 chipid2;
	chipid2 = nand_getid2();				
	RETAILMSG(1, (TEXT("nand_getid2: %x!\r\n"),chipid2));
	
  for(index = 0; ChipInfoList[index].chipid; index++)
  {
   	if(chipid2 == ChipInfoList[index].chipid)    
    {	
      pChipInfo2 = &ChipInfoList[index];
      break;
    }
  }    
  
  if(pChipInfo2 == NULL)
  	{
  	isCE2 = 0;
  	RETAILMSG(1, (TEXT("Single Chip Nand\r\n")));	
  	}
  else
  	{
  	if(chipid != chipid2)
		RETAILMSG(1, (TEXT("en? two company!\r\n")));
  	RETAILMSG(1, (TEXT("Double Chip Nand\r\n")));
	isCE2 = 1;
  	pChipInfo->blocknum *= 2;
  	}
  }
#endif
  RETAILMSG(1, (TEXT("FMD::FMD_Init Done!!!\r\n")));	

⌨️ 快捷键说明

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