www.pudn.com > jtagsrc.rar > jtag.c


/* 
 * jtag.c : implement jtag control interface 
 * 
 * Copyright (C) 2004, OPEN-JTAG, All rights reserved. 
 */ 
 
 
#include  
#include  
#include  
 
#include "jtag.h" 
#include "xjerr.h" 
#include "types.h" 
 
 
//Used to record the status of the parallel port 
static u8 port_data = 0;	 
 
 
/* 
 * paral_output() - 
 *		Used to output a 8-bit wide data to the parallel 
 *		port. 
 */ 
static void paral_output(u8 data) 
{ 
	_outp(PARAL_DATA_PORT, data); 
} 
 
 
/* 
 * paral_intput() - 
 *		Used to read a 8-bit wide input data from the parallel 
 *		port. 
 */ 
static u8 paral_input(void)  
{ 
	u8 data; 
	 
	data = _inp(PARAL_STAT_PORT); 
	return data; 
} 
 
 
/*  
 * jtag_load_giveio() - 
 *		Load device driver GIVEIO for direct access to  
 *		parallel port. 
 */ 
static BOOL jtag_load_giveio(void) { 
	u32 errno; 
	BOOL ret = FALSE; 
	BOOL status; 
	SC_HANDLE drv = 0; 
	SC_HANDLE scm = 0; 
 
	scm = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 
	drv = OpenService(scm,"giveio",SERVICE_ALL_ACCESS); 
	if (drv){ 
		status = StartService(drv,0,NULL); 
		if( status == TRUE) 
			ret = TRUE; 
		else { 
			errno = GetLastError(); 
			if(errno == ERROR_SERVICE_ALREADY_RUNNING) 
				ret = TRUE; 
			else 
				ret = FALSE; 
		} 
 
		CloseServiceHandle(drv); 
	} 
 
	CloseServiceHandle(scm); 
 
    return ret; 
} 
 
 
/*  
 * jtag_init() -  
 *		Initialize the parallel port and load the device driver  
 *		(GIVEIO) to allow direct access to paralle port. 
 *		This route should be called first before any operation 
 *		on the TAP Controller. 
 */ 
int jtag_init(void)  
{ 
	FILE *fp = NULL; 
 
	if( jtag_load_giveio() == FALSE) 
		return XJERR_GIVEIO_FAIL; 
 
	fp = fopen("\\\\.\\giveio","wb"); 
 
	paral_output(PARAL_INIT_DATA); 
 
	/*  
	 * Improve our priority in hope of avoiding interruptoins 
	 * during I/O operations 
	 */ 
	SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); 
 
	return XJ_OK; 
} 
 
 
/*  
 * jtag_wri_tms() -  
 *		Write the TMS signal.  
 */ 
void jtag_wri_tms(u8 new_val) 
{ 
	port_data &= (~JTAG_TCK_MASK); 
 
	if(new_val) 
		port_data |= JTAG_TMS_MASK; 
	else 
		port_data &= (~JTAG_TMS_MASK); 
	 
	paral_output(port_data);	 
} 
 
 
/*  
 * jtag_wri_tdi() -  
 *		Write the TDI signal.  
 */ 
void jtag_wri_tdi(u8 new_val) 
{ 
	port_data &= (~JTAG_TCK_MASK); 
 
	if(new_val) 
		port_data |= JTAG_TDI_MASK; 
	else 
		port_data &= (~JTAG_TDI_MASK); 
	 
	paral_output(port_data);	 
} 
 
 
/*  
 * jtag_wri_tck() -  
 *		Write the TCK signal.  
 */ 
void jtag_wri_tck(void) 
{ 
	port_data |= JTAG_TCK_MASK; 
	paral_output(port_data); 
} 
 
 
/*  
 * jtag_wri_ntrst() -  
 *		Write the nTRST signal.  
 */ 
void jtag_wri_ntrst(u8 new_val) 
{ 
	if(new_val) 
		port_data |= JTAG_NTRST_MASK; 
	else 
		port_data &= (~JTAG_NTRST_MASK); 
 
	paral_output(port_data); 
} 
 
 
 
/* 
 * jtag_rd_rdo() -  
 *		Read TDO singal. 
 * 
 *		Please note that:  
 *		If TDO is connected to pin-11 of the parallel port,  
 *		the read signal is inverted automatically in this route. 
 */ 
int jtag_rd_rdo(void) 
{ 
	u8 data; 
	 
	data = paral_input(); 
 
	if(JTAG_TDO_MASK == PARAL_PIN_11){  	//Invert before return 
		if(data & JTAG_TDO_MASK) 
			return 0; 
		else 
			return 1; 
	}else{									//Don't need to invert	 
		if(data & JTAG_TDO_MASK) 
			return 1; 
		else 
			return 0; 
	} 
 
}