Пример #1
0
void close_ifport ()
{
	BYTE spicmd[2] = {FLAG, SPI_DISABLE};


	switch (PortType) {
		case TY_COMM :
			_outp(COM_MCR, 0);
			_outp(COM_LCR, 3);
			break;

		case TY_VCOM :
			break;

		case TY_BRIDGE :
			send_bridge(spicmd, 2);
			read_bridge(spicmd, 1);
			break;

		case TY_AVRSP :
			_outp(LPT_DAT, 0);
			break;

		case TY_STK200 :
			_outp(LPT_DAT, BS_DIS);
			break;

		case TY_XILINX :
			_outp(LPT_DAT, BX_DIS1|BX_DIS2);
			break;

		case TY_LATTICE :
			_outp(LPT_DAT, BL_DIS);
			break;

		case TY_ALTERA :
			_outp(LPT_CTL, 0);
			break;
#if AVRSPX
		case TY_USBASP :	//@@@ by t.k
			usbasp_close();
			break;

		case TY_HIDASP :	//@@@ by k-k
			hidasp_close();
			break;

		case TY_RSCR :	//@@@ by t.k
			if (hComm != INVALID_HANDLE_VALUE)
				rscr_close();
			break;
#endif

	}

	if(hComm != INVALID_HANDLE_VALUE)
		CloseHandle(hComm);

	PortType = 0;
}
Пример #2
0
void spi_delayedget (BYTE *ptr, DWORD cnt)
{
	if(PortType == TY_BRIDGE) {
		if(!read_bridge(ptr, cnt))			/* Get data from pipeline */
			memset(ptr, 0xFF, cnt);
	}
	else {
		while (cnt--)
			*ptr++ = fifo_get();		/* Get data form receiving fifo */
	}
}
Пример #3
0
void close_ifport ()
{
	BYTE spicmd[2] = {FLAG, SPI_DISABLE};


	switch (PortType) {
		case TY_COMM :
			_outp(COM_MCR, 0);
			_outp(COM_LCR, 3);
			break;

		case TY_VCOM :
			break;

		case TY_BRIDGE :
			send_bridge(spicmd, 2);
			read_bridge(spicmd, 1);
			break;

		case TY_AVRSP :
			_outp(LPT_DAT, 0);
			break;

		case TY_STK200 :
			_outp(LPT_DAT, BS_DIS);
			break;

		case TY_XILINX :
			_outp(LPT_DAT, BX_DIS1|BX_DIS2);
			break;

		case TY_LATTICE :
			_outp(LPT_DAT, BL_DIS);
			break;

		case TY_ALTERA :
			_outp(LPT_CTL, 0);
			break;
	}

	if(hComm != INVALID_HANDLE_VALUE)
		CloseHandle(hComm);

	PortType = 0;
}
Пример #4
0
int spi_flush ()
{
	BYTE spicmd[2] = { FLAG, SPI_NOP };
	int n;


	if(PortType != TY_BRIDGE) return 0;	/* Exit if no bridge */

	send_bridge(spicmd, 2);		/* Send SPI_NOP command */

	n = 50;
	do {						/* Wait until SPI_NOP is processed in timeout of 5 sec */
		read_bridge(spicmd, 1);
		if(spicmd[0] == SPI_NOP) break;
	} while(--n);

	return n ? 1 : 0;
}
Пример #5
0
int open_ifport (PORTPROP *pc)
{
	OSVERSIONINFO vinfo = { sizeof(OSVERSIONINFO) };
	LARGE_INTEGER val1;
	BYTE cmdspi[6];
	static const WORD PortsCom[] = { COM1ADR, COM2ADR, COM3ADR, COM4ADR };
	static const WORD PortsLpt[] = { LPT1ADR, LPT2ADR, LPT3ADR };
	char sComm[16];
	DCB dcb = { sizeof(DCB),
				9600, TRUE, FALSE, TRUE, FALSE,
				DTR_CONTROL_DISABLE, FALSE,
				TRUE, FALSE, FALSE, FALSE, FALSE,
				RTS_CONTROL_DISABLE, FALSE, 0, 0,
				10, 10,
				8, NOPARITY, ONESTOPBIT, '\x11', '\x13', '\xFF', '\xFF', 0 };
	COMMTIMEOUTS commtimeouts = { 0, 1, 100, 1, 300};


	/* Check if high resolution timer is supported */
	QueryPerformanceFrequency(&val1);
	if (val1.QuadPart == 0) {
		pc->Info1 = "Incompatible envilonment.\n";
		return 1;
	}

#if AVRSPX
	if (pc->Delay < 0) {				//@@x by t.k
		PortDly = DEFAULT_DELAY_VALUE;	//@@x
	} else {
		PortDly = pc->Delay;	/* I/O delay for direct I/O control */
	}

	if (pc->Baud < 0) {					//@@x by t.k
		if (pc->PortClass == TY_RSCR)
			dcb.BaudRate = 19200;
		else {
			dcb.BaudRate = DEFAULT_BAUDRATE;
		}
	} else {
		dcb.BaudRate = pc->Baud;	/* Bit rate for SPI bridge */
	}
#else
	PortDly = pc->Delay;		/* I/O delay for direct I/O control */
	dcb.BaudRate = pc->Baud;	/* Bit rate for SPI bridge */
#endif

	/* Open direct I/O driver if needed */
	if(GetVersionEx(&vinfo) == FALSE) {
		pc->Info1 = "Incompatible envilonment.\n";
		return 1;
	}
	if((vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
	    && ((pc->PortClass == TY_COMM)||(pc->PortClass == TY_LPT))) {
		if(init_driver()) {
			pc->Info1 = "I/O driver, giveio.sys, is not available due to any reason.\n"
			"To install giveio, type \"move giveio.sys %%windir%%\\system32\" and retry any function.\n";
			return 1;
		}
	}

	/* Use COM port in direct I/O */
	if(pc->PortClass == TY_COMM) {
		if((pc->PortNum < 1)||(pc->PortNum > 4)) {
			pc->Info1 = "Invalid Port#.\n";
			return 1;
		}
		PortBase = PortsCom[pc->PortNum - 1];
		sprintf(str_info, "No COM%u(0x%X) port.\n", pc->PortNum, PortBase);
		pc->Info1 = str_info;
		if(check_comport()) {
			if(vinfo.dwPlatformId != VER_PLATFORM_WIN32_NT) return 1;
			sprintf(sComm, "\\\\.\\COM%u", pc->PortNum);
			hComm = CreateFile(sComm, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
			if(hComm == INVALID_HANDLE_VALUE) return 1;
			if(check_comport()) return 1;
		}

		_outp(COM_IMR, 0x00);		/* Mask interrupts */
		pc->Info1 = NULL;
		PortType = TY_COMM;
#if AVRSPX
		sprintf(str_info, "Use COM%u(0x%X) port in direct I/O.\n", pc->PortNum, PortBase);
		pc->Info1 = str_info;
#endif
		return 0;
	}

#if AVRSPX
	if (pc->PortClass == TY_HIDASP)	{ //@@+ by k-k
		if (hidasp_init(pc->SerialNumber) == HIDASP_MODE_ERROR)	{
			sprintf(str_info, "HIDaspx(%s) not found.\n", pc->SerialNumber);
			pc->Info1 = str_info;
			return 1;
		}
		PortType = TY_HIDASP;
		return 0;
	}
#endif

	/* Use COM port via API */
	if(pc->PortClass == TY_VCOM) {
		sprintf(sComm, "\\\\.\\COM%u", pc->PortNum);
		hComm = CreateFile(sComm, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		if(hComm == INVALID_HANDLE_VALUE) {
			sprintf(str_info, "%s could not be opened.\n", sComm);
			pc->Info1 = str_info;
			return 1;
		}
#if AVRSPX
		if (SetCommState(hComm, &dcb)==0) {
			sprintf(str_info, "%s (%dbps) parameter error. \n", sComm, (int)dcb.BaudRate);
			pc->Info1 = str_info;
			return 1;
		}
#else
		SetCommState(hComm, &dcb);
#endif
		EscapeCommFunction(hComm, CLRRTS);
		EscapeCommFunction(hComm, CLRDTR);
		PortType = TY_VCOM;
#if AVRSPX
		sprintf(str_info, "Use COM%u port via API.\n", pc->PortNum);
		pc->Info1 = str_info;
#endif
		return 0;
	}

	/* Use SPI bridge attached on COM port */
	if(pc->PortClass == TY_BRIDGE) {
		sprintf(sComm, "\\\\.\\COM%u", pc->PortNum);
		hComm = CreateFile(sComm, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		if(hComm == INVALID_HANDLE_VALUE) {
			sprintf(str_info, "%s could not be opened.\n", sComm);
			pc->Info1 = str_info;
			return 1;
		}
		dcb.fOutxCtsFlow = TRUE;
		dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
#if AVRSPX
		if (SetCommState(hComm, &dcb)==0) {
			sprintf(str_info, "%s (%dbps) parameter error. \n", sComm, (int)dcb.BaudRate);
			pc->Info1 = str_info;
			return 1;
		}
#else
		SetCommState(hComm, &dcb);
#endif
		SetCommTimeouts(hComm, &commtimeouts);
		EscapeCommFunction(hComm, SETDTR);
		EscapeCommFunction(hComm, CLRDTR);
		delay_ms(10);
		while(read_bridge(cmdspi, sizeof(cmdspi)));
		cmdspi[0] = FLAG-1;
		cmdspi[1] = FLAG; cmdspi[2] = SPI_ENABLE;	/* Enable Bridge */
#if AVRSPX
		cmdspi[3] = FLAG; cmdspi[4] = SPI_SETDLY;
		if (PortDly > 255) {
			cmdspi[5] = 255; /* Set SPI delay */
		} else {
			cmdspi[5] = (BYTE)PortDly; /* Set SPI delay */
		}
#else
		cmdspi[3] = FLAG; cmdspi[4] = SPI_SETDLY; cmdspi[5] = (BYTE)PortDly; /* Set SPI delay */
#endif
		send_bridge(cmdspi, 6);		/* Send bridge initialization commands */
		read_bridge(cmdspi, 2);		/* Check if the commands are accepted */
		if((cmdspi[0] != SPI_ENABLE) || (cmdspi[1] != SPI_SETDLY)) {
			sprintf(str_info, "No SPI bridge on the %s.\n", sComm);
			pc->Info1 = str_info;
			return 1;
		}
#if AVRSPX
		cmdspi[0] = FLAG; cmdspi[1] = SPI_GETREV;
		send_bridge(cmdspi, 2);		/* Send f/w revision commands */
		read_bridge(cmdspi, 1);		/* Check if AVR specific command is supported */
		BridgeRev = (cmdspi[0] == 0xFF) ? 0 : cmdspi[0];
		if (BridgeRev < 4) {
			sprintf(str_info, "*** Use SPI bridge R4+ for fast operation ***\n");
		} else {
			sprintf(str_info, "SPI bridge on the %s (baud=%d).\n", sComm, (int)dcb.BaudRate);
		}
		pc->Info1 = str_info;
#endif
		PortType = TY_BRIDGE;
		return 0;
	}

	/* Use LPT port in direct I/O */
	if(pc->PortClass == TY_LPT) {
		if((pc->PortNum < 1)||(pc->PortNum > 3)) {
			pc->Info1 = "Invalid Port#.\n";
			return 1;
		}
		PortBase = PortsLpt[pc->PortNum - 1];
		if(check_lptport()) {
			sprintf(str_info, "No LPT%u(0x%X) port.\n", pc->PortNum, PortBase);
			pc->Info1 = str_info;
			return 1;
		}

		_outp(LPT_DAT, 0x20);	/* Check if the adapter is ByteBlasterMV (D5-ACK) */
		if(_inp(LPT_STA) & S_ACK) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & S_ACK) == 0) {
				_outp(LPT_CTL, BA_ENA);
				pc->Info1 = "Altera ByteBlasterMV was found.\n";
				PortType = TY_ALTERA;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		_outp(LPT_DAT, 0x80);	/* Check if the adapter is AVRSP (D7-PE) */
		if(_inp(LPT_STA) & S_PE) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & S_PE) == 0) {
				_outp(LPT_DAT, B_ENA);
				pc->Info1 = "AVRSP adapter was found.\n";
				PortType = TY_AVRSP;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		_outp(LPT_DAT, 0x40);	/* Check if the adapter is Xilinx JTAG (D6-BUSY-PE) */
		if((_inp(LPT_STA) & (S_PE | S_BUSY)) == S_PE) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & (S_PE | S_BUSY)) == S_BUSY) {
				pc->Info1 = "Xilinx JTAG adapter was found.\n";
				PortType = TY_XILINX;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		_outp(LPT_DAT, 0x40);	/* Check if the adapter is Lattice ISP (D6-PE) */
		if(_inp(LPT_STA) & S_PE) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & S_PE) == 0) {
				pc->Info1 = "Lattice ISP adapter was found.\n";
				PortType = TY_LATTICE;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		_outp(LPT_DAT, 0x01);	/* Check if the adapter is STK200 dongle (D0-PE) */
		if(_inp(LPT_STA) & S_PE) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & S_PE) == 0) {
				pc->Info1 = "STK200 ISP dongle was found.\n";
				PortType = TY_STK200;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		sprintf(str_info, "No ISP adapter on the LPT%u.\n", pc->PortNum);
		pc->Info1 = str_info;
		return 1;
	}
#if AVRSPX
	if (pc->PortClass == TY_USBASP) {	//@@+ by t.k
		if (usbasp_initialize(pc->SerialNumber) != 0) {
			pc->Info1 = "USBasp not fonud.\n";
			return 1;
		}
		PortType = TY_USBASP;
		if (usbasp_connect(pc->Delay) != 0) {
			pc->Info1 = "USBasp connection error!!.\n";
			return 1;
		}
		return 0;
	}

	/* Use COM port via API */
	if (pc->PortClass == TY_RSCR) {	//@@+ by t.k
		sprintf(sComm, "\\\\.\\COM%u", pc->PortNum);
		hComm = CreateFile(sComm, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		if (hComm == INVALID_HANDLE_VALUE) {
			sprintf(str_info, "%s could not be opened.\n", sComm);
			pc->Info1 = str_info;
			return 1;
		}
		dcb.fOutxCtsFlow = FALSE;
		dcb.fOutxDsrFlow = FALSE;
		dcb.fDtrControl  = DTR_CONTROL_DISABLE;
		dcb.fRtsControl  = RTS_CONTROL_DISABLE;
#if AVRSPX
		if (SetCommState(hComm, &dcb)==0) {
			sprintf(str_info, "%s (%dbps) parameter error. \n", sComm, (int)dcb.BaudRate);
			pc->Info1 = str_info;
			return 1;
		}
#else
		SetCommState(hComm, &dcb);
#endif
		EscapeCommFunction(hComm, CLRRTS);
		EscapeCommFunction(hComm, CLRDTR);
		PortType = TY_RSCR;
		rscr_setup(hComm, dcb.BaudRate);
		return 0;
	}

#endif

	pc->Info1 = "Invalid port class.\n";
	return 1;
}
Пример #6
0
BYTE spi_rcvr (BYTE mode)
{
	BYTE rd = 0;
	BYTE spicmd[2] = { FLAG, SPI_RCVZ };
	int n = 8;


	switch (PortType) {
		case TY_COMM :
			_outp(COM_LCR, 0);						/* MOSI(TD) = "L" */
			iodly();
			do {
				rd <<= 1;							/* Read MISO(DR) */
				if(_inp(COM_MSR) & C_MISO) rd++;
				_outp(COM_MCR, C_SCK);				/* SCK(RS) = "H" */
				iodly();							/* delay */
				_outp(COM_MCR, 0);					/* SCK(RS) = "L" */
				iodly();							/* delay */
			} while (--n);
			if(mode == RM_ASYNC)
				fifo_put(rd);
			break;

		case TY_VCOM :
			if(!sig_break) {
				EscapeCommFunction(hComm, CLRBREAK);/* MOSI(TD) = "L" */
				sig_break = FALSE;
			}
			iodly();
			do {
				rd <<= 1;							/* Read MISO(DR) */
				GetCommModemStatus(hComm, &ModemStat);
				if(ModemStat & MS_DSR_ON) rd++;
				EscapeCommFunction(hComm, SETRTS);	/* SCK(RS) = "H" */
				iodly();							/* delay */
				EscapeCommFunction(hComm, CLRRTS);	/* SCK(RS) = "L" */
				iodly();							/* delay */
			} while (--n);
			if(mode == RM_ASYNC)
				fifo_put(rd);
			break;

		case TY_BRIDGE :
			send_bridge(spicmd, 2);					/* Send SPI_RCVZ command */
			if(mode == RM_SYNC) {
				if(!read_bridge(&rd, 1))			/* Get a read data from pipeline */
					rd = 0xFF;
			}
			break;

		case TY_AVRSP :
			_outp(LPT_DAT, B_ENA);					/* MOSI(D0) = "L" */
			iodly();
			do {
				rd <<= 1;							/* Read MISO(BUSY) */
				if((_inp(LPT_STA) & S_BUSY) == 0) rd++;
				_outp(LPT_DAT, B_ENA | B_SCK);		/* SCK(D1) = "H" */
				iodly();							/* delay */
				_outp(LPT_DAT, B_ENA);				/* SCK(D1) = "L" */
				iodly();							/* delay */
			} while (--n);
			if(mode == RM_ASYNC)
				fifo_put(rd);
			break;

		case TY_STK200 :
			_outp(LPT_DAT, 0);						/* MOSI(D5) = "L" */
			iodly();
			do {
				rd <<= 1;							/* Read MISO(ACK) */
				if(_inp(LPT_STA) & S_ACK) rd++;
				_outp(LPT_DAT, BS_SCK);				/* SCK(D4) = "H" */
				iodly();							/* delay */
				_outp(LPT_DAT, 0);					/* SCK(D4) = "L" */
				iodly();							/* delay */
			} while (--n);
			if(mode == RM_ASYNC)
				fifo_put(rd);
			break;

		case TY_XILINX :
			_outp(LPT_DAT, BX_DIS2);				/* MOSI(D0) = "L" */
			iodly();
			do {
				rd <<= 1;							/* Read MISO(SEL) */
				if(_inp(LPT_STA) & S_SEL) rd++;
				_outp(LPT_DAT, BX_DIS2 | BX_SCK);	/* SCK(D1) = "H" */
				iodly();							/* delay */
				_outp(LPT_DAT, BX_DIS2);			/* SCK(D1) = "L" */
				iodly();							/* delay */
			} while (--n);
			if(mode == RM_ASYNC)
				fifo_put(rd);
			break;

		case TY_LATTICE :
			_outp(LPT_DAT, 0);						/* MOSI(D0) = "L" */
			iodly();
			do {
				rd <<= 1;							/* Read MISO(ACK) */
				if(_inp(LPT_STA) & S_ACK) rd++;
				_outp(LPT_DAT, BL_SCK);				/* SCK(D1) = "H" */
				iodly();							/* delay */
				_outp(LPT_DAT, 0);					/* SCK(D1) = "L" */
				iodly();							/* delay */
			} while (--n);
			if(mode == RM_ASYNC)
				fifo_put(rd);
			break;

		case TY_ALTERA :
			_outp(LPT_DAT, 0);						/* MOSI(D6) = "L" */
			iodly();
			do {
				rd <<= 1;							/* Read MISO(BUSY) */
				if((_inp(LPT_STA) & S_BUSY) == 0) rd++;
				_outp(LPT_DAT, BA_SCK);				/* SCK(D0) = "H" */
				iodly();							/* delay */
				_outp(LPT_DAT, 0);					/* SCK(D0) = "L" */
				iodly();							/* delay */
			} while (--n);
			if(mode == RM_ASYNC)
				fifo_put(rd);
			break;
	}

	return(rd);
}
Пример #7
0
int open_ifport (PORTPROP *pc)
{
	OSVERSIONINFO vinfo = { sizeof(OSVERSIONINFO) };
	LARGE_INTEGER val1;
	BYTE cmdspi[6];
	static const WORD PortsCom[] = { COM1ADR, COM2ADR, COM3ADR, COM4ADR };
	static const WORD PortsLpt[] = { LPT1ADR, LPT2ADR, LPT3ADR };
	char sComm[16];
	DCB dcb = { sizeof(DCB),
				9600, TRUE, FALSE, TRUE, FALSE,
				DTR_CONTROL_DISABLE, FALSE,
				TRUE, FALSE, FALSE, FALSE, FALSE,
				RTS_CONTROL_DISABLE, FALSE, 0, 0,
				10, 10,
				8, NOPARITY, ONESTOPBIT, '\x11', '\x13', '\xFF', '\xFF', 0 };
	COMMTIMEOUTS commtimeouts = { 0, 1, 100, 1, 300};


	/* Check if high resolution timer is supported */
	QueryPerformanceFrequency(&val1);
	if (val1.QuadPart == 0) {
		pc->Info1 = "Incompatible envilonment.\n";
		return 1;
	}

	PortDly = pc->Delay;		/* I/O delay for direct I/O control */
	dcb.BaudRate = pc->Baud;	/* Bit rate for SPI bridge */

	/* Open direct I/O driver if needed */
	if(GetVersionEx(&vinfo) == FALSE) {
		pc->Info1 = "Incompatible envilonment.\n";
		return 1;
	}
	if((vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
	    && ((pc->PortClass == TY_COMM)||(pc->PortClass == TY_LPT))) {
		if(init_driver()) {
			pc->Info1 = "I/O driver initialization failed.\n";
			return 1;
		}
	}

	/* Use COM port in direct I/O */
	if(pc->PortClass == TY_COMM) {
		if((pc->PortNum < 1)||(pc->PortNum > 4)) {
			pc->Info1 = "Invalid Port#.\n";
			return 1;
		}
		PortBase = PortsCom[pc->PortNum - 1];
		sprintf(str_info, "No COM%u(0x%X) port.\n", pc->PortNum, PortBase);
		pc->Info1 = str_info;
		if(check_comport()) {
			if(vinfo.dwPlatformId != VER_PLATFORM_WIN32_NT) return 1;
			sprintf(sComm, "\\\\.\\COM%u", pc->PortNum);
			hComm = CreateFile(sComm, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
			if(hComm == INVALID_HANDLE_VALUE) return 1;
			if(check_comport()) return 1;
		}

		_outp(COM_IMR, 0x00);		/* Mask interrupts */
		pc->Info1 = NULL;
		PortType = TY_COMM;
		return 0;
	}

	/* Use COM port via API */
	if(pc->PortClass == TY_VCOM) {
		sprintf(sComm, "\\\\.\\COM%u", pc->PortNum);
		hComm = CreateFile(sComm, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		if(hComm == INVALID_HANDLE_VALUE) {
			sprintf(str_info, "%s could not be opened.\n", sComm);
			pc->Info1 = str_info;
			return 1;
		}
		SetCommState(hComm, &dcb);
		EscapeCommFunction(hComm, CLRRTS);
		EscapeCommFunction(hComm, CLRDTR);
		PortType = TY_VCOM;
		return 0;
	}

	/* Use SPI bridge attached on COM port */
	if(pc->PortClass == TY_BRIDGE) {
		sprintf(sComm, "\\\\.\\COM%u", pc->PortNum);
		hComm = CreateFile(sComm, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		if(hComm == INVALID_HANDLE_VALUE) {
			sprintf(str_info, "%s could not be opened.\n", sComm);
			pc->Info1 = str_info;
			return 1;
		}
		dcb.fOutxCtsFlow = TRUE;
		dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
		SetCommState(hComm, &dcb);
		SetCommTimeouts(hComm, &commtimeouts);
		EscapeCommFunction(hComm, CLRDTR);
		delay_ms(10);
		while(read_bridge(cmdspi, sizeof(cmdspi)));
		cmdspi[0] = FLAG-1;
		cmdspi[1] = FLAG; cmdspi[2] = SPI_ENABLE;	/* Enable Bridge */
		cmdspi[3] = FLAG; cmdspi[4] = SPI_SETDLY; cmdspi[5] = (BYTE)PortDly; /* Set SPI delay */
		send_bridge(cmdspi, 6);		/* Send bridge initialization commands */
		read_bridge(cmdspi, 2);		/* Check if the commands are accepted */
		if((cmdspi[0] != SPI_ENABLE) || (cmdspi[1] != SPI_SETDLY)) {
			sprintf(str_info, "No SPI bridge on the %s.\n", sComm);
			pc->Info1 = str_info;
			return 1;
		}
		PortType = TY_BRIDGE;
		return 0;
	}

	/* Use LPT port in direct I/O */
	if(pc->PortClass == TY_LPT) {
		if((pc->PortNum < 1)||(pc->PortNum > 3)) {
			pc->Info1 = "Invalid Port#.\n";
			return 1;
		}
		PortBase = PortsLpt[pc->PortNum - 1];
		if(check_lptport()) {
			sprintf(str_info, "No LPT%u(0x%X) port.\n", pc->PortNum, PortBase);
			pc->Info1 = str_info;
			return 1;
		}

		_outp(LPT_DAT, 0x20);	/* Check if the adapter is ByteBlasterMV (D5-ACK) */
		if(_inp(LPT_STA) & S_ACK) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & S_ACK) == 0) {
				_outp(LPT_CTL, BA_ENA);
				pc->Info1 = "Altera ByteBlasterMV was found.\n";
				PortType = TY_ALTERA;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		_outp(LPT_DAT, 0x80);	/* Check if the adapter is AVRSP (D7-PE) */
		if(_inp(LPT_STA) & S_PE) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & S_PE) == 0) {
				_outp(LPT_DAT, B_ENA);
				pc->Info1 = "AVRSP adapter was found.\n";
				PortType = TY_AVRSP;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		_outp(LPT_DAT, 0x40);	/* Check if the adapter is Xilinx JTAG (D6-BUSY-PE) */
		if((_inp(LPT_STA) & (S_PE | S_BUSY)) == S_PE) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & (S_PE | S_BUSY)) == S_BUSY) {
				pc->Info1 = "Xilinx JTAG adapter was found.\n";
				PortType = TY_XILINX;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		_outp(LPT_DAT, 0x40);	/* Check if the adapter is Lattice ISP (D6-PE) */
		if(_inp(LPT_STA) & S_PE) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & S_PE) == 0) {
				pc->Info1 = "Lattice ISP adapter was found.\n";
				PortType = TY_LATTICE;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		_outp(LPT_DAT, 0x01);	/* Check if the adapter is STK200 dongle (D0-PE) */
		if(_inp(LPT_STA) & S_PE) {
			_outp(LPT_DAT, 0);
			if((_inp(LPT_STA) & S_PE) == 0) {
				pc->Info1 = "STK200 ISP dongle was found.\n";
				PortType = TY_STK200;
				if(_inp(LPT_STA) & S_ERR) return 0;	/* Check target power */
				pc->Info2 = "But target power is OFF.\n";
				return 1;
			}
		}
		sprintf(str_info, "No ISP adapter on the LPT%u.\n", pc->PortNum);
		pc->Info1 = str_info;
		return 1;
	}

	pc->Info1 = "Invalid port class.\n";
	return 1;
}