EZ-USB 68013学习笔记--CYAPI的使用之批量传输方式

cyapi一些重要函数说明见:http://blog.csdn.net/lg2lh/article/details/7369793

最近一直在调slavefifo,做上位机的时候总是调不过去,总是报错:说内存不能为写。或者乱起八糟的错误。我总结了一下应该注意一下几点:

1、cyapi.lib这个库的位置放在哪,在工程中添加的lib文件路径要和Tools->Option->directories里的lib路径要一致,否则可能报错!我开始添加的cyapi.lib是我拷到工程目录下的,而Tools->Option->directories下设置的却是cypress安装目录下的路径。最后添加相应的cyapi.h的头文件。这个最好也一致。不过我没一致。



2、(这一步不是必须的)有时候会报LIBCMT库相关的错误,设置方法,  工程(PROJECT)-设置(settings)-链接(link)-输入(input)-忽略库:LIBCMT


3、其他你就按照cybulk那个例程自己慢慢添加,一定要信心,这需要说明的一点的是 USBDevice结构体中的EndPoints[i]这个EndPoints,我一直没搞清楚  i 值对应哪个端点,今天试了试,大家千万注意了EndPoints[1]对应的是端点2,EndPoints[2]对应的端点4,EndPoints[3]对应的端点6,EndPoints[4]对应的端点8.CY的API帮助文档中根本没有说,而且给的例程里,这里的值是通过列表框返回的,单步跟一下,返回的值,就知道了。


4、变量的定义,最好按照他的,都定义成对话框的成员变量,我后来测试,不能定义成全局的,必须在一个窗口中定义  new CCyUSBDevice(m_hWnd); 最初创建的时候m_hWnd不能丢掉。

bool bLooping;
CUSB20Dlg(CWnd* pParent = NULL);// standard constructor
CCyUSBDevice *USBDevice;
CCyUSBEndPoint *OutEndpt;
CCyUSBEndPoint *InEndpt;
   int DeviceIndex;

CWinThread *XferThread;


5、在OnInitDialog()函数里要完成USBDevice 初始化和线程初始化

XferThread = NULL;
USBDevice = new CCyUSBDevice(m_hWnd);   // Create an instance of CCyUSBDevice

6、在添加启动线程按钮

void CSECGDlg::OnStartBtn() 
{
// TODO: Add your control notification handler code here
if (XferThread) 

·{
        bLooping = false;
        m_StartBtn.SetWindowText("Start");
   

else

 {
        LONG xfer = 1024;
        int n = USBDevice->DeviceCount();
        if (n != 0)
        {
                 USBDevice->Open(0);
            OutEndpt = USBDevice->EndPoints[2];
            InEndpt = USBDevice->EndPoints[4];     

            if (xfer && USBDevice->IsOpen()) 

   {
                bLooping = true;
                XferThread = AfxBeginThread(XferLoop, this);//启动线程
            }
            m_StartBtn.SetWindowText("Stop");
        }
    }

}

7.线程函数

UINT XferLoop( LPVOID params ) {


    OVERLAPPED outOvLap, inOvLap;  //  OVERLAPPED 结构体,用于异步IO可以看这篇文章有介绍 http://blog.csdn.net/lg2lh/article/details/7369793
    CSECGDlg *dlg = (CSECGDlg *) params;
char s[24]; ZeroMemory(s, 24);
LONG xfer = 512;
PUCHAR data = new UCHAR[512];ZeroMemory(data,512);
PUCHAR inData = new UCHAR[512];ZeroMemory(inData,512);
    outOvLap.hEvent  = CreateEvent(NULL, false, false, "CYUSB_OUT"); 
    inOvLap.hEvent   = CreateEvent(NULL, false, false, "CYUSB_IN"); 
LONG seed = 1;
stuffBuff(data,xfer,seed,1);   ///此函数在后面有定义,或者直接用cybulk例程即可
bool success;
LONG nSuccess = 0;
LONG nFailure = 0;
        dlg->OutEndpt->TimeOut = 2000;
   dlg->InEndpt->TimeOut = 2000;
for (;dlg->bLooping;) {
        LONG outlen,inlen,len;
        outlen = inlen = len = xfer;     // Use temp var because XferData can change the value of len
   UCHAR  *outContext = dlg->OutEndpt->BeginDataXfer(data,outlen,&outOvLap);
   UCHAR  *inContext = dlg->InEndpt->BeginDataXfer(inData,inlen,&inOvLap);

        dlg->OutEndpt->WaitForXfer(&outOvLap,2000); 
        dlg->InEndpt->WaitForXfer(&inOvLap,2000); 

        success = dlg->OutEndpt->FinishDataXfer(data, outlen, &outOvLap,outContext); 
        success = dlg->InEndpt->FinishDataXfer(inData,inlen, &inOvLap,inContext); 
if (success) {
bool pass = (memcmp(data,inData,len) == 0);
if (pass)
nSuccess++;
else
nFailure++;
} else 
nFailure++;
sprintf(s,"%d",nSuccess);
dlg->m_SuccessCount.SetWindowText(s);
sprintf(s,"%d",nFailure);
dlg->m_FailureCount.SetWindowText(s);

if ((!success) ) dlg->bLooping = false;


    CloseHandle(outOvLap.hEvent); 
    CloseHandle(inOvLap.hEvent); 
delete [] data;
delete [] inData;
dlg->XferThread = NULL;
  dlg->USBDevice->Close();
return true;
}

8、stuffBuff函数

void stuffBuff(PUCHAR buf, LONG len, LONG seed, int method) {


DWORD *dwBuf = (DWORD *) buf;
srand((UINT)seed);


int cnt = (method == 3) ? len / 4 : len;
for (int i=0; i<cnt; i++) 
switch (method) {
case 0:
buf[i] = (CHAR) seed;
break;
case 1:
buf[i] = rand();
break;
case 2:
buf[i] = (UCHAR)seed + i;
break;
case 3:
dwBuf[i] = seed + i;
break;
}


}


其他的没了,我个人经历总结,难免不对,只是给大家个参考。祝大家顺利

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
CyAPI原版资料, CyAPI.lib provides a simple, powerful C++ programming interface to USB devices. More specifically, it is a C++ class library that provides a high-level programming interface to the CyUsb3.sys device driver. The library is only able to communicate with USB devices that are served by (i.e. bound to) this driver. Rather than communicate with the driver via Windows API calls such as SetupDiXxxx and DeviceIoControl, applications can call simpler CyAPI methods such as Open, Close, and XferData to communicate with these USB devices. To use the library, you need to include the header file, CyAPI.h, in files that access the CCyUSBDevice class. In addition, the statically linked CyAPI.lib file must be linked to your project. Versions of the .lib files are available for use with Microsoft Visual Studio 2008. The library employs a Device and EndPoints use model. To use the library you must create an instance of the CCyUSBDevice class using the new keyword. A CCyUSBDevice object knows how many USB devices are attached to the CyUsb3.sys driver and can be made to abstract any one of those devices at a time by using the Open method. An instance of CCyUSBDevice exposes several methods and data members that are device-specific, such as DeviceName, DevClass, VendorID, ProductID, and SetAltIntfc. When a CCyUSBDevice object is open to an attached USB device, its endpoint members provide an interface for performing data transfers to and from the device's endpoints. Endpoint-specific data members and methods such as MaxPktSize, TimeOut, bIn, Reset and XferData are only accessible through endpoint members of a CCyUSBDevice object. In addition to its simplicity, the class library facilitates creation of sophisticated applications as well. The CCyUSBDevice constructor automatically registers the application for Windows USB Plug and Play event notification. This allows your application to support "hot plugging" of devices. Also, the asynchronous BeginDataXfer/WaitForXfer/FinishDataXfer methods allow queuing of multiple data transfer requests on a single endpoint, thus enabling high performance data streaming from the application level
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值