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

📄 jflash.cpp

📁 老外的一个开源项目
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{	
	
	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
*
*
*					Wiggler interface...
*						LPT D0   Pin 2  .. RST   .. Arm JTAG Pin 15 nRESET
*						LPT D1   Pin 3  .. TMS   .. Arm JTAG Pin 7  TMS
*						LPT D2   Pin 4  .. TCLK  .. Arm JTAG Pin 9  TCK
*						LPT D3   Pin 5  .. TDI   .. Arm JTAG Pin 5  TDI
*						LPT D4   Pin 6  .. NTRST .. Arm JTAG Pin 3  nTRST
*						LPT BUSY Pin 11 .. TDO   .. Arm JTAG Pin 13 TDO
*						LPT D7   Pin 9  .. PORT VCC
*
* Note: Wiggler looks like it sets D7 high all the time. (sometimes this is used for power)
*       System reset signal (RST) is inverted by an open collector xistor befor driving nRESET.
*       Pay special attention to nRESET and nTRST timing!
*
* 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)
	{
//Intel		// TMS is D2, TDI is D1, and TCK is D0, so construct an output by creating a 
//Intel		// rising edge on TCK with TMS and TDI data set.
//Intel		_outp(lpt_address, tms*4+tdi*2);				// TCK low 
//Intel		_outp(lpt_address, tms*4+tdi*2+1);				// TCK high 
		// nTRST is D4 and PWR is D7 .. they should be high all the time.
		// TMS is D1, TDI is D3, and TCK is D2, so construct an output by creating a 
		// rising edge on TCK with TMS and TDI data set.
		_outp(lpt_address, tms*2+tdi*8 +128 +16);		// TCK low 
		_outp(lpt_address, tms*2+tdi*8+4 +128 +16);		// 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)
//Intel			_outp(lpt_address, tms*4+tdi*2);			// TCK low
			_outp(lpt_address, tms*2+tdi*8 +128 +16);		// 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
		lpt_data &= ~nINSIGHT_CTRL;	//Enable the outputs

		if(tms == 1) lpt_data |= INSIGHT_TMS_IN;
		if(tdi == 1) lpt_data |= INSIGHT_DIN;

   		// construct an output by creating a 
		// rising edge on TCK with TMS and TDI data set.
		lpt_data &= ~INSIGHT_CLK;
		_outp(lpt_address, lpt_data);	// TCK low

		lpt_data |= INSIGHT_CLK;
		_outp(lpt_address, lpt_data);	// 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)
		{
			lpt_data &= ~INSIGHT_CLK;
			_outp(lpt_address, lpt_data);	// TCK high ??? Low?
			tdo = ((int)_inp(lpt_address + 1) & TDO_INPUT) >> TDO_INPUT_BITPOS;	// get TDO data
		}
	}

//	#ifdef DEBUG
//    printf("TDI = %d, TMS = %d, TDO = %d\n",tdi,tms,tdo);
//  #endif

    return tdo;
}
/*
*******************************************************************************
*
* FUNCTION:         id_command
*
* DESCRIPTION:      extract and verify the id codes of the devices in the chain
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
*******************************************************************************
*/

void id_command(void)
{
    char constructed_string[256];
//    int device;
   	int bitscount = 0;
    
	IR_Command(IR_Idcode);

    pre_DRSCAN();
	
	strcpy(constructed_string, "");

	// construct the processor ID
	strcat(constructed_string, "**** ");
	strcat(constructed_string, &WORDARRAY[p_proc_id][0]);
	strcat(constructed_string, " ");
	strcat(constructed_string, &WORDARRAY[p_proc_mfg][0]);
	strcat(constructed_string, " ");
	strcat(constructed_string, &WORDARRAY[p_proc_std][0]);

	if(check_id(constructed_string))
		error_out("failed to read device ID for this Platform");
	
	DebugProgress |= foundProcID;
    post_DRSCAN();
}


/*
*******************************************************************************
*
* FUNCTION:         IR_Command
*
* DESCRIPTION:      sends an instruction to the controller and puts all other 
*                   devices into bypass mode.
*
* INPUT PARAMETERS: int	command
*
* RETURNS:          void
*
*******************************************************************************
*/

void IR_Command(int command)
{
    int device;

    pre_IRSCAN();

	for(device = DEVICES_IN_CHAIN -1 ; device >= 0; device--)
	{
		if(device == DEVICE_CONTROLLER)
		{
			if(DEVICEISLAST[device])
			{
				controller_scan_code(command, IGNORE_PORT, TERMINATE);
			}
			else // controller is not the last in the chain
			{
				controller_scan_code(command, IGNORE_PORT, CONTINUE);
			}
		}
		else
		{
			if(DEVICEISLAST[device])
			{
				    other_bypass(IGNORE_PORT, TERMINATE, (int)DEVICEIRLENGTH[device]);
			}
			else 
			{
				    other_bypass(IGNORE_PORT, CONTINUE, (int)DEVICEIRLENGTH[device]);
			}
		} 
	}

    post_IRSCAN();

}


/*
*******************************************************************************
*
* FUNCTION:         access_rom
*
* DESCRIPTION:      This is just an access-bus with the address multiplied by 4
*
* INPUT PARAMETERS: int - rw, or access mode
*                   DWORD - address
*                   DWORD - data
*                   int rp - whether to read or ignore the parallel port
*
* RETURNS:          DWORD - returned data
*
*******************************************************************************
*/
DWORD access_rom(int rw, DWORD address, DWORD data, int rp)
{
	DWORD returnvalue;
    DWORD HalfData = data & FIRST_HALF_WORD_MASK;	// Get the rightmost half of the word only

    // Shift Flash address making A2 the LSB
    
	if(Debug_Mode)
	{
	   	if(PlatformIs16bit)
		 printf("ACCESS_ROM: inp addr = %X, inp HalfData = %X\n", address, HalfData);
		else
		 printf("ACCESS_ROM: inp addr = %X, inp data = %X\n", address, data);
    }

	if(PlatformIs16bit)
		returnvalue = (access_bus(rw, address << 1, HalfData, rp) & FIRST_HALF_WORD_MASK);
    else
		returnvalue = access_bus(rw, address << 2, data, rp);

    if(Debug_Mode)
    	printf("ACCESS_ROM Returns %X\n", returnvalue);

    return returnvalue;
}

/*
*******************************************************************************
*
* FUNCTION:         Write_Rom
*
* DESCRIPTION:      This routine manipulates the memory bus to do reads 
*                   and writes to any memory location.
*
* INPUT PARAMETERS: int address - address to which you need to write to rom
*					int data    - data you need to write to rom
*
* RETURNS:          none
*
*******************************************************************************
*/
void Write_Rom(DWORD address, DWORD data)
{
	if(Debug_Mode)
		printf("Writing to ROM ...\n");

	access_rom(SETUP, address, data, IGNORE_PORT);
	access_rom(WRITE, address, data, IGNORE_PORT);
	access_rom(HOLD, address, data, IGNORE_PORT);
}

/*
*******************************************************************************
*
* FUNCTION:         Read_Rom
*
* DESCRIPTION:      This routine uses access_rom to read from rom
*                   
*
* INPUT PARAMETERS: int address - address from which you need to read from rom
*
* RETURNS:          DWORD - data read
*
*******************************************************************************
*/

DWORD Read_Rom(DWORD address)
{
	if(Debug_Mode)
		printf("Reading from ROM ...\n");
		
		return access_rom(READ, address, 0, READ_PORT);
}


/*
*******************************************************************************
*
* FUNCTION:         access_bus
*
* DESCRIPTION:      This routine manipulates the memory bus to do reads 
*                   and writes to any memory location.
*
* INPUT PARAMETERS: int rw - mode of READ, WRITE, SETUP, HOLD, or RS
*                   DWORD - address of access
*                   DWORD - data to write
*                   int rp - read or ignore port data
*
* RETURNS:          DWORD - retturned data
*
*******************************************************************************
*/


DWORD access_bus(int rw, DWORD address, DWORD data, int rp)
{
	DWORD i;
//	int j;
	int device;

	// Preset SA-1110 or Cotulla pins to default values (all others set in Cotullajtag.h)
    
    clear_chip_selects();
	mem_output_enable(ENABLE);
	mem_write_enable(DISABLE);
	mem_rw_mode(WRITE);
    mem_data_driver(HIZ);
    set_address(address);

    //----------------------------------------------
    if(rw == READ)
	{
		if(Debug_Mode)
        	printf("Read Mode\n");

		mem_rw_mode(READ);
        set_pin_chip_select(address);
	}

    //----------------------------------------------
	else if(rw == WRITE)
	{
		if(Debug_Mode)
        	printf("Write Mode\n");

        mem_write_enable(ENABLE);
		mem_output_enable(DISABLE);
        mem_data_driver(DRIVE);
        set_pin_chip_select(address);
        set_data(data);
	}
    
    //----------------------------------------------
	else if(rw == SETUP || rw == HOLD)	// just like a write except WE, WE needs setup time
	{
		if(Debug_Mode)
        	printf("Setup or Hold Mode\n");

⌨️ 快捷键说明

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