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

📄 mainfrm.cpp

📁 一个USB驱动程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "DEC55XXUSB.h"
#include "seeddecusb.h"
#include "ezusbsys.h" 
#include "UsbvreqDialog.h"
#include "Audiocopygo.h"
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "MainFrm.h"

#define VR_LED_OPTION       0xB7
	#define LED_ON          0x0
	#define LED_OFF         0x1
	#define LED_BLINK       0x2
#define VR_USB_VERION       0xB8

#define VR_CODEC_CIRCLE     0xBE
#define VR_CODEC_HALT       0xBF

	#define CODEC_REV_START 0x1
	#define CODEC_REV_STOP  0x2
#define VR_CODEC_PLAY       0xC1
#define VR_CODEC_DATA       0xC2

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

char PIPE_TYPE_STRINGS[4][4] = 
{
"CTL",
"ISO",
"BLK",
"INT"
};

char PIPE_DIRECTION[2][4] = 
{
"OUT",
"IN "
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_TEST_GETDEVICE, OnTestGetdevice)
	ON_COMMAND(ID_TEST_GETSTRING, OnTestGetstring)
	ON_COMMAND(ID_TEST_GETCONFIG, OnTestGetconfig)
	ON_COMMAND(ID_TEST_VENDOR, OnTestVendor)
	ON_COMMAND(ID_TEST_GETPIPEINFO, OnTestGetpipeinfo)
	ON_COMMAND(ID_BULK_READ, OnBulkRead)
	ON_COMMAND(ID_BULK_WRITE, OnBulkWrite)
	ON_COMMAND(ID_LED_ON, OnLedOn)
	ON_COMMAND(ID_LED_OFF, OnLedOff)
	ON_COMMAND(ID_LED_BLINK, OnLedBlink)
	ON_COMMAND(ID_USB_20OR11, OnUsb20or11)
	ON_COMMAND(ID_CODEC_CIRCLE, OnCodecCircle)
	ON_COMMAND(ID_CODEC_HALT, OnCodecHalt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnTestGetdevice() 
{
	// TODO: Add your command handler code here
	PVOID psx2_device= 0;
	psx2_device = Sx2GetDeviceDesc();
	if(psx2_device== NULL)
	{
		AfxMessageBox("操作失败,请复位SEED-DEC55XX系统");
	}
	/*显示获取的描述表*/
	theApp.Out("*********************************************************************\n");
	theApp.Out("the current cy7c68001's device descriptor is:\n");
	theApp.Out("-----------------------------------------------------------\n");
	theApp.Out("	Vendor ID is: 0x%x \n ",((pUsb_Device_Descriptor)psx2_device )->idVendor);
	theApp.Out("	Product ID is: 0x%x \n",((pUsb_Device_Descriptor)psx2_device )->idProduct);
	theApp.Out("	Device ID is: 0x%x \n",((pUsb_Device_Descriptor)psx2_device )->bcdDevice);
	theApp.Out("	Maximum Packet Size is: 0x%x\n",((pUsb_Device_Descriptor)psx2_device )->bMaxPacketSize0);
	theApp.Out("	Device class is: 0x%x\n",((pUsb_Device_Descriptor)psx2_device )->bDeviceClass);
	theApp.Out("	Device sub-class is: 0x%x\n",((pUsb_Device_Descriptor)psx2_device )->bDeviceSubClass);
	theApp.Out("	Manufacturer string index is: 0x%x\n",((pUsb_Device_Descriptor)psx2_device )->iManufacturer);
	theApp.Out("	Product string index is: 0x%x \n",((pUsb_Device_Descriptor)psx2_device )->idProduct);
	theApp.Out("	Serial number string index is: 0x%x\n",((pUsb_Device_Descriptor)psx2_device )->iSerialNumber);
	theApp.Out("	Number of configurations is: 0x%x\n",((pUsb_Device_Descriptor)psx2_device )->bNumConfigurations);
	theApp.Out("\n\n");
}

void CMainFrame::OnTestGetstring() 
{
	// TODO: Add your command handler code here
	char viewstr[100];
	int i = 0;
	PVOID psx2_string= 0;
	int stringindex = 0;
	psx2_string = Sx2GetStringDesc(stringindex++);
	if(psx2_string == NULL)
	{
		AfxMessageBox("操作失败,请复位SEED-DEC55XX系统");
	}
	for(i =0;i<(((pUsb_String_Descriptor)psx2_string)->bLength)/2-1;i++)
	{
		viewstr[i] = ((pUsb_String_Descriptor)psx2_string)->bString[i*2];
	}
	viewstr[i] = '\0';
	/*显示获取的描述表*/
	theApp.Out("*********************************************************************\n");
	theApp.Out("the current cy7c68001's string Decriptor is:\n");
	theApp.Out("-----------------------------------\n");
	theApp.Out("%s ",viewstr);
	psx2_string = Sx2GetStringDesc(stringindex++);
	if(psx2_string == NULL)
	{
		AfxMessageBox("操作失败,请复位SEED-DEC55XX系统");
	}
	for(i =0;i<(((pUsb_String_Descriptor)psx2_string)->bLength)/2-1;i++)
	{
		viewstr[i] = ((pUsb_String_Descriptor)psx2_string)->bString[i*2];
	}
	viewstr[i] = '\0';
	theApp.Out("%s\n\n ",viewstr);
}

void CMainFrame::OnTestGetconfig() 
{
	//显示USB的配置信息
	int i = 0;
	PVOID psx2_config = 0;
	psx2_config = Sx2GetconfigDesc();
	pUsb_Configuration_Descriptor pCD = (pUsb_Configuration_Descriptor) psx2_config;
    PUSB_INTERFACE_DESCRIPTOR     pID = NULL; 
    PUSB_ENDPOINT_DESCRIPTOR      pED = NULL;
	if(psx2_config == NULL)
	{
		AfxMessageBox("操作失败,请复位SEED-DEC55XX系统");
	}
	/*显示获取的描述表*/
	theApp.Out("*********************************************************************\n");
	theApp.Out("The current cy7c68001's configuration Decriptor is:\n");
	theApp.Out("	bLength: 0x%x\n", pCD->bLength);
	theApp.Out("	bDescriptorType:  %d\n", pCD->bDescriptorType);
	theApp.Out("	wTotalLength:  %d (0x%x)\n", pCD->wTotalLength, pCD->wTotalLength);
	theApp.Out("	bNumInterfaces:  %d\n", pCD->bNumInterfaces);
	theApp.Out("	bConfigurationValue:  %d\n", pCD->bConfigurationValue);
	theApp.Out("	iConfiguration:  %d\n", pCD->iConfiguration);
	theApp.Out("	bmAttributes: 0x%x\n", pCD->bmAttributes);
	theApp.Out("	MaxPower:  %d\n", pCD->MaxPower);
	theApp.Out("***********************************\n");
	/*显示接口的描述表*/
	for( pID = (PUSB_INTERFACE_DESCRIPTOR) (((char*)pCD) + pCD->bLength); 
       pID < (PUSB_INTERFACE_DESCRIPTOR) (((char*)pCD) + pCD->wTotalLength);
	   pID->bLength)
	{
		theApp.Out("Interface Descriptor:  \n");
		theApp.Out("-----------------------------------\n");
		theApp.Out("	bLength:  0x%x\n", pID->bLength);
		theApp.Out("	bDescriptorType:  %d\n", pID->bDescriptorType);
		theApp.Out("	bInterfaceNumber:  %d\n", pID->bInterfaceNumber);
		theApp.Out("	bAlternateSetting:  %d\n", pID->bAlternateSetting);
		theApp.Out("	bNumEndpoints:  %d\n", pID->bNumEndpoints);
		theApp.Out("	bInterfaceClass:  %d (0x%x)\n", pID->bInterfaceClass, pID->bInterfaceClass);
		theApp.Out("	bInterfaceSubClass:  %d (0x%x)\n", pID->bInterfaceSubClass, pID->bInterfaceSubClass);
		theApp.Out("	bInterfaceProtocol:  %d (0x%x)\n", pID->bInterfaceProtocol, pID->bInterfaceProtocol);
		theApp.Out("	iInterface:  %d\n", pID->iInterface);
		theApp.Out("************************************\n");
		// 显示节点的描述表
		pED = (PUSB_ENDPOINT_DESCRIPTOR) (((char*)pID) + pID->bLength);
		for (i=0;i<pID->bNumEndpoints;i++)
		{
			theApp.Out("Endpoint Descriptor %d  \n", i);
			theApp.Out("-----------------------------------\n");
			theApp.Out("	bLength:  0x%x\n", pED->bLength);
			theApp.Out("	bDescriptorType:  %d\n", pED->bDescriptorType);
			theApp.Out("	bEndpointAddress:  0x%x\n", pED->bEndpointAddress);
			theApp.Out("	bmAttributes:  0x%x\n", pED->bmAttributes);
			theApp.Out("	wMaxPacketSize:  %d\n", pED->wMaxPacketSize);
			theApp.Out("	bInterval:  %d\n", pED->bInterval);
			theApp.Out("*************************************\n");
			pED = (PUSB_ENDPOINT_DESCRIPTOR) (((char*)pED) + (pED->bLength));
		}
		// pointer should grow
		pID = (PUSB_INTERFACE_DESCRIPTOR) (char*)pED; 
	}

}

void CMainFrame::OnTestVendor() 
{
	// 测试命令输入
	int i = 0;
	int recnBytes;
	char buffer[64];
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest;
	CUsbvreqDialog vendordlg;
	if(vendordlg.DoModal()== IDCANCEL)
	{
		return;
	}
	theApp.Out("USB vendor requset!");
	myRequest.direction = vendordlg.m_VendorDir;
	myRequest.index = vendordlg.m_VendorIndex;
	myRequest.value = vendordlg.m_VendorValue;
	myRequest.request = vendordlg.m_VendorReq;
	// vendor specific request type (2)
	myRequest.requestType=2; //configuration
    // recepient is device (0)
	myRequest.recepient=0;	//device
	for(i = 0 ;i <vendordlg.m_VendorLength; i++)
	{
		buffer[i] = vendordlg.m_Endpoint0data[i];
	}
	Sx2SendVendorReq(&myRequest,
					 &buffer[0],
					 vendordlg.m_VendorLength,
					 &recnBytes);
	if(recnBytes !=0)
	{
		for(i = 0;i<recnBytes;i++)
		{
			theApp.Out("The data we recieved from the Endpoint0 is 0x%4x\n",buffer[i]);
		}
	}
}

void CMainFrame::OnTestGetpipeinfo() 
{
	// 显示节点的信息
	int i = 0;
	USBD_INTERFACE_INFORMATION Sx2InterfaceInfo;
	PUSBD_INTERFACE_INFORMATION	pSx2InterfaceInfo = &Sx2InterfaceInfo;
	PUSBD_PIPE_INFORMATION pPipe;
	/*获得USB的接口信息*/
	if(TRUE !=Sx2GetPipeInfo((PVOID)&Sx2InterfaceInfo))
	{
		return;
	}
	pPipe = pSx2InterfaceInfo->Pipes;
	theApp.Out("*********************************************************************\n");
	theApp.Out("The pipe information is :\n");
	theApp.Out("------------------------------------------------\n");
	theApp.Out("Interface Size %d\n",pSx2InterfaceInfo->Length);
	for(i= 0; i < (int) pSx2InterfaceInfo->NumberOfPipes;i++)
	{
		theApp.Out( "	Pipe: %2d   Type: %s Endpoint: %2d %s  MaxPktSize: 0x%x\n",
					i,
					PIPE_TYPE_STRINGS[pPipe[i].PipeType],
					(pPipe[i].EndpointAddress & 0x0F),PIPE_DIRECTION[(pPipe[i].EndpointAddress >> 7)],
					pPipe[i].MaximumPacketSize);
	}
	theApp.Out("\n");
}

void CMainFrame::OnBulkRead() 
{
	// BULK数据的读操作
	int i = 0;
	char *buffer;
	char recievedat[1024] = {0};
    ULONG length;
    BULK_TRANSFER_CONTROL bulkControl;
	int recnBytes = 0;
	/*命令请求定义*/
	char buffer1[64];
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest;
	ULONG venderlong;
	/*发送命令,使DSP先定入固定的数据*/
	myRequest.direction = 0;
	myRequest.index = 0;
	myRequest.value = 2;
	myRequest.request =0xb6;
	// vendor specific request type (2)
	myRequest.requestType=2; 
    // recepient is device (0)
	myRequest.recepient=0;
	venderlong = 0;
	Sx2SendVendorReq(&myRequest,
					 &buffer1[0],
					 venderlong,
					 &recnBytes);
	/*延时250ms*/

⌨️ 快捷键说明

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