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

📄 flash_test.c

📁 Redboot, boot-loader of Linux for Freescale ARM family.
💻 C
📖 第 1 页 / 共 2 页
字号:
	}
	ub_reset();
	return 1;
}

/*****/

/*** Erasure Commands ***/

/* Chip Erase */

// The Fifth Horseman of the Apocalypse. Nothing will survive.
// Note that this takes some time to occur, 2-3 minutes
// Returns: 1 on succesful erase, null on failure

int erase_chip(void){
//	volatile unsigned int intermediate;
	reset();
	if(!UBM){
		unlock();
	}
	*(CS_BASE + 0x555)=0x00800080;
	if(!UBM){
		unlock();
	}
	*(CS_BASE + 0x555)=0x00100010;
	
	// Polling for completion
	while(((*CS_BASE^0x00800080)&DQ7)!=0){
		if((*CS_BASE&DQ5)==DQ5){
			if(((*CS_BASE^0x00800080)&DQ7)==0){
				return 1;
			} else {
				return 0;
			}
		}
	}

	return 1;
}

/*****/

/* Sector Erase */

// Practices a less wanton destruction, erasing only a specific sector.
// Parameters:	int sector = either the negative # of the sector to be erased,
//				            or a flash or system address within the sector to be erased
//				valid range: [0:-269], [0:0x7FFFFF] or [(CS_BASE) + 4 * (0:0x7FFFFF)]
// Returns: 1 on succesful erase, null on failure

int erase_sector(int sector){
	volatile unsigned int * sector_p=sector_bcalc(sector);
	sector_p+=0x555;
	reset();
	if(!UBM){
		unlock();
	}
	*(CS_BASE + 0x555)=0x00800080;
	if(!UBM){
		unlock();
	}
	*sector_p=0x00300030;
	// Polling for completion
	while(((*sector_p^0x00800080)&DQ7)!=0){
		if((*sector_p&DQ5)==DQ5){
			if(((*sector_p^0x00800080)&DQ7)==0){
				return 1;
			} else {
				return 0;
			}
		}
	}

	return 1;
}

/*****/

/* Erase Suspend */

// Valid only during a sector erase.
// Allows erase to be temporarily suspended so data can be read from a different sector.
// Parameter: 	int sector = preferably the same input used to initialize the erase.
//				technically, it only needs to be a sector # (negative) or address in the same bank
//				as the sector being currently erased. constants BANK_A to BANK_D will
//				also function correctly as inputs

void erase_suspend(int sector){
	*sector_bcalc(sector)=0x00B000B0;
	reset();
}

/*****/

/* Erase Resume */

// Valid only during erase suspend mode, resumes suspended erase.
// Parameter: identical to erase suspend

void erase_resume(int sector){
	reset();
	*sector_bcalc(sector)=0x00300030;
}

/*** End of Erasure Commands ***/

/*****/

/* Set Configuration Register */

// Command not valid during program/write, erase, or sector lock

// Parameter: unsigned char config = detailed bit-wise:
//		[7]	Set Device Read Mode (1 = Async, 0 = Synchronous)
//		[6]	RDY signal (1 = active with data, 0 = active one clock cycle before data)
//		[5]	Clock (1 = output triggers on rising edge, 0 = output triggers on falling edge)
//		[4:3]	Read Mode:
//					00 = Continous
//					01 = 8-word linear with wrap around
//					10 = 16-word linear with wrap around
//					11 = 32-word linear with wrap around
//		[2:0]	Programmable Wait State:
//					Data is valid on the [...] active CLK edge after AVD# transition to Vih
//					000 = 2nd
//					001 = 3rd
//					010 = 4th
//					011 = 5th
//					100 = 6th
//					101 = 7th
//					110 = <reserved>
//					111 = <reserved>
					
void set_config(unsigned int build_address){
	build_address = build_address << 12;
	build_address += 0x555;
	unlock();
	(*(volatile unsigned int *)(CS_BASE + build_address))=0x00C000C0;
}

void Enable_I_cache (void)
{
    __asm
    {
	   	mrc p15,0,r0,c1,c0,0
	    mov r0, #0x00001000
   		mcr p15,0,r0,c1,c0,0 
	}
}

/*****/

/****************************
******   The Program   ******
****************************/

#define SECTORSIZE  (32*4*1024)

int main(void){
	volatile unsigned int q=0;
	int j = 0;

	int * data, * data1;
// int programSize = 0x100000*32;
	int programSize = 0x100000/4;
	int programSectors = (programSize+SECTORSIZE-1)/SECTORSIZE;
	
	int *sourceAddr = (int*)0xC2000000;	
	int dstAddr	= 0;
	
	printf("Program %d sectors\n", programSectors);
	
	if(auto_manu_id()){
//		printf("Manufacturer ID is correct!\n");
	} else {
		printf("**Manufacturer ID is incorrect**\n");
	}	
	if(auto_dev_id()){
//		printf("Device ID is correct!\n");
	} else {
		printf("**Device ID is incorrect**\n");
	}

	
	if(programSectors == (0x2000000/SECTORSIZE))
	{
		// erase all
		printf("Beginning full chip erase, may take 2-3 minutes...\n");
		if(erase_chip())
		{
			printf("Chip erased\n");
		}
	}
	else
	{
		for(j=0; j>-8; j--)
		{
			if(erase_sector(j)){
				reset();
//				printf("Sector %d Erased\n", abs(j));
			} else {
				printf("Sector %d Erase failed.\n", abs(j));
				return 0;
			}
		}
	

		for(j=-8; j>(-8-programSectors+1); j--)
		{
			if(erase_sector(j)){
				reset();
//				printf("Sector %d Erased\n", abs(j));
			} else {
				printf("Sector %d Erase failed.\n", abs(j));
				return 0;
			}
		}
	}


	j=0;
	
	write_m(0, programSize/4, sourceAddr);
	
	printf("Program complete. Now verifying ...\n");

	gFailCount = 0;	// initialize 

	// checking
	sourceAddr = (int*)0xC2000000;	
	dstAddr	= 0;
	for (q = 0; q < programSize/4; q++)
	{
		if(*sourceAddr++ != read((int)dstAddr++))
		{
			printf("Failure, write didn't take!, Address = 0x%x\n", q); 
			gFailCount++;
		}
	}
	
	if (gFailCount ==0)
	{
		printf("Write test passed!\n");
	}
	
	return 0;
	
	reset();




#if 0
	// Fill Sector 0's address locations with their own addresses
	for(q=0;q<0x000FFF;q+=write(q,q));
//	for(q=0x001000;q<0x001FFF;q+=write(q,q));

	printf("Sector 0 written\n");
	
	// read out what's just been written to dynamically allocated memory
	data=read_m(0x000000,0x1000);
	printf("Data read from Sector 0 to array\n");
	
	// Clear Sector 1 for writing
	if(erase_sector(-1)){
		printf("Sector 1 Erased\n");
	}	
	
	// Copy the data in the array to Sector 1 using write_m command
	if(write_m(0x001000,0x1000,data)){
		printf("Array written to Sector 1\n");
	}	
	
	// Read Sector 1 data into second array
	data1=read_m(0x001000,0x1000);
	printf("Data read from Sector 1 to second array\n");
	
	for(q=0;q<0x1000;q+=1){
		if(data[q]!=data1[q]){
			printf("Data does not match\n");
			break;
		}
	}
	if(q==0x1000){printf("First and Second array data match\n");}

	
	// Erase full chip
	printf("Beginning full chip erase, may take 2-3 minutes...\n");
	if(erase_chip()){
		printf("Chip erased\n");
	}
	
	// release allocated memory for arrays
	free(data);
	free(data1);
	
	//verify erasure
	data=read_m(0x000000,0x2000);	
	for(q=0;q<0x2000;q+=1){
		if(data[q]!=0xFFFFFFFF){
			printf("Erase failed\n");
			break;
		}
	}
	if(q==0x2000){printf("Erase successful\n");}
	
	// release memory
	free(data);

	
	printf("Now, as a test, let's write the entire flash content with known data.\n");
	// now write to entire flash, just a test to ensure we can write to the entire flash
	for (q = 0; q < 0x800000; q++)
	{
		// data written to each address is the address that the data is written to
		write(q,q);
		
		// print out occasional status of writes
		if (q == (q&0xFFFF0000))
		{
			printf("Status, now writing address 0x%x\n", q);
		}
	}
	
	gFailCount = 0;	// initialize 
	// now let's read the entire flash content to ensure the writes took
	for (q = 0; q < 0x800000; q++)
	{
		if(q != read(q))
		{
			printf("Failure, write didn't take!, Address = 0x%x\n", q); 
			gFailCount++;
		}
	}
	
	if (gFailCount ==0)
	{
		printf("Write test passed!\n");
	}
	
	
	
	reset();

	/**************************
	** Test 3: Burst testing **
	**************************/
	
	// to be implemented later
#endif
	return (0);
}





















⌨️ 快捷键说明

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