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

📄 jflash.cpp

📁 ARM7202上通过JTAG烧录Flash的工具源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*******************************************************************************
* FileName	:   jflash.cpp
* Author    :   lokwon.kim@hynix.com
* Data   	:   2004.03.10
* Modify	:	armblogger@gmail.com , http://armblogger.blogchina.com
* History	:   
2005-07-29 9:39	增加了针对ARM7202开发板的LED测试功能
2005-08-02 9:41	能够成功读取29LV160 ID
	
* Desc		:   通过JTAG烧写HMS30C720X开发板上的FLASH,官方发布的版本是烧写
				INTEL 28FXXX系列FLASH的,此版本针对AM29LVXXX些列,可以烧写的
				FLASH列表如下:
					AM29LV160BT
					MX29LV160BB
					HY29LV160BT
*******************************************************************************/
//JFLASH V1.2
#define VERSION "1.2"

#include <stdio.h>
#include <afx.h>
#include <conio.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>

//#include "EP7312.h"
#include "jflash.h"



#define VIEW_ON		1
#define VIEW_OFF	0

unsigned int LedTest    = FALSE;
unsigned int VerifyTest = FALSE;
unsigned int DUMPTest   = FALSE;
unsigned int FlashErase = FALSE;
unsigned int FlashID	= FALSE;
unsigned int HelpView	= FALSE;
unsigned int chip = FALSE;
unsigned int bus32 = FALSE;

char *GFilename = NULL;
unsigned int GFlashOffset = 0;
unsigned int GFlashSize   = 0;
unsigned int GFlashStartAddress =0;

unsigned int Context[16];


//******************************************************************************
// 2005-08-01 16:10

// Because S3C44B0X is connected to AM29LV800B,
// the addr parameter has to be a WORD address, so called in AMD specification.

#define _RESET()			Flash_Write_Halfword(0, 0xf0f0)		// 29lv160 reset命令
#define BADDR2WADDR(addr)  	(addr>>1)
#define BIOS_START			0x0

static unsigned int targetAddress;
static unsigned int targetSize;

#define TOP_BOOT	1
#define BOTTOM_BOOT	2

static int BootMode;

unsigned int WriteFileToFlash(char *filename, unsigned int vaddress);
void Program_29LV160(unsigned int downloadAddress, unsigned int downloadProgramSize, unsigned int targetAddress);
static int Flash_Write(unsigned int realAddr,unsigned int data);
static void Flash_EraseChip(void);
static void Flash_EraseSector(int targetAddr);
static int Flash_CheckId(void);
static int Flash_BlankCheck(int targetAddr);
static int _WAIT(void);
static void InputTargetAddr(void);
void FlashDump(int targetAddr, int size);
void TestLed(void);


/*******************************************************************************
 *	LPT Opration
 */
#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)
int lpt_address;	// Global variable assigned to parallel port address
//LPT 器飘甫 茫绰促. 
int test_port(void)
{
// search for valid parallel port

_outpw(LPT1, 0xff77);

	if((int)_inp(LPT1) == 0x77)
		return LPT1;

_outpw(LPT2, 0xff77);

	if((int)_inp(LPT2) == 0x77)
		return LPT2;

_outpw(LPT3, 0xff77);

	if((int)_inp(LPT3) == 0x77)
		return LPT3;

return(0);	// return zero if none found
}

//俊矾 皋矫瘤甫 免仿窍绊 辆丰茄促. 
void error_out(char *error_string)
{
	printf("%s\n",error_string);
	Sleep(1000);
	exit(0);
}

//nt 拌凯俊辑绰 流立 菩矾凡 器飘俊 佬绊 静绰 巴阑 倾侩窍瘤 臼绰促. 
//菩矾凡 器飘甫 荤侩且荐 乐档废 靛扼捞滚啊 牢胶喷 登绢乐绰瘤 咯何甫 
//八荤茄促. 
void TestGiveIODriver()
{
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);

			//giveio 啊 汲摹登瘤 臼篮 版快 
			//汲摹秦霖促. 
			if(h == INVALID_HANDLE_VALUE)
				{
				char szPath[255];
				GetCurrentDirectory(sizeof(szPath), szPath);
			
				CString strCommand;
				strCommand.Format("instdrv giveio %s\\giveio.sys", szPath);
				WinExec(strCommand, SW_HIDE);
				Sleep(500);
				TestGiveIODriver();
				return;
		 		}

		CloseHandle(h);
		}
}

//菩矾凡 器飘甫 茫绰促. 
void FoundLPT()
{
lpt_address = test_port();	// find a valid parallel port address

	if(!lpt_address)
		error_out("Error, unable to find parallel port\n");
	else
		printf("\n> Found printer port & GIVEIO \n> Port Num : 0x%x  \n", lpt_address);
}

unsigned int CheckParam(int argc, char* argv[])
{
int lp;
   
	for(lp = 1; lp < argc; lp++)
   		{
	  		if(strcmp("10", argv[lp]) == 0) 
      				{
          			chip = TRUE;
          			continue;
      				}

	  		if(strcmp("bus32", argv[lp]) == 0) 
      				{
          			bus32 = TRUE;
          			continue;
      				}			

	  		if(strcmp("-h", argv[lp]) == 0) 
      				{
          			HelpView = TRUE;
          			continue;
      				}
			
      			if(strcmp("-l", argv[lp]) == 0) 
      				{
          			LedTest = TRUE;
          			continue;
      				}
				
      			if(strcmp("-v", argv[lp]) == 0) 
      				{
          			VerifyTest = TRUE;
          			continue;
      				}
				
      			if(strcmp("-i", argv[lp]) == 0) 
      				{
         			FlashID = TRUE;
          			continue;
      				}

      			if(strcmp("-e", argv[lp]) == 0) 
      				{
          			FlashErase = TRUE;

		  			if((lp + 1) > argc) return FALSE;

		  		sscanf(argv[lp+1], "%xl", &GFlashSize);
          			lp++; 
          			continue;
      				}
      
      			if(strcmp("-d", argv[lp]) == 0) 
      				{
          			DUMPTest = TRUE;

					if((lp + 2) > argc) return FALSE;

		  		sscanf(argv[lp+1], "%xl", &GFlashOffset);
          			lp++; 
          			sscanf(argv[lp+1], "%xl", &GFlashSize);
          			lp++; 
          
          			continue;
      				}
				
      			if(GFilename == NULL)
      				{
         			GFilename = argv[lp];
      				}
				
   		} 

return TRUE;
}

void TestLed(void)
{
	JTAG_Write_Memory(0x80023020, 0x00000000, WORD);	// BDATA, PORTB输出低电平
	Sleep(100);
	JTAG_Write_Memory(0x80023020, 0x00000247, WORD);	// BDATA, PORTB输出高电平
	Sleep(100);
}

void Set_7210(void)
{

//printf("> chip : HMS30C7210 \n" ); 

/*
	JTAG_Write_Memory(0x8005e000, 0x00000000, WORD);	// WTCON , Disable Watch-Dog Timer
	JTAG_Write_Memory(0x80050000, 0x00000000, WORD);	// INTMSK, All interrupt disable

// PMU Run Mode setting
	JTAG_Write_Memory(0x80010000, 0x00000001, WORD);	//  	
*/
}

void Set_720x(void)
{       	
printf("> chip : HMS30C720x \n" );  

	JTAG_Write_Memory(0x8002b000, 0x00000000, WORD);	// WTCON , Disable Watch-Dog Timer
	JTAG_Write_Memory(0x80024000, 0x00000000, WORD);	// INTMSK, All interrupt disable

// PMU Run Mode setting
	JTAG_Write_Memory(0x80001000, 0x00000001, WORD);	//  	

	JTAG_Write_Memory(0x8002303c, 0x0000003F, WORD);	// BEN , PORTB设为I/O
	JTAG_Write_Memory(0x80023024, 0x00000000, WORD);	// BDIR, PORTB设为输出OUT
}

void main(int argc, char* argv[])
{
unsigned int i, j, addr;

printf( "\n\n" );
printf( "------------------------------------------------------------\n" );
printf( "    Jflash-Hynix for HMS30C7201, 7202, 7210        V1.0 \n" );
printf( "============================================================\n" );
    
//Test operting system, if WinNT or Win2000 then get device driver handle
TestGiveIODriver();

//foudn lpt port;
FoundLPT();

    	if(!CheckParam(argc, argv)) 
		goto goto_end;
	
    	if( argc < 2 || HelpView == TRUE) 
    		{
        	printf("> Usage for flash bus width 16 \n");
        	printf("> Command help       : jflash -h -> this print\n" );
        	printf("> Write Flash        : jflash file_name\n" );
        	printf("> Verify Flash       : jflash -v file_name\n" );
        	printf("> Test Led           : jflash -l\n" );
        	printf("> Detect Flash ID    : jflash -i\n" );
        	printf("> Erase Flash Sector : jflash -e [sizes] sizes:hex\n" );
        	printf("> Dump Memory        : jflash -d [startaddr] [sizes] startaddr:hex sizes:hex\n" );

//        	printf("> Usage for flash bus width 32 \n");
//        	printf("> Command help       : jflash bus32 -h -> this print\n" );
//        	printf("> Write Flash        : jflash bus32 file_name\n" );
//        	printf("> Verify Flash       : jflash bus32 -v file_name\n" );
//        	printf("> Test Led           : jflash bus32 -l\n" );
//        	printf("> Detect Flash ID    : jflash bus32 -i\n" );
//        	printf("> Erase Flash Sector : jflash bus32 -e [sizes] sizes:hex\n" );
//        	printf("> Dump Memory        : jflash bus32 -d [startaddr] [sizes] startaddr:hex sizes:hex\n" );
        	
        	goto goto_end; 
    		} 

printf( "> FileName : %s\n", GFilename );

    	for(i = 0; i < 15; i++)
		Context[i] = 0;

Context[15] = 0x10000000;

JTAG_Init();
JTAG_SYS_Reset();
//JTAG_HW_Reset();

//	if(JTAG_Stop() == FALSE)
//		goto goto_end;

JTAG_Go_new(Context);

printf( "> ARM720T ID Test...\n" );

	if(JTAG_Stop() == FALSE)
		goto goto_end;

	if( Read_ID_Code() == FALSE ) 
		goto goto_end;  
   
printf("> Change Debug Mode ARM720T...\n" );
Reset();

printf("> Setting Registers for 720x...\n");
	Set_720x();
			
    if(LedTest == TRUE)
    	{
			printf("> Test ARM7202CORE LED\n");
			for(i=0; i<5; i++) 
				TestLed();
		}

    	if(FlashID == TRUE)
    	{
			printf("> Detect Flash ID\n");
			if( !Flash_CheckId() ) 
				{
				printf("> Detect Flash ID ERROR!!\n");
				goto goto_end;
				}
		}
	
    	if(FlashErase == TRUE)
    		{
    		printf("> Erase Flash Area[00000-%05X]\n", GFlashSize);
   			Flash_EraseChip();
    		}

    	if( GFilename != NULL )
    		{
        	printf( "> Write file to Flash...\n" );
  			WriteFileToFlash( GFilename , 0);

    		}

    	if( DUMPTest == TRUE )
    		{
        	printf( "> Flash Dump...\n" );
//        	INTEL_DumpBlock( (GFlashOffset) , GFlashSize );
			FlashDump(GFlashOffset, GFlashSize);
    		}

goto_end:

/*******************************************************************************
 * 	My test 2005-7-27 8:52
 */

//    printf("\n [ My Test ]\n");
//	
//    for(i = 0; i < 15; i++)
//    	Context[i] = 0;
//    Context[15] = 0x10000000;
//    JTAG_Init();
//    JTAG_SYS_Reset();
//    //JTAG_HW_Reset();
//    //	if(JTAG_Stop() == FALSE)
//    //		goto goto_end;
//    JTAG_Go_new(Context);
//    //printf( "> ARM720T ID Test...\n" );
//    if(JTAG_Stop() == FALSE)
////    	goto goto_end;
//    if( Read_ID_Code() == FALSE ) 
////    	goto goto_end;  
//    //printf("> Change Debug Mode ARM720T...\n" );
//    Reset();
//    Set_720x();
//	for(i=0; i<5; i++)
//		TestLed();
//    printf("> Check FLASH:\n");
//    if(!Flash_CheckId())
//    {
//    	printf("> ID Check Error!!!\n");
//    	return;
//    }
//		
//	WriteFileToFlash( "u-boot.bin", 0x4000);
//	while(1)
//	{
//		TestLed();
//	printf("\n> Read addr:");
//	scanf("%xl", &addr);
//	printf("%d:", addr);
//	FlashDump(addr, 512);
//	}
	
//******************************************************************************

				 
printf( "\n------------------------------------------------------------\n" );
//JTAG_HW_Reset();
JTAG_SYS_Reset();
printf( "program end\n" );

}


void JTAG_prnout(void)
{	
_outpw(lpt_address, JTAG_DOValue);		// TCK low  tms:d2:p4    ,tdi:d1:p3
}

unsigned char JTAG_prnin(void)
{
return ((int)_inp(lpt_address + 1) );
}	

void JTAG_TCK(unsigned int state)
{
	if(state) 
		JTAG_DOValue |=   JTAG_IO_TCK;
	else 
		JTAG_DOValue &= (~JTAG_IO_TCK);
	
JTAG_prnout();
}

void JTAG_TMS(unsigned int state)
{
	if(state) JTAG_DOValue |= JTAG_IO_TMS;
	else JTAG_DOValue &= (~JTAG_IO_TMS);

	JTAG_prnout();
}

void JTAG_TDI(unsigned int state)
{
	if(state) 
		JTAG_DOValue |= JTAG_IO_TDI;
	else 
		JTAG_DOValue &= (~JTAG_IO_TDI);

JTAG_prnout();
}


unsigned int JTAG_TDO(void)
{
	return( JTAG_prnin() & JTAG_IO_TDO ) ? HIGH : LOW ;
}

void JTAG_HW_Reset(void)
{
	JTAG_DOValue &= (~JTAG_IO_TRST);
	JTAG_prnout();
	Sleep(1);
	JTAG_DOValue |= JTAG_IO_TRST;
	JTAG_prnout();
}	

void JTAG_SYS_Reset(void)
{
//	JTAG_DOValue &= (~JTAG_IO_STRST);
	JTAG_DOValue &= ~(JTAG_IO_STRST | JTAG_IO_TRST);
	JTAG_prnout();
	Sleep(1000);
	JTAG_DOValue |= JTAG_IO_TRST;
	JTAG_prnout();
	Sleep(100);	
	JTAG_DOValue |= JTAG_IO_STRST;
	JTAG_prnout();
}	

void JTAG_Init(void)
{
	JTAG_DOValue = 0xff77;
//	JTAG_prnout();
}	

void JTAG_Reset(void)
{
int lp;

JTAG_TMS( HIGH );

	for( lp = 0; lp <= 7; lp++ )
		{
		JTAG_TCK( LOW  ); JTAG_TCK( HIGH );
		}
	
JTAG_TMS( LOW  ); JTAG_TCK( LOW  );	JTAG_TCK( HIGH );
}

⌨️ 快捷键说明

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