📄 bulkfunc.cpp
字号:
/*----------------------------------------------------------------------------
* Copyright (c) 2001 by National Semiconductor Corporation
* National Semiconductor Corporation
* 2900 Semiconductor Drive
* Santa Clara, California 95051
*
* All rights reserved
*
*<<<-------------------------------------------------------------------------
* File Contents:
* BulkFunc.cpp - this class handles Bulk test. It gets arguments from the Bulk 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 "BulkFunc.h"
#include "BulkCmdTab.h"
BulkFunc::BulkFunc(CDialog *curBulkTab)
{
bulkTab = curBulkTab;
processThread = NULL;
readThread = NULL;
writeThread = NULL;
testRun = FALSE;
}
BulkFunc::~BulkFunc()
{
}
bool BulkFunc::ProcessOneDirTest(int pipeNum,int dtSize, BYTE data, BOOL useDMA)
{
int i = 0;
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
if (curUSBDriver->IsUSBDriverHaveError())
{
bulkTab->MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
return FALSE;
}
if (curUSBDriver->CurUSBUsers() > 0)
{
bulkTab->MessageBox("Cannot process current command.\nWait until isochronous 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 = BULK_SEND_DATA;
/* allocate memory for input buffer */
if ((readBuff = (BYTE *)malloc(dataSize))==NULL)
{
bulkTab->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 = BULK_GET_DATA;
if (curUSBDriver->IsIntTestRun())
{
/* Cannot run this test */
bulkTab->MessageBox("Cannot process current command.\nWait until currently running test is finished.","Error",MB_ICONERROR | MB_OK);
curUSBDriver->DecreaseUSBUsers();
return FALSE;
}
curUSBDriver->CnangeBulkOutTestStatus(TRUE);
/* allocate memory for output buffer */
if ((writeBuff = (BYTE *)malloc(dataSize))==NULL)
{
bulkTab->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 */
bulkTab->MessageBox("Cannot process current command.\nPlease check USB connection.","Error",MB_ICONERROR | MB_OK);
curUSBDriver->DecreaseUSBUsers();
return FALSE;
}
/* start process thread */
processThread = AfxBeginThread(ProcessBulkThread,this);
return TRUE;
}
bool BulkFunc::ProcessLoopTestFile(LoopbackTest loopbackTestType,CString fileName, int pipeNumToDMA)
{
CFile dataFile;
CFileException fileException;
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
if (curUSBDriver->IsUSBDriverHaveError())
{
bulkTab->MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
return FALSE;
}
if (curUSBDriver->CurUSBUsers() > 0)
{
bulkTab->MessageBox("Cannot process current command.\n Wait until isochronous test is finished.","Error",MB_ICONERROR | MB_OK);
return FALSE;
}
curUSBDriver->IncreaseUSBUsers();
readTime = writeTime = 0;
numOfErrors = 0;
if (!dataFile.Open(fileName,CFile::modeRead))
{
bulkTab->MessageBox("Cannot open output file.","Error",MB_ICONERROR | MB_OK);
curUSBDriver->DecreaseUSBUsers();
return FALSE;
}
dataSize = dataFile.GetLength();
restToRead = restToWrite = dataSize;
command = (loopbackTestType == DELAYED) ? BULK_SEND_AFTER_LAST_BACK : BULK_SEND_IMM_BACK;
/* allocate memory for input buffer */
if ((readBuff = (BYTE *)malloc(dataSize))==NULL)
{
bulkTab->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)
{
bulkTab->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)
{
bulkTab->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 */
bulkTab->MessageBox("Cannot process current command.\nPlease check USB connection.","Error",MB_ICONERROR | MB_OK);
curUSBDriver->DecreaseUSBUsers();
return FALSE;
}
/* start process thread */
processThread = AfxBeginThread(ProcessBulkThread,this);
return TRUE;
}
bool BulkFunc::ProcessLoopTestData(int dtSize, LoopbackTest loopbackTestType, int pipeNumToDMA)
{
int i;
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
if (curUSBDriver->IsUSBDriverHaveError())
{
bulkTab->MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
return FALSE;
}
if (curUSBDriver->CurUSBUsers() > 0)
{
bulkTab->MessageBox("Cannot process current command.\n Wait until isochronous test is finished.","Error",MB_ICONERROR | MB_OK);
return FALSE;
}
curUSBDriver->IncreaseUSBUsers();
numOfErrors = 0;
readTime = writeTime = 0;
dataSize = dtSize;
restToRead = restToWrite = dataSize;
command = (loopbackTestType == DELAYED) ? BULK_SEND_AFTER_LAST_BACK : BULK_SEND_IMM_BACK;
/* allocate memory for input buffer */
if ((readBuff = (BYTE *)malloc(dataSize))==NULL)
{
bulkTab->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)
{
bulkTab->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 */
bulkTab->MessageBox("Cannot process current command.\nPlease check USB connection.","Error",MB_ICONERROR | MB_OK);
curUSBDriver->DecreaseUSBUsers();
return FALSE;
}
/* start process thread */
processThread = AfxBeginThread(ProcessBulkThread,this);
return TRUE;
}
CString BulkFunc::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 BULK_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 Errors: %d\n"),dataSize - restToWrite,dataSize - restToRead, numOfErrors);
break;
case BULK_SEND_AFTER_LAST_BACK:
lastTest.Format(_T("in loopback.\n"));
time.Format(_T(" Time: %.3f sec\n"),(double)(readTime + writeTime)/(double)1000);
statistic.Format(_T("\nStatistics :\n Sent: %d bytes\n Received: %d bytes\n Errors: %d\n"),dataSize - restToWrite,dataSize - restToRead, numOfErrors);
break;
case BULK_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 BULK_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 BulkFunc::StopLastProcess()
{
readThreadRun = FALSE;
writeThreadRun = FALSE;
/* USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
if (curUSBDriver->IsUSBDriverHaveError())
return;
if (processThread)
processThread->ResumeThread();
if (testRun)
switch (command)
{
case BULK_SEND_IMM_BACK:
case BULK_SEND_AFTER_LAST_BACK:
readThreadRun = FALSE;
writeThreadRun = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -