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

📄 jflash.cpp

📁 这个程序是我自己写的
💻 CPP
📖 第 1 页 / 共 5 页
字号:
   //		Write_Rom(0, F_SET_READ_CFG_REG);
   //		Write_Rom(0, F_SET_READ_CFG_REG_2ND);
		// force the flash into read mode again
   //		Write_Rom(0, F_READ_ARRAY);	// put back into read mode
		access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
		time(&start);
		gpio_lock_flash();

		verify_flash(base_address, fsize);

	}
	else if(option == 'V')
	{

		Write_Rom(0, F_READ_ARRAY);				// put back into read mode
		access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
		time(&start);
		gpio_lock_flash();
		verify_flash(base_address, fsize);
	}
	else if(option == 'E')
	{
		if(AskQuestions)
		{
			printf("About to erase the entire flash memory..... \n");
			printf("Is this what you want to do? (Y or N)\n");
			if(toupper(_getche()) == 'Y')
			{
				fsize = dsize - 1;
				EraseBlocks(0, fsize);
			}
			else
			{
				error_out("Cancelling the erase....\n");
			}
		}
		else
		{
			fsize = dsize - 1;
			EraseBlocks(0, fsize);
		}
	}
	else
	{
		printf("error specifying programming option. \n");
		error_out("P: to program, V: to verify, E: to erase all\n");
	}

	fclose(in_file);

	test_logic_reset();
}


/*
*******************************************************************************
*
* FUNCTION:         Set_Platform_Global_Variables
*
* DESCRIPTION:    Evaluates some global variables based on the selected platform             
*                   
*
* INPUT PARAMETERS: none
					
* RETURNS:          None
*
* GLOBAL EFFECTS:   sets the global varibales in Global_Variables.h
*
*******************************************************************************
*/
void Set_Platform_Global_Variables()
{

	// Evaluate the following variables based on the selected platform
	ChipSelect0		= convert_to_dword(&WORDARRAY[p_cs0][0]); 	// Global variable for chip select 0
	ChipSelect1		= convert_to_dword(&WORDARRAY[p_cs1][0]); 	// Global variable for chip select 1
	ChipSelect2		= convert_to_dword(&WORDARRAY[p_cs2][0]);	// Global variable for chip select 2
	ChipSelect3		= convert_to_dword(&WORDARRAY[p_cs3][0]);	// Global variable for chip select 3
	ChipSelect4		= convert_to_dword(&WORDARRAY[p_cs4][0]);	// Global variable for chip select 4
	ChipSelect5		= convert_to_dword(&WORDARRAY[p_cs5][0]);	// Global variable for chip select 5
	OutputEnable	= convert_to_dword(&WORDARRAY[p_nOE_OUT][0]);	// Global variable for output enable
	WriteEnable		= convert_to_dword(&WORDARRAY[p_nWE_OUT][0]);	// Global variable for write enable
	MdUpperControl	= convert_to_dword(&WORDARRAY[p_mdupper_ctrl][0]);	// Global variable for MD upper control
	MdLowerControl	= convert_to_dword(&WORDARRAY[p_mdlower_ctrl][0]);	// Global variable for MD lower control
	ReadWriteMode	= convert_to_dword(&WORDARRAY[p_RD_nWR_OUT][0]);	// Global variable for Read Write access mode
	IR_Idcode		= convert_to_dword(&WORDARRAY[p_idcode][0]);	// Global variable for the IDCODE instruction of the IR
	IR_Bypass		= convert_to_dword(&WORDARRAY[p_bypass][0]);	// Global variable for the BYPASS instruction of the IR
	IR_Extest		= convert_to_dword(&WORDARRAY[p_extest][0]);  	// Global variable for the EXTEST instruction of the IR
	ChainLength		= convert_to_dword(&WORDARRAY[p_blength][0]);  	// Global variable for the chain length of the selected platform
	IrLength		= convert_to_dword(&WORDARRAY[p_irlength][0]);  	// Global variable for setting the correct IR length

	if(!strcmp("16", &WORDARRAY[p_datawidth][0] ))
		PlatformIs16bit = true;
	else
		PlatformIs16bit = false;

	// set 16 and 32 bit command and query values

	if (PlatformIs16bit)
	{
	     F_READ_ARRAY = 		0x00FFL;
	     F_READ_IDCODES =   	0x0090L;
	     F_READ_QUERY =     	0x0098L;
	     F_READ_STATUS =    	0x0070L;
	     F_CLEAR_STATUS =   	0x0050L;
	     F_WRITE_BUFFER =   	0x00E8L;
	     F_WORDBYTE_PROG =  	0x0040L;

	     F_BLOCK_ERASE =    	0x0020L;
	     F_BLOCK_ERASE_2ND =	0x00D0L; 

	     F_BLK_ERASE_PS =       0x00B0L;
	     F_BLK_ERASE_PR =       0x00D0L;
	     F_CONFIGURATION =      0x00B8L;

	     F_SET_READ_CFG_REG =   0x0060L;
	     F_SET_READ_CFG_REG_2ND = 0x0003L;

	     F_SET_BLOCK_LOCK =      0x0060L;
	     F_SET_BLOCK_LOCK_2ND =  0x0001L;

	     F_CLEAR_BLOCK_LOCK =    0x0060L;
	     F_CLEAR_BLOCK_LOCK_2ND =0x00D0L;

	     F_PROTECTION =          0x00C0L;

	     F_ATTR_Q =              0x0051L;
	     F_ATTR_R =              0x0052L;
	     F_ATTR_Y =              0x0059L;

	     F_BLOCK_LOCKED =        0x0001L;
	     F_STATUS_READY =        0x0080L;
	}
	else
	{ 
	     F_READ_ARRAY = 		0x00FF00FFL;
	     F_READ_IDCODES =   	0x00900090L;
	     F_READ_QUERY =     	0x00980098L;
	     F_READ_STATUS =    	0x00700070L;
	     F_CLEAR_STATUS =   	0x00500050L;
	     F_WRITE_BUFFER =   	0x00E800E8L;
	     F_WORDBYTE_PROG =  	0x00400040L;

	     F_BLOCK_ERASE =    	0x00200020L;
	     F_BLOCK_ERASE_2ND =	0x00D000D0L; 

	     F_BLK_ERASE_PS =       0x00B000B0L;
	     F_BLK_ERASE_PR =       0x00D000D0L;
	     F_CONFIGURATION =      0x00B800B8L;

	     F_SET_READ_CFG_REG =   0x00600060L;
	     F_SET_READ_CFG_REG_2ND = 0x00030003L;

	     F_SET_BLOCK_LOCK =      0x00600060L;
	     F_SET_BLOCK_LOCK_2ND =  0x00010001L;

	     F_CLEAR_BLOCK_LOCK =    0x00600060L;
	     F_CLEAR_BLOCK_LOCK_2ND =0x00D000D0L;

	     F_PROTECTION =          0x00C000C0L;

	     F_ATTR_Q =              0x00510051L;
	     F_ATTR_R =              0x00520052L;
	     F_ATTR_Y =              0x00590059L;

	     F_BLOCK_LOCKED =        0x00010001L;
	     F_STATUS_READY =        0x00800080L;

	}

	if (PlatformIs16bit)
	{
		ADDR_MULT = 2;
	}
	else
	{
		ADDR_MULT = 4;
	}

}

/*
*******************************************************************************
*
* FUNCTION:         get_rom_info
*
* DESCRIPTION:      get the flash information such as max erase time, flash size, 
*                   max write buffer, block number               
*                   
*
* INPUT PARAMETERS: DWORD *max_erase_time	: max erase time, number of seconds
*					DWORD *dsize			: each flash size, number of 16-bit word
*					DWORD *max_write_buffer : each flash max write buffer, number of 16-bit word
					DWORD *block_size		: each block size, number of 16-bit word
*					DWORD *nblocks			: number of erase blocks in each Flash*
					
* RETURNS:          None
*
* GLOBAL EFFECTS:   None
*
*******************************************************************************
*/
void check_rom_info(DWORD *max_erase_time, DWORD * dsize, DWORD * max_write_buffer)

{	
	DWORD FlashId;
	char constructed_string[50];
	char FlashIdString[5];
	bool IDFound = false;
	int IDAttempts = 5;

	do // Check for a recognized Flash ID a few times. Some devices are harder to read
	{
		Write_Rom(0x0, F_READ_ARRAY);	
		Write_Rom(0x0, F_READ_IDCODES);	
	   	
	  	FlashId = (Read_Rom(0x1) & 0xFFFF);
	    FlashId = (Read_Rom(0x1) & 0xFFFF); // buggy K flash needs another read



		// open the data file for the flash device. If there is no file, then
		// either the flash is not supported, not released, or there was an error
		// reading the Device ID.

		// construct the filename
		sprintf(FlashIdString, "%x", FlashId);
		strcpy(constructed_string, "");

		// construct the processor ID
		strcat(constructed_string, "Flash_");
		strcat(constructed_string, FlashIdString);
		strcat(constructed_string, "_");
		strcat(constructed_string, &WORDARRAY[p_fdevsacross][0]); 
		strcat(constructed_string, "_");
		strcat(constructed_string, &WORDARRAY[p_datawidth][0]);
		strcat(constructed_string, ".dat");

		strcpy(flash_data_filename, constructed_string);  // set the filename
		
		// See if the file exists. If not then try again to confirm the flash ID
		if((flash_file_pointer = fopen(flash_data_filename, "rb")) == NULL)
		{
			printf("Failed to read the Flash ID. Retrying %d more times...\n", IDAttempts -1);
			IDAttempts--;
			if (IDAttempts == 0)
			{
				printf("Cannot open input file: %s\n\n", flash_data_filename);
				printf("This program supports flash devices defined by DAT files\n");
				printf("contained in the same directory as the executable program. \n\n");
				printf("If the file cannot be opened, there are three possibilities:\n\n");
				printf(" 1 - The flash device installed is not supported.\n");
				printf(" 2 - The flash device is a licensed product.\n");
				printf(" 3 - The device ID could not be read, resulting in a poorly\n");
				printf("     constructed filename. The first numeric value in the\n");
				printf("     filename is the device ID. Verify this value with the\n");
				printf("     component specification.\n");
				printf(" 4 - The memory bus is not functional. Check all CPLD and FPGA\n");
				printf("     devices. Make sure that you are using the correct \n");
				printf("     platform data file. \n"); 
				exit(1);

			}
			else
			{
				// hit the system with a JTAG reset and try again
				test_logic_reset();
				// try putting the flash into read mode again
				Write_Rom(0, F_READ_ARRAY);	// put back into read mode
				
				// try forcing asynchronous read mode
				Write_Rom(0x8000, F_SET_READ_CFG_REG);
				Write_Rom(0x8000, F_SET_READ_CFG_REG_2ND);

				
//				Write_Rom(0x0, F_READ_ARRAY);	// put back into read mode
//				Write_Rom(0x0, F_READ_IDCODES);	
			   	
//			  	FlashCR = (Read_Rom(0x5) & 0xFFFF);
//			    FlashCR = (Read_Rom(0x5) & 0xFFFF); // buggy K flash needs another read
	
//				printf("configuration register = %X\n",FlashCR);
				
				Write_Rom(0, F_READ_ARRAY);	// put back into read mode

				access_rom(READ, 0x0L, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
			}
		}
		else
		{
			IDFound = true;	
		}
	 } while(!IDFound);	

	ParseAndLoadFlashData();
	InitFlashGlobals();
	printf("Found flash type: %s\n", &FLASHWORDARRAY[pf_type][0]); 

	// set the required variables by using the previousely defined globals

	*max_erase_time = BlockEraseTime;
	*dsize = FlashDeviceSize;
	*max_write_buffer = FlashBufferSize;
}

/*
*******************************************************************************
*
* FUNCTION:         check_file_info
*
* DESCRIPTION:      get the file information and check with rom size
*                   
*
* INPUT PARAMETERS: DWORD *fsize			: file size
					DWORD *last_non_zero	: the point where only 0 or FF remain
					DWORD *last_non_ff		: the point where only ff remain
										
										    if LUBBOCK_SABINAL or
										    LUBBOCK_DBPXA262 is defined,
										    the upper 3 parameters 
											are in 16-bit word number
											else they are in 32-bit DWORD number

					DWORD rom_size			: the rom size in 
*					
					
* RETURNS:          None
*
* GLOBAL EFFECTS:   None
*
*******************************************************************************
*/
void check_file_info(DWORD *fsize , DWORD *last_non_zero, DWORD *last_non_ff, DWORD rom_size)
{	
	
	WORD li_WORD;
	DWORD li_DWORD;

    if(PlatformIs16bit)
	{
		for(;;)
		{
			int n = fread((WORD *)&li_WORD, sizeof(WORD) , 1, in_file);

    		if(feof(in_file))break; // Any bytes not on a 4 byte boundry at end-of-file will be ignored
			{
				(*fsize)++;
			}
			if(li_WORD != 0 && li_WORD != -1) // Find point in file were only 0's and ff's remain
			{						// For 32 bit data bus, -1 is 0xffffffff, for 16 bit data bus -1 is 0xffff
        		*last_non_zero = *fsize;
			}
			if(li_WORD != -1)  // Find point in file were only ff's remain
			{
        		*last_non_ff = *fsize;
			}
		}

		rewind(in_file);

		// if 16-bit data width used, it assume only one 16-bit flash installed
		if ((*fsize) > rom_size)
			error_out("error, file size is bigger than device size");
	}
	else
	{
		for(;;)
		{
			int n = fread((DWORD *)&li_DWORD, sizeof(DWORD) , 1, in_file);

    		if(feof(in_file))break; // Any bytes not on a 4 byte boundry at end-of-file will be ignored
			{
				(*fsize)++;
			}
			if(li_DWORD != 0 && li_DWORD != -1) // Find point in file were only 0's and ff's remain
			{						// For 32 bit data bus, -1 is 0xffffffff, for 16 bit data bus -1 is 0xffff
        		*last_non_zero = *fsize;
			}
			if(li_DWORD != -1)  // Find point in file were only ff's remain
			{
        		*last_non_ff = *fsize;
			}
		}

		rewind(in_file);

		// if 32-bit data width used. It assume 2 16-bit flash installed. each flash size=fsize
		if ((*fsize) * 2 > rom_size * 2)
			error_out("error, file size is bigger than device size");
	}
}


/*
*******************************************************************************
*
* FUNCTION:         putp
*
* DESCRIPTION:      Drives TCK, TDI, and TMS signals and reads TDO signal 
*                   via _outp and _inp calls.
*                   
*                   Cable used had 100 ohm resistors between the following pins
*                   (Cable shipped as part of SA-1110 Development Kit)
*                   Output pins (LPT driving)
*                       LPT D0 Pin 2 and TCK J10 Pin 4
*                       LPT D1 Pin 3 and TDI J10 Pin 11
*                       LPT D2 Pin 4 and TMS J10 Pin 9
*
*                   Input pin (SA-1110 board drives)
*                       LPT Busy Pin 11 and TDO J10 Pin 13
*
*
*
* INPUT PARAMETERS: int tdi - test data in
*
* RETURNS:          int - TDO (Test Data Out)
*
* GLOBAL EFFECTS:   None
*
*******************************************************************************
*/
// Future work: this procedure needs to be cleaned up and extended. There is a 
// strong possibility that that the Altera Byte-Blaster cable could also be
// supported. 

int putp(int tdi, int tms, int rp)
{
	int tdo = -1;

	if(CableType == Parallel_Jtag)
	{
		// TMS is D2, TDI is D1, and TCK is D0, so construct an output by creating a 
		// rising edge on TCK with TMS and TDI data set.
		_outp(lpt_address, tms*4+tdi*2);				// TCK low 
		_outp(lpt_address, tms*4+tdi*2+1);				// TCK high 

		// if we want to read the port, set TCK low because TDO is sampled on the 
		// TCK falling edge.
		if(rp == READ_PORT)
			_outp(lpt_address, tms*4+tdi*2);			// TCK low
		if(rp == READ_PORT)
			tdo = !((int)_inp(lpt_address + 1) >> 7);	// get TDO data
	}
	else if (CableType == Insight_Jtag)
	{
		// There's some bit clearing here that isn't needed. It should make this 
		// code easier to understand.

		//defines for the INSIGHT IJC-1 JTAG cable

		/* the output port (lpt_address) */
		#define INSIGHT_DIN		0x01
		#define INSIGHT_CLK		0x02
		#define INSIGHT_TMS_IN  0x04  
		
		/* Output Enable for the standard JTAG outputs 
		(not TDO since this is an output from the 
		chip we want to talk to */
		#define nINSIGHT_CTRL	0x08
		#define nINSIGHT_PROG	0x10  /* This causes the TDO line to be driven. We'll leave it high.*/


		/*the input port (lpt_address + 1)*/
		#define TDO_INPUT			0x10
		#define TDO_INPUT_BITPOS	4

		int lpt_data;

		//form the data we want to write to the parallel port
		lpt_data = nINSIGHT_PROG;   //Output to TDO off

⌨️ 快捷键说明

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