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

📄 m3usbc.cpp

📁 PC通过串口、USB与目标板进行通信的工具
💻 CPP
字号:
/*
   //*******************************************************************
   //
   //                 A n y k a     P r o p r i e t a r y
   //
   //                 COPYRIGHT (c)   2004 BY ANYKA CHINA
   //                      --   ALL RIGHT RESERVE  --          
   //
   //FileName:     m3usbc.cpp
   //Author:       skyxiang 
   //Creat time:   12 Fre 2004
   //Modified:
   //Revision:     1.0

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


//Includes
#include "stdafx.h"
//#include <afxwin.h>
//#include <afxcmn.h>
#include <iostream.h>
#include <afxtempl.h>
#include <objbase.h>
#include <setupapi.h>
#include "GConsole.h"
#include <initguid.h>
#include "M3USBFUN.h"
#include "USBCRC16.h"
#include "devioctl.h"

//DEFINE_GUID(M3USB_GUID, 
//0xa7111f79, 0x1460, 0x4d8d, 0xbf, 0xff, 0x8, 0xbc, 0x59, 0xfd, 0xf8, 0x7c);

DEFINE_GUID(M3USB_GUID, 
0x6e7ac6a7, 0x1a4c, 0x4a72, 0x83, 0x54, 0x89, 0x95, 0x11, 0xf8, 0x13, 0x51);

//DEFINE_GUID(M3USB_GUID,
//0xe4c3a4bc, 0xa77a, 0x40d4, 0x80, 0xa0, 0xeb, 0xde, 0xf4, 0x6, 0xf7, 0xe2);

#define IOCTL_MYDEV_GET_FIRMWARE_REV \
		CTL_CODE(FILE_DEVICE_UNKNOWN,0,METHOD_OUT_DIRECT,FILE_ANY_ACCESS)

extern BYTE *filetemp;

void CM3USBC::M3USBC()
{
	M3USB_HD = INVALID_HANDLE_VALUE;
}



bool CM3USBC::M3USBOPEN()
{
	ULONG					          i;								
    GUID                              *m3usb_guid; 
	HDEVINFO                          m3info;
    SP_INTERFACE_DEVICE_DATA		  m3InterfaceInfo;
	PSP_INTERFACE_DEVICE_DETAIL_DATA  m3InterfaceDetail;
    ULONG						      m3requiredsize;
	ULONG							  m3predictedsize;
	char							  name[260];
    CString							  m3devicepath;						  
	


	m3usb_guid =(LPGUID)&M3USB_GUID;
	m3info     = SetupDiGetClassDevs(						//get the device info
                    m3usb_guid,
        			NULL,
					NULL,
                    DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);

	if( m3info == INVALID_HANDLE_VALUE )
		return false;

    m3InterfaceInfo.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    for (i=0; i<8; i++)				//8 usb interface  that can more than 8
	{ 
		if( SetupDiEnumInterfaceDevice(					//get the interface info
			   m3info,
			   0,
			   m3usb_guid,
			   i,
			   &m3InterfaceInfo) )
		break;							
		if( i==7 )
			return false;
	}

    SetupDiGetDeviceInterfaceDetail(                  //get the device interface detail
	   m3info,
	   &m3InterfaceInfo,
       NULL,
       0,
	   &m3requiredsize,
       NULL);

    m3predictedsize           = m3requiredsize;
    m3InterfaceDetail         = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(m3predictedsize);
	m3InterfaceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
    if(! SetupDiGetDeviceInterfaceDetail(
		    m3info,
			&m3InterfaceInfo,
			m3InterfaceDetail,
			m3predictedsize,
			&m3requiredsize,
			NULL) )
	{
      free((PVOID) m3InterfaceDetail);
      return false;
	}
    strncpy(name,m3InterfaceDetail->DevicePath,sizeof(name));
    m3devicepath = name;
    free((PVOID) m3InterfaceDetail);
    
    M3USB_HD =	CreateFile(
		           m3devicepath,
                   GENERIC_READ | GENERIC_WRITE,
                   0,
				   NULL,
				   OPEN_EXISTING,
				   0,
				   NULL
				   );
    if(M3USB_HD == INVALID_HANDLE_VALUE)
	{
		return false;
	}

	BYTE filettemp[64];
	if( !M3USBOK.M3USBREAD(filettemp,64) )				//to void the bug
	{
		MessageBox(NULL, "did't initialize the m3chip usb","failed in initialize",MB_ICONSTOP);
		M3USBOK.M3USBCLOSE();
		return false;
	}
	Sleep(2);
	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 initialize the m3chip usb","failed in initialize",MB_ICONSTOP);
		if(filetemp!=NULL)
		{
			delete[] filetemp;
			filetemp=NULL;
		}
		M3USBOK.M3USBCLOSE();
		return false;
	}

	return(true);
	
}

bool CM3USBC::M3USBREAD(BYTE *rbuf, int rbufcount)
{
	ULONG nReaden;
	if(rbufcount>0)
	{
		if( M3USB_HD !=INVALID_HANDLE_VALUE )
		{
			if(!ReadFile(M3USB_HD,rbuf,rbufcount,&nReaden,NULL))
			{
				return false;
			}
			return true;
		}
		
	}
	else
		return false;
   return false;
}
      

bool CM3USBC::M3USBWRITE(BYTE *wbuf, int wbufcount)
{	
	ULONG nWritten;

	BYTE *true_w = new BYTE[wbufcount*2];
	BYTE w_temp[64];
	BYTE w_temp1[4096];
	BYTE crcsymbol=0xb0;					//to imform the device usb is wrong
	int i,j,k;
	int offset = 0;
	int num_4096 = 0;
	int leave_4096 = 0;

	unsigned short crctemp;

	USBCRC16* okcrc=new USBCRC16();
	for( i = 0; i < wbufcount; i=i+64 )
	{
		for( j = 0; j < 64; j++ )
		{
			w_temp[j] = *(wbuf+i+j);
		}
		crctemp = okcrc->USB_CRC_16(w_temp,64);
		crcsymbol=0xb0;					//to imform the device usb is wrong
		while( ((crctemp&0x001f) == 0x001f) || ( (crctemp&0x007f)==0x007e ) )
		{
			w_temp[63] = w_temp[63]-1;
			crcsymbol=crcsymbol+1;
			crctemp = okcrc->USB_CRC_16(w_temp,64);
		}
		if(crcsymbol!=0xb0)
		{
			BYTE crcwrong[64];
			for( k=0; k<64; k++)
			{
				crcwrong[k]=0x50;
			}
			crcwrong[31]=0x52;
			crcwrong[32]=0x0;
			crcwrong[60]=0xb0;			//indicate the crc wrong
			crcwrong[61]=crcsymbol;
			crcwrong[62]=0x13;
			crcwrong[63]=0x14;
			for( k = 0; k < 64; k++ )
			{
				true_w[offset + k]= crcwrong[k];
			}
			offset = offset + 64;
		}

		for( k = 0; k < 64; k++ )
		{
				true_w[offset + k]= w_temp[k];
		}
		offset = offset + 64;
	
	}
	delete[] okcrc;
	okcrc=NULL;

	num_4096 = offset/4096;
	leave_4096 = offset%4096;
	for( i = 0; i < num_4096; i++ )
	{
		for( j = 0; j < 4096; j++ )
		{
			w_temp1[j] = true_w[i*4096+j];
		}
		if(!WriteFile(M3USB_HD,w_temp1,4096,&nWritten,NULL))
		{
				return false;
		}

	}
    
	for( i = 0; i < leave_4096; i++ )
	{
		w_temp1[i] = true_w[ num_4096 * 4096 + i ];
	}

	if(!WriteFile(M3USB_HD,w_temp1,leave_4096,&nWritten,NULL))
	{
				return false;
	}


	delete[] true_w;
    return true;

}


bool CM3USBC::M3Download( int dladdress, BYTE *filetemp, int filesize )
{
	return true;
}

bool CM3USBC::M3USBDeviceIo(BYTE *obuf, int obufcount)
{
	DWORD nBytes;   
	BOOLEAN bResult=0;
	if( M3USB_HD !=INVALID_HANDLE_VALUE )
	{
		bResult = DeviceIoControl(M3USB_HD,    
        IOCTL_MYDEV_GET_FIRMWARE_REV,    
                    NULL, 0,               // no input buffer
                    obuf, obufcount,     // output buffer
                    &nBytes, NULL);               // # bytes returned
	}

    if(bResult)
	{
		return true;
	}

	return false;
	
}


bool CM3USBC::M3USBCLOSE()
{                     
   if(M3USB_HD != INVALID_HANDLE_VALUE)
   {
	   if( !CloseHandle(M3USB_HD) )
	   {
        MessageBox(NULL,"Failed to close m3chip usb interace","failed to close", MB_OK);  
		return false;
	   }
	   else 
		   return true;

   }

  return false;

}








/**********************future Revision: *********************************************
	1. to suport more than one same device:   change the SetupDiEnumInterfaceDevice() 
	                                          to see the USB_WIN APPLICATION
    2.  
**************************************************************************************/

⌨️ 快捷键说明

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