新人,虽然没分,但请各位高手指点一下迷津。搞了两个礼拜了,仍然没有头绪,总觉得是时间匹配不好,反正FIFO没有用好。也不太了解这个USB的fifo用法。PC端取数据的时候是在fifo满的时候还是半满的时候,还是非空的时候?
Usb:CY7C68013A,使用断点6向PC传数据,512*2字节FIFO
Dsp:TI DM6437
PC:VC++,CPAPI
要做的功能其实就是DSP发送数据给PC,但是发现PC端接收的数据有时会错乱,串位。特别是前1K的数据很容易就是上一次发送的数据。而本次发送数据时,接收端会丢失一些。
DSP发送的程序如下:
void Send_Test_Data_With_USB()
{
Uint32 i = 0,j = 1,k = 1,m = 1,len;
INT8U temp;
INT8U testdata = 0;
INT16U CheckSum = 0;
INT16U framenum = 1;
SelectUSB();//EMIF总线
for(len = 0;len < 576000;len++)
{
*(Uint8 *)USB_FIFO6_ADDR = testdata; //在EMIF总线上写数据,给USB。
testdata++;
if((len+1)%512 == 0)
_waitusec(100);//没有这个延时,会发现PC端接收数据时,在接收512字节整数倍容易丢几个字节的数据
while(USB_FLAGB==0) ;//等待FIFO缓冲区为非满后退出循环
}
DeselectUSB();
}
PC端接收程序:
bool CImageWithUSBDlg::ReadUsbData(PUCHAR pDataBuf, long &lenBytes)
{
OVERLAPPED inOvLap;
bool bResult=FALSE;
inOvLap.hEvent = CreateEvent(NULL, false, false, "CYUSB_IN");
m_InEndpt->TimeOut =100;
UCHAR *inContext = m_InEndpt->BeginDataXfer(pDataBuf,m_512,&inOvLap);
m_InEndpt->WaitForXfer(&inOvLap,100);
bResult=m_InEndpt->FinishDataXfer(pDataBuf,lenBytes, &inOvLap,inContext);
m_InEndpt->Abort();
CloseHandle(inOvLap.hEvent);
return bResult;
}
UINT ReadThread(LPVOID param)
{
CImageWithUSBDlg *pDlg=(CImageWithUSBDlg *)param;
CString strRecvData;
unsigned int iLoop=0;
unsigned int iTotalCount=0;
unsigned int iTotalBytesCount=0;
unsigned char szDataBuffer[1125][512];
memset(szDataBuffer,0,1125*512);
unsigned char RecvData[512];
unsigned char CmpData[512];
int iResult=0;
int iShakeHead=0;
unsigned char *pRecv=NULL;
unsigned char *pDestBuff=NULL;
memset(CmpData,0xFF,512);
for(iLoop=0;iLoop<512;iLoop++)
{
RecvData[iLoop]=2;
}
FILE *pFileRecvData=NULL;
pFileRecvData=fopen("ImageData.dat","wb");
if(NULL==pFileRecvData)
{
AfxMessageBox("Open File Error!");
return 111;
}
//开始接收有效数据
pDlg->ReadUsbData(RecvData,pDlg->m_LenBytes);//
pDlg->ReadUsbData(RecvData,pDlg->m_LenBytes);//清空FIFO?
while(iTotalCount<1125)
{
iResult=pDlg->ReadUsbData(RecvData,pDlg->m_LenBytes);
if((iResult)&&(pDlg->m_LenBytes)>0)
{
memcpy(szDataBuffer[iTotalCount],RecvData,512);
iTotalCount++;
iTotalBytesCount+=(pDlg->m_LenBytes);
}
}
pDlg->ReadUsbData(RecvData,pDlg->m_LenBytes);//
pDlg->ReadUsbData(RecvData,pDlg->m_LenBytes);//清空FIFO?
//在此写文件
for(iLoop=0;iLoop<1125;iLoop++)
{
fwrite(szDataBuffer[iLoop],1,512,pFileRecvData);
}
iTotalCount;
iTotalBytesCount;
fclose(pFileRecvData);
pFileRecvData = NULL;
pDlg->SetDlgItemInt(IDC_EDIT_RECV_BTYE,iTotalBytesCount);
CWnd *pTempWnd=(CWnd *)(pDlg->GetDlgItem(IDC_READ_USB_DATA));
pTempWnd->EnableWindow(TRUE);
pTempWnd=(CWnd *)(pDlg->GetDlgItem(IDC_SAVE_INVALID_IMAGE));
pTempWnd->EnableWindow(TRUE);
//解决View重新刷新问题;
pDlg->m_pImageView->m_bImageDraw=TRUE;
pDlg->m_pImageView->m_load=false;
pDlg->m_pImageView->Invalidate();
(CWnd *)pDlg->GetDlgItem(IDC_LINE_NUM)->EnableWindow(TRUE);
(CWnd *)pDlg->GetDlgItem(IDC_DRAW_IMAGE_LINE)->EnableWindow(TRUE);
return 0;
} |