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;
}
}
其他的没了,我个人经历总结,难免不对,只是给大家个参考。祝大家顺利