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

📄 flash_org.c

📁 FreeScale imx21开发板Nand flash烧写程序
💻 C
📖 第 1 页 / 共 2 页
字号:
//
//                  This function erases all flash memory sectors overlapping
//                  the address range specified in the parameters.
//
//
//  Parameters:     U32             sAddress        start address for erase
//                  U32             nAddress        end address for erase
//
//  Return Value:   U32             number of bytes erased (rounded up to
//                                  next sector limit), or zero on error
//
// ***************************************************************************
U32 FlashSectorErase(U32 sAddress, 
               		 U32 nAddress)
{
    volatile U32*       pBase;
    volatile U32*       pWalk;
    BOOL                bFailTotal;
    BOOL                bDone;
    BOOL                bFail;
    U32                 nWalk;
    U32                 nSize;
    U32                 nPoll;
    U32                 nDone;
    U32                 nSec;
    U32					SA;
    U32					i = 0;


        // Check the Flash Starting Address
        pBase = (volatile U32*)(sAddress & 0xFE000000);


        // Reset flash devices before starting erase sequences
        // ***************************************************
        *(pBase + 0x000) = 0x00f000f0;
		
		nSec = (U32)pBase;
		nDone = 0;
		nWalk = sAddress;
		#ifdef DEBUG
		printf("Sector Erase at [0x%08X - 0x%08X]...\n",sAddress, nAddress);
		#endif		
		
		for (SA = 1; SA <= 270; SA++)
		{
			if ((SA >= 1 && SA <= 8) || (SA >= 262 && SA <= 270))
			{	
				nSize = 0x1000*4;
			}
			else
			{
				nSize = 0x8000*4;
			}
			
			if ((nSec <= nWalk) && (nSec + nSize > nWalk) && (nSec <= nAddress))
			{
				
				// This sector overlaps the address range. Erase it
				// ************************************************
				pWalk = (volatile U32*) nWalk;

				// Execute "normal" sector erase algorithm
				// ***************************************
				*(pBase + 0x555) = 0x00aa00aa;
				*(pBase + 0x2aa) = 0x00550055;
				*(pBase + 0x555) = 0x00800080;
				*(pBase + 0x555) = 0x00aa00aa;
				*(pBase + 0x2aa) = 0x00550055;
				*(pWalk)         = 0x00300030;


				// Data polling algorithm for erase operation
				// ******************************************
				bDone = FALSE;
				bFail = FALSE;

				while ((!bDone) && (!bFail))
				{
					nPoll = *(pWalk);
					if (((~nPoll) & FLASH_DQ7_2X16) == 0)
					{  
						bDone = TRUE;
					}
					else if ((nPoll & FLASH_DQ5_2X16) == FLASH_DQ5_2X16)
					{
						nPoll = *(pWalk);
						if (((~nPoll) & FLASH_DQ7_2X16) == 0)
						{
							bDone = TRUE;
						}
						else
						{
							bFail      = TRUE;
							bFailTotal = TRUE;
						}
					}
					if ( (i &= 0xFFFF) ==0)
					{
						#ifdef DEBUG
						printf(".");
						#endif
					}
					i++;
                }
                nDone += nSize;
                nWalk += nSize;
            }
            nSec += nSize;
        }


        // Reset flash devices
        // *******************
        *(pBase + 0x000) = 0x00f000f0;

    return bFailTotal ? 0 : nDone;
}

// ***************************************************************************
//
//  Function:       FlashRead
//
//                  
//
//  Parameters:     U32             sAddress        start address for read
//                  void*           pData           data to read
//                  U32             nData           number of bytes to read
//
//  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;
	
	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
			}
		}
		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;

		// Status Code
		U32 FlashComplete 		= 0x67676767;
		U32 FlashTimeOut 		= 0x57575757;
		U32 FlashError 			= 0x47474747;
		
		#ifdef DEBUG
		printf("\nStarting Flash Programming\n");		
		#endif
		
		nAddress = fAddress + (nWords - 1) * sizeof(fAddress);
		FlashSectorErase(fAddress,nAddress);
		FlashWrite(fAddress, (U32*)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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -