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

📄 downloadcmd.cpp

📁 M16C Flash Starter Software Ver.2.0.0.46 Source Files.zip是瑞萨的M16C系列单片机的flash烧写程序。
💻 CPP
字号:
// DownloadCmd.cpp: the implementation of the CDownloadCmd class
//
// This software can be offered for free and used as necessary to aid 
// in your program developments.
//
// RENESAS TECHNOLOGY CORPORATION, RENESAS SOLUTIONS CORPORATION,
// and related original software developers assume no responsibility 
// for any damage or infringement of any third-party's rights, originating 
// in the use of the following software.
// Please use this software under the agreement and acceptance of these conditions. 
//
// Copyright(C)1998(2003) RENESAS TECHNOLOGY CORPORATION AND RENESAS SOLUTIONS CORPORATION
// ALL RIGHTS RESERVED
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "m16cflsh.h"
#include "DownloadCmd.h"
#include "DownloadDlg.h"
#include "PrgssDlg.h"
#include "SettingCmd.h"
#include "SaveIni.h"
#include "SetCommDlg.h" // add intervals
#include "defining.h"
#include "LineImage.h"
#include	"PreWriteDlg.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/disappearance
//////////////////////////////////////////////////////////////////////

CDownloadCmd::CDownloadCmd()
{

}

CDownloadCmd::~CDownloadCmd()
{

}

BOOL CDownloadCmd::exec()
{

	BOOL bResult = FALSE;
	CM16CflshApp*		pApp = (CM16CflshApp*)AfxGetApp();
	MSG				msg;
    CSetCommDlg  dlg2;

	// Get Communication object.
	CSerialComm& comObj = GetSerialComm();

	// Get Programm address range.
	unsigned int nStartAddress = 0;
	unsigned int nEndAddress = 0;
	if (GetFlashType())			// Flash Type = M16C/80 Boot loader.
	{
		if(!GetDownloadAddress(&nStartAddress, &nEndAddress, ADDRESS_SIZE))
		{
			return FALSE;
		}
	}
	else
	{
		if(!GetFileAddress(&nStartAddress, &nEndAddress, ADDRESS_SIZE))
		{
			return FALSE;
		}
	}
	
	// get Intervals time
	DWORD ndwintervals = 0;
	if(!dlg2.GetIntervals(&ndwintervals))
	{
		return FALSE;
	}


	// If a start address is larger than an end address, it will end.
	if(!ChkStartToEnd(nStartAddress, nEndAddress))
	{
		AfxGetMainWnd()->MessageBox(GetResString(IDS_SMALESTART), GetResString(IDS_FLASH_TITLE), MB_ICONWARNING);
		return FALSE;
	}

	// If Flash Type = "M16C/80 boot loader"  End address check.
	if (GetFlashType())
	{
		// If a end address is over than 0x2A00, it will end.
		if(nEndAddress >= 0x2A00)
		{
			AfxGetMainWnd()->MessageBox(GetResString(IDS_OVERENDADDRESS), GetResString(IDS_FLASH_TITLE), MB_ICONWARNING);
			return FALSE;
		}
	}


	// Download confirm.
	CDownloadDlg	dlg;
	int nResponse = dlg.DoModal();
	if(IDCANCEL == nResponse)
	{
		AfxGetMainWnd()->MessageBox(GetResString(IDS_STOP_DOWNLOAD),GetResString(IDS_FLASH_TITLE));
		return FALSE;
	}

	// Progress dialog setup.
	CProgressDlg* pDlg;
	BOOL bCancel = FALSE;
	pDlg = CProgressDlg::Instance(&bCancel, NULL);
	pDlg->InitialDialog("Downloading.", "Download", 
							0, (nEndAddress - nStartAddress - 0x0100) / TO_INT, 0);

	// Make cheksum.
	BYTE	checkSum = 0;
	for(int nSendAddress = nStartAddress; (long)nSendAddress < (long)nEndAddress; nSendAddress += 0x0100)
	{
		// Get hex data from file.
		BYTE byteData[PAGE_SIZE];
		BlankPage(byteData);
		int nPage = nSendAddress / 0x0100;
		GetPage(byteData, nPage);

		// Prepare send data.
		for(int nIndex = 0; nIndex < PAGE_SIZE; nIndex++)
		{
			checkSum += byteData[nIndex];
		}
	}

	// Make Datasize.
	int	nSendSize = nEndAddress - nStartAddress + 1;

	// Write data.
	int	sendFirst = 0;
	int	nHead	= 0;
	for(nSendAddress = nStartAddress; (long)nSendAddress < (long)nEndAddress; nSendAddress += 0x0100)
	{
		// Get hex data from file.
		BYTE byteData[PAGE_SIZE];
		BlankPage(byteData);
		int nPage = nSendAddress / 0x0100;
		GetPage(byteData, nPage);

		// Prepare send data.
		BYTE send[4 + PAGE_SIZE];
		if (sendFirst == 0)
		{
			send[0] = 0xFA;
			send[2] = nSendSize / 0x0100;
			send[1] = nSendSize - (send[2] * 0x0100);
			send[3] = checkSum;
			//send[3] = 0x55;
			//ToSendStyle(nSendSize, &send[2], &send[1]);
			nHead = 4;
			sendFirst = 1;
		}
		else
		{
			nHead = 0;
		}
		for(int nIndex = 0; nIndex < PAGE_SIZE; nIndex++)
		{
			send[nIndex + nHead] = byteData[nIndex];
		}

		// Send data.
		bResult = comObj.Write(send, nHead + PAGE_SIZE);
	
		// Prograss Bar Update.
		pDlg->SetProgressPos((nSendAddress - nStartAddress) / TO_INT);

		// Implement the event.
  		while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
			if (!pApp->PumpMessage()) {
				::PostQuitMessage(0);
				break;
			}
		}
		if(bCancel)
		{
			AfxGetMainWnd()->MessageBox(GetResString(IDS_STOP_DOWNLOAD), GetResString(IDS_FLASH_TITLE), MB_ICONINFORMATION);
			pDlg->Done();
			return TRUE;
			//break;
		}

		// Wait next Communicate.
		//Sleep(SLEEP_IN); // change set Intervals time
		Sleep(ndwintervals);

	}
	pDlg->Done();

	if (GetFlashType())
	{						// Flash Type = M16C/80 boot loader
		// Get Download result.
		BYTE btRESULT[2];
		if(!comObj.Read(btRESULT, 2))
		{
			//Download Error.
			//CommError();
			AfxGetMainWnd()->MessageBox(GetResString(IDS_DOWNLOAD_NG), GetResString(IDS_FLASH_TITLE), MB_ICONWARNING);
			return FALSE;
	
		}
		else if (btRESULT[1] == 0x01)
		{
			//Download Error.
			//CommError();
			AfxGetMainWnd()->MessageBox(GetResString(IDS_DOWNLOAD_NG), GetResString(IDS_FLASH_TITLE), MB_ICONWARNING);
			return FALSE;
	
		}
		else if (btRESULT[1] == 0x00)
		{
			//Download Completed.
			AfxGetMainWnd()->MessageBox(GetResString(IDS_DOWNLOAD_OK),GetResString(IDS_FLASH_TITLE));
		}
	}
	else
	{						// Flash Type = Internal Flash memory.
			//Download Completed.
			AfxGetMainWnd()->MessageBox(GetResString(IDS_DOWNLOAD_OK),GetResString(IDS_FLASH_TITLE));
	}

	return	TRUE;

}

⌨️ 快捷键说明

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