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

📄 isofunc.cpp

📁 国半usbn9604芯片开发办的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------
 *  Copyright (c) 2001 by National Semiconductor Corporation
 *  National Semiconductor Corporation
 *  2900 Semiconductor Drive
 *  Santa Clara, California 95051
 *
 *  All rights reserved
 *
 *<<<-------------------------------------------------------------------------
 * File Contents:
 *	IsoFunc.cpp - this class handles Isochronous test. It gets arguments from the Isochronous 
 *				  Tab and operates the USB driver accordingly.
 *
 *  Project: USB Demo Application
 *  Author : Yan Nosovitsky
 *  Date   : Dec 2001
 *----------------------------------------------------------------------->>>*/

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

#include "stdafx.h"
#include "resource.h"
#include "command_api.h"
#include "CommonDefines.h"
#include "ControlFunc.h"
#include "USBDriver.h"
#include "IsoFunc.h"
#include "isoCmdTab.h"


IsoFunc::IsoFunc(CDialog *curIsoTab)
{
	isoTab = curIsoTab;
	processThread = NULL;
	readThread = NULL;
	writeThread = NULL;
	testRun = FALSE;
}

IsoFunc::~IsoFunc()
{
}

bool IsoFunc::ProcessOneDirTest(int pipeNum,int dtSize, BYTE data, BOOL useDMA)
{
	USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
	if (curUSBDriver->IsUSBDriverHaveError())
	{
		isoTab->MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
		return FALSE;
	}

	if (curUSBDriver->CurUSBUsers() > 0)
	{
		isoTab->MessageBox("Cannot process current command.\n Wait until Bulk test is finished.","Error",MB_ICONERROR | MB_OK);
		return FALSE;
	}

	curUSBDriver->IncreaseUSBUsers();

	dataSize = dtSize;
	numOfErrors = 0;
	hexByte = data;
	restToWrite = restToRead = dtSize;
	readTime = writeTime = 0;

	/* check direction of selected pipe */
	if ( Pipe[pipeNum].direction == USB_DIR_IN)
	{   /*  from device to the host */
		command = ISOCH_SEND_DATA;
		/* allocate memory for input buffer */
		if ((readBuff = (BYTE *)malloc(dataSize))==NULL)
		{
			isoTab->MessageBox("Cannot allocate memory for input buffer.","Error",MB_ICONERROR | MB_OK);
			curUSBDriver->DecreaseUSBUsers();
			return FALSE;
		}
		readPipe = pipeNum;		
	}			
	else
	{	/* from the host to the device */
		command = ISOCH_GET_DATA;
		/* allocate memory for output buffer */
		if ((writeBuff = (BYTE *)malloc(dataSize))==NULL)
		{
			isoTab->MessageBox("Cannot allocate memory for output buffer.","Error",MB_ICONERROR | MB_OK);
			curUSBDriver->DecreaseUSBUsers();
			return FALSE;
		}
		
		/* prepare buffer with the data */

		memset(writeBuff,hexByte,dataSize);
		writePipe = pipeNum;
	}

		
	/*  send command to device */
	if (!ControlFunc::SendSetupPacketOut(command, dataSize&0xffff,((dataSize&0xffff0000)>>16), hexByte))
	{
		/* Can't send setup packet */
		isoTab->MessageBox("Cannot process current command.\nPlease check USB connection.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}

	/* start process thread */

	processThread = AfxBeginThread(ProcessIsoThread,this);
	return TRUE;
}
	
bool IsoFunc::ProcessLoopTestFile(CString fileName, int pipeNumToDMA)
{
	
	CFile dataFile;
	CFileException fileException;

	USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
	if (curUSBDriver->IsUSBDriverHaveError())
	{
		isoTab->MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
		return FALSE;
	}

	if (curUSBDriver->CurUSBUsers() > 0)
	{
		isoTab->MessageBox("Cannot process current command.\n Wait until bulk test is finished.","Error",MB_ICONERROR | MB_OK);
		return FALSE;
	}

	curUSBDriver->IncreaseUSBUsers();

	readTime = writeTime = 0;
	numOfErrors = 0;
	if (!dataFile.Open(fileName,CFile::modeRead))
	{
		isoTab->MessageBox("Cannot open output file.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}
			
	dataSize  = dataFile.GetLength();
	restToRead = restToWrite = dataSize;
	command = ISOCH_SEND_IMM_BACK;

	/* allocate memory for input buffer */
	if ((readBuff = (BYTE *)malloc(dataSize))==NULL)
	{
		isoTab->MessageBox("Cannot allocate memory for input buffer.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}
	
	/* allocate memory for output buffer */
	if ((writeBuff = (BYTE *)malloc(dataSize))==NULL)
	{
		isoTab->MessageBox("Cannot allocate memory for output buffer.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}
	/* fill output buffer */


	if (dataFile.Read((void *)writeBuff,dataSize) != dataSize)
	{
		isoTab->MessageBox("Cannot fill output buffer.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}

	/*  send command to device */
	if (!ControlFunc::SendSetupPacketOut(command, dataSize&0xffff,((dataSize&0xffff0000)>>16), 0))
	{
		/* Can't send setup packet */
		isoTab->MessageBox("Cannot process current command.\nPlease check USB connection.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}

	/* start process thread */

	processThread = AfxBeginThread(ProcessIsoThread,this);
	return TRUE;
}

bool IsoFunc::ProcessLoopTestData(int dtSize, int pipeNumToDMA)
{
	int i;
	
	
	USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
	if (curUSBDriver->IsUSBDriverHaveError())
	{
		isoTab->MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
		return FALSE;
	}

	if (curUSBDriver->CurUSBUsers() > 0)
	{
		isoTab->MessageBox("Cannot process current command.\nWait until bulk test is finished.","Error",MB_ICONERROR | MB_OK);
		return FALSE;
	}

	curUSBDriver->IncreaseUSBUsers();


	numOfErrors = 0;
	
	readTime = writeTime = 0;
	dataSize = dtSize;
	restToRead = restToWrite = dataSize;
	command = ISOCH_SEND_IMM_BACK;

	/* allocate memory for input buffer */
	if ((readBuff = (BYTE *)malloc(dataSize))==NULL)
	{
		isoTab->MessageBox("Cannot allocate memory for input buffer.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}
	
	/* allocate memory for output buffer */
	if ((writeBuff = (BYTE *)malloc(dataSize))==NULL)
	{
		isoTab->MessageBox("Cannot allocate memory for output buffer.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}
	/* fill output buffer */

	for (i=0; i<dataSize; i++)
		writeBuff[i] = (byte)i;
	

	/*  send command to device */
	if (!ControlFunc::SendSetupPacketOut(command, dataSize&0xffff,((dataSize&0xffff0000)>>16), 0))
	{
		/* Can't send setup packet */
		isoTab->MessageBox("Cannot process current command.\nPlease check USB connection.","Error",MB_ICONERROR | MB_OK);
		curUSBDriver->DecreaseUSBUsers();
		return FALSE;
	}

	/* start process thread */

	processThread = AfxBeginThread(ProcessIsoThread,this);
	return TRUE;
}

CString IsoFunc::GetDetails()
{
	CString details;
	CString lastTest;
	CString statistic;
	CString time;

	details.Format(_T("The last test was supposed to transfer\n %d bytes "),dataSize);
	
	switch(command)
	{
	case ISOCH_SEND_IMM_BACK:
		lastTest.Format(_T("in loopback.\n"));
		time.Format(_T(" Time:         %.3f sec\n"),(double)(readTime)/(double)1000);
		statistic.Format(_T("\nStatistics :\n Sent:         %d bytes\n Received:  %d bytes\n Lost\n packets:    %d\n"),dataSize - restToWrite,dataSize - restToRead, numOfErrors);
		break;
	case ISOCH_SEND_DATA:
		lastTest.Format(_T("from device to host.\n"));
		time.Format(_T(" Time:         %.3f sec\n"),(double)(readTime)/(double)1000);
		statistic.Format(_T("\nStatistics :\n Received:  %d bytes\n Errors:       %d\n"),dataSize - restToRead, numOfErrors);
		break;
	case ISOCH_GET_DATA:
		lastTest.Format(_T("from host to device.\n"));
		time.Format(_T(" Time:         %.3f sec\n"),(double)(writeTime)/(double)1000);
		statistic.Format(_T("\nStatistics :\n Sent:         %d bytes\n Errors:       %d\n"),dataSize - restToWrite, numOfErrors);
		break;
	}

	details += lastTest;
	details += statistic;
	
	
	details += time;
	return details;
}

void IsoFunc::StopLastProcess()
{
	readThreadRun = FALSE;
	writeThreadRun = FALSE;
	
/*	USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
	if (curUSBDriver->IsUSBDriverHaveError())
		return;

⌨️ 快捷键说明

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