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

📄 jflash.cpp

📁 非常经典的用并口作为jtag工具的源码。首先是需要装入并口的驱动
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//JFLASH V1.2
#define VERSION "1.2"
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include "sa1110jtag.h"

#define LPT1 0x3bc	// hardware base address for parallel port
#define LPT2 0x378	// the search order is LPT1 then 2 then 3
#define LPT3 0x278	// first valid address found is used (re-order if needed for multiple ports)

#define SA1110ID  "**** 1001001001100001 00000001001 1"	// JTAG ID-codes for the SA-1110

#define READ 0		// Flags used to modify the SA-1110 JTAG chain data depending on
#define WRITE 1		// the access mode of the Flash Memory
#define SETUP 2
#define HOLD 3
#define RS 4

#define IP 0		// Flag used when accessing the parallel port
#define RP 1		// RP = 'read port', IP = 'ignore port', using IP will speed access

#define MAX_IN_LENGTH 100 // max length for user input strings
#define STATUS_UPDATE 2	// time between updates of program/verify status in seconds

int lpt_address;	// Global variable assigned to parallel port address

FILE *in_file;
int putp(int,int, int);  // writes the JTAG data on the parallel port
void id_command(void);	// issues the JTAG command to read the device ID for all 3 chips
void bypass_all(void);	// issues the JTAG command to put all 3 device in bypass mode
void extest(void);		// issues the JTAG command EXTEST to the SA-1110
DWORD access_rom(int, DWORD, DWORD, int);	// Passes read/write/setup data for the Flash memory
DWORD access_bus(int, DWORD, DWORD, int);	// Read/write access to the SA-1110 pins
int test_port(void);	// Looks for and finds a valid parallel port address
int check_id(char*);	// Compares the device IDs for the 3 JTAG chips to expected values
void error_out(char*);	// Prints error and exits program
void erase_flash(DWORD, DWORD, DWORD, DWORD, int);
void program_flash(DWORD, DWORD, DWORD);
void verify_flash(DWORD, DWORD);
void test_logic_reset(void);
void test_lock_flash(DWORD, DWORD, DWORD, DWORD, int);
void set_lock_flash(DWORD, DWORD, DWORD, DWORD, int);

void mywait(unsigned int timernum)
{
	unsigned int ii;
	unsigned int bb;
	for(ii=0;ii<timernum;ii++)
	{
		bb=bb+1;
	}
}
void myTestPin()
{
	char cc;
	access_rom(SETUP, 0, 1L, IP); // clear status register
	printf("1");
	scanf("%c",&cc);

	access_rom(SETUP, 1, 0L, IP); // clear status register
	printf("2");
	scanf("%c",&cc);

	access_rom(WRITE, 0, 1L, IP);
	printf("3");
	scanf("%c",&cc);

	access_rom(HOLD, 1, 0L, IP);
	printf("4");
	scanf("%c",&cc);

	access_rom(HOLD, 0, 1L, IP);
	printf("5");
	scanf("%c",&cc);

	access_rom(HOLD, 1, 0L, IP);
	printf("6");
	scanf("%c",&cc);

	access_rom(HOLD, 0, 0x1L, IP);
	printf("7");
	scanf("%c",&cc);

	access_rom(HOLD, 1, 0x0L, IP);
	printf("8");
	scanf("%c",&cc);

	access_rom(HOLD, 0, 0x1L, IP);
	printf("9");
	scanf("%c",&cc);

	access_rom(HOLD, 1, 0x0L, IP);
	printf("a");
	scanf("%c",&cc);

	access_rom(HOLD, 0, 0x1L, IP);
	printf("b");
	scanf("%c",&cc);

	access_rom(HOLD, 1, 0x0L, IP);
	printf("c");
	scanf("%c",&cc);

	access_rom(HOLD, 0, 0x1L, IP);
	printf("d");
	scanf("%c",&cc);
}

void main( int argc, char *argv[] )
{
	time_t start;
	DWORD temp;
	printf("JFLASH Version %s\n",VERSION);

//Test operating system, if WinNT or Win2000 then get device driver handle
	OSVERSIONINFO osvi;
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	GetVersionEx(&osvi);
	if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
		{
		HANDLE h;

		h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL,
					OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		if(h == INVALID_HANDLE_VALUE)
			error_out("Couldn't access giveio device");
		CloseHandle(h);
		}

	lpt_address = test_port();	// find a valid parallel port address
	if(!lpt_address)
		error_out("Error, unable to find parallel port");

	char filename[MAX_IN_LENGTH];
	if(argc >= 2)
		strcpy(filename,argv[1]);
	else
	{
		printf("enter binary file name: ");
		gets(filename);
	}

	test_logic_reset();

	id_command();

	extest();
	//myTestPin();

	access_rom(SETUP, 0, 0x500050L, IP); // clear status register
	access_rom(WRITE, 0, 0x500050L, IP);
	access_rom(HOLD, 0, 0x500050L, IP);

	access_rom(SETUP, 0, 0x980098L, IP); // read query
	access_rom(WRITE, 0, 0x980098L, IP);
	access_rom(HOLD, 0, 0x980098L, IP);

	// To read data from the Flash Memory you must first fill the SA-1110 JTAG chain
	// with the Address, then pump the entire chain out.
	// however while pumping data out you can be pumping the next cycle's Address in
	// Therefore the JTAG chain looks like a pipeline, valid read data always coming
	// out one cycle late.

	// Note that both Flash Memory devices are accessed in parallel (i.e. 32 data bits)
	temp=access_rom(READ, 0x10, 0, IP);  // Test to see if the Flash supports Query Structured Output
	//printf("   %lx",temp);

	//temp=access_rom(READ, 0x11, 0, RP);
	//printf("   %lx",temp);
	//if( temp!= 0x510051L) //Q
	if(access_rom(READ, 0x11, 0, RP) != 0x510051L) //Q
		error_out("error reading flash attribute space\ncheck cables, power and flash sockets");

	//temp=access_rom(READ, 0x12, 0, RP);
	//printf("   %lx",temp);
	//if( temp != 0x520052L) //R
	if(access_rom(READ, 0x12, 0, RP) != 0x520052L) //R
		error_out("error reading flash attribute space R");

	//temp=access_rom(READ, 0x25, 0, RP);
	//printf("   %lx",temp);
	//if(temp != 0x590059L) //Y
	if(access_rom(READ, 0x25, 0, RP) != 0x590059L) //Y
		error_out("error reading flash attribute space Y\n");



	DWORD max_erase_time = access_rom(READ, 0x27, 0, RP);  // "n" such that the max block erase time = 2^n
	if((max_erase_time & 0xffffL) != (max_erase_time >> 16)) // both devices should be the same time
		error_out("error, max erase time of E11 and E12 are different");
	max_erase_time = 1 << (max_erase_time & 0xffffL);  // convert erase time 2^n to the number of seconds
	DWORD dsize = access_rom(READ, 0x2a, 0, RP);  // "n" such that the device size = 2^n in number of bytes
	if((dsize & 0xffffL) != (dsize >> 16)) // both devices should be the same size
		error_out("error, device size of E11 and E12 are different");
	dsize = 1 << (dsize & 0xffffL);  // convert data size from 2^n to the number of bytes

	DWORD max_write_buffer = access_rom(READ, 0x2d, 0, RP);  // "n" such that the max num of bytes in write buffer = 2^n
	if((max_write_buffer & 0xffffL) != (max_write_buffer >> 16)) // both devices should have the same write buffer size
		error_out("error, write buffer size of E11 and E12 are different");
	max_write_buffer = (1 << (max_write_buffer & 0xffffL)) / 2;  // convert from 2^n bytes to the number of words

	int nblocks = access_rom(READ, 0x2e, 0, RP);  // get the number of erase blocks in Flash - 1
	if((nblocks & 0xffffL) != (nblocks >> 16))	// both devices should have the same number
		error_out("error, number of blocks of E11 and E12 are different");
	nblocks = (nblocks & 0xffffL) + 1L;  // need to add '1' for true number

	if( (in_file = fopen(filename, "rb" )) == NULL)
		error_out("error, can not open binary input file");

	DWORD fsize = 0;
	DWORD last_non_zero = 0 ;
	DWORD last_non_ff = 0;
	DWORD li;
	for(;;)
	{
		int n = fread((DWORD *)&li, 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 != 0 && li != 0xffffffff) // Find point in file were only 0's and ff's remain
			last_non_zero = fsize;
		if(li != 0xffffffff)  // Find point in file were only ff's remain
			last_non_ff = fsize;
	}
	
	rewind(in_file);

	if(fsize * 2 > dsize)
		error_out("error, file size is bigger than device size");
	fsize = last_non_ff;  // Don't wast time programming ff's at the end of the file this is the erase state

	int block_number = 0;
	DWORD block_size = dsize / 2 / nblocks;
	if(argc >= 4)
		block_number = atoi(argv[3]);
	DWORD base_address = block_number * block_size;

	printf("Number of blocks in device = %ld\n",nblocks);
	printf("Block size = %ld 0x%lx\n",block_size,block_size);
	printf("Device size = %ld 0x%lx\n",dsize,dsize);

	if(block_number >= nblocks || block_number < 0)
		error_out("error specifying block number");


	if(100 - (last_non_zero * 100)/last_non_ff > 20)
	{
		printf("The last %2ld percent of image file is all zeros\n",100 - (last_non_zero * 100)/last_non_ff);
		printf("Would you like to save time by not programming that area? [y/n]: ");
		if(toupper(_getche()) == 'Y')
			fsize = last_non_zero;
	}

	printf("\n");
	char option = 'P';
	if(argc >= 3)
		option = toupper(*argv[2]);

	if(option == 'P')
	{

		test_lock_flash(base_address, fsize, block_size, max_erase_time, block_number);
		erase_flash(base_address, fsize, block_size, max_erase_time, block_number);
		program_flash(max_write_buffer, base_address, fsize);
	}

	access_rom(SETUP, 0, 0xff00ffL, IP); // put back into read mode
	access_rom(WRITE, 0, 0xff00ffL, IP);
	access_rom(HOLD, 0, 0xff00ffL, IP);

	access_rom(READ, base_address, 0x0L, IP); //extra read to get the pipeline going

	time(&start);

	verify_flash(base_address, fsize);

	fclose(in_file);

	test_logic_reset();
}

int putp(int tdi, int tms, int rp)
{
// 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
	
	int tdo = -1;
	_outp(lpt_address, tms*4+tdi*2);	// TCK low
	//mywait(10);
	_outp(lpt_address, tms*4+tdi*2+1);	// TCK high
	//mywait(10);
	if(rp == RP)_outp(lpt_address, tms*4+tdi*2);	// TCK low
	//mywait(10);
	if(rp == RP)tdo = !((int)_inp(lpt_address + 1) >> 7);	// get TDO data
	return tdo;

}

void id_command(void)
{
	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);
	putp(1,1,IP);	//select IR scan
	putp(1,0,IP);	//capture IR
	putp(1,0,IP);	//shift IR

	putp(0,0,IP);	//
	putp(1,0,IP);	//
	putp(1,0,IP);	//
	putp(0,0,IP);	//
	putp(0,1,IP);	//Exit1-IR

	putp(1,1,IP);	//Update-IR
	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);
	putp(1,0,IP);

	if(check_id(SA1110ID))
		error_out("failed to read device ID for the SA-1110");

	putp(1,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
}

void bypass_all(void)
{
	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);
	putp(1,1,IP);	//select IR scan
	putp(1,0,IP);	//capture IR
	putp(1,0,IP);	//shift IR

	putp(1,0,IP);	//SA1110 BYPASS
	putp(1,0,IP);	//
	putp(1,0,IP);	//
	putp(1,0,IP);	//
	putp(1,1,IP);	//Exit1-IR

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

}

void extest(void)
{
	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);
	putp(1,1,IP);	//select IR scan
	putp(1,0,IP);	//capture IR
	putp(1,0,IP);	//shift IR

	putp(0,0,IP);	//SA1110 extest
	putp(0,0,IP);	//
	putp(0,0,IP);	//
	putp(0,0,IP);	//
	putp(0,1,IP);	//Exit1-IR

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

}

DWORD access_rom(int rw, DWORD address, DWORD data, int rp)
{
	// Shift Flash address making A2 the LSB
	return(access_bus(rw, address << 2, data, rp));
}
DWORD access_bus1(int rw, DWORD address, DWORD data, int rp)

⌨️ 快捷键说明

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