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

📄 downproc.cpp

📁 PC通过串口、USB与目标板进行通信的工具
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#define STRICT
#define WIN32_LEAN_AND_MEAN

#include "StdAfx.h"
#include "GConsole.h"
#include "resource.h"
#include "DownProc.h"
#include "Engine.h"
#include "OpenFile.h"
#include "M3USBFUN.h"
#include "USBCRC16.h"

HWND _hDlgDownloadProgress = NULL;
HWND _hProgressControl;
HWND _hDlgSerialSettings;
HWND _hDlgDumpSettings;
HWND _hDlgUsbDownload;
int downloadCanceled;
unsigned long	LoadFileSize;
BYTE	*filetemp = NULL;
static CString sLastAddress = "0x10000000";
static int tempIdBaudRate,tempComPort;
// for dump to file
unsigned char	ipos;
#define		FLASH_RAM_ADDR		"Address:"
#define		FLASH_RAM_LEN		"Length:"
#define		NAND_FLASH_ADDR		"Page:"
#define		NAND_FLASH_BLOCK	"Block:"
#define		NAND_FLASH_COUNT	"Count:"
static unsigned char ucLastSelPos = 0;
static CString csLastFilePath;
static unsigned int iLastAddr;
static unsigned int iLastLength;
static unsigned int iLastBlock;

UINT hex2int(TCHAR *str);
bool DownloadByUsb(unsigned int FileSize);
bool CheckDownData(unsigned int FileSize);
bool CheckHexAddr(char * phex);
void InitDumpSet(HWND hwnd);
void SetToNandFlash();
void SetToFlashRam();
int GetInputParm();
bool DumpToFile();

BOOL CALLBACK DownloadProgressProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    TCHAR szTitle[256];
    switch(message)
    {
    case WM_INITDIALOG:
		lstrcpy(szTitle,TEXT("Downloading "));
		lstrcat(szTitle,szFileName);
		SetWindowText(hDlg,szTitle);
		_hDlgDownloadProgress=hDlg;
		return TRUE;
    case WM_CLOSE: 
		//If the [X] button is clicked, WM_CLOSE message is received.
		//Originally, DefWindowProc() called DestroyWindow();
		//But, there is no DefWindowProc(), We have to process WM_CLOSE message.
		DestroyWindow(hDlg);
		_hDlgDownloadProgress=NULL;
		downloadCanceled=1;
		//break;
		return TRUE; //why not?
	default:
		break;
    }

    return FALSE;
}

BOOL CALLBACK DumpToFileProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	int result;
	TCHAR szFilter[] =	TEXT ("TEXT Files (*.txt)\0*.txt\0")  \
				TEXT ("All Files (*.*)\0*.*\0\0") ;	

	switch(message)
    {
    case WM_INITDIALOG:
		_hDlgDumpSettings=hDlg;
		InitDumpSet(hDlg);
		return TRUE;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case WM_CLOSE: 
			DestroyWindow(hDlg);
			_hDlgDumpSettings=NULL;
			return TRUE;
		case IDCANCEL:
			EndDialog(hDlg,0);
			return TRUE;
		case IDOK:
			if (CreateDialog(_hInstance,MAKEINTRESOURCE(IDD_DIALOG1),_hDlgDownloadProgress,DownloadProgressProc) == NULL)
			{
				MessageBox(_hDlgUsbDownload, "Can't creat download window",
					"failed in Create download window", MB_ICONSTOP);
				return TRUE;
			}
			if (DumpToFile())
			{
				EndDialog(hDlg,1);
			}
			return TRUE;
		case IDC_RADIO_FLASH:
		case IDC_RADIO_RAM:
			if (ucLastSelPos == 2)
				SetToFlashRam();
			ucLastSelPos=LOWORD(wParam) - IDC_RADIO_FLASH;
			return TRUE;
		case IDC_RADIO_NAND:
			if (ucLastSelPos != 2)
				SetToNandFlash();
			ucLastSelPos=LOWORD(wParam) - IDC_RADIO_FLASH;
			return TRUE;
		case IDC_BTN_BROWSE:	
			PopFileSetFilter(szFilter);
			result = PopFileOpenDlg(_hDlgUsbDownload, szFileName, szTitleName);
			if(result == IDOK)
			{
				csLastFilePath = szFileName;
				SetDlgItemText(hDlg, IDC_EDIT_DUMP_FILE, csLastFilePath);
			}
			PopFileSetFilter();
			return TRUE;
		default:
			break;
		}
	default:
		return FALSE;
    }

    return FALSE;
}

void InitDownloadProgress(int range)
{
    while(_hDlgDownloadProgress==NULL); //wait until the progress dialog box is ready.

	if (range == 0)
		range = 100;
    _hProgressControl=GetDlgItem(_hDlgDownloadProgress,IDC_PROGRESS1);
    SendMessage(_hProgressControl,PBM_SETRANGE,0,MAKELPARAM(0, range));
    downloadCanceled=0;
}

void DisplayDownloadProgress(int percent)
{
    SendMessage(_hProgressControl,PBM_SETPOS,(WPARAM)percent,0); 
}
    
void CloseDownloadProgress(void)
{
    if(_hDlgDownloadProgress!=NULL)
    {
    	//DestroyWindow(_hDlgDownloadProgress); 
	//Doesn't work because CloseDownloadProgress() is called another thread,
	//which is different the thread calling CreatDialog().
    
        SendMessage(_hDlgDownloadProgress,WM_CLOSE,0,0); 
    	_hDlgDownloadProgress=NULL;
    }
}

BOOL CALLBACK OptionsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    int baudTable[]={CBR_115200, CBR_57600, CBR_38400, CBR_19200};
	switch(message)
    {
    case WM_INITDIALOG:
		_hDlgSerialSettings=hDlg;

		tempIdBaudRate=idBaudRate;
		tempComPort=userComPort;
		CheckRadioButton(hDlg,IDC_RADIO115200,IDC_RADIO19200,IDC_RADIO115200+tempIdBaudRate);
		CheckRadioButton(hDlg,IDC_RADIOCOM1,IDC_RADIOCOM4,IDC_RADIOCOM1+(tempComPort-1));

		return TRUE;
    case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hDlg,1);
			idBaudRate=tempIdBaudRate;
			userBaudRate=baudTable[idBaudRate];
			userComPort=tempComPort;
			WriteUserConfig();
			return TRUE;
		case IDCANCEL:
			EndDialog(hDlg,0);
			return TRUE;
		case IDC_RADIOCOM1:
		case IDC_RADIOCOM2:
		case IDC_RADIOCOM3:
		case IDC_RADIOCOM4:
			tempComPort=LOWORD(wParam)-IDC_RADIOCOM1+1;
			return TRUE;
		case IDC_RADIO115200:
		case IDC_RADIO57600:
		case IDC_RADIO38400:
		case IDC_RADIO19200:
			tempIdBaudRate=LOWORD(wParam)-IDC_RADIO115200;
			return TRUE;
		}
		return FALSE;
    default:
		return FALSE;
    }
    return FALSE;
}

BOOL CALLBACK UsbDownProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	int				result;
	HANDLE			hFile;
	char			cBuf[128];

	switch(message)
    {
    case WM_INITDIALOG:
		_hDlgUsbDownload = hDlg;
		SetDlgItemText(_hDlgUsbDownload, IDC_EDIT_ADDR, sLastAddress);
		EnableWindow(GetDlgItem(_hDlgUsbDownload, IDC_BTN_DOWN), false);
		return TRUE;
    case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDC_BTN_DOWN:
			if (DownloadByUsb(LoadFileSize))
			{
				EndDialog(hDlg,0);
				return TRUE;
			}
			EnableWindow(GetDlgItem(_hDlgUsbDownload, IDC_BTN_DOWN), true);
			EnableWindow(GetDlgItem(_hDlgUsbDownload, IDC_BTN_EXPLORE), false);
			return TRUE;
		case IDC_BTN_CANCEL:
			EndDialog(hDlg,0);
			return TRUE;
		case IDC_BTN_EXPLORE:
			result = PopFileOpenDlg(_hDlgUsbDownload, szFileName, szTitleName);
			if(result == 0)
				return TRUE;
			hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
			if(hFile == INVALID_HANDLE_VALUE)
			{
				MessageBox(_hDlgUsbDownload, "Open file failure", "Open Error", MB_ICONSTOP);
				return TRUE;
			}

			SetDlgItemText(_hDlgUsbDownload, IDC_EDIT_FILE, szFileName);
			LoadFileSize = GetFileSize(hFile, NULL);
			itoa(LoadFileSize, cBuf, 10);
			strcat(cBuf, " Bytes");
			SetDlgItemText(_hDlgUsbDownload, IDC_STATIC_FL, cBuf);
			if(filetemp != NULL)
			{
				delete[] filetemp;
				filetemp=NULL;
			}
			filetemp=new BYTE[LoadFileSize];

			if(!ReadFile(hFile, filetemp, LoadFileSize, &LoadFileSize, NULL))
			{
				MessageBox(_hDlgUsbDownload, "Read file failure", "Read Error", MB_ICONSTOP);
				return TRUE;
			}
			CloseHandle(hFile);

			EnableWindow(GetDlgItem(_hDlgUsbDownload, IDC_BTN_DOWN), true);
			EnableWindow(GetDlgItem(_hDlgUsbDownload, IDC_BTN_EXPLORE), false);
			return TRUE;
		case IDC_BTN_CHK:
			CheckDownData(LoadFileSize);
			return TRUE;
		default:
			break;
		}
		return FALSE;
    default:
		return FALSE;
    }
    return FALSE;
}


//ReadUserConfig() is called once at first
int ReadUserConfig(void)
{
    int baudTable[] = {CBR_115200, CBR_57600, CBR_38400, CBR_19200};
   
    userComPort=1;
	idBaudRate=0;
	userBaudRate=baudTable[idBaudRate];
	return TRUE;
}

int WriteUserConfig(void)
{
	return TRUE;
}

UINT hex2int(TCHAR *str)
{
	int		i;
	UINT	number = 0; 
	int		order = 1;
	TCHAR	ch;

	for (i = lstrlen(str) - 1; i >= 0; i--)
	{
		ch = str[i];
		if (ch == 'x' || ch=='X')
			break;
		if (ch >= '0' && ch <= '9')
		{
			number += order * (ch-'0');
			order *= 16;
		}
		else if (ch >= 'A' && ch <= 'F')
		{
			number += order * (ch - 'A' + 10);
			order *= 16;
		}
		else if(ch >= 'a' && ch <= 'f')
		{
			number += order * (ch - 'a' + 10);
			order *= 16;
		}
	}

	return number;
}

bool DownloadByUsb(unsigned int FileSize)
{
	char cBuf[16];
	BYTE filettemp[64];
	BYTE temp[4096];
	BYTE crc_first[64];
    int sizeblock=FileSize/4096;
	int remain=FileSize%4096;
	int rsizeblock=remain/64;
	int rremain = 0;
	int blocktemp,rblocktemp;
	int i;
	int dladdress;
	BYTE filelen[4];
	BYTE usbaddress[5];

	filelen[0]=FileSize/100000;					//to separate to FileSize to 4 byte
	filelen[1]=(FileSize%100000)/1000;
	filelen[2]=(FileSize%1000)/10;
	filelen[3]=FileSize%10;

	dladdress = 0;
	GetDlgItemText(_hDlgUsbDownload, IDC_EDIT_ADDR, cBuf, 16);
	sLastAddress = cBuf;
	if (CheckHexAddr(cBuf))
	{
		dladdress = hex2int(cBuf);
	}
	else
	{
		MessageBox(_hDlgUsbDownload, "Wrong input download address", "Error", MB_ICONSTOP);
		return false;
	}

	if (dladdress < 0x10000)
	{
		MessageBox(_hDlgUsbDownload, "Overwrite BIOS is not allowed, please input another address", "Error", MB_ICONSTOP);
		return false;
	}
	
	usbaddress[0]=dladdress/10000000;
	usbaddress[1]=(dladdress%10000000)/100000;
	usbaddress[2]=(dladdress%100000)/1000;
	usbaddress[3]=(dladdress%1000)/10;
	usbaddress[4]=dladdress%10;
	
	

	for(i=0; i<64; i++)						//send 3f data begin	
	{
		filettemp[i]=0x50;
	}
	filettemp[31]=0x52;						//the order symbol
	filettemp[32]=0x0;						//the order symbol
	filettemp[60]=0x3f;						//0x3f indicate the download start
	filettemp[61]=0x0;						//order data
	filettemp[62]=0x13;						//the order symbol
	filettemp[63]=0x14;						//the order symbol

	filettemp[58]=usbaddress[4];			//the startaddress
	filettemp[57]=usbaddress[3];
	filettemp[56]=usbaddress[2];
	filettemp[55]=usbaddress[1];
	filettemp[54]=usbaddress[0];

	filettemp[53]=filelen[0];
	filettemp[52]=filelen[1];
	filettemp[51]=filelen[2];
	filettemp[50]=filelen[3];

	if(dladdress<0x10000000)
	{
		filettemp[49]=1;
	}
	else
	{
		filettemp[49]=0;
	}

    USBCRC16* okcrc1=new USBCRC16();
    unsigned short crctemp1 = okcrc1->USB_CRC_16(filettemp,64);

    while( ((crctemp1 & 0x001f)) == 0x001f || ((crctemp1 & 0x007f) == 0x007e) )
	{
	 	 filettemp[59] = filettemp[59]-1;
	     crctemp1 = okcrc1->USB_CRC_16(filettemp,64);
	}
	delete[] okcrc1;
	okcrc1=NULL;

	if( !M3USBOK.M3USBWRITE(filettemp,64) )			
	{
		 MessageBox(NULL,"did't write data to the m3chip usb","failed in writen",MB_ICONSTOP);
		 if(filetemp!=NULL)
		 {
				delete[] filetemp;
				filetemp=NULL;
		 }
		 M3USBOK.M3USBCLOSE();
		 return false;
	}

	if (CreateDialog(_hInstance, MAKEINTRESOURCE(IDD_DIALOG1), _hDlgDownloadProgress, DownloadProgressProc) == NULL)
	{
		MessageBox(_hDlgDownloadProgress, "Can't create download window", "Error", MB_ICONSTOP);
		return false;
	}

	InitDownloadProgress(sizeblock);
	DisplayDownloadProgress(0);

	for(i=0; i<sizeblock; i++)
	{
		 for(int j=0; j<4096; j++)
		 {
			 temp[j]=*(filetemp+i*4096+j);
		 }

	     if( !M3USBOK.M3USBWRITE(temp,4096) )
		 {
			 MessageBox(NULL,"did't write data to the m3chip usb","failed in writen",MB_ICONSTOP);
			 M3USBOK.M3USBCLOSE();
			 if(filetemp!=NULL)
			 {
				 delete[] filetemp;
				 filetemp=NULL;
			 }
			 CloseDownloadProgress();
			 return false;
		 }

		 
avoid_leak:		 
		          // to avoid leak package
//*********************************************************************************************
         for(j=0; j<64; j++)
		 {
		     filettemp[j]=0x50;
			 //filettemp[bb]=0x6f;
		 }
		 filettemp[31]=0x52;
		 filettemp[32]=0x0;
		 filettemp[60]=0x6f;
		 filettemp[61]=64;			//to make sure the M3chip received 64 packages
		 filettemp[62]=0x13;
		 filettemp[63]=0x14;
		 
		 filettemp[56] = 0;
		 filettemp[57] = 0;
		 filettemp[58] = 0;

		 for( j = 0; j < 4096; j++ )
		 {
			 if( filettemp[57] + temp[j] >0xff )
			 {
				 if( ( filettemp[58] + 1 ) >  0xff )
				 {
					 filettemp[56] = filettemp[56] +1 ;
				 }
				 filettemp[58] = filettemp[58] + 1;
				 filettemp[57] = filettemp[57] + temp[j];
			 }
			 else 
			 {
				 filettemp[57] = filettemp[57] + temp[j];
			 }
			 if( j%64 == 0 )
			 {
				 crc_first[ j / 64 ] = temp[j];
			 }
		 }

		 USBCRC16* okcrc1=new USBCRC16();
		 unsigned short crctemp1 = okcrc1->USB_CRC_16(crc_first,64);
		 
		 filettemp[55] = crctemp1 & 0xff;
		 filettemp[54] = ( crctemp1 >> 8 ) & 0xff;

		 
		 crctemp1 = okcrc1->USB_CRC_16(filettemp,64);

		 while( ((crctemp1 & 0x001f)) == 0x001f || ((crctemp1 & 0x007f) == 0x007e) )
		 {
	 		 filettemp[59] = filettemp[59]-1;
			 crctemp1 = okcrc1->USB_CRC_16(filettemp,64);
		 }
		 delete[] okcrc1;
		 okcrc1=NULL;
		 
						
		 if( !M3USBOK.M3USBWRITE(filettemp,64) )			
		 {
			 MessageBox(NULL,"did't write data to the m3chip usb","failed in writen",MB_ICONSTOP);
			 if(filetemp!=NULL)
			 {
				 delete[] filetemp;
				 filetemp=NULL;
			 }
		     M3USBOK.M3USBCLOSE();
			 CloseDownloadProgress();
		     return false;
		 }
	   	 if(dladdress<0x10000000)		//very important to add this command.
		 {
		     Sleep(1);				//wait the soft write the flash.
		 }
	     else
		 {
		     Sleep(1);
		 }

		if( !M3USBOK.M3USBREAD(filettemp,64) )				//to verify if m3chip usb is get ready
		{
			MessageBox(NULL,"did't write data to the m3chip usb","failed in writen",MB_ICONSTOP);
			if(filetemp!=NULL)
			 {
				 delete[] filetemp;
				 filetemp=NULL;
			 }
		     M3USBOK.M3USBCLOSE();
			 CloseDownloadProgress();
			return false;
		}

		for(j=0; j<16; j++)
		{
			if(filettemp[j]!=0x50 || filettemp[60]!=0x6f || filettemp[31]!=0x52 ||
				filettemp[32]!=0x0 || filettemp[62]!=0x13 || filettemp[63]!=0x14)
			{
				goto avoid_leak;
			}
		}

		if(filettemp[61]==0x44)
		{
			i--;
		}
		else if(filettemp[61]==0x88)
		{
			DisplayDownloadProgress(i);
		}
		else
		{
			
			MessageBox(NULL,"check coordination failure","coordination failure",MB_ICONSTOP);
			if(filetemp!=NULL)
			 {
				 delete[] filetemp;
				 filetemp=NULL;
			 }
		     M3USBOK.M3USBCLOSE();
			 CloseDownloadProgress();
			return false;
		}


			for(j=0; j<64; j++)						//to avoid the m3chip interrupt one less
			{
				filettemp[j]=0x50;
			}
			filettemp[31]=0x52;
			filettemp[32]=0x0;

⌨️ 快捷键说明

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