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

📄 downproc.cpp

📁 PC通过串口、USB与目标板进行通信的工具
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			filettemp[60]=0x5f;
			filettemp[61]=0x0;
			filettemp[62]=0x13;						//to avoid the data is the same with crc
			filettemp[63]=0x14;						//to avoid the data is the same with crc
			for( j = 0; j < 2; j++ )				//it is important to add this two times
			{
 				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;
				}
			}

//*********************************************************************************************
		 

	}

	CloseDownloadProgress();
	blocktemp=sizeblock*4096;
	rblocktemp=rsizeblock*64;

	if( remain != 0 )
	{
			for(i=0; i< remain; i++)
			{
					temp[i]=*(filetemp+blocktemp+i);
			}
			
			rremain=remain%64;
			if( rremain != 0 )
			{
					for( i = 0; i <64; i++ )
					{
						if( i >= rremain )
						{
							temp[remain +i] = 0;
						}
						else
						{
							temp[remain + i] = *(filetemp+blocktemp+remain+i);
						}
					}
					rsizeblock = rsizeblock + 1;
			}


resent_last:
			if(!M3USBOK.M3USBWRITE(temp, rsizeblock*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;
			}


//*********************************************************************************************

 

			 for( int j=0; j<64; j++)
			 {
				 filettemp[j]=0x50;
				 //filettemp[bb]=0x6f;
			 }
			 filettemp[31]=0x52;
			 filettemp[32]=0x0;
			 filettemp[60]=0x6f;
			 filettemp[61]=rsizeblock;			//to make sure the M3chip received 64 packages
			 filettemp[62]=0x13;
			 filettemp[63]=0x14;

			 filettemp[58] = 0;
			 filettemp[57] = 0;
			 filettemp[56] = 0;
    		 for( j = 0; j < rsizeblock*64; 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 , rsizeblock);			 
             
			 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;
			 }



			 Sleep(1);				//very important to add this command.
			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)
				{
					MessageBox(NULL,"check coordination failure","coordination failure",MB_ICONSTOP);
					M3USBOK.M3USBCLOSE();
					return false;
				}
			}

			if(filettemp[61]==0x44)
			{
				for(i=0; i<64; i++)						//to avoid the m3chip interrupt one less
				{
					filettemp[i]=0x50;
				}
				filettemp[31]=0x52;
				filettemp[32]=0x0;
				filettemp[60]=0x5f;
				filettemp[61]=0x0;
			    filettemp[62]=0x13;						//to avoid the data is the same with crc
				filettemp[63]=0x14;						//to avoid the data is the same with crc
				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;
				}
				goto resent_last;
			}
			else if(filettemp[61]==0x88)
			{
		
			}
			else
			{
				MessageBox(NULL,"check coordination failure","coordination failure",MB_ICONSTOP);
				if(filetemp!=NULL)
			 {
				 delete[] filetemp;
				 filetemp=NULL;
			 }
		     M3USBOK.M3USBCLOSE();
			 CloseDownloadProgress();
				return false;
			}

    }

//*********************************************************************************************
	

	for(i=0; i<64; i++)						//end of send usbdata
	{
		filettemp[i]=0x50;
	}
	filettemp[31]=0x52;
	filettemp[32]=0x0;
	filettemp[60]=0x4f;
	filettemp[61]=0x0;
	filettemp[62]=0x13;						//to avoid the data is the same with crc
	filettemp[63]=0x14;						//to avoid the data is the same with crc
 	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;
	}
    
	for(i=0; i<64; i++)						//to avoid the m3chip interrupt one less
	{
		filettemp[i]=0x50;
	}
	filettemp[31]=0x52;
	filettemp[32]=0x0;
	filettemp[60]=0x5f;
	filettemp[61]=0x0;
    filettemp[62]=0x13;						//to avoid the data is the same with crc
	filettemp[63]=0x14;						//to avoid the data is the same with crc

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

	MessageBox(_hDlgDownloadProgress, "Completed!", "Message", MB_ICONINFORMATION);
	return true;

}



bool CM3USBC::M3Upload( int up_address, BYTE *filetemp, int upload_len )
{
	return true;
}

bool CheckHexAddr(char * phex)
{
	if (phex == NULL)
		return false;

	if (strncmp("0x", phex, 2) != 0 && strncmp("0X", phex, 2) != 0)
	{
		return false;
	}

	phex += 2;
	if (lstrlen(phex) <= 8)
	{
		while (*phex != '\0')
		{
			if (*phex >= '0' && *phex <= '9')
			{
				phex++;
			}
			else if (*phex >= 'A' && *phex <= 'F')
			{
				phex++;
			}
			else if(*phex >= 'a' && *phex <= 'f')
			{
				phex++;
			}
			else
				return false;
		}
	}
	else
		return false;

	return true;
}

bool CheckDownData(unsigned int FileSize)
{
	BYTE DataFrame[64];
	BYTE *allfilet = new BYTE[FileSize+64];
//	char errorshow;
//	char tempstr[128];
	int error_num=0;
	BYTE crcsymbol=0;

// inform the m3chip to upload and check the data

	for(UINT i=0; i<64; i++)
	{
		DataFrame[i]=0x50;
	}
	DataFrame[31]=0x52;
	DataFrame[32]=0x0;
	DataFrame[60]=0x7f;				
    DataFrame[61]=0x0;
	DataFrame[62]=0x13;
	DataFrame[63]=0x14;

 	if( !M3USBOK.M3USBWRITE(DataFrame,64) )
	{
		MessageBox(_hDlgUsbDownload, "did't coordinate checking the data in the m3chip usb",
			"failed in checking",MB_ICONSTOP);
		if(filetemp!=NULL)
		{
			delete[] filetemp;
			filetemp=NULL;
		}
		return false;
	}

// verify if m3chip usb upload is get ready
	if( !M3USBOK.M3USBREAD(DataFrame,64) )				
	{
		MessageBox(_hDlgUsbDownload, "did't coordinate checking the data in the m3chip usb",
			"failed in checking",MB_ICONSTOP);
		if(filetemp!=NULL)
		{
			delete[] filetemp;
			filetemp=NULL;
		}
		return false;
	}

	for(i=0;i<16;i++)
	{
		if(DataFrame[i]!=0x50 || DataFrame[31]!=0x52 || DataFrame[32]!=0x0 || 
			DataFrame[60]!=0x7e || DataFrame[62]!=0x13 || DataFrame[63]!=0x14)
		{
			MessageBox(_hDlgUsbDownload, "did't coordinate checking the data in the m3chip usb",
			"failed in checking",MB_ICONSTOP);
			if(filetemp!=NULL)
			{
				delete[] filetemp;
				filetemp=NULL;
			}
			return false;
		}
	}

//	to upload the m3chip data
	if (CreateDialog(_hInstance,MAKEINTRESOURCE(IDD_DIALOG1),_hDlgDownloadProgress,DownloadProgressProc) == NULL)
	{
		MessageBox(_hDlgUsbDownload, "Can't creat download window",
			"failed in Create download window", MB_ICONSTOP);
		return false;
	}
	InitDownloadProgress(FileSize);
	DisplayDownloadProgress(0);

	for (i = 0; i < FileSize + 128; i += 64)
	{
		if( !M3USBOK.M3USBREAD(DataFrame,64) )				//to verify if m3chip usb is get ready
		{
			MessageBox(_hDlgUsbDownload, "did't check the data in the m3chip usb","failed in checking",MB_ICONSTOP);
			return false;
		}

		if(DataFrame[31]==0x52 && DataFrame[32]==0x0 && DataFrame[62]==0x13 && 
			DataFrame[63]==0x14 && DataFrame[60]==0x7f)
		{
			for(int j=0; j<30; j++)
			{
				if(DataFrame[j]!=0x50)
				{
					goto preceive;
				}
			}
			break;
		}
		else if( DataFrame[31]==0x52 && DataFrame[32]==0x0 && 
   				 DataFrame[62]==0x13 && DataFrame[63]==0x14 && DataFrame[60]==0xb0)
		{
			for( int j=0; j<30; j++ )
			{
				if( DataFrame[j] != 0x50 )
				{
				   goto preceive;
				}
			}
			crcsymbol = 0x0f & DataFrame[61];
			i= i - 64;
			continue;
		}   
preceive:
        if(crcsymbol != 0 )
		{
			DataFrame[63] = DataFrame[63] + crcsymbol;
			crcsymbol = 0;
		}
		for(int k=0; k<64; k++)
		{
			allfilet[k+i]=DataFrame[k];
		}
		DisplayDownloadProgress(i);
	}

// to compare the upload data and the source file data
	for(i=0; i<FileSize; i++)
	{
		if(filetemp[i]!=allfilet[i])
		{
//			wrongdata[error_num] = allfilet[i];
//			wrongaddress[error_num] = i;
			error_num++;
			break;
		}
	}

	delete[] allfilet;
	allfilet=NULL;

	if(error_num == 0)
	{
		MessageBox(_hDlgUsbDownload, "No error was found", "Download sucess", MB_ICONINFORMATION);
		return true;
	}
	else// if(error_num>20)
	{
		MessageBox(_hDlgUsbDownload, "The errors are more than 20 bytes, please download it again.",
			"Too much errors", MB_ICONINFORMATION);
	    return false; 
	}
/*
	else
	{
		tempstr.Format("%d",error_num);
		errorshow =" " + tempstr + " data error found!\r\n The address is follow:\r\n ";
		for(int j=0; j< error_num; j++)
		{
			tempstr.Format("%d",wrongaddress[j]);
			errorshow = errorshow + "0x" + tempstr + "\r\n ";
		}
	}
*/
}

void InitDumpSet(HWND hwnd)
{
	CheckRadioButton(hwnd, IDC_RADIO_FLASH, IDC_RADIO_NAND, IDC_RADIO_FLASH + ucLastSelPos);
	if (ucLastSelPos == 2)
	{
		SetToNandFlash();
		SetDlgItemInt(hwnd, IDC_EDIT_ADDRESS, iLastAddr, false);
	}
	else
		SetDlgItemText(hwnd, IDC_EDIT_ADDRESS, sLastAddress);
	SetDlgItemInt(hwnd, IDC_EDIT_LENGTH, iLastLength, false);
	SetDlgItemInt(hwnd, IDC_EDIT_BLOCK, iLastBlock, false);
	SetDlgItemText(hwnd, IDC_EDIT_DUMP_FILE, csLastFilePath);
}

void SetToFlashRam()
{
	SetDlgItemText(_hDlgDumpSettings, IDC_ADDRESS, FLASH_RAM_ADDR);
	SetDlgItemText(_hDlgDumpSettings, IDC_EDIT_ADDRESS, "0X");
	SetDlgItemText(_hDlgDumpSettings, IDC_LENGTH, FLASH_RAM_LEN);
	SetDlgItemInt(_hDlgDumpSettings, IDC_EDIT_LENGTH, 0, false);
	ShowWindow(GetDlgItem(_hDlgDumpSettings, IDC_BLOCK), SW_HIDE);
	ShowWindow(GetDlgItem(_hDlgDumpSettings, IDC_EDIT_BLOCK), SW_HIDE);
}

void SetToNandFlash()
{
	SetDlgItemText(_hDlgDumpSettings, IDC_ADDRESS, NAND_FLASH_ADDR);
	SetDlgItemText(_hDlgDumpSettings, IDC_EDIT_ADDRESS, "0");
	SetDlgItemText(_hDlgDumpSettings, IDC_LENGTH, NAND_FLASH_COUNT);
	SetDlgItemInt(_hDlgDumpSettings, IDC_EDIT_LENGTH, 0, false);
	ShowWindow(GetDlgItem(_hDlgDumpSettings, IDC_BLOCK), SW_SHOW);
	ShowWindow(GetDlgItem(_hDlgDumpSettings, IDC_EDIT_BLOCK), SW_SHOW);
}

int GetInputParm()
{
	char cBuf[16];
	int  Translated;
	char *pch = NULL;
	unsigned int itemp;

	// get begin address / begin page
	GetDlgItemText(_hDlgDumpSettings, IDC_EDIT_ADDRESS, cBuf, 16);
	pch = cBuf;
	if (ucLastSelPos != 2)
	{
		if (!CheckHexAddr(cBuf))
			return 1;
		sLastAddress = cBuf;
		iLastAddr = (UINT)hex2int(cBuf);
	}
	else
	{
		while(*pch)
		{
			if(*pch < '0' || *pch > 9)
				return 1;
			pch++;
		}
		iLastAddr = (UINT)atoi(cBuf);
	}
	// get length / page count
	Translated = 0;
	itemp = GetDlgItemInt(_hDlgDumpSettings, IDC_EDIT_LENGTH, &Translated, false);
	if (Translated)
		iLastLength = itemp;
	else
		return 2;
	// get block number
	if (ucLastSelPos == 2)
	{
		Translated = 0;
		itemp = GetDlgItemInt(_hDlgDumpSettings, IDC_EDIT_LENGTH, &Translated, false);
		if (Translated)
			iLastBlock = itemp;
		else
			return 3;
	}
	
	return 0;
}

bool DumpToFile()
{
	int result;
	CFile	DumpFile;

	result = GetInputParm();
	if (result == 1)		// wrong address
	{
		if (ucLastSelPos == 2)
		{
			MessageBox(_hDlgDumpSettings, "Wrong page number", "Input Error", MB_ICONHAND);
		}
		else
		{
			MessageBox(_hDlgDumpSettings, "Wrong Address", "Input Error", MB_ICONHAND);
		}
	}
	else if (result == 2)  // wrong length
	{
		if (ucLastSelPos == 2)
		{
			MessageBox(_hDlgDumpSettings, "Wrong page count", "Input Error", MB_ICONHAND);
		}
		else
		{
			MessageBox(_hDlgDumpSettings, "Wrong length", "Input Error", MB_ICONHAND);
		}
	}
	else if (result == 3)	// wrong block
	{
		if (ucLastSelPos == 2)
		{
			MessageBox(_hDlgDumpSettings, "Wrong block number", "Input Error", MB_ICONHAND);
		}
	}
	else if (result == 0)
	{
		RxFile(szFileName, ucLastSelPos, iLastAddr, iLastLength, iLastBlock);
		return true;
	}

	return false;
}

⌨️ 快捷键说明

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