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

📄 jflash.cpp

📁 pxa255用的并口烧写jtag口的源代码。可以修改为其他的cpu等下载工具
💻 CPP
📖 第 1 页 / 共 5 页
字号:

    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 - returned 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");

		mem_output_enable(DISABLE);
        mem_data_driver(DRIVE);
		set_data(data);
	}
    //----------------------------------------------
	else if(rw == RS)	// setup prior to RD_nWR_OUT
	{
		if(Debug_Mode)
        	printf("RS Mode\n");

        mem_output_enable(DISABLE);
	}
	else if(rw == K3)
		clear_chip_selects();

    // Common finish

    putp(1,0,IP);	//Run-Test/Idle
	putp(1,0,IP);	//Run-Test/Idle
	putp(1,0,IP);	//Run-Test/Idle
	putp(1,0,IP);	//Run-Test/Idle
	putp(1,1,IP);	//select DR scan
	putp(1,0,IP);	//capture DR
	putp(1,0,IP);	//shift IR ---> (Rami: should be DR?)


	int out_dat[MAX_CHAIN_LENGTH];

	for(device = DEVICES_IN_CHAIN -1 ; device >= 0; device--)
	{
		if(device == DEVICE_CONTROLLER)
		{
			for(i = 1; i < ChainLength; i++)	// shift write data in to JTAG port and read data out
				out_dat[i] = putp(pin[i-1],0,rp);
//			for(i = 0; i < ChainLength; i++)	// shift write data in to JTAG port and read data out
//				out_dat[i] = putp(pin[i],0,rp);

		}
		else
		{
			putp(0,0,IP); // extra clicks for devices in the chain
		} 
	}

#ifdef serious_error
    if(Debug_Mode)
    {
		for(i = 1; i < ChainLength; i++)	
		{
    		if(rw == READ)
			{
				if(i%30 == 0) printf("\n");
				printf(" %d", out_dat[i]);
			}
		}
		printf("\n");
	}
#endif

	putp(0,1,IP);	//Exit1-DR
	putp(1,1,IP);	//Update-DR
	putp(1,0,IP);	//Run-Test/Idle
	putp(1,0,IP);	//Run-Test/Idle
	putp(1,0,IP);	//Run-Test/Idle

	DWORD busdat = 0;

	for(i = 0; i < 32; i++)	// convert serial data to single DWORD
		busdat = busdat | ((DWORD)(out_dat[input_dat_order[i]] << i));
    
    //extest();
	IR_Command(IR_Extest);

	if(Debug_Mode)
	{
    printf("just before return\n");
    dump_chain();
	}

    return(busdat);
}

/*
*******************************************************************************
*
* FUNCTION:         test_port
*
* DESCRIPTION:      Searches for a valid parallel port
*
* INPUT PARAMETERS: void
*
* RETURNS:          int - Address of the port or zero if none available
*
*******************************************************************************
*/

int test_port(void)
{
	// search for valid parallel port
		_outp(LPT1, 0x55);
		if((int)_inp(LPT1) == 0x55)
        {
            if(Debug_Mode)
            	printf("Parallel Com port found at I/O address: %X\n", LPT1);

            return LPT1;
        }

		_outp(LPT2, 0x55);
		if((int)_inp(LPT2) == 0x55)
        {
            if(Debug_Mode)
            	printf("Parallel Com port found at I/O address: %X\n", LPT2);
           
            return LPT2;
		}
 
        _outp(LPT3, 0x55);
		if((int)_inp(LPT3) == 0x55)
        {
            if(Debug_Mode)
            	printf("Parallel Com port found at I/O address: %X\n", LPT3);
            
            return LPT3;
        }
        
	return(0);	// return zero if none found
}
/*
*******************************************************************************
*
* FUNCTION:         check_id
*
* DESCRIPTION:      Compare an ID string returned from the device with the expected string.
*
* INPUT PARAMETERS: char * device_id - a pointer to the string returned from the device
*
* RETURNS:          int - 0 if ID matches expected, -1 if a match fails
*
*******************************************************************************
*/

int check_id(char *device_id)
{
	// compare passed device ID to the one returned from the ID command
	char in_id[40];
	BOOL error_flag = FALSE;

	if(DEVICES_IN_CHAIN > 1)
		putp(1,0,IGNORE_PORT); // this is a bug fudge factor - look into this
	
	
	for(int i = 34; i >= 0; i--)
	{
		// skip over the spaces in the ID string
        if(i == 4 || i == 21 || i == 33)
		{
			in_id[i] = ' ';
			i--;
		}
        
		if(putp(1,0,READ_PORT) == 0)
			in_id[i] = '0';
		else
			in_id[i] = '1';

		if((in_id[i] != *(device_id + i)) && (*(device_id + i) != '*'))
		{
			error_flag = TRUE;
			
		}
	}
	in_id[35] = 0;

	
	if(error_flag)
	{
		printf("error, failed to read device ID\n");
		printf("check cables and power\n");
		printf("ACT: %s\n",in_id);
		printf("EXP: %s\n\n",device_id);
		return -1;
	}

	int revision =	(int)(in_id[0] - '0') * 8 +
					(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
*

⌨️ 快捷键说明

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