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

📄 复件 jtag.cpp

📁 c8051.rar
💻 CPP
字号:
// jtag.cpp : implementation file
//

#include "stdafx.h"
#include "C8051JTAG.h"
#include "jtag.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// jtag

jtag::jtag()
{
	m_iCcabletype = ALTERABLASTER;
	m_nPort = (WORD)0x378;
	m_ucWriteValue = 0x20;
	SetPortVal(m_nPort, m_ucWriteValue, 1);				//SET TMS,TCK,TDI to low
	
}

jtag::~jtag()
{
}


BEGIN_MESSAGE_MAP(jtag, CButton)
	//{{AFX_MSG_MAP(jtag)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// jtag message handlers
//TCK函数
void jtag::TCK(bool bValue)
{
	unsigned char ucTemp;
	ucTemp = 0;
	ucTemp = 1<<m_woTCKPin;
	if(bValue)
	{
		m_ucWriteValue |= ucTemp; 
	}
	else
	{
		ucTemp = ~ucTemp;
		m_ucWriteValue &= ucTemp; 
	}
	SetPortVal(m_nPort, m_ucWriteValue, 1);
}

void jtag::TMS(bool bValue)
{
	unsigned char ucTemp;
	ucTemp = 0;
	ucTemp = 1<<m_woTMSPin;
	if(bValue)
	{
		m_ucWriteValue |= ucTemp; 
		
	}
	else
	{
		ucTemp = ~ucTemp;
		m_ucWriteValue &= ucTemp; 
	}
	SetPortVal(m_nPort, m_ucWriteValue, 1);

}

void jtag::TDI(bool bValue)
{
	unsigned char ucTemp;
	ucTemp = 0;
	ucTemp = 1<<m_woTDIPin;
	if(bValue)
	{
		m_ucWriteValue |= ucTemp; 
	}
	else
	{
		ucTemp = ~ucTemp;
		m_ucWriteValue &= ucTemp; 
	}
	SetPortVal(m_nPort, m_ucWriteValue, 1);
}

bool jtag::TDO()
{
	//获得控制端口的值,保持高位值不变,将要输出的值从低4位输出,且使连接器上的电位状态与想输出的值一致
	DWORD temp_dwPortVal;
	unsigned int temp_aa,uiTempPin;
	GetPortVal(m_nPort+m_iTDOAddP, &temp_dwPortVal, 1); //reads a BYTE value from an I/O port
	temp_aa = (unsigned int)temp_dwPortVal;
	uiTempPin = 1<<m_wiTDOPin;
	if(uiTempPin & temp_aa)
		return 0;
	else
		return 1;
	return 0;
}

void jtag::JTAG_StrobeTCK()
{
	TCK(1);
	TCK(0);
}

void jtag::JTAG_Reset()
{
	TMS (1);
	JTAG_StrobeTCK ();								// move to Test Logic Reset state
	JTAG_StrobeTCK ();
	JTAG_StrobeTCK ();
	JTAG_StrobeTCK ();
	JTAG_StrobeTCK ();
	TMS (0);
	JTAG_StrobeTCK ();								// move to Run_Test/Idle state
}

unsigned int jtag::JTAG_IR_Scan(unsigned int instruction, int num_bits)
{
	unsigned int retval;							// JTAG instruction read
	int i;											// JTAG IR bit counter
	retval = 0x0;
	TMS(1);
	JTAG_StrobeTCK ();								// move to SelectDR
	TMS(1);
	JTAG_StrobeTCK ();								// move to SelectIR
	TMS(0);
	JTAG_StrobeTCK ();								// move to Capture_IR
	TMS(0);
	JTAG_StrobeTCK ();								// move to Shift_IR state
	for (i=0; i < num_bits; i++) 
	{
		TDI(instruction & 0x01);					// shift IR, LSB-first
		instruction = instruction >> 1;
		retval = retval >> 1;
		if (TDO()) 
		{
			retval |= (0x01 << (num_bits - 1));
		}
		if (i == (num_bits - 1))
		{
			TMS(1);								// move to Exit1_IR state
		}
		JTAG_StrobeTCK();
	}
	TMS(1);
	JTAG_StrobeTCK ();								// move to Update_IR
	TMS(0);
	JTAG_StrobeTCK ();								// move to RTI state
	return retval;
}

unsigned long jtag::JTAG_DR_Scan(unsigned long dat, int num_bits)
{
	unsigned long retval; // JTAG return value
	int i; // JTAG DR bit counter

	retval = 0x0L;
	TMS(1);
	JTAG_StrobeTCK (); // move to SelectDR
	TMS(0);
	JTAG_StrobeTCK (); // move to Capture_DR
	TMS(0);
	JTAG_StrobeTCK (); // move to Shift_DR state
	for (i=0; i < num_bits; i++)
	{
		TDI(dat & 0x01); // shift DR, LSB-first
		dat = dat >> 1;
		retval = retval >> 1;
		if(TDO()) 
		{
			retval |= (0x01L << (num_bits - 1));
		}
		if ( i == (num_bits - 1))
		{
			TMS(1); // move to Exit1_DR state
		}
		JTAG_StrobeTCK();
	}
	TMS(1);
	JTAG_StrobeTCK (); // move to Update_DR
	TMS(0);
	JTAG_StrobeTCK (); // move to RTI state
	return retval;
}

void jtag::JTAG_IWrite(unsigned int ireg, unsigned long dat, int num_bits)
{
	int done;							// TRUE = write complete; FALSE otherwise
	JTAG_IR_Scan (ireg, INST_LENGTH);	// load IR with <ireg>
	dat |= (0x03L << num_bits);			// append ‘WRITE’ opcode to data
										// load DR with <dat>
	JTAG_DR_Scan (dat, num_bits + 2);	// initiate the JTAG write
	
	// load DR with ‘0’, and check for BUSY bit to go to ‘0’.
	do
	{
		done = !(JTAG_DR_Scan (0x0L, 1)); // poll for JTAG_BUSY bit
	} while (!done);
}

unsigned long jtag::JTAG_IRead(unsigned int ireg, int num_bits)
{
	unsigned long retval;						// value returned from READ operation
	int done;									// TRUE = write complete; FALSE otherwise
	JTAG_IR_Scan (ireg, INST_LENGTH);			// load IR with <ireg>
	// load DR with read opcode (0x02)
	JTAG_DR_Scan (0x02L, 2);					// initiate the JTAG read
	do 
	{
		done = !(JTAG_DR_Scan (0x0L, 1));		// poll for JTAG_BUSY bit
	} while (!done);
	retval = JTAG_DR_Scan (0x0L, num_bits + 1); // allow poll operation to
	// read remainder of the bits
	retval = retval >> 1;						// shift JTAG_BUSY bit off the end
	return retval;
}

int jtag::FLASH_ByteWrite(unsigned int addr, unsigned char dat)
{
	unsigned long testval;						// holds result of FLASHDAT read
	int done;									// TRUE/FALSE flag
	int retval;									// TRUE if operation successful
	JTAG_IWrite (FLASHSCL, 0x86L, FLSC_LEN);	// set FLASHSCL based on SYSCLK
	// frequency (2MHz = 0x86)
	// set FLASHADR to address to write to
	JTAG_IWrite (FLASHADR, (unsigned long) addr, FLA_LEN);
	JTAG_IWrite (FLASHCON, 0x10L, FLCN_LEN);	// set FLASHCON for FLASH Write
	// operation (0x10)
	// initiate the write operation
	JTAG_IWrite (FLASHDAT, (unsigned long) dat, FLD_WRLEN);
	JTAG_IWrite (FLASHCON, 0x0L, FLCN_LEN);		// set FLASHCON for ‘poll’ operation
	do 
	{
		done = !(JTAG_IRead (FLASHDAT, 1));		// poll for FLBusy to de-assert
	} while (!done);
	testval = JTAG_IRead (FLASHDAT, 2);			// read FLBusy and FLFail
	retval = (testval & 0x02) ? FALSE:TRUE;		// FLFail is next to LSB
	return retval;								// return FLASH Pass/Fail
}

int jtag::FLASH_PageErase(unsigned int addr)
{
	unsigned long testval;						// holds result of FLASHDAT read
	int done;									// TRUE/FALSE flag
	int retval;									// TRUE if operation successful
	JTAG_IWrite (FLASHSCL, 0x86L, FLSC_LEN);	// set FLASHSCL based on SYSCLK
	// frequency (2MHz = 0x86)
	// set FLASHADR to address within page to erase
	JTAG_IWrite (FLASHADR, (unsigned long) addr, FLA_LEN);
	JTAG_IWrite (FLASHCON, 0x20L, FLCN_LEN);	// set FLASHCON for FLASH Erase
	// operation (0x20)
	JTAG_IWrite (FLASHDAT, 0xa5L, FLD_WRLEN);	// set FLASHDAT to 0xa5 to initiate
	// erase procedure
	JTAG_IWrite (FLASHCON, 0x0L, FLCN_LEN);		// set FLASHCON for ‘poll’ operation
	do 
	{
		done = !(JTAG_IRead (FLASHDAT, 1));		// poll for FLBusy to de-assert
	} while (!done);
	testval = JTAG_IRead (FLASHDAT, 2);			// read FLBusy and FLFail
	retval = (testval & 0x02) ? FALSE: TRUE;	// FLFail is next to LSB
	// set return value based on FLFail bit
	return retval;								// return FLASH Pass/Fail
}

int jtag::FLASH_ByteRead(unsigned int addr, unsigned char *pdat)
{
	unsigned long testval;								// holds result of FLASHDAT read
	int done;											// TRUE/FALSE flag
	int retval;											// TRUE if operation successful
	JTAG_IWrite (FLASHSCL, 0x86L, FLSC_LEN);			// set FLASHSCL based on SYSCLK
	// frequency (2MHz = 0x86)
	// set FLASHADR to address to read from
	JTAG_IWrite (FLASHADR, (unsigned long) addr, FLA_LEN);
	JTAG_IWrite (FLASHCON, 0x02L, FLCN_LEN);			// set FLASHCON for FLASH Read
	// operation (0x02)
	JTAG_IRead (FLASHDAT, FLD_RDLEN);					// initiate the read operation
	JTAG_IWrite (FLASHCON, 0x0L, FLCN_LEN);				// set FLASHCON for ‘poll’ operation
	do 
	{

		done = !(JTAG_IRead (FLASHDAT, 1));				// poll for FLBUSY to de-assert

	} while (!done);
	testval = JTAG_IRead (FLASHDAT, FLD_RDLEN);			// read the resulting data
	retval = (testval & 0x02) ? FALSE: TRUE;			// FLFail is next to LSB
	testval = testval >> 2;								// shift data.0 into LSB position
	*pdat = (unsigned char) testval;					// place data in return location
	return retval;										// return FLASH Pass/Fail
}

unsigned char jtag::CableSetup()
{
	DWORD DWTemp;
	switch(m_iCcabletype)
	{
	case ALTERABLASTER:
		{
			m_wiTDOPin = 7;
			m_iTDOAddP = 1;
			m_woTCKPin = 0;
			m_woTMSPin = 1;
			m_woTDIPin = 6;
			SetPortVal(m_nPort+2, 0x0f, 1);				//244使能 OE = 0,
			SetPortVal(m_nPort, 0x20, 1);				//设置DB25-5为高
			GetPortVal(m_nPort+1,&DWTemp,1);			//读取
			DWTemp = DWTemp & 0x10;
			if (DWTemp)									//DB25-13为高,连接OK
			{
				OutputDebugString("DB25-13连接OK");
			}
			else
			{
				OutputDebugString("DB25-13与VCC连接失败!");
				return 1;						
			}

			//判断DB25 7-10脚是否相连
			GetPortVal(m_nPort+1,&DWTemp,1);			//读取
			DWTemp = DWTemp & 0x40;						//DB25-10为高,连接OK
			if (DWTemp)									
			{
				OutputDebugString("DB25-7 -- 10 连接OK");
			}
			else
			{
				OutputDebugString("DB25-7 -- 10 连接失败");
				return 2;						
			}
			JTAG_Reset ();							// Reset the JTAG state machine on DUT
		}break;
	}
	return 0;

}

⌨️ 快捷键说明

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