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

📄 flashupgraderdlg.cpp

📁 PW芯片方案Flash ROM烧写程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	if (m_bGoMode)
	{
		PostMessage(WM_COMMAND, HIWORD(BN_CLICKED)|LOWORD(IDC_BUT_FLASH), (long)m_butFlash.m_hWnd);
	}

	UpdateControls();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CFlashUpgraderDlg::OnDestroy()
{
	WinHelp(0L, HELP_QUIT);
	CDialog::OnDestroy();
}

// 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 CFlashUpgraderDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFlashUpgraderDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CFlashUpgraderDlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CFlashUpgraderDlg::OnOK() 
{
	if (CanExit())
		CDialog::OnOK();
}

void CFlashUpgraderDlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CFlashUpgraderDlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}

void CFlashUpgraderDlg::OnSelchangeCmbConnection() 
{
	UpdateData();

	EnableSerialControls();
}

void CFlashUpgraderDlg::EnableSerialControls(BOOL bEnable)
{
	BOOL bEn = bEnable && (m_strConnection == "Serial");
	m_cmbComPort.EnableWindow(bEn);
	m_cmbBaudRate.EnableWindow(bEn);
}

void CFlashUpgraderDlg::OnButBrowse() 
{
	static char BASED_CODE szFilter[] = "Firmware Flash Files (*.inf)|*.inf|All Files (*.*)|*.*||";

	UpdateData();

	if (this->m_strInfFile.IsEmpty())
	{
		m_strInfFile = pTheApp->m_strLaunchDir+"..\\embedded\\bin\\pwSDK.inf";
	}

	CString strFile = pTheApp->GetFilename(m_strInfFile);
	CString strPath = pTheApp->GetPath(m_strInfFile);

	if (!strPath.IsEmpty())
	{
		SetCurrentDirectory(strPath);
	}

	if (strFile.IsEmpty())
	{
		strFile = "pwSDK.inf";
	}

	CFileDialogPW fileDlg(atFLASHUPGRADER, TRUE, !m_bBrowsed, "Select a flash file to open",
		                  TRUE, "inf", (LPCSTR)strFile, OFN_HIDEREADONLY, szFilter, this);
	
	if (IDOK == fileDlg.DoModal())
	{
		CString strPath = fileDlg.GetPathName();
		
        m_bBrowsed = TRUE;
		m_strDirectory = pTheApp->GetPath(strPath);
		m_strInfFile = pTheApp->GetFilename(strPath);
		UpdateData(FALSE);

		m_cmbInfFile.SetCurSel(GatherInfFiles(pTheApp->GetPath(m_strInfFile), pTheApp->GetFilename(m_strInfFile)));		

		ProcessInfFile();
	}
}

void CFlashUpgraderDlg::OnButFlash() 
{
	//dennisd - auto flash
	if(m_bNoReset)
	{
		m_eState = psNONE;
		// Open COM port
		CreateComPort();
		CComPortCmd *pComPort = GetComPortCmdPtr();
		BOOL bStatus = TRUE;
		pComPort->SetBaudRate(19200);
		if (m_nComPort == -1)
		{
			pComPort->m_eComm = ccUSB;
		}
		if (m_nComPort > -1 )// Not USB 
		{
			if (!m_bFirstTime)
			{
				Sleep(500);
			}
			m_bFirstTime = FALSE;
			bStatus = pComPort->SetComPort(m_nComPort + 1);
		}
		if (FALSE == bStatus)
   		{
			CString strError;
			strError.Format("Unable to open the specified port. Ensure that\n no other application or device driver is using it.");
			DisplayErrorMsg(strError);
		}
		else
		{
			//
			// wdten_code 0x2a
			//
			pComPort->SendData(0xbe);
			pComPort->SendData(0xef);
			pComPort->SendData(0x0b);
			pComPort->SendData(0x10);
			pComPort->SendData((BYTE)0x00);
			pComPort->SendData(0x70);
			pComPort->SendData(0x4b);
			pComPort->SendData(0x01);
		
			for(int ii=0; ii < 8; ii++)
				pComPort->SendData((BYTE)0x00);

			pComPort->SendData(g_iResetWatchDogAddr & 0xff);
			pComPort->SendData((g_iResetWatchDogAddr >> 8) & 0xff);
			pComPort->SendData((g_iResetWatchDogAddr >> 16) & 0xff);
			pComPort->SendData((BYTE)0x00);
			pComPort->SendData(0x01);
			pComPort->SendData((BYTE)0x00);
			pComPort->SendData(g_iResetWatchDogData);

			Sleep(g_iResetDelay1);

			//
			// DCRST 1
			//
			pComPort->SendData(0xbe);
			pComPort->SendData(0xef);
			pComPort->SendData(0x0b);
			pComPort->SendData(0x10);
			pComPort->SendData((BYTE)0x00);
			pComPort->SendData(0xb5);
			pComPort->SendData(0x50);
			pComPort->SendData(0x01);
		
			for(ii=0; ii < 8; ii++)
				pComPort->SendData((BYTE)0x00);
	
			pComPort->SendData(g_iResetAddr & 0xff);
			pComPort->SendData((g_iResetAddr >> 8) & 0xff);
			pComPort->SendData((g_iResetAddr >> 16) & 0xff);
			pComPort->SendData((BYTE)0x00);
			pComPort->SendData(0x01);
			pComPort->SendData((BYTE)0x00);
			pComPort->SendData(g_iResetData);

			Sleep(g_iResetDelay2);
		}
	}


	// if we want binary image rather than flashing - here is shortcut.
	if( m_chkBinary ) {
		CaptureToImage();
		return;
	}

	if (m_bFlashingInProgress)
	{
		DeleteButton();
	}
	else
	{
		m_bCancel = FALSE;
		//
		// Getting ready to start the flash operation.  Disable all the controls so
		// that the user doesn't try to change anything mid flash.
		//
		DisableControls(TRUE);

		//
		// Start the Flash Threads
		//
		m_bFlashingInProgress = TRUE;
		if(!BeginFlashProcess())
			DeleteButton();
	}
}

void CFlashUpgraderDlg::OnSelchangeCmbInffile() 
{
	UpdateData(TRUE);
	ProcessInfFile();
}

void CFlashUpgraderDlg::OnCmdEnterKey()
{
	// Ignore enter key...
	return;
}

void CFlashUpgraderDlg::OnButClose() 
{
	EndDialog(IDOK);	
}

int CFlashUpgraderDlg::GetServerList()
{
	m_Servers.RemoveAll();

	EnumWindows( (WNDENUMPROC) GatherWndTitles, (LPARAM) &m_Servers);

	if (m_Servers.IsEmpty() || m_Servers.GetCount()==1)
	{
		m_bFirstInstance = TRUE;
		return 0;
	}
	else
	{
		m_bFirstInstance = FALSE;
		return m_Servers.GetCount();
	}
}

void CFlashUpgraderDlg::OnKillfocusCmbInffile() 
{
	// TODO: Add your control notification handler code here
	
}

int CFlashUpgraderDlg::GatherInfFiles(CString strPath, CString strFile)
{
	// Gather a list of all the inf file in the specified directory
	// and populate the dropdown box with these files.
	int nIndex = -1;

	CString strTarget = strFile;
	if (strTarget.IsEmpty())
	{
		strTarget = "pwsdk.inf";
	}

//	strTarget.MakeUpper(); // MIKEH 05/24/2005- Don't change case. Doesn't work with Linux shared files

	// First, remove any entries already in the dropdown...
	m_cmbInfFile.ResetContent();

	int nAdd = 0;
	CFileFind finder;
	BOOL bWorking = finder.FindFile(strPath+"*.inf");
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		CString strName = finder.GetFileName();
		
		// dennisd - auto flash
		if(strName != "FlashReset.inf")
		{
			nAdd = m_cmbInfFile.AddString(strName);
		
			// strName.MakeUpper(); // MIKEH 05/24/2005- Don't change case. Doesn't work with Linux shared files
			if (0 == strName.CompareNoCase(strTarget)) // MIKEH 05/24/2005- Changed to CompareNoCase
			{
				nIndex = nAdd;
			}
		}
	}

	return nIndex;
}


// dennisd - auto flash
BOOL CFlashUpgraderDlg::ProcessResetInfFile(CString szChip)
{
	CString strVal;

	CStdioFile InfFile;
	CString strTmp =  "..\\embedded\\bin\\FlashReset.inf";

	if (!m_InfFile.OpenInfFile(strTmp))
	{
		return FALSE;
	}

	m_InfFile.GetLineText(szChip, "WATCH_DOG_ON_ADDR", strVal);
	g_iResetWatchDogAddr = atoi(strVal);

	m_InfFile.GetLineText(szChip, "WATCH_DOG_ON_DATA", strVal);
	g_iResetWatchDogData = atoi(strVal);

	m_InfFile.GetLineText(szChip, "DELAY1", strVal);
	g_iResetDelay1 = atoi(strVal);

	m_InfFile.GetLineText(szChip, "DCRST_ADDR", strVal);
	g_iResetAddr = atoi(strVal);

	m_InfFile.GetLineText(szChip, "DCRST_DATA", strVal);
	g_iResetData = atoi(strVal);
	
	m_InfFile.GetLineText(szChip, "DELAY2", strVal);
	g_iResetDelay2 = atoi(strVal);

	return TRUE;
}

BOOL CFlashUpgraderDlg::ProcessInfFile()
{
	// Make sure we are looking at the correct file...
	UpdateData(TRUE);

	CStdioFile InfFile;

	DeleteHexFileData();

	CString strTmp = m_strDirectory + m_strInfFile;

	CString strPath = pTheApp->GetPath(strTmp);

	if (!InfFile.Open(strTmp, CFile::modeRead))
	{
		return FALSE;
	}

	CString strLine;
	int nCount = 0;

	//
	// Quickly count the number of files to be processed...
	//
	while (InfFile.ReadString(strLine))
	{
		CString strKey = strLine.Left(strLine.Find("=",0));
		strKey.TrimLeft();
		strKey.TrimRight();

		BOOL bExecute = (strKey.CompareNoCase("EXECUTEFILE")==0);
		BOOL bFlash = (strKey.CompareNoCase("FILE")==0);
		BOOL bVerify = (strKey.CompareNoCase("VERIFYFILE")==0);

		// See if this line specifies a hex file to be flashed.
		if (bExecute || bFlash || bVerify)
		{	
			nCount++;
		}
	}

	InfFile.SeekToBegin();

	m_ctrlProgress.SetRange(0, nCount);
	m_ctrlProgress.SetStep(1);
	m_ctrlProgress.SetPos(0);

	while (InfFile.ReadString(strLine))
	{
		CString strKey = strLine.Left(strLine.Find("=",0));
		strKey.TrimLeft();
		strKey.TrimRight();

		BOOL bExecute = (strKey.CompareNoCase("EXECUTEFILE")==0);
		BOOL bFlash = (strKey.CompareNoCase("FILE")==0);
		BOOL bVerify = (strKey.CompareNoCase("VERIFYFILE")==0);

		// See if this line specifies a hex file to be flashed.
		if (bExecute || bFlash || bVerify)
		{
			CString strFile = strLine.Mid(strLine.Find("=")+1);

			strFile.TrimRight(" \t=\"");
			strFile.TrimLeft(" \t=\"");

			m_strFileName = "Processing "+strFile+"...";
			UpdateData(FALSE);
			m_ctrlProgress.StepIt();

			//strFile.MakeLower(); // MIKEH 05/24/2005- Don't case. Doesn't work with Linux shared files
			GatherHexFileRegions(strPath, strFile, bExecute, bVerify);
		}

		if (strKey.CompareNoCase("RESET")==0)
		{
			CString strValue = strLine.Mid(strLine.Find("=",0)+1);
			m_bResetAfterLastFileLoaded = strValue.CompareNoCase("Enable")==0;
		}
	}

	InfFile.Close();

	LoadRegionList();

	//
	// Save off the current settings in the registry...
	// 1. INF Filename
	//
	CString strKey;
	strKey.LoadString(IDS_REGKEY_BASE);
	CString strValueName;
	strValueName.LoadString(IDS_REGVALUE_INFFILE);
	gSetValueInRegistry(HKEY_CURRENT_USER, strKey, strValueName, (LPCSTR)m_strInfFile);

	m_strFileName = "";
	UpdateData(FALSE);
	m_ctrlProgress.SetPos(0);

	return TRUE;
}

BOOL CFlashUpgraderDlg::GatherHexFileRegions(CString strPath, CString strHexFile, BOOL bExecute, BOOL bVerify)
{
	BOOL bResult = TRUE;
	CStdioFile HexFile;
	CWaitCursor WaitCur;

    BOOL bStartOffset = FALSE; // PATCH CR8629
	//  Parse the hex file gathering up all the regions that will be flashed...
	CHexFileData* pHex = new CHexFileData;
	pHex->m_strName = strHexFile;
	pHex->m_strPath = strPath;

	m_HexFiles.AddTail((void*)pHex);

	BOOL m_bOpened = HexFile.Open(strPath+strHexFile, CFile::modeRead);

	if (!m_bOpened)
	{
		pHex->m_bMissing = TRUE;
		bResult = FALSE;
	}

	// 
	// Only gather region info on HEX files.

⌨️ 快捷键说明

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