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

📄 jflash.cpp

📁 jtag工具
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//JFLASH V1.2
#define VERSION "1.2"
#include <stdio.h>
#include <time.h>
#ifndef __linux__
#include <windows.h>
#include <conio.h>
#else
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <sys/io.h>
#include <errno.h>
#define _getche getchar
#define BOOL	int
#define FALSE	0
#define TRUE	1
#endif
#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 PZ3128ID  "**** 0000000010001100 00000010101 1"	// and the 2 CPLD also in the chain
//#define PZ3032ID  "0000 0000000000011010 00000010101 1"	// contains version, part, and vendor codes
//#define PZ3032AID "0000 0000100000001010 00000010101 1"
#define PZ3032SID "**** 0000*000000*1010 00000010101 1"	// support both the 'A' and non 'A' versions of the part


#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;

#ifdef __linux__

/* Some types */
typedef long DWORD;

/* in/out macros */
#include <asm/io.h>
#define _inp(a) inb(a)
#define _outp(a,d) outb(d,a)

#endif

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 main( int argc, char *argv[] )
{
	time_t start;
	unsigned int flash_test, i;
	
	printf("JFLASH Version %s\n",VERSION);

#ifndef __linux__
//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);
		}
#endif

	lpt_address = test_port();	// find a valid parallel port address
	if(!lpt_address)
		error_out("Error, unable to find parallel port");
	printf("Using LPT port at 0x%x\n", lpt_address);

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

	test_logic_reset();

	id_command();
	bypass_all();


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

/*	flash_test = access_rom(READ, 0x00, 0, RP);
	printf("flash = %08x\n", flash_test);
*/	/*printf( " read 0x10 =%08x\n",access_rom(READ, 0x10, 0, IP));  // Test to see if the Flash supports Query Structured Output
	flash_test = access_rom(READ, 0x11, 0, RP);
	printf("flash = %08x\n", flash_test);*/
	
	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)

	/*printf( " read 0x10 =%08x\n",access_rom(READ, 0x10, 0, IP));  // Test to see if the Flash supports Query Structured Output
	flash_test = access_rom(READ, 0x11, 0, RP);
	printf("flash = %08x\n", flash_test);
	for( i=0; i<20; i++)
	{
		flash_test = access_rom(READ, 0x11, 0, RP);
		printf("flash = %08x\n", flash_test);
	}
	flash_test = access_rom(READ, 0x13, 0, RP);
	printf("flash = %08x\n", flash_test);

	flash_test = access_rom(READ, 0x14, 0, RP);
	printf("flash = %08x\n", flash_test);

	flash_test = access_rom(READ, 0x15, 0, RP);
	printf("flash = %08x\n", flash_test);

	flash_test = access_rom(READ, 0x16, 0, RP);
	printf("flash = %08x\n", flash_test);

	flash_test = access_rom(READ, 0x17, 0, RP);
	printf("flash = %08x\n", flash_test);

	flash_test = access_rom(READ, 0x18, 0, RP);
	printf("flash = %08x\n", flash_test);*/
	

	access_rom(READ, 0x10, 0, IP);
	if(access_rom(READ, 0x11, 0, RP) != 0x510051L)  //Q
		error_out("error reading flash attribute space\ncheck cables, power and flash sockets");
	if(access_rom(READ, 0x12, 0, RP) != 0x520052L) //R
		error_out("error reading flash attribute space R");
	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
	printf("Number of blocks in device = %08x\n",nblocks);
	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
	_outp(lpt_address, tms*4+tdi*2+1);	// TCK high
	if(rp == RP)_outp(lpt_address, tms*4+tdi*2);	// TCK low
	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,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 IDCODE
	putp(1,0,IP);	//
	putp(1,0,IP);	//
	putp(0,0,IP);	//
	putp(0,1,IP);	//

//	putp(1,0,IP);	//pz3128 IDCODE
//	putp(0,0,IP);	//
//	putp(0,0,IP);	//
//	putp(0,0,IP);	//

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

	if(check_id(SA1110ID))
		error_out("failed to read device ID for the SA-1110");
//	if(check_id(PZ3128ID))
//		error_out("failed to read device ID for the PZ3128");
//	if(check_id(PZ3032ID))
//		error_out("failed to read device ID for the PZ3032");

	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);	//

//	putp(1,0,IP);	//pz3128 BYPASS
//	putp(1,0,IP);	//
//	putp(1,0,IP);	//
//	putp(1,0,IP);	//

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

	//putp(1,0,IP);	//pz3128 BYPASS
	//putp(1,0,IP);	//
	//putp(1,0,IP);	//
	//putp(1,0,IP);	//

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

}

⌨️ 快捷键说明

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