flash.c

来自「FreeScale imx21开发板Nand flash烧写程序」· C语言 代码 · 共 1,195 行 · 第 1/3 页

C
1,195
字号
//
//  Return Value:   U32             ?
//
// ***************************************************************************
U32* FlashRead(U32 sAddress, U32* pData, U32 nData)
{
    U32					nWalk;
    U32					nAddress;
    U32*				dWalk;
    U32*				sWalk;
    U32					i;
    
    nWalk = sAddress;
    nAddress = sAddress + nData;
    dWalk = (U32*)sAddress;
    sWalk = pData;
	
	#ifdef DEBUG
    printf("Flash Reading at [0x%08X - 0x%08X]...\n", sAddress, nAddress);
    #endif
    
    // Data Read
    // *******************
    //pData=memcpy(pData, (U32*) nAddress, nData);
    //nDone = nData;
    
    while (nWalk < nAddress)
    {
    	i++;
    	if ((i & 0xFFFF) == 0)
    	{
    		#ifdef DEBUG
    		printf("R");
    		#endif
    	}
    	*dWalk = *sWalk;
    	dWalk++;
    	sWalk++;
    	nWalk +=sizeof(*dWalk);
    }
    #ifdef DEBUG
    printf("\nFlash Read Complete\n");
    #endif
	return (U32*)sAddress;

}

// ***************************************************************************
//
//  Function:       FlashVerify
//					 
//                  
//
//  Parameters:     U32             rAddress        RAM starting address 
//					U32				fAddress		Flash starting address to be verified
//                  U32		        nWords			number of words to check
//
//  Return Value:   U32				Errors			number of programming errors 
//													in word             
//
// ***************************************************************************
U32	FlashVerify(U32 fAddress, U32 rAddress, U32 nWords)
{
	U32		i;
	U32		Errors = 0;
	U32*	fWalk;
	U32*	rWalk;
	U32		nAddress;
	
	U32 Flash_Verify		= 0x37373737;

	fWalk = (U32*)fAddress;
	rWalk = (U32*)rAddress;
	nAddress = fAddress + (nWords - 1) * sizeof(*fWalk);
	#ifdef DEBUG
	printf("Flash Verifing at [0x%08X - 0x%08X]...\n", fAddress, nAddress);
	#endif
	for (i = 0; i < nWords;i++)
	{ 
		if (*fWalk != *rWalk)
		{
			#ifdef DEBUG
			printf("Programming Error at 0x%08X\n", (U32)fWalk);
			#endif
			Errors++;	
		}
		else
		{
			if ((i & 0xFFFF) == 0)
			{
				// i increment for 32 bit
				// print "S" for 0x10000* 32bit
				#ifdef DEBUG
				printf("S");
				#endif
				
				CheckFlashComplete(Flash_Verify);
			}
		}
		fWalk++;
		rWalk++;
	}
	return Errors;
}



// ***************************************************************************
//
//  Function:       FlashLoader
//					 
//                  
//
//  Parameters:     U32             rAddress        RAM starting address
//					U32				fAddress		Flash starting address to be programmmed
//                  U32		        nWords			number of words to write
//
//  Return Value:   void             
//
// ***************************************************************************
void FlashLoader(U32 fAddress, U32 rAddress, U32 nWords)
{
		U32		nAddress;
		U32		Errors;
		U32 	HAB_Status;		

		// Status Code
		U32 FlashComplete 		= 0x67676767;
		U32 FlashTimeOut 		= 0x57575757;
		U32 FlashError 			= 0x47474747;
		
		
		//Nand_bad_test();
		#ifdef DEBUG
		printf("\nStarting Flash Programming\n");		
		#endif
		
		//Nand_bad_test();
		
		nAddress = fAddress + (nWords - 1) * sizeof(fAddress);
		nand_unconditional_erase_all();
		//FlashSectorErase(fAddress,nAddress);
				

		//FlashWrite(fAddress, (U32*)rAddress, nWords);
		FlashWrite(fAddress, rAddress, nWords);

/*
		Errors = FlashVerify(fAddress,rAddress,nWords);
		
		if (Errors)
		{
			//#ifdef DEBUG
			printf("\nFlash Programming Error:\n");
			printf("Total no. of Errors: %d words\n",Errors);
			//#else
			//CheckFlashComplete(FlashError);
			//#endif
		}
*/
//		else
		{
			#ifdef DEBUG
			printf("\nFlash Programming Complete\n");
			#else
			CheckFlashComplete(FlashComplete);
			#endif
		}

		return;
}



/*
// ***************************************************************************
//
//  Function:       FlashTest
//					Fill the internal RAM with a test pattern and then the 
//                  Flash Routine would copy this test pattern into the Flash 
//
//  Parameters:     BOOL			Burst			BurstMode??
//					U32             sAddress        base address of the Flash
//					U32				tPat			test pattern
//                  U32		        dSize           data size in word
//                  U32             rAddress        RAM start address 
//
//  Return Value:   U32             ?
//
// ***************************************************************************
U32	FlashTest(BOOL Burst, U32 sAddress, U32 tPat, U32 dSize, U32 rAddress)
{
	U32				nWalk;
	U32*			rWalk;
	U32				dAddress;
	U32				nError = 0;
	
	rWalk = (U32*) rAddress;
	nWalk = rAddress; 
	dAddress = rAddress + dSize;

	// fill the RAM with the test pattern
	//***********************************
	while (nWalk < dAddress)
	//for (nWalk = 1; nWalk <= dSize/4; nWalk++)
	{
			*rWalk = tPat;
			rWalk++;
			nWalk+=sizeof(*rWalk);
	}

  BoardControlBurstMode(Burst, sAddress); 
  FlashChipErase(sAddress);
 	//  FlashSectorErase(0xC8000000, 0xC8001000);
 
 	// copy filled RAM into Flash
 	// **********************************
    FlashWrite(sAddress, (void*)rAddress, dSize);
    
    // Read the filled Flash and check for 
    // data integrity
    // **********************************
   	FlashRead(dAddress, (U32*) sAddress, dSize);
   	for (nWalk = 1; nWalk <= dSize; nWalk++)
   	{	
   		if (*(U32*)rAddress != *(U32*)dAddress)
   		{	
   			nError++;
   		}
   		rAddress ++;
   		dAddress ++;
   	}
   	return nError;
   		
}



// ***************************************************************************
//
//  Function:       Flash_Full_Test - 32Mbytes
//					Fill the SDRAM with a test pattern and then the 
//                  Flash Routine copy them from RAM into the Flash 
//
//  Parameters:     BOOL			Burst			BurstMode??

//
//  Return Value:   U32             Success			Test Pass/Fail
//
// ***************************************************************************
BOOL Flash_Full_Test(BOOL Burst)
{
		U32		SourceAddress = 0xC2000000;
		U32		TargetAddress = 0xC8000000;
		U32		WordSize = 8*1024*1024;
		U32		Testpat = 0x55AA55AA;
		U32		Error = 0;
		BOOL 	BurstOn = FALSE;
		BOOL	Success;
		
		printf("\nFlash Full Test\n");
		Memory_Fill_U32(SourceAddress,WordSize,Testpat);
	//	BoardControlBurstMode(BurstOn,TargetAddress); 
		FlashChipErase(TargetAddress);
		FlashWrite(TargetAddress, (U32*)SourceAddress, WordSize);
		Error = MemCheck_U32(TargetAddress,WordSize,Testpat);
		printf("\nFlash Full Test Complete\n");
		
		return Error ? FALSE:TRUE;
}
		


// ***************************************************************************
//
//  Function:       Flash_Quick_Test - 1Mbytes at each bank
//					Fill the internal RAM with a test pattern and then the 
//                  Flash Routine copy this test pattern into the Flash 
//
//  Parameters:     BOOL			Burst			BurstMode??

//
//  Return Value:   U32             Success			Test Pass/Fail
//
// ***************************************************************************
BOOL Flash_Quick_Test(BOOL BurstOn)
{
		U32		SourceAddress = 0xC2000000;
		U32		TargetAddress = 0xC8000000;
		U32		Bank_one = TargetAddress;
		U32		Bank_two = Bank_one + (8*4*1024+31*32*1024)*4;	
		U32		Bank_three = Bank_two + (96*32*1024)*4;				
		U32		Bank_four = Bank_three + (96*32*1024)*4;
		
		U32		WordSize = 0x40000;
		U32		Testpat = 0x55AA55AA;
		U32		Error = 0;
		U32		Total_errors = 0;
		U32 	i = 0;
		
		
		printf("\nFlash Quick Test\n");
		Memory_Fill_U32(SourceAddress,WordSize,Testpat);
//		BoardControlBurstMode(BurstOn,TargetAddress);
		
		for (i = 1;i <= 4; i++)
		{
			switch (i)
			{
				case (1):
					printf("\nBank One of Flash Memory:\n");
					FlashSectorErase(Bank_one, Bank_one + (4*WordSize));
					printf("\nBlank Check at Bank one\n");
					MemCheck_U32(Bank_one,WordSize,0xFFFFFFFF);
					FlashWrite(Bank_one, (U32*)SourceAddress, WordSize);
					Error = MemCheck_U32(Bank_one,WordSize,Testpat);
					if (Error)
					{
						printf("Flash Memory fail at Bank 1\n");

					}
					else
					{
						printf("Flash Memory pass at Bank 1\n");
					}
					break;
				
				case (2):
					printf("\nBank Two of Flash Memory:\n");
					FlashSectorErase(Bank_two, Bank_two + (4*WordSize));
					printf("\nBlank Check at Bank two\n");
					MemCheck_U32(Bank_two,WordSize,0xFFFFFFFF);
					FlashWrite(Bank_two, (U32*)SourceAddress, WordSize);
					Error = MemCheck_U32(Bank_two,WordSize,Testpat);
					if (Error)
					{
						printf("Flash Memory fail at Bank 2\n");

					}
					else
					{
						printf("Flash Memory pass at Bank 2\n");
					}
					break;
					
				case (3):
					printf("\nBank Three of Flash Memory:\n");
					FlashSectorErase(Bank_three, Bank_three + (4*WordSize));
					printf("\nBlank Check at Bank three\n");
					MemCheck_U32(Bank_three,WordSize,0xFFFFFFFF);
					FlashWrite(Bank_three, (U32*)SourceAddress, WordSize);
					Error = MemCheck_U32(Bank_three,WordSize,Testpat);
					if (Error)
					{
						printf("Flash Memory fail at Bank 3\n");

					}
					else
					{
						printf("Flash Memory pass at Bank 3\n");
					}
					break;	
					
				case (4):
					printf("\nBank Four of Flash Memory:\n");
					FlashSectorErase(Bank_four, Bank_four + (4*WordSize));
					printf("\nBlank Check at Bank four\n");
					MemCheck_U32(Bank_four,WordSize,0xFFFFFFFF);
					FlashWrite(Bank_four, (U32*)SourceAddress, WordSize);
					Error = MemCheck_U32(Bank_four,WordSize,Testpat);
					if (Error)
					{
						printf("Flash Memory fail at Bank 4\n");

					}
					else
					{
						printf("Flash Memory pass at Bank 4\n");
					}
					break;
			}
			Total_errors += Error * sizeof(Error);
		}
			
		if (Total_errors)
		{
			printf("\nFlash Quick Test Fail\n");
			printf("Total no. of Errors is %dbytes\n", Total_errors);
		}
		else
		{
			printf("\nFlash Quick Test Complete\n");
		}
		return Total_errors ? FALSE:TRUE;
}*/

⌨️ 快捷键说明

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