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

📄 jtagup.cpp

📁 TI MSP430 JTAG program for internal flash upgarde from PC printer port
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// $Header:  $
// Copyright Askey Computer Corp. 2003

/*
 * JTAGup.cpp - Jtag program for uP
 *                
 *                
 *
 * History:
 * 29-Aug-2003  Lucas Kuo
 *
*/


#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include "jtagup.h"

int putp(byte,byte, byte); // writes the JTAG data on the parallel port
void error_out(char*);	// Prints error and exits program
int test_port(void);	// Looks for and finds a valid parallel port address
int convert_to_word(byte *element,int len); //convert string to integer
word GetDevice(void);
word IR_Shift(byte instruction);
word ReadMem(word Format, word Addr);
void ReadMemQuick(word StartAddr, word Length, word *DataArray);
void WriteMem(word Format, word Addr, word Data);
void WriteFLASH(word StartAddr, word Length, word *DataArray);
void writeflash_a43(FILE *in_file);
void EraseFLASH(word EraseMode, word EraseAddr);
word EraseCheck(word StartAddr, word Length);
void ReleaseDevice(word Addr);
void ReleaseTarget(void);
void Delay(int millisec);
void uPreset();
void FlashTest();
BOOL DeviceChk(void);

word DEVICE = 0;	// Global target device ID (e.g. 0xF149); is set 
                        // during GetDevice() function execution.
                      
int lpt_address;									// Global variable assigned to parallel port address
int lpt_ECR;										// global for LPT extended control register
int lpt_CTL;										// global for the LPT control port
int lpt_STAT;										// global for LPT status register
char filename[MAX_IN_LENGTH] = "readfile.bin";		// Global variable for storing the file name
bool Debug_Mode = false;
int usDelay = 0;
byte g_tck = 0;
byte g_tms = 0;
byte g_tdi = 0;
static int  gBoardSelect=0;    // For Real Board  gBoardSelect=0(Default)
			       //     Experimentaion Board  gBoardSelect=1

/*
 * History:
 * 10-SEP-2003  Lucas Kuo
 */
void main( int argc, char *argv[] )
{
    FILE *stream;
    word buf[128];
    char select[2];
	char tmp[10];
	int i = 0;



    select[1] = '0';
    select[2] = '0';
//print description of this program
	printf("\nTI MSP430 JTAG Updater Version 1.00\n");

    if(argc >= 2) 
    {
        strcpy(filename,argv[1]);
	   	if(argc >= 3) strcpy(select,argv[2]);
    }
    else 
    {
        printf("JTAGUP [filename[/D]] [/S]\n");
		printf("/D : Check Device at which Board only\n");
		printf("/S : A (default) write *.a43 to uP flash \n");
        printf("     R read 8K flash to a file \n");
        printf("     E erase 8K flash\n");
        printf("     W write 8K file to uP flash\n");
        printf("     T u-P flash & memory read/write testing\n");
		printf("\nEnter file name: ");
        gets(filename);
		strcpy(tmp,"/D");
		if(!strcmp(filename,tmp)) 
		{
			select[1]='N';
			goto ignoreselection;
		}
        printf("Enter your selection: ");
        gets(&select[1]);
    }
ignoreselection:
   
    //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");
        
    // set the extended control register and others for the parallel port
    lpt_ECR = lpt_address + 0x402;
    lpt_CTL = lpt_address + 0x02;
    lpt_STAT = lpt_address + 0x01;
	//uPreset();  //lucas
	//if (!GetDevice())                     // Set global DEVICE variable, stop
     //   error_out("Couldn't access Device");
	if(!DeviceChk()) goto End_Process;
   
    switch (toupper(select[1])) {
        case '0':
        case 'A':
            printf("Erase FLASH memory!\n");
            EraseFLASH(ERASE_MASS, 0x1000);       // Mass-Erase Flash (all types)
            printf("Write %s to FLASH memory!\n\r",filename);
            if( (stream = fopen(filename, "rb" )) == NULL)
                error_out("error, can not open binary input file");
            writeflash_a43(stream);
            fclose(stream);
            break;
        case 'R':
            printf("Read FLASH memory!\n");
            if( (stream = fopen(filename, "wb" )) == NULL)
                error_out("error, can not open binary output file");
            for(i = 0xE000; i<= 0xFFFF; i+=2) {
                buf[0] = ReadMem(F_WORD,i);
                //printf("%x",buf[0]);
                fwrite(buf,sizeof(word),1,stream);
            }
            /*for(i = 0; i< 0x20; i+=2) {
                ReadMemQuick(0xE000 + i*128, 128, &buf[0]);
                fwrite(buf,sizeof(word),128,in_file);
            }*/
            fclose(stream);
            break;
        case 'E':
            printf("Erase FLASH memory!\n");
            EraseFLASH(ERASE_MASS, 0x1000);       // Mass-Erase Flash (all types)
            //if (!EraseCheck(0xF000, 0x0800))      // Check main memory erasure (Fxx2..9)
            //    error_out("Erase main memory failed!");
            //if (!EraseCheck(0x1000, 0x0080))      // Check info memory erasure (Fxx2..9)
            //    error_out("Erase info memory failed!");
            break;
        case 'W':
            printf("Erase FLASH memory!\n");
            EraseFLASH(ERASE_MASS, 0x1000);       // Mass-Erase Flash (all types)
            printf("Write %s to FLASH memory!\n\r",filename);
            if( (stream = fopen(filename, "rb" )) == NULL)
                error_out("error, can not open binary input file");
            for(i = 0xE000; i<= 0xFFFF; i+=2) {
				fread(buf,sizeof(word),1,stream);
                WriteFLASH(i,1,&buf[0]);
            }
            fclose(stream);
            break;
        case 'T':
             FlashTest();
             break;
		case 'N':
			 break;	
        default :
            printf("Unkown your selection!\n");
            exit(0);
    }
End_Process:    
    ReleaseDevice(V_RESET);               // Perform Reset, release CPU from JTAG control
    Sleep(3000);                          // Target application blinks for 3.0s
    ReleaseTarget();                      // Release Target Board from power
	printf("......Done\n");
    exit(0);
}
/*
 * chk the device exist or not
 *
 */
BOOL DeviceChk(void)
{
	//printf("Device Check exist or not!!!!!\n");
//**********************
	gBoardSelect=0;
	uPreset();  //lucas
    if (GetDevice()) 
    {
		printf("Find Device at Real Board\n");
		goto exitDeviceChk;
	}
	//else printf("Couldn't access Device at assume Real Board\n");
//*************************
    gBoardSelect=1;	
	uPreset();  //lucas
	if (GetDevice())                     
    {
		printf("Find Device at Experimentation Board\n");
		goto exitDeviceChk;
	}
	//else printf("Couldn't access Device at assume Experimentation Board\n");
//**************************
	printf("Device not found at connected Target!!!\n");
	return FALSE;
exitDeviceChk:
	return TRUE;
}

/*
 * write *.a43 format file to uP flash
 *
 * History:
 * 10-SEP-2003  Lucas Kuo
 */
void writeflash_a43(FILE *in_file)
{
    byte line_buf[128];
    word uP_buf[1];
    int count,addr,i;
    
    
    while( fscanf( in_file, " %s", line_buf)!=EOF)
    {
        if (line_buf[0] == ':') {
            count = convert_to_word(&line_buf[1],2);
            addr = convert_to_word(&line_buf[3],4);
            //printf("%x\n",addr);
            for(i=0;i<(count/2);i++) {    //byte len / 2 = word len
                uP_buf[0] = convert_to_word(&line_buf[9+i*4],2) + (convert_to_word(&line_buf[11+i*4],2) << 8);
                WriteFLASH(addr+i*2,1,uP_buf);
            }
            if (count%2) {      //if remain one byte data
                uP_buf[0] = convert_to_word(&line_buf[9+i*4],2) + 0xFF00;
                WriteFLASH(addr+i*2,1,uP_buf);
            }
        } 
        else error_out("error, unkown file format");
    }    
}

/*
 * convert string to integer
 * 
 * History:
 * 10-SEP-2003  Lucas Kuo
 */
int convert_to_word(byte *element,int len)
{
    int ret,i;
    
    ret = 0;

⌨️ 快捷键说明

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