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

📄 jtagdlg.cpp

📁 原创
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// jtagDlg.cpp : implementation file
//

#include "stdafx.h"
#include "jtag.h"
#include "jtagDlg.h"
//#include "jtagdef.h"

#include <stdio.h>
#include <conio.h>
#include "def.h"

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


#define  ASSERT_ADDR_FOR_GCS 0x2000000

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CWinThread	*pThreadProgram;
U8 chrFileBuffer[MAX_FILE_BUFFER]={0};
U8 chrFlashBuffer[MAX_FILE_BUFFER]={0};
int iFileSize;

UINT _ProgramFlashThread(LPVOID lparam)
{		
	CJtagDlg *pDlg=(CJtagDlg *)lparam;		
	unsigned int uiStateAddr = 0;
	char *pStopString;	
	CString tempstr;
	CString strTemp;
	tempstr.Empty();
	pDlg->iPrograming = 1;
	if(iFileSize == 0)
	{
		tempstr.Format("Program failure!");		
	}
	else
	{
		pDlg->NOR_Init();
		((CStatic *)pDlg->GetDlgItem(IDC_EDIT_START_ADDR))->GetWindowText(pDlg->strStartAddr);		
		uiStateAddr = ::strtoul(pDlg->strStartAddr, &pStopString, 16);
		pDlg->NOR_Program(uiStateAddr, chrFileBuffer, iFileSize);
	}
	
	if(pDlg->OnCheckFlash(chrFileBuffer, chrFlashBuffer, iFileSize))
	{
		AfxMessageBox("烧写失败!", MB_ICONINFORMATION | MB_OK);
	}
	else
	{
		AfxMessageBox("烧写成功!", MB_ICONINFORMATION | MB_OK);
	}

	pDlg->iPrograming = 0;

	return 0;
}

UINT _ProgramFlashReadThread(LPVOID lparam)
{		
	CJtagDlg *pDlg=(CJtagDlg *)lparam;		
	int iReadLength=0;
	unsigned int uiReadFlash=0;
	int i=0;
	char *pStopString;
	CString strTemp;

	pDlg->GetDlgItem(IDC_EDIT_FLASH_CONTENT)->SetWindowText("");	
	strTemp.Empty();
	pDlg->strReadAddr.Empty();
	pDlg->strReadLength.Empty();
	((CStatic *)pDlg->GetDlgItem(IDC_EDIT_READ_ADDR))->GetWindowText(pDlg->strReadAddr);
	((CStatic *)pDlg->GetDlgItem(IDC_EDIT_READ_LENGTH))->GetWindowText(pDlg->strReadLength);
	
	iReadLength = ::atoi(pDlg->strReadLength);
	uiReadFlash = ::strtol(pDlg->strReadAddr, &pStopString,16);
	
	if(iReadLength > MAX_FILE_BUFFER)
	{
		strTemp.Format("读取长度出错!");
		AfxMessageBox(strTemp, MB_ICONINFORMATION | MB_OK);
		iReadLength = 0;
	}
	else
	{ 	
		//读FLASH数据
		pDlg->m_program.ShowWindow(TRUE);
		pDlg->m_program.SetRange32(0, iReadLength);
		pDlg->m_program.SetPos(0);
		pDlg->GetDlgItem(IDC_STATIC_FLASH_STATE)->SetWindowText("正在读FLASH......");
		
		pDlg->NOR_Reset();
		for(i=0; i<iReadLength; i++)
		{
			chrFlashBuffer[i] = (unsigned char)pDlg->NOR_RdHW(uiReadFlash);
			uiReadFlash++;
			pDlg->m_program.SetPos(i);
		}
		pDlg->DisplayImageFile(chrFlashBuffer, iReadLength, 1);
		pDlg->GetDlgItem(IDC_STATIC_FLASH_STATE)->SetWindowText("读FLASH完成!");	

	}		
	return 0;
}

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJtagDlg dialog

CJtagDlg::CJtagDlg(CWnd* pParent /*=NULL*/)
: CDialog(CJtagDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CJtagDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CJtagDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CJtagDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_program);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CJtagDlg, CDialog)
//{{AFX_MSG_MAP(CJtagDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_GET_CPU, OnButtonGetCpu)
ON_BN_CLICKED(IDC_BUTTON_GET_FLASH, OnButtonGetFlash)
ON_BN_CLICKED(IDC_BUTTON_ERASE, OnButtonEraseFlash)
ON_BN_CLICKED(IDC_BUTTON_OPEN_FILE, OnButtonOpenFile)
ON_BN_CLICKED(IDC_BUTTON_WRITE_FLASH, OnButtonWriteFlash)
ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonReadFlash)
ON_BN_CLICKED(IDC_BUTTON_WRITE_TEST, OnButtonWriteTest)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJtagDlg message handlers

BOOL CJtagDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_fileName.Empty();
    m_program.ShowWindow(TRUE);
	OpenPpt();
	
	iFileSize=0;
	iPrograming = 0;//烧写完成或没有烧写
	
	strCpuId.Empty();
	strFlashName.Empty();
	strFlashId.Empty();
	strFlashManuf.Empty();	
	
	strReadAddr.Format("0x00");
	strStartAddr.Format("0x00");
	strReadLength.Format("1024");
	
	GetDlgItem(IDC_EDIT_START_ADDR)->SetWindowText(strStartAddr);
	GetDlgItem(IDC_EDIT_READ_ADDR)->SetWindowText(strReadAddr);
	GetDlgItem(IDC_EDIT_READ_LENGTH)->SetWindowText(strReadLength);
	
    ((CComboBox *)GetDlgItem(IDC_COMBO1))->SetCurSel(0);
	//i=((CComboBox *)GetDlgItem(IDC_COMBO1))->GetCurSel();
	
	GetDlgItem(IDC_EDIT1)->SetWindowText("0xffffffff");
	GetDlgItem(IDC_EDIT2)->SetWindowText("0xff");
	GetDlgItem(IDC_EDIT3)->SetWindowText("0xff");
	   
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CJtagDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CJtagDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CJtagDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CJtagDlg::OnButtonGetCpu() 
{
	// TODO: Add your control notification handler code here
	if(iPrograming==1)
	{
		if(AfxMessageBox("正在烧写FLASH,确定退出吗?", MB_ICONINFORMATION | MB_OK | IDCANCEL) == MB_OK)
			return;
	}
	JTAG_ReadId();	
}

void CJtagDlg::OnButtonGetFlash() 
{
	// TODO: Add your control notification handler code here
    S4510_InitCell();	
	JTAG_Init();	//enter drive mode
	CString strInfo;	
	int id, vid, pid;
	if(iPrograming==1)
	{
		if(AfxMessageBox("正在烧写FLASH,确定退出吗?", MB_ICONINFORMATION | MB_OK | IDCANCEL) == MB_OK)
			return;
	}
	id = NOR_ReadId();
	vid = id>>16;
	pid = id&0xffff;
	strInfo.Empty();
	strFlashId.Empty();
	strFlashManuf.Empty();
	switch(vid)
	{
	case 0xad:
		switch(pid)
		{
		case 0xda:
			strFlashName.Format("Hynix29lv800T");
			break;
		case 0x5b:
			strFlashName.Format("Hynix29lv800B");
			break;
		default:
			strFlashName.Format("Unknow Hynix Flash!");
			strInfo.Format("Unknow Hynix Flash is detected!\n");
		break;
		}
	break;
	case 0xc2:
		switch(pid)
		{
		case 0xda:
			strFlashName.Format("MXic29lv800T");
			break;
		case 0x5b:
			strFlashName.Format("MXic29lv800B");			
			break;
		default:
			strFlashName.Format("Unknow MXic Flash!");
			strInfo.Format("Unknow MXic Flash is detected!\n");
		break;
		}
	break;
	case 0x01: 
		switch(pid)
		{
		case 0x49:
			strFlashName.Format("AM29lv160B");			
			break;
		case 0x5b:
			strFlashName.Format("AM29lv800B");	
			break;					
		case 0x4c:
			strFlashName.Format("AM29lv160T");			
			break;
		default:
			strFlashName.Format("Unknow AMD Flash!");
			strInfo.Format("Unknow AMD Flash is detected!\n");
		break;
		}
	break;
	case 0xbf://制造商ID地址0000H  数据 00BFH; 
		switch(pid)
		{
		case 0x82://器件ID(SST39LF/VF160)地址0001H  数据2782H
			strFlashName.Format("SST39VF160");	
			break;
		default:
			strFlashName.Format("Unknow SST Flash!");
			strInfo.Format("Unknow SST Flash is detected!\n");
		break;
		}
	break;
	default:
		strFlashName.Format("Unknow Flash!");
		strInfo.Format("Unknow Flash is detected!\n");
		break;					
	}
	
	if(strInfo.IsEmpty())
	{
		strInfo.Format("%s(VID=0x%02x PID=0x%02x) is detected!\n\n", strFlashName, vid, pid);	
		strFlashId.Format("0x%02x", vid);
		GetDlgItem(IDC_EDIT2)->SetWindowText(strFlashId);
		strFlashManuf.Format("0x%02x", pid);
		GetDlgItem(IDC_EDIT3)->SetWindowText(strFlashManuf);
	}
	else
		strInfo.Format("Detecte flash error: %s(VID=0x%02x PID=0x%02x)!\n\n", strFlashName, vid, pid);

	
	GetDlgItem(IDC_STATIC_DPFLASH)->SetWindowText(strFlashName);
	AfxMessageBox(strInfo, MB_ICONINFORMATION | MB_OK);
	//exit(0);
}


/**paralle function application**/
int CJtagDlg:: GetValidPpt(void)
{
	// search for valid parallel port
	_outp(LPT1, 0x55);
	if((int)_inp(LPT1) == 0x55)
		return LPT1;
	
	_outp(LPT2, 0x55);
	if((int)_inp(LPT2) == 0x55)
		return LPT2;
	
	_outp(LPT3, 0x55);
	if((int)_inp(LPT3) == 0x55)
		return LPT3;
	
	return 0;	
	
}

void CJtagDlg:: SetPptCompMode(void)
{
    //configure the parallel port at the compatibility mode.
    _outp(validPpt+ECP_ECR, ECR_STANDARD | ECR_DISnERRORINT | ECR_DISDMA | ECR_DISSVCINT);
}

int CJtagDlg:: InstallGiveIo(void)
{
    HANDLE h;
    OSVERSIONINFO osvi;
    
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osvi);
	
    if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
		//OS=NT/2000/XP
		h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL,
			OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		
		//CloseHandle(h);
		if(h == INVALID_HANDLE_VALUE)
		{
			//打开文件失败
			DWORD dwError=GetLastError();
			if(dwError==ERROR_FILE_NOT_FOUND)
			{				
				setUpDrive();
				CloseHandle(h);
				h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

				CloseHandle(h);
				if(h == INVALID_HANDLE_VALUE)
				{
					//打开文件失败
					AfxMessageBox("ERROR: Couldn't open giveio.sys");					
					return 0;
				}
				else
				{
					return 0x2000;
				}

			}
			else
				AfxMessageBox("ERROR: Fail to open giveio.sys!");
			CloseHandle(h);
			return 0;
		}
		else
		{
			CloseHandle(h);
			return 0x2000;
		}
    }
    else
    {	//OS=WIN98
		return 0x0098;
    }
}


/**flash function application**/
void CJtagDlg:: S4510_Assert_nGCS(U32 addr)
{  
	if(addr<ASSERT_ADDR_FOR_GCS)
		S4510_SetPin(nRCS0 ,LOW);
}

void CJtagDlg:: S4510_Deassert_nGCS(U32 addr)
{
    if(addr<ASSERT_ADDR_FOR_GCS)
		S4510_SetPin(nRCS0 ,HIGH);
}

/* write a half word to the address */
void CJtagDlg:: NOR_WrHW(U32 addr,U16 data)
{
    S4510_SetPin(DATA0_31_CON, LOW); //HIGH=input, LOW=output
    S4510_SetAddr(addr);
    S4510_Assert_nGCS(addr);
	
    S4510_SetDataHW(data);
    JTAG_ShiftDRStateNoTdo(outCellValue); //tCOS
	
    S4510_SetPin(nWBE0, LOW);
    JTAG_ShiftDRStateNoTdo(outCellValue); 
	
    S4510_SetPin(nWBE0, HIGH);
    JTAG_ShiftDRStateNoTdo(outCellValue); //tCOH
	
    S4510_SetPin(DATA0_31_CON, HIGH);     //nBE is deasserted here.
	
    S4510_Deassert_nGCS(addr);
    JTAG_ShiftDRStateNoTdo(outCellValue); 
}
/* write a byte to the address */
void CJtagDlg:: NOR_WrByte(U32 addr,U8 data)
{
    S4510_SetPin(DATA0_31_CON, LOW); //HIGH=input, LOW=output
    S4510_SetAddr(addr);
    S4510_Assert_nGCS(addr);
	
    S4510_SetDataByte(data);
    JTAG_ShiftDRStateNoTdo(outCellValue); //tCOS
	
    S4510_SetPin(nWBE0, LOW);
    JTAG_ShiftDRStateNoTdo(outCellValue); 
	
    S4510_SetPin(nWBE0, HIGH);
    JTAG_ShiftDRStateNoTdo(outCellValue); //tCOH
	
    S4510_SetPin(DATA0_31_CON, HIGH);     //nBE is deasserted here.
	
    S4510_Deassert_nGCS(addr);
    JTAG_ShiftDRStateNoTdo(outCellValue); 
}

// Read a half word from the address
short CJtagDlg:: NOR_RdHW(U32 addr)
{
	S4510_SetPin(DATA0_31_CON, HIGH); 
	
    S4510_SetAddr(addr);

⌨️ 快捷键说明

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