📄 intfunc.cpp
字号:
/*----------------------------------------------------------------------------
* Copyright (c) 2001 by National Semiconductor Corporation
* National Semiconductor Corporation
* 2900 Semiconductor Drive
* Santa Clara, California 95051
*
* All rights reserved
*
*<<<-------------------------------------------------------------------------
* File Contents:
* IntFunc.cpp - this class handles Interrupt test. It gets arguments from the Interrupt
* 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 "IntFunc.h"
#include "intCmdTab.h"
IntFunc::IntFunc(CDialog *curIntTab)
{
intTab = curIntTab;
command = INTERRUPT_NO_COMMAND;
testRun = FALSE;
readThread = NULL;
processThread = NULL;
}
IntFunc::~IntFunc()
{
}
bool IntFunc::ProcessIntTest(int intNum,int interval, int pipeNum)
{
interruptNum = intNum;
intInterval = interval;
command = INTERRUPT_GET_INT;
restToDo = intNum;
wasTransferred = 0;
waitForNum = 0;
numOfErrors = 0;
readTime = 0;
readPipe = pipeNum;
/* check USB Driver */
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
if (curUSBDriver->IsUSBDriverHaveError())
{
intTab->MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
return FALSE;
}
if (curUSBDriver->IsBulkOutTestRun())
{
/* Bulk OUT test run */
intTab->MessageBox("Cannot process current command.\nWait until Bulk OUT test is finished.","Error",MB_ICONERROR | MB_OK);
return FALSE;
}
curUSBDriver->CnangeIntTestStatus(TRUE);
/*send command to device */
if (!ControlFunc::SendSetupPacketOut(command, intNum&0xffff,interval, 0))
{
/* Can't send setup packet */
intTab->MessageBox("Cannot process current command.\nPlease check USB connection.","Error",MB_ICONERROR | MB_OK);
return FALSE;
}
/* start process thread */
processThread = AfxBeginThread(ProcessIntThread,this);
return TRUE;
}
UINT IntFunc::ProcessIntThread(LPVOID par)
{
IntFunc* curIntFunc = (IntFunc *)par;
bool errorDetected = FALSE;
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
curIntFunc->testRun = TRUE;
switch (curIntFunc->command)
{
case INTERRUPT_GET_INT:
curIntFunc->readThreadRun = TRUE;
curIntFunc->readThread = AfxBeginThread(ReadIntThread,curIntFunc);
::WaitForSingleObject(curIntFunc->readThread->m_hThread,INFINITE);
curIntFunc->readThread = NULL;
if (!curIntFunc->readThreadRun)
/* Error detected */
errorDetected = TRUE;
curIntFunc->readThreadRun = FALSE;
curUSBDriver->CnangeIntTestStatus(FALSE);
break;
}
if (!errorDetected)
{
(curIntFunc->intTab)->MessageBox("Interrupt operation is complete.","Success",MB_ICONINFORMATION | MB_OK);
}
curIntFunc->testRun = FALSE;
((intCmdTab *)(curIntFunc->intTab))->processStoped();
curIntFunc->processThread = NULL;
return 0;
}
void IntFunc::StopLastProcess()
{
readThreadRun = FALSE;
/* USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
if (curUSBDriver->IsUSBDriverHaveError())
return;
if (testRun)
switch (command)
{
case INTERRUPT_GET_INT:
if (processThread)
processThread->ResumeThread();
readThreadRun = FALSE;
curUSBDriver->StopTransferPipe(Pipe[readPipe].dwNumber);
if (readThread)
readThread->ResumeThread();
break;
}
*/
}
UINT IntFunc::ReadIntThread(LPVOID par)
{
IntFunc* curIntFunc = (IntFunc *)par;
int pipeAdd = curIntFunc->Pipe[curIntFunc->readPipe].dwNumber;
BYTE buffer[8] = {0};
int read = 0;
CString intNum;
DWORD start = 0;
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
while (curIntFunc->readThreadRun)
{
/* read interrupt pipe */
read = curUSBDriver->WorkWithPipe(pipeAdd,buffer,1,TRUE);
if (!curIntFunc->readThreadRun)
return -1;
if (read ==-1)
{
/* error */
if (curIntFunc->readThreadRun)
(curIntFunc->intTab)->MessageBox("Cannot process current command.\nPlease check USB connection.","Error",MB_ICONERROR | MB_OK);
curIntFunc->readThreadRun = FALSE;
return -1;
}
if (read>0)
{
/* interrupt arrived */
BYTE curInt = buffer[0];
if (curInt != curIntFunc->waitForNum)
{ /* wrong interrupt number */
curIntFunc->numOfErrors +=(curInt - curIntFunc->waitForNum);
curIntFunc->waitForNum = curInt+1;
}
else
curIntFunc->waitForNum++;
if (start != 0)
curIntFunc->readTime +=(GetTickCount() - start);
start = GetTickCount();
curIntFunc->restToDo -- ;
curIntFunc->wasTransferred ++;
intNum.Format(_T("%d"),curIntFunc->waitForNum);
((intCmdTab *)(curIntFunc->intTab))->m_arrivedInt.SetWindowText(intNum);
((intCmdTab *)(curIntFunc->intTab))->m_progresBar.SetPos(curIntFunc->waitForNum);
if (curIntFunc->waitForNum == curIntFunc->interruptNum)
/* all interrupts arrived */
return 0;
}
}
return -1;
}
UINT IntFunc::PrintMessage(LPVOID par)
{
IntFunc* curIntFunc = (IntFunc *)par;
CString msg;
msg.Format(_T("Interrupt number %d arrived."),curIntFunc->waitForNum);
(curIntFunc->intTab)->MessageBox(msg,"Inerrup test",MB_ICONINFORMATION | MB_OK);
return 0;
}
int IntFunc::CheckPipes()
{
int i = 0;
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
numOfPipes = 0;
if ( !curUSBDriver->IsUSBDriverHaveError())
{
for (i = 0; i < WD_USB_MAX_PIPE_NUMBER; i++)
if (curUSBDriver->CheckPipe(PIPE_TYPE_INTERRUPT, i, &Pipe[numOfPipes]))
{
if ( Pipe[numOfPipes].direction == USB_DIR_IN)
readPipe = numOfPipes;
else
writePipe = numOfPipes;
numOfPipes++;
}
}
return numOfPipes;
}
CString IntFunc::getPipeInfo(int num)
{
CString pipeInfo;
CString pipeDir;
if ( Pipe[num].direction == USB_DIR_IN)
pipeDir.Format(_T("IN "));
else
pipeDir.Format(_T("OUT "));
pipeInfo.Format(_T("%d. Pipe : 0x%02x Type : INTERRUPT MaxPktSize : 0x%x Dir : "),num+1,Pipe[num].dwNumber,Pipe[num].dwMaximumPacketSize);
pipeInfo += pipeDir;
return pipeInfo;
}
CString IntFunc::GetDetails()
{
CString details;
CString lastTest;
CString statistic;
details.Format(_T("The last test was supposed to transfer\n %d interrupts "),interruptNum);
switch(command)
{
case INTERRUPT_GET_INT :
lastTest.Format(_T("from device to host.\n"));
break;
}
details += lastTest;
// statistic.Format(_T("\nStatistics :\n Sent: %d bytes\n Errors: %d\n Time: \n Byte Rate: \n"),wasTransferred,numOfErrors);
statistic.Format(_T("\nStatistics :\n Received: %d interrupts\n Errors: %d\n Interval: %.3f sec\n"),wasTransferred,numOfErrors,((double)readTime/(double)1000)/(double)(wasTransferred - 1));
details += statistic;
return details;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -