📄 pwusbflash.cpp
字号:
//---------------------------------------------------------------------------
// Pixelworks Inc. Company Confidential Strictly Private
//
// $Archive: $
// $Revision: 1.1.1.1 $
// $Author: KevinM $
// $Date: 2003/09/29 18:19:04 $
//
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Copyright 1997-2003 (c) Pixelworks Inc.
//
// Pixelworks owns the sole copyright to this software. Under international
// copyright laws you (1) may not make a copy of this software except for
// the purposes of maintaining a single archive copy, (2) may not derive
// works herefrom, (3) may not distribute this work to others. These rights
// are provided for information clarification, other restrictions of rights
// may apply as well.
//
// This is an unpublished work.
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>> WARRANTEE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Pixelworks Inc. MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THE USE OF
// THIS SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
// PURPOSE.
// --------------------------------------------------------------------------
//
// PWUsb.cpp: implementation of the CPWUsb class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PWUsbFlash.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
const GUID g_GUID = PWUSB_IID;
HDEVINFO g_DevList = NULL;
static BOOL bReadInProgress = FALSE;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPWUsb::CPWUsb()
{
m_nDeviceNumber = 0;
m_nEndpoint = 0x2;
m_nInterface = 0;
m_nConfiguration= 0;
}
CPWUsb::~CPWUsb()
{
m_Writer.m_bShutdown = TRUE;
m_InPipe.CircBuf.m_bShutdown = TRUE;
m_Writer.CircBuf.m_bShutdown = TRUE;
m_InPipe.CircBuf.m_pReadEv->SetEvent();
m_InPipe.CircBuf.m_pWriteEv->SetEvent();
m_InPipe.ShutdownThread();
m_Writer.CircBuf.m_pReadEv->SetEvent();
m_Writer.CircBuf.m_pWriteEv->SetEvent();
m_Writer.ShutdownThread();
}
BOOL CPWUsb::Initialize(int nTimeoutSec)
{
// If (nTimeout == 0) then only try once to open the device,
// otherwise, keep trying to open the device until it opens,
// or the specified number of seconds elapses.
CWaitCursor WaitCur;
bool bResult = TRUE;
m_Writer.DestroyDeviceList(g_DevList);
g_DevList = NULL;
g_DevList = m_Writer.CreateDeviceList(&g_GUID);
bResult = (g_DevList != NULL);
int nSleepTimeMSec = 500;
int nElapsedTimeMSec = 0;
int nTimeoutMSec = nTimeoutSec*1000;
while (nElapsedTimeMSec <= nTimeoutMSec)
{
if (bResult)
{
bResult = (m_Writer.Open(m_nDeviceNumber, g_DevList, &g_GUID) == 0);
}
if (nTimeoutSec == 0 || bResult)
{
// No timeout specified, or the device is open...
break;
}
if (!bResult)
{
// Didn't open the device yet, sleep, then try again.
CString strMsg;
strMsg.Format("Open USB Failed (ET: %dms)\n", nElapsedTimeMSec);
OutputDebugString(strMsg);
Sleep(nSleepTimeMSec);
nElapsedTimeMSec += nSleepTimeMSec;
}
}
if (bResult)
{
bResult = (m_InPipe.Open(m_nDeviceNumber, g_DevList, &g_GUID) == 0);
}
if (bResult)
{
bResult = DoSetConfiguration();
}
if (bResult)
{
bResult = SetupOutPipe();
}
if (bResult)
{
bResult = SetupInPipe();
}
return bResult;
}
#define MAX_PACKET_SIZE 4096
bool CPWUsb::DoSetConfiguration()
{
bool bResult = TRUE;
USBIO_SET_CONFIGURATION SetConfig;
SetConfig.ConfigurationIndex = m_nConfiguration;
SetConfig.NbOfInterfaces = 1;
SetConfig.InterfaceList[0].AlternateSettingIndex = 0;
SetConfig.InterfaceList[0].InterfaceIndex = m_nInterface;
SetConfig.InterfaceList[0].MaximumTransferSize = MAX_PACKET_SIZE;
bResult = !m_InPipe.SetConfiguration(&SetConfig);
if (!bResult)
{
// Try the next interface
m_nInterface++;
SetConfig.InterfaceList[0].InterfaceIndex = m_nInterface;
bResult = !m_InPipe.SetConfiguration(&SetConfig);
}
return bResult;
}
bool CPWUsb::SendData(LPBYTE pByte, int nCount /* = 1 */)
{
bool bResult = TRUE;
m_InPipe.Flush();
m_Writer.WriteData(pByte, nCount);
return bResult;
}
int CPWUsb::Read(LPBYTE szBuff, int nCount, BYTE cMarker)
{
int nResult = 0;
int nGot = m_InPipe.FetchBytes(&szBuff[nResult], (nCount-nResult), cMarker);
nResult += nGot;
return nResult;
}
bool CPWUsb::SetupInPipe()
{
bool bResult = TRUE;
int nEndpoint = (0x80|m_nEndpoint);
if (m_bReaderRunning == TRUE)
{
m_InPipe.ShutdownThread();
m_InPipe.Unbind();
m_bReaderRunning = FALSE;
}
if (bResult)
{
bResult = (m_InPipe.Bind(m_nDeviceNumber,
nEndpoint, g_DevList, &g_GUID) == USBIO_ERR_SUCCESS);
}
if (bResult)
{
USBIO_CONFIGURATION_INFO Info;
USBIO_PIPE_CONFIGURATION_INFO m_PipeInfo;
m_InPipe.GetConfigurationInfo(&Info);
for (DWORD i=0;i<Info.NbOfPipes;i++)
{
if (Info.PipeInfo[i].EndpointAddress == nEndpoint)
{
m_PipeInfo=Info.PipeInfo[i]; // struct copy
break;
}
}
m_InPipe.m_nMaxPacketSize = m_PipeInfo.MaximumPacketSize;
}
if (bResult)
{
USBIO_PIPE_PARAMETERS PipeParameters;
PipeParameters.Flags=USBIO_SHORT_TRANSFER_OK;
bResult = (m_InPipe.SetPipeParameters(&PipeParameters) == USBIO_ERR_SUCCESS);
}
if (bResult)
{
m_InPipe.AbortPipe();
m_InPipe.AbortPipe();
m_InPipe.ResetPipe();
//m_InPipe.ResetPipe();
}
if (bResult)
{
bResult = !!m_InPipe.StartThread();
}
if (bResult)
{
m_bReaderRunning = TRUE;
}
return bResult;
}
bool CPWUsb::SetupOutPipe()
{
bool bResult = TRUE;
if (m_bWriterRunning == TRUE)
{
m_Writer.ShutdownThread();
m_Writer.Unbind();
m_bWriterRunning = FALSE;
}
if (bResult)
{
bResult = (m_Writer.Bind(m_nDeviceNumber,
m_nEndpoint, g_DevList, &g_GUID) == USBIO_ERR_SUCCESS);
}
if (bResult)
{
USBIO_CONFIGURATION_INFO Info;
USBIO_PIPE_CONFIGURATION_INFO m_PipeInfo;
m_InPipe.GetConfigurationInfo(&Info);
for (DWORD i=0;i<Info.NbOfPipes;i++)
{
if (Info.PipeInfo[i].EndpointAddress == m_nEndpoint)
{
m_PipeInfo=Info.PipeInfo[i]; // struct copy
break;
}
}
m_Writer.m_nMaxPacketSize = m_PipeInfo.MaximumPacketSize;
}
if (bResult)
{
USBIO_PIPE_PARAMETERS PipeParameters;
PipeParameters.Flags=USBIO_SHORT_TRANSFER_OK;
bResult = (m_Writer.SetPipeParameters(&PipeParameters) == USBIO_ERR_SUCCESS);
}
if (bResult)
{
m_Writer.AbortPipe();
m_Writer.AbortPipe();
m_Writer.ResetPipe();
//m_Writer.ResetPipe();
}
if (bResult)
{
bResult = !!m_Writer.StartThread();
}
if (bResult)
{
m_bWriterRunning = TRUE;
}
return bResult;
}
int CPWUsb::GetDataSize()
{
return m_InPipe.GetAvailData();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -