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

📄 jflash.cpp

📁 s3c44b0 + 29lv160 jtag下载程序编写
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#define VERSION "0.1"
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <math.h>
#include "44b0jtag.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 S44B0ID  "**** 1111000011110000 11110000111 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 RD 0
#define WR 1
#define ER 2
#define PR 3
#define UP 4

#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;
FILE *out_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
WORD access_rom(int, DWORD, WORD, int);	// Passes read/write/setup data for the Flash memory
WORD access_bus(int, DWORD, WORD, 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 write_test(void);
void write_flash(void);
void flash_reset(void);
int flash_write_word(DWORD,WORD);
void read_flash_info(void);
void read_query(void);
void unlock_bypass(void);
void unlock_bypass_reset(void);
int unlock_bypass_write_word(DWORD,WORD);
int erase_sector(DWORD,WORD);
int erase_chip(void);
int write_command(DWORD,WORD);

int write_command(DWORD addr, WORD command)
{
	access_rom(SETUP, addr, command, IP); 
	access_rom(WRITE, addr, command, IP);
	access_rom(HOLD, addr, command, IP);
	return 0;
}
int erase_sector(DWORD sec,WORD max_erase_time)
{
	time_t start, now;

//	printf("Starting erase\n");
	write_command(0x555, 0xaa); 
	write_command(0x2aa, 0x55); 
	write_command(0x555, 0x80); 
	write_command(0x555, 0xaa); 
	write_command(0x2aa, 0x55); 
	write_command(sec,0x30);
		time(&start);
		printf("Erasing block at address: %#5x   ........\r",sec);
	for(;;)
	{
		time(&now);
		if(difftime(now,start) > max_erase_time + 1)	// Check for erase timeout
			break;	
	}
#if 0
	while((access_rom(RS, 0, 0, RP)&0x0008) != 0)	// Loop until successful status return
	{
		access_rom(READ, 0, 0, RP);
		time(&now);
		if(difftime(now,start) > max_erase_time + 1)	// Check for erase timeout
			error_out("Error, Block erase timed out               ");
		//break;	
	}
#endif

	printf("Erasing block %#5x done                                           \n",sec);
//	for(int i=0;i<18800000L;i++);
	return 0;
}

int erase_chip()
{
	write_command(0x555, 0xaa); 
	write_command(0x2aa, 0x55); 
	write_command(0x555, 0x80); 
	write_command(0x555, 0xaa); 
	write_command(0x2aa, 0x55); 
	write_command(0x555, 0x10); 

	return 0;
}

void unlock_bypass()
{
	write_command(0x555, 0xaa); 
	write_command(0x2aa, 0x55); 
	write_command(0x555, 0x20); 
	return;
}

int unlock_bypass_write_word(DWORD addr, WORD data)
{
	write_command(0x555, 0xa0); 
	write_command(addr, data); 
	return 0;
}

void unlock_bypass_reset()
{
	write_command(0x555, 0x90); 
	write_command(0x2aa, 0x00); 
	return;
}

int flash_write_word(DWORD addr, WORD data)
{
	write_command(0x555, 0xaa); 
	write_command(0x2aa, 0x55); 
	write_command(0x555, 0xa0); 
	write_command(addr, data); 
	return 0;
}

void read_flash_info()
{	
	WORD readword = 0;
	write_command(0x555, 0xaa); 
	write_command(0x2aa, 0x55); 
	write_command(0x555, 0x90); 

	access_rom(READ, 0x00, 0, IP);
	readword = access_rom(READ, 0x00, 0, RP);
	printf(" The manufacture ID: %x\n", readword);
	access_rom(READ, 0x01, 0, IP);
	readword = access_rom(READ, 0x01, 0, RP);
	printf(" The Device ID: %x\n", readword);
	return;
}

void flash_reset()
{
	access_rom(SETUP, 0x0, 0xf0, IP); 
	access_rom(WRITE, 0x0, 0xf0, IP);
	access_rom(HOLD, 0x0, 0xf0, IP);
	return;
}

DWORD atoh(char* str)
{
	int hex_data = 0;
	int len = strlen(str);
	if(len > 5)
		error_out(" error address length!!\n");
	for(int i = 0; i < len; i++)
	{
		if((str[i]>='a') && (str[i] <='f'))
			hex_data = hex_data + (str[i]-87)*((int)pow(16,len-i-1));
		else if((str[i]>='0') && (str[i] <= '9'))
			hex_data = hex_data + (str[i]-48)*((int)pow(16,len-i-1));
		else
			error_out("error address value!!\n");
	}
	return hex_data;
}

void read_query()
{

}
void print_help()
{
	printf(" This is help information!!\n");
}

void main( int argc, char *argv[] )
{
//	time_t start;
	int i =0;
	int op_flag = 0;
	
	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");
	/*  parse the option  */
	int out2file = 0, rd_count = 0;
	DWORD sec_addr0 = 0, sec_addr1 = 0, rd_addr0 = 0, rd_addr1 = 0x20, wr_addr = 0;
	char in_filename[MAX_IN_LENGTH];
	char out_filename[MAX_IN_LENGTH];

	if(argc >= 2)
	{
		if(*argv[1] == 'w')
		{
			if(argc < 4)
				error_out(" input parameters are too few!!!");
			op_flag = WR;
			wr_addr = atoh(argv[2]);
			strcpy(in_filename,argv[3]);
		}
		else if(*argv[1] == 'e')
		{
			if(argc < 3)
				error_out(" input parameters are too few!!!");
			op_flag = ER;
			if(argc >= 3)
			{
				sec_addr0 = atoh(argv[2]);
				sec_addr1 = atoh(argv[2]);
			}
			if(argc >=4 )
				sec_addr1 = atoh(argv[3]);
			
		}
		else if(*argv[1] == 'r')
		{
			op_flag = RD;
			rd_count = 0x20;

			if(argc >= 3)
			{
				rd_addr0 = atoh(argv[2]);
				rd_addr1 = rd_addr0 + rd_count;
				if(argc >= 4)
				{
					rd_addr1 = atoh(argv[3]);
					rd_count = rd_addr1 - rd_addr0 + 1;
					if(rd_count < 0)
						error_out(" rd_addr1 < rd_addr0!!\n");
				}
			}
			if(argc >= 5)
			{
				out2file = 1;
				strcpy(out_filename, argv[4]);
			}
		}
		else if(*argv[1] == 'h')
		{
			print_help();
			exit(0);
		}
		else
			error_out(" Error option!!\n Please show the help use: jflash h\n");
	}
	else
	{
		op_flag = RD;
		rd_count = 0x20;
	}


//		gets(filename);
	test_logic_reset();
	id_command();
//	bypass_all();
	extest();
	read_flash_info();
	WORD readword = 0;
	write_command(0x55, 0x0098); // read query
	access_rom(READ, 0x10, 0x0, RP);
	readword = access_rom(READ, 0x11, 0x0, RP);
	printf(" readword:%x\n",readword);
	readword = access_rom(READ, 0x12, 0x0, RP);
	printf(" readword:%x\n",readword);
	readword = access_rom(READ, 0x25, 0x0, RP);
	printf(" readword:%x\n",readword);

	WORD max_erase_time = access_rom(READ, 0x27, 0, RP);  // "n" such that the max block erase time = 2^n
//	printf(" max_erase_time:%x\n",max_erase_time);
	max_erase_time = 1 << (max_erase_time & 0xffffL);  // convert erase time 2^n to the number of seconds
	printf(" max_erase_time:%x\n",max_erase_time);
	WORD dsize = access_rom(READ, 0x2a, 0, RP);  // "n" such that the device size = 2^n in number of bytes
	dsize = 1 << (dsize & 0xffffL);  // convert data size from 2^n to the number of bytes
	WORD max_write_buffer = access_rom(READ, 0x2f, 0, RP);  // "n" such that the max num of bytes in write buffer = 2^n
	max_write_buffer = (1 << (max_write_buffer & 0xffffL)) / 2;  // convert from 2^n bytes to the number of words
	int nblocks = access_rom(READ, 0x43, 0, RP);  // get the number of erase blocks in Flash - 1
	nblocks = (nblocks & 0xffffL) + 1L;  // need to add '1' for true number
	printf("\n\n");
	if(op_flag == RD)
	{
		flash_reset();
		WORD data_buffer[4096];
		if(!out2file)
		{
			printf(" %#x -- %#x\n",rd_addr0,rd_addr1);
			printf("           00    01    02    03    04    05    06    07\n");
			printf("        ===============================================");
		}
		else
		{
			if((out_file = fopen(out_filename, "wb" )) == NULL)
				error_out("error, can not create binary input file");
		}

		access_rom(READ,rd_addr0,0,RP);
		int j = 0, file_size=0;
		for(i = 0; i < rd_count; i++)
		{
			if((!out2file) && (!(i%8)))
				printf("\n%5x##",i);
			readword = access_rom(READ,(i+rd_addr0+1),0,RP);
			if(out2file)
			{

				data_buffer[j] = readword;
				j++;
				if(!(file_size%1024))
					printf(" File size: %dk\r",file_size/1024);
				file_size++;

				if((j == 4096) || (i == (rd_count -1)))
				{
			//		printf(" file size:%dk\n",j/1024);
					fwrite(data_buffer,2,j,out_file);
					j = 0;
				}
			}
			if(!out2file)
				printf(" %4xh",readword);
		}
		printf("\n");
	}

	else if(op_flag == WR)
	{
		WORD data;
		int j = 0, file_size=0;
		DWORD add = wr_addr;

		flash_reset();
		if( (in_file = fopen(in_filename, "rb" )) == NULL)
			error_out("error, can not open binary input file");
		printf(" write flash..  (words)\n");
		unlock_bypass();

		for(;;)
		{
			if(!(file_size%1024))
				printf(" File size: %dk\r",file_size/1024);
			fread((WORD *)&data, sizeof(WORD) , 1, in_file);
	//		printf(" add:%x, data:%x\n",add,data);
			unlock_bypass_write_word(add,data);
			if(feof(in_file))break; 
			add++;
			file_size++;
		}
		unlock_bypass_reset();
		fclose(in_file);
	}

	else if(op_flag ==ER)
	{
		DWORD off = 0;
		flash_reset();
		printf(" erase address: %#x --> %#x\n", sec_addr0, sec_addr1);
		for(DWORD add = sec_addr0; add <= sec_addr1; )
		{
		//	printf("   ## add:%#x\n",add);
			max_erase_time = 0x1;
			erase_sector(add,max_erase_time);
			if(add == 0)
				off = 0x02000;
			else if((add >= 0x02000) &&(add < 0x04000))
				off = 0x01000;
			else if(add == 0x04000)
				off = 0x04000;
			else 
				off = 0x08000;
			add=add+off;
		}
	}

	else
		error_out(" Error option!!\n");


⌨️ 快捷键说明

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