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

📄 k9s1208.c

📁 在arm realview下的s3c2410开发板测试工程及代码
💻 C
📖 第 1 页 / 共 2 页
字号:
    U8 *bufPt=buffer;
    U8 se[16];	   
    
    page=page&0x1f;
    blockPage=(block<<5)+page;
    NF_RSTECC();    // Initialize ECC
    
    NF_nFCE_L();    
    NF_CMD(0x00);   // Read command
    NF_ADDR(0);	    // Column = 0
    NF_ADDR(blockPage&0xff);	    //
    NF_ADDR((blockPage>>8)&0xff);   // Block & Page num.
    NF_ADDR((blockPage>>16)&0xff);  //

    for(i=0;i<10;i++); //wait tWB(100ns)/////??????
    
    NF_WAITRB();    // Wait tR(max 12us)
    for(i=0;i<512;i++)
    {
    	*bufPt++=NF_RDDATA();	// Read one page
    }
    ecc0=rNFECC0;
    ecc1=rNFECC1;
    ecc2=rNFECC2;
    for(i=0;i<16;i++)
    {
    	se[i]=NF_RDDATA();	// Read spare array
    }
    
    //__RdPage528(pPage);
    NF_nFCE_H();    

    if(ecc0==se[0] && ecc1==se[1] && ecc2==se[2])
    {
	Uart_Printf("[ECC OK:%x,%x,%x]\n",se[0],se[1],se[2]);
    	return 1;
    }
    else
    {
	Uart_Printf("[ECC ERROR(RD):read:%x,%x,%x, reg:%x,%x,%x]\n",
		se[0],se[1],se[2],ecc0,ecc1,ecc2);
    	return 0;
    }    	
}


static int NF_WritePage(U32 block,U32 page,U8 *buffer)
{
    int i;
    U32 blockPage=(block<<5)+page;
    U8 *bufPt=buffer;

    NF_RSTECC();    // Initialize ECC
    
    NF_nFCE_L(); 
    NF_CMD(0x0);//??????
    NF_CMD(0x80);   // Write 1st command
    NF_ADDR(0);			    // Column 0
    NF_ADDR(blockPage&0xff);	    //
    NF_ADDR((blockPage>>8)&0xff);   // Block & page num.
    NF_ADDR((blockPage>>16)&0xff);  //

    for(i=0;i<512;i++)
    {
	NF_WRDATA(*bufPt++);	// Write one page to NFM from buffer
    }  
    
    seBuf[0]=rNFECC0;
    seBuf[1]=rNFECC1;
    seBuf[2]=rNFECC2;
    seBuf[5]=0xff;		// Marking good block
    
    for(i=0;i<16;i++)
    {
	NF_WRDATA(seBuf[i]);	// Write spare array(ECC and Mark)
    }  

    NF_CMD(0x10);   // Write 2nd command
    
    for(i=0;i<10;i++);  //tWB = 100ns. ////??????

    NF_WAITRB();    //wait tPROG 200~500us;
 
    NF_CMD(0x70);   // Read status command   
    
    for(i=0;i<3;i++);  //twhr=60ns
    
    if (NF_RDDATA()&0x1) // Page write error
    {	
    	NF_nFCE_H();
	Uart_Printf("[PROGRAM_ERROR:block#=%d]\n",block);
	NF_MarkBadBlock(block);
	return 0;
    }
    else 
    {
    	NF_nFCE_H();
    #if (WRITEVERIFY==1)
	//return NF_VerifyPage(block,page,pPage);	
    #else
	return 1;
    #endif
    }
}



static U16 NF_CheckId(void)
{
    int i;
    U16 id;
    
    NF_nFCE_L();
    
    NF_CMD(0x90);
    NF_ADDR(0x0);
    
    for(i=0;i<10;i++); //wait tWB(100ns)////?????
    
    id=NF_RDDATA()<<8;	// Maker code(K9S1208V:0xec)
    id|=NF_RDDATA();	// Devide code(K9S1208V:0x76)
    
    NF_nFCE_H();
    
    return id;
}


static void NF_Reset(void)
{
    int i;
    U16 id;
    
    
    NF_nFCE_L();

    NF_CMD(0xFF);	//reset command

    for(i=0;i<10;i++);  //tWB = 100ns. //??????

    NF_WAITRB();      //wait 200~500us;
     
    NF_nFCE_H();
}



static void NF_Init(void)
{
    rNFCONF=(1<<15)|(1<<14)|(1<<13)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0);	
    // 1  1    1     1,   1      xxx,  r xxx,   r xxx        
    // En 512B 4step ECCR nFCE=H tACLS   tWRPH0   tWRPH1
    
    NF_Reset();
}

int DownloadData(void)
{
    int i,tmp;
    U16 checkSum=0,dnCS;
    U32 fileSize=10;
    U8 *downPt;

    Uart_Printf("Download through UART0 from 0x30100000.\n");

    downPt=(U8 *)downloadAddress;
  
    Uart_Printf("\ndownloadAddress=%x\n",downloadAddress);

    Uart_Printf("Download the plain binary file to be written\n");
    Uart_Printf("The file format: <n+6>(4)+(n)+CS(2)\n");
    Uart_Printf("Download methods: 115.2KBPS,8Bit,NP,1STOP\n");
 
    Uart_Printf("\nSTATUS:");

    rINTMSK=BIT_ALLMSK;
	
    tmp=RdURXH0(); //To remove overrun error state.

    i=0;    
    while(i<fileSize)
    {
	while(!(rUTRSTAT0&0x1));
	*(downPt+i)=RdURXH0();
	if(i==3)
	{
	    fileSize=*((U8 *)(downloadAddress+0))+
	    (*((U8 *)(downloadAddress+1))<<8)+
	    (*((U8 *)(downloadAddress+2))<<16)+
	    (*((U8 *)(downloadAddress+3))<<24);
	}
	
	if((i%1000)==0)WrUTXH0('#');
	i++;
    }

    downloadProgramSize=fileSize-6;

    for(i=4;i<(fileSize-2);i++)
    {
	checkSum+=*((U8 *)(i+downloadAddress));
    }

    dnCS=*((U8 *)(downloadAddress+fileSize-2))+
    	  (*( (U8 *)(downloadAddress+fileSize-1) )<<8);

    if(checkSum!=dnCS)
    {
    	Uart_Printf("Checksum Error!!! MEM:%x DN:%x\n",checkSum,dnCS);
	return 0;
    }

    Uart_Printf("\nDownload O.K.\n");
	
    return 1;
}


char Check10(int data, char seq) // Check '10' pattern
{
    if(data & (0x2<< seq*2) )
	return 1;
    else
	return 0;
}

int TestECC(void)
{
    int i;
    U32 colECC=0,lowECC=0;
    U32 col=0, low=0;

    rNFCONF=(1<<15)|(1<<12);

    for(i=0;i<512;i++)
	NF_WRDATA(0x0);	// Write one page
    
    seBuf[0]=rNFECC0;
    seBuf[1]=rNFECC1;
    seBuf[2]=rNFECC2;

    Uart_Printf("Old: ECC0=0x%x, ECC1=0x%x, ECC2=0x%x\n",seBuf[0],seBuf[1], seBuf[2]);

    rNFCONF=(1<<15)|(1<<12);

    for(i=0;i<200;i++)
	NF_WRDATA(0x0);	// Write 200 byte
    NF_WRDATA(0x20);	// Write one btte(col: 5th, line: 200th)
    for(i=0;i<311;i++)
	NF_WRDATA(0x0);	// Write 311 byte

    seBuf[3]=rNFECC0;
    seBuf[4]=rNFECC1;
    seBuf[5]=rNFECC2;

    Uart_Printf("New: ECC0=0x%x, ECC1=0x%x, ECC2=0x%x\n",seBuf[3],seBuf[4],seBuf[5]);

    seBuf[6]=seBuf[0]^seBuf[3];
    seBuf[7]=seBuf[1]^seBuf[4];
    seBuf[8]=seBuf[2]^seBuf[5];
    
    Uart_Printf("XOR: ECC0=0x%x, ECC1=0x%x, ECC2=0x%x\n",seBuf[6],seBuf[7],seBuf[8]);
/*
    for(i=4;i>0;i--)
    {
	if( seBuf[6]>>(i*2-2) == 0x0 | seBuf[6]>>(i*2-2) == 0x3 )
	{
	    Uart_Printf("The error is above 2bits-Can not recover!!!\n");
	    return 0;
	}
    }
*/
    colECC=seBuf[8]>>2;
    lowECC=((seBuf[8]&0x3)<<16)|(seBuf[7]<<8)|(seBuf[6]<<0);
    Uart_Printf("Low ECC=0x%x, Col ECC=0x%x\n", lowECC, colECC);

    // Column parity
    for(i=0;i<3;i++)
	col |= (Check10(colECC, i) <<i);

    Uart_Printf("col=%dth\n",col); 

    // Line parity
    for(i=0;i<9;i++)
	low |= (Check10(lowECC, i) <<i);
    Uart_Printf("low=%dth\n",low); 
}

void NandFlashTest()
{

	U16 id;
	//print Flash menu
	char ch;
	
	//initilize the nand flash
	NF_Init();

	NF_Menu();

/**************my test****************/	
{
	unsigned int i,j;
	char *buf=(char *)0x30800000;
	NF_EraseBlock(0);//114*512
	NF_EraseBlock(1);
	NF_EraseBlock(2);
	NF_EraseBlock(3);
	for(i=0;i<4;i++)
		for(j=0;j<32;j++)
			{
				NF_WritePage(i, j, buf);
				buf+=512;
			}
}	
/***************************************/
	while(1)
	{
		ch=0;
		while(ch==0)
		{
			ch=Uart_GetKey();
		}
		switch(ch)
		{
		case '0':
			//check id
			Uart_Printf("\nCheck the nand flash ID\n");
			id=NF_CheckId();

			if(id!=0)
				Uart_Printf("The ID of the Nand flash is id=0x%x\n",id);
			else
				Uart_Printf("ERROR!!!No nand flash is detected!\n");
		//print flash menu
			NF_Menu();
			break;
		case '1':
			//erase the whole id
			Uart_Printf("\nShow the bad block of the nand flash\n");
			K9S1208_PrintBadBlockNum();
			//print flash menu
			NF_Menu();
			break;
			
	
		case '2':
			//erase the whole id
			Uart_Printf("\nShow the bad block of the nand flash\n");
			K9S1208_PrintBlock();
			//print flash menu
			NF_Menu();
			break;
		case '3':
			//erase the whole id
			Uart_Printf("\nWrite Nand Flash\n");
			K9S1208_Program();
			//print flash menu
			NF_Menu();
			break;
		case 'q':
			break;
		default:
			Uart_Printf("\nAn Inavidate item,Please input it again\n");
			//print flash menu
			NF_Menu();
			break;
		}
		//exit the flash test
		if(ch=='q')
			break;
	}
}


void NF_Menu(void)
{
	Uart_Printf("\n###########################################\n");
	Uart_Printf("\n		Testing the Nand FLASH");
	Uart_Printf("\n#Please use the item number!");
	Uart_Printf("\n[0]	Check nand flash ID!");
	Uart_Printf("\n[1]	Show bad block info!");
	Uart_Printf("\n[2]	Show all block info!");
	Uart_Printf("\n[3]	Write Nand Flash!");
	Uart_Printf("\n[q]	Quit");	
	Uart_Printf("\n###########################################\n");
	return ;
}

⌨️ 快捷键说明

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