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

📄 jflash.cpp

📁 这个程序是我自己写的
💻 CPP
📖 第 1 页 / 共 5 页
字号:
					(int)(in_id[1] - '0') * 4 +
					(int)(in_id[2] - '0') * 2 +
					(int)(in_id[3] - '0');

	switch(revision)	   
	{
		case 0: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID0][0]); 
			break;
		case 1: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID1][0]); 
			break;
		case 2: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID2][0]); 
			break;
		case 3: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID3][0]); 
			break;
		case 4: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID4][0]); 
			break;
		case 5: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID5][0]); 
			break;
		case 6: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID6][0]); 
			break;
		case 7: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID7][0]); 
			break;
		case 8: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID8][0]); 
			break;
		case 9: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID9][0]); 
			break;
		case 10: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID10][0]); 
			break;
		case 11: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID11][0]); 
			break;
		case 12: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID12][0]); 
			break;
		case 13: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID13][0]); 
			break;
		case 14: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID14][0]); 
			break;
		case 15: 
			printf("%s revision %s\n", &WORDARRAY[p_processor][0], &WORDARRAY[p_CID15][0]); 
			break;

		default: printf("Unknown revision number. Out of range!");	// should never get here
	}

	return 0;
}
/*
*******************************************************************************
*
* FUNCTION:         error_out
*
* DESCRIPTION:      generic error printout and program exit.
*
* INPUT PARAMETERS: char * error_string to print before exit
*
* RETURNS:          void
*
* GLOBAL EFFECTS:   Exits the program
*******************************************************************************
*/

void error_out(char *error_string)
{
	printf("%s\n",error_string);
	exit(0);
}

/*
*******************************************************************************
*
* FUNCTION:         program_flash
*
* DESCRIPTION:      program the flash using buffered writes
*
* INPUT PARAMETERS: DWORD max_write_buffer derived from the flash query
*                   DWORD base_address
*                   DWORD fsize (flash size)
*
* RETURNS:          void
*
*******************************************************************************
*/

void program_flash(DWORD max_write_buffer, DWORD base_address, DWORD fsize)
{
	time_t start = 0;
	time_t now = 0;
	WORD li_WORD;
	DWORD li_DWORD;
	DWORD write_word_count;
	int bus_width;
//	DWORD Status;
	printf("Starting programming\n");

	if(!strcmp("WORD", &WORDARRAY[P_progmode][0] ))
	{
		printf("Using WORD programming mode...\n");

		for(DWORD lj = base_address; lj < fsize + base_address; lj = lj +1)
		{
			
			time(&now);
			if(difftime(now,start) > STATUS_UPDATE)	// Update status every 2 seconds
				{
			   	printf("Writing flash at hex address %8lx, %5.2f%% done    \r"
					,lj * ADDR_MULT ,(float)(lj - base_address)/(float)fsize*100.0);
				time(&start);
				}

			if(!PlatformIs16bit)
			{
				fread((DWORD *)&li_DWORD, sizeof(DWORD) , 1, in_file);
				access_rom(WRITE, lj, F_WORDBYTE_PROG, IGNORE_PORT); 
				access_rom(HOLD, lj, F_WORDBYTE_PROG, IGNORE_PORT);
				access_rom(WRITE, lj, li_DWORD, IGNORE_PORT); 
				access_rom(HOLD, lj, li_DWORD, IGNORE_PORT);
			}
			else
			{
				fread((WORD *)&li_WORD, sizeof(WORD) , 1, in_file);
				access_rom(WRITE, lj, F_WORDBYTE_PROG, IGNORE_PORT); 
				access_rom(HOLD, lj, F_WORDBYTE_PROG, IGNORE_PORT);
				access_rom(WRITE, lj, li_WORD, IGNORE_PORT); 
				access_rom(HOLD, lj, li_WORD, IGNORE_PORT);
			}
		}
			 	Write_Rom(lj, F_READ_ARRAY);
	}

	else if(!strcmp("BUFFER", &WORDARRAY[P_progmode][0] ))
	{
		printf("Using BUFFER programming mode...\n");

		// "Write Buffer" flow.
		// This uses almost half the cycles required by "word programming" flow
		// Status register is not read to save time.  There is also no checking to see
		// if maximum "Write Buffer Program Time" is violated.  However even with the
		// fastest parallel port bus speed this should not be a problem
		// (i.e. 16 words * 300 JTAG chain length * 4 parallel port cycles * 1uS fast
		// parallel port cycle = 19mS, typical write buffer program times are in the 200uS range).

	if(!PlatformIs16bit)
		write_word_count = (max_write_buffer - 1) + ((max_write_buffer - 1) << 16);
	else
		write_word_count = max_write_buffer - 1;


		time(&start);

		for(DWORD lj = base_address; lj < fsize + base_address; lj = lj + max_write_buffer)
		{

			access_rom(WRITE, lj, F_WRITE_BUFFER, IGNORE_PORT); // write buffer command
			access_rom(HOLD, lj, F_WRITE_BUFFER, IGNORE_PORT);

			access_rom(WRITE, lj, write_word_count, IGNORE_PORT); // write word count (max write buffer size)
			access_rom(HOLD, lj, write_word_count, IGNORE_PORT);

			time(&now);
			if(difftime(now,start) > STATUS_UPDATE)	// Update status every 2 seconds
				{
			   	printf("Writing flash at hex address %8lx, %5.2f%% done    \r"
					,lj * ADDR_MULT ,(float)(lj - base_address)/(float)fsize*100.0);
				time(&start);
				}

			if(!PlatformIs16bit)
			{
				for(DWORD lk = 0; lk < max_write_buffer; lk++)
				{
					fread((DWORD *)&li_DWORD, sizeof(DWORD) , 1, in_file);
					access_rom(WRITE, lj+lk, li_DWORD, IGNORE_PORT);  // Write buffer data
					access_rom(HOLD, lj+lk, li_DWORD, IGNORE_PORT);  // New
				    //	printf("writing %x at address %x\n", li_DWORD, lj+lk);
				}
			}
			else
			{
				for(DWORD lk = 0; lk < max_write_buffer; lk++)
				{
					fread((WORD *)&li_WORD, sizeof(WORD) , 1, in_file);
					access_rom(WRITE, lj+lk, li_WORD, IGNORE_PORT);  // Write buffer data
					access_rom(HOLD, lj+lk, li_WORD, IGNORE_PORT);  // New
				}
			}

				// No need to diferentiate between 16 and 32 bit access_rom functions anymore!
				access_rom(WRITE, 0, F_BLOCK_ERASE_2ND, IGNORE_PORT); // Program Buffer to Flash Confirm
				access_rom(HOLD, 0, F_BLOCK_ERASE_2ND, IGNORE_PORT);  //New
		}
			 	Write_Rom(lj, F_READ_ARRAY);
	}
	else if(!strcmp("XSCALE", &WORDARRAY[P_progmode][0] ))
	{
		printf("Using XSCALE programming mode...\n");
		
		xscale_init_handler();
		if(!PlatformIs16bit) 
		{
			bus_width = 32;
		}
		else
		{
			bus_width = 16;
		}
		
		xscale_program_flash(in_file, base_address, fsize, bus_width);	

	}


	printf("\nProgramming done\n");

	rewind(in_file);
}
/*
*******************************************************************************
*
* FUNCTION:         verify_flash
*
* DESCRIPTION:      compares data programmed in flash with the original binary file.
*
* INPUT PARAMETERS: DWORD base_address
*                   DWORD flash_size
*
* RETURNS:          void
*
*******************************************************************************
*/

void verify_flash(DWORD base_address, DWORD fsize)
{
	bool ccc=false;
	time_t start, now;
	DWORD li_DWORD=0, li1_DWORD=0;
	WORD li_WORD, li1_WORD;
	int dumpcount = 0;
	bool verified = false;
	bool retry = false;

	//<wcy
	bool is_k3_flash;
	//wcy>


	printf("Starting Verify\n");
	
	//<wcy
	if (strcmp ("K", &FLASHWORDARRAY[pf_type][0])==-1 )
	{
		is_k3_flash = true;
	}
	else
	{
		is_k3_flash = false;
	}
	//wcy>

	//<wcy
	Read_Rom(base_address);
	//wcy>
	time(&start);


	if(PlatformIs16bit)
	{
		for(DWORD lj = base_address + 1; lj <= fsize + base_address; lj++)
			{
			fread((WORD *)&li_WORD, sizeof(WORD) , 1, in_file);
			retry = false;
			verified = false;
			do
			{
			    // toggle the chip select for K3 flash
				//<wcy
				if (is_k3_flash)
					access_rom(RS, lj, 0x0L, READ_PORT);  
				//wcy>
				
				li1_WORD = (WORD) Read_Rom(lj);
				
				if (is_k3_flash)
					Read_Rom(lj);  //hack to fix K3 flash bug?

				time(&now);
				if(difftime(now,start) > STATUS_UPDATE)	// Update status every 2 seconds
					{
					printf("Verifying flash at hex address %8lx, %5.2f%% done    \r"
						,lj*ADDR_MULT ,(float)(lj - base_address)/(float)fsize*100.0);
					time(&start);
					}
				if(li_WORD != li1_WORD)
					{
						//<wcy
						printf("verify error at address = %lx exp_dat = %lx act_dat = %lx\n",(lj - 1) *ADDR_MULT,li_DWORD,li1_DWORD);
						if(VerifyRetry)
						{
							verified = false;
							if (is_k3_flash)
								access_rom(RS, lj-1, 0x0L, READ_PORT);
							dumpcount++;
							if(dumpcount > 10)
							{
								if (ErrorReturn)
								{
									exit(1);
								}
								else
								{
									verified=true;
								}
							}
							printf("Retrying....\n");
							retry = true;
							//wcy>
						}
						else
						{
							verified = true;
							if (ErrorReturn)  exit(1);	
						}
					}
			    }while(!verified);
			}
	}
	else 
	{
		for(DWORD lj = base_address + 1; lj <= fsize + base_address; lj++)
		{
			fread((DWORD *)&li_DWORD, sizeof(DWORD) , 1, in_file);
			retry = false;
			verified = false;
		 do
		 {
		    // toggle the chip select for K3 flash
			//<wcy
			if (is_k3_flash)
				access_rom(RS, lj, 0x0L, READ_PORT);
			//wcy>
			li1_DWORD = Read_Rom(lj);

			//<wcy
			if (is_k3_flash)
				Read_Rom(lj);  //hack to fix K3 flash bug?
			//wcy>
			
			time(&now);
			if(difftime(now,start) > STATUS_UPDATE)	// Update status every 2 seconds
				{
				printf("Verifying flash at hex address %8lx, %5.2f%% done    \r"
					,lj*ADDR_MULT ,(float)(lj - base_address)/(float)fsize*100.0);
				time(&start);
				}
			if(li_DWORD != li1_DWORD)
				{
					//<wcy
					printf("verify error at address = %lx exp_dat = %lx act_dat = %lx\n",(lj - 1) *ADDR_MULT,li_DWORD,li1_DWORD);
					if(VerifyRetry)
					{
						verified = false;
						if (is_k3_flash)
							access_rom(RS, lj-1, 0x0L, READ_PORT);
						dumpcount++;
						if(dumpcount > 10)
						{
							if (ErrorReturn)
							{
								exit(1);
							}
							else
							{
								verified=true;
							}
						}
						printf("Retrying....\n");
						retry = true;
						//wcy>
					}
					else
					{
						verified = true;
						if (ErrorReturn)  exit(1);
					}
				}
				else
				{
					verified = true;
					if(retry) dumpcount--;
				}
			}while(!verified);
		}
	}


	printf("\nVerification successful!                                                    \n");
}
/*
*******************************************************************************
*
* FUNCTION:         test_logic_reset
*
* DESCRIPTION:      initializes the JTAG state machine to a known state
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
*******************************************************************************
*/
 
void test_logic_reset(void)
{
    if(Debug_Mode)
    printf("begin test logic reset\n");
    

	// keep TMS set to 1 force a test logic reset
	// no matter where you are in the TAP controller
	for(int i=0; i < 6; ++i)
		putp(1,1,IGNORE_PORT);

    if(Debug_Mode)
    printf("finish test logic reset\n");
    
}

/*
*******************************************************************************
*
* FUNCTION:         set_lock_flash
*
* DESCRIPTION:      sets locks bits in specified block
*
* INPUT PARAMETERS: DWORD base_address of flash
*                   DWORD fsize - size of flash
*                   DWORD block_size - block size of flash
*                   DWORD max_erase_time - used for a timeout 
*                   int block_number - block number of interest
*
* RETURNS:          void
*
*******************************************************************************
*/


void set_lock_flash(DWORD base_address, DWORD fsize, DWORD block_size, DWORD max_erase_time, int block_number)
{
	time_t start, now;

	printf("Starting set block lock bit\n");

	for(DWORD lj = base_address; lj < fsize + base_address; lj = lj + block_size)  // locks only blocks to be programmed
		{

		// Rami: Should this be lj instead of 0 ???
		Write_Rom(0, F_SET_BLOCK_LOCK);			//  block lock bit command

⌨️ 快捷键说明

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