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

📄 eprcmd.cpp

📁 M16C Flash Starter Software Ver.2.0.0.46 Source Files.zip是瑞萨的M16C系列单片机的flash烧写程序。
💻 CPP
字号:
// EPRCmd.cpp: the implementation of the CEPRCmd class.
// add EPRCommand function
//
// 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 "EraseCmd.h"
#include "EPRCmd.h"

#include "SetCommDlg.h"

#include "PrgssDlg.h"

#include "PreWriteDlg.h"

#include "SaveIni.h"



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

//////////////////////////////////////////////////////////////////////
// CEPRCommand Class
//////////////////////////////////////////////////////////////////////

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

CEPRCmd::CEPRCmd()
{

}

CEPRCmd::~CEPRCmd()
{

}


//////////// This is the minute when I added later. //////////////////////////////////////////
BOOL CEPRCmd::exec(void)
{

	// Erase command 
	{
		BOOL	bResult = FALSE;
		BOOL	bResult0 = FALSE;
		BOOL	bResult1 = FALSE;
		BOOL	bResult2 = FALSE;
		BOOL	bResult3 = FALSE;
		BYTE	send[4];
	BYTE	e_type = 0; // erace type normal = 0, apple_old =1
	CString	D_Type = "-a"; // f_sptr type nomal = -a, apple = +a

	//I acquire the communication object.  
	CSerialComm& comObj = GetSerialComm();

	// check version
	// Get result.
	{
	BYTE send = 0xFB;
	BYTE btVER[8];
	BYTE ctVER[8];
	ctVER[0] = 'V';
	ctVER[1] = 'E';
	ctVER[2] = 'R';
	ctVER[3] = '.';
	ctVER[4] = '2';
	ctVER[5] = '.';
	ctVER[6] = '0';
	ctVER[7] = '3';
	ctVER[8] = '\0';
	comObj.Write(&send, 1);
	if(!comObj.Read(btVER, 8))
	{
		//CommError();
		//return FALSE;
		btVER[0] = 'E';
		btVER[1] = 'R';
		btVER[2] = 'R';
		btVER[3] = 'O';
		btVER[4] = 'R';
		btVER[5] = '\0';
	}
	else
	{
		btVER[8] = '\0';
	}
    if(strcmp ( (const char *)btVER, (const char *)ctVER) >0 )
	{
		e_type = 0; // apple old
	}else{
		e_type = 1; // normal

	}
#if	_DEBUG
	printf(" chip ver = %s \n",btVER);
	printf(" check ver = %s \n",ctVER);
#endif
	}
	CSaveIni objInit;
	if(NULL != objInit.LoadDType())
	{
		objInit.GetDType(&D_Type);
	}
#if	_DEBUG
	printf(" func type = %s\n", D_Type);
	printf(" ver flg 1:old 0:new = %i \n", e_type); 
	printf("elase if \n");
#endif
	if( e_type == 1 && D_Type == "+a"){// apple old
#if	_DEBUG
		printf("apple old  \n");
#endif
		//I acquire the communication object.  
		CSerialComm& comObj = GetSerialComm();

		//It does status the clear.
		send[0] = 0x50;
		comObj.Write(&send[0], 1);

		//I wait until next it becomes a transmission passable.
		Sleep(SLEEP_IN);

		//A command is transmitted.
		send[0] = 0x75;
		comObj.Write(&send[0], 1);
		Sleep(SLEEP_IN);
		send[0] = 0x20;
		send[1] = 0xff;
		send[2] = 0x0f;
		send[3] = 0xd0;
		bResult = comObj.Write(send, 4);

		//I wait until next it becomes a transmission passable.
		Sleep(SLEEP_IN);

		// add M16C/20 2Mhz 
		GetSerialComm().SetTimeout(TIMEOUT_ELS);

		//I check status.
		bResult0 = !CheckState(&comObj, "erase");

		//A command is transmitted.
		send[0] = 0x75;
		comObj.Write(&send[0], 1);
		Sleep(SLEEP_IN);
		send[0] = 0x20;
		send[1] = 0xbf;
		send[2] = 0x0f;
		send[3] = 0xd0;
		bResult = comObj.Write(send, 4);

		//I wait until next it becomes a transmission passable.
		Sleep(SLEEP_IN);

		// add M16C/20 2Mhz 
		GetSerialComm().SetTimeout(TIMEOUT_ELS);

		//I check status.
		bResult1 = !CheckState(&comObj, "erase");

		GetSerialComm().SetTimeout(TIMEOUT5);
		// add M16C/20 2Mhz end

		//A command is transmitted.
		send[0] = 0x75;
		comObj.Write(&send[0], 1);
		Sleep(SLEEP_IN);
		send[0] = 0x20;
		send[1] = 0x9f;
		send[2] = 0x0f;
		send[3] = 0xd0;
		bResult = comObj.Write(send, 4);

		//I wait until next it becomes a transmission passable.
		Sleep(SLEEP_IN);
		// add M16C/20 2Mhz 
		GetSerialComm().SetTimeout(TIMEOUT_ELS);
		//I check status.
		bResult2 = !CheckState(&comObj, "erase");


		GetSerialComm().SetTimeout(TIMEOUT5);


		//A command is transmitted.
		send[0] = 0x75;
		comObj.Write(&send[0], 1);
		Sleep(SLEEP_IN);
		send[0] = 0x20;
		send[1] = 0x7f;
		send[2] = 0x0f;
		send[3] = 0xd0;
		bResult = comObj.Write(send, 4);

		//I wait until next it becomes a transmission passable.
		Sleep(SLEEP_IN);
		// add M16C/20 2Mhz 
		GetSerialComm().SetTimeout(TIMEOUT_ELS);
		//I check status.
		bResult3 = !CheckState(&comObj, "erase");

		if((!bResult0) && (!bResult1) &&(!bResult2) &&(!bResult3))
		{
			bResult = FALSE;
		}	


		if(!bResult)
		{
			AfxGetMainWnd()->MessageBox(GetResString(IDS_ERASE_ERROR), GetResString(IDS_FLASH_TITLE), MB_ICONERROR);
			return	bResult;
		}
		else
		{
			// Check box not make
			// AfxGetMainWnd()->MessageBox(GetResString(IDS_ERASE_OK), GetResString(IDS_FLASH_TITLE), MB_ICONINFORMATION);
			
		}
		GetSerialComm().SetTimeout(TIMEOUT5);
		// add M16C/20 2Mhz end
		}else{	// nomal
#if _DEBUG
			printf("apple old  \n");
#endif
			//I acquire the communication object.  
			CSerialComm& comObj = GetSerialComm();

			//It does status the clear.
			send[0] = 0x50;
			comObj.Write(&send[0], 1);

			//I wait until next it becomes a transmission passable.
			Sleep(SLEEP_IN);

			//A command is transmitted.
			send[0] = 0x75;
			comObj.Write(&send[0], 1);
			Sleep(SLEEP_IN);
			send[0] = 0xa7;
			send[1] = 0xd0;
			bResult = comObj.Write(send, 2);

			//I wait until next it becomes a transmission passable.
			Sleep(SLEEP_IN);

			// add M16C/20 2Mhz 
			GetSerialComm().SetTimeout(TIMEOUT_ELS);

			//I check status.
			bResult = !CheckState(&comObj, "erase");
			if(!bResult)
			{
				AfxGetMainWnd()->MessageBox(GetResString(IDS_ERASE_ERROR), GetResString(IDS_FLASH_TITLE), MB_ICONERROR);
				return	bResult;
			}
			else
			{
				// Check box not make
				// AfxGetMainWnd()->MessageBox(GetResString(IDS_ERASE_OK), GetResString(IDS_FLASH_TITLE), MB_ICONINFORMATION);
			
			}
		GetSerialComm().SetTimeout(TIMEOUT5);
		// add M16C/20 2Mhz end
		}
	}

	// Program Command

	unsigned int nStartAddress = 0;
	unsigned int nEndAddress = 0;
	BOOL bIsEnd;
	{
		BOOL bResult = FALSE;
		CM16CflshApp*		pApp = (CM16CflshApp*)AfxGetApp();
		MSG				msg;
		CSetCommDlg  dlg2;

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

		// Get Programm address range.
		// if(!GetFileAddress(&nStartAddress, &nEndAddress, ADDRESS_SIZE))
		// {
		// 	return FALSE;
		// }
		GetFileAddressOnly(&nStartAddress, &nEndAddress);

		// 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;
		}

		// write confirm.
		// CPreWriteDlg dlg;
		// dlg.SetAddress(HexToStr(nStartAddress), HexToStr(nEndAddress));
		// int nResponse = dlg.DoModal();
		// if(IDCANCEL == nResponse)
		// {
		//	AfxGetMainWnd()->MessageBox(GetResString(IDS_STOP_PROGRAM), GetResString(IDS_FLASH_TITLE));
		//	return FALSE;
		//}

		// Clear status
		BYTE send = 0x50;
		comObj.Write(&send, 1);

		// Wait for Communication.
		Sleep(SLEEP_IN);

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

		// add 99.4.27 for M16C/20 2Mhz
		GetSerialComm().SetTimeout(TIMEOUT_PRG);
		// Write data.
		for(int nSendAddress = nStartAddress; (long)nSendAddress < (long)nEndAddress; nSendAddress += 0x0100)
		{
			// Get hex data from file.
			BYTE byteData[PAGE_SIZE];
			ClearPage(byteData);
			int nPage = nSendAddress / 0x0100;
			GetPage(byteData, nPage);

			// Prepare send data.
			BYTE send[3 + PAGE_SIZE];
			send[0] = 0x41;
			ToSendStyle(nSendAddress, &send[2], &send[1]);
			for(int nIndex = 0; nIndex < PAGE_SIZE; nIndex++)
			{
				send[nIndex + 3] = byteData[nIndex];
			}

			// Send data.
			bResult = comObj.Write(send, 3 + 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_PROGRAM), GetResString(IDS_FLASH_TITLE), MB_ICONINFORMATION);
				pDlg->Done();
				return FALSE;
			}

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

			// add 99.4.27 for 20 2Mhz
			// Check Status
			bResult = !CheckState(&comObj, "program");
			if(!bResult)
			{
				AfxGetMainWnd()->MessageBox(GetResString(IDS_PROGRAM_ERROR), GetResString(IDS_FLASH_TITLE), MB_ICONERROR);
				pDlg->Done();
				return FALSE;
			}
			else
			{
				// AfxGetMainWnd()->MessageBox(GetResString(IDS_PROGRAM_OK), GetResString(IDS_FLASH_TITLE), MB_ICONINFORMATION);
			}
			// add 99.4.27 for 20 2Mhz end


		}

		bIsEnd = bCancel;
		pDlg->Done();// add 99.4.27 for 20 2Mhz
		GetSerialComm().SetTimeout(TIMEOUT5);
	}
	

	// Read Command
	{
		BOOL bResult = FALSE;
		CM16CflshApp*		pApp = (CM16CflshApp*)AfxGetApp();
		MSG				msg;

		// Get Comm Object.
		CSerialComm& comObj = GetSerialComm();


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

		// verify address range.
		for(int nSendAddress = nStartAddress; (long)nSendAddress < (long)nEndAddress;
												nSendAddress += 0x0100)
		{
			// read data.
			comObj.CleanUp(NULL);
			BYTE receive[PAGE_SIZE];
			if(!DoRead(&comObj, receive, nSendAddress))
			{
				pDlg->Done();
				return FALSE;
			}
	
			// Get Hex Data.
			BYTE byteData[PAGE_SIZE];
			ClearPage(byteData);
			int nPage = nSendAddress / 0x0100;
			GetPage(byteData, nPage);

			// verify
			CString strOther;
			for(int nIndex = 0; PAGE_SIZE > nIndex; nIndex++)
			{
				if( byteData[nIndex] != receive[nIndex])
				{
					// The difference was found.
					CString strFindMsg = GetResString(IDS_FIND_NFILES);
					strFindMsg += HexToStr(nSendAddress + nIndex);
					AfxGetMainWnd()->MessageBox(strFindMsg, GetResString(IDS_FLASH_TITLE));
					pDlg->Done();
					return bResult;
				}
			}

			// Progress Bar Update.
			pDlg->SetProgressPos((nSendAddress - nStartAddress) / TO_INT);

			// Event Loop
  			while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
				if (!pApp->PumpMessage()) {
					::PostQuitMessage(0);
					break;
				}
			}
			if(bCancel)
			{
				break;
			}
			Sleep(10);
		}
		pDlg->Done();

		if(!bIsEnd)
		{
			// Check Status
			bResult = !CheckState(&comObj, "program");
			if(!bResult)
			{
				AfxGetMainWnd()->MessageBox(GetResString(IDS_PROGRAM_ERROR), GetResString(IDS_FLASH_TITLE), MB_ICONERROR);
			
			}
			else
			{
				AfxGetMainWnd()->MessageBox(GetResString(IDS_PROGRAM_OK), GetResString(IDS_FLASH_TITLE), MB_ICONINFORMATION);
			}
		}
		return	bResult;
	}

}

⌨️ 快捷键说明

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