大家好,
我在使用fx3的串口接收时发现了一个奇怪的现象。我的系统架构是这样:fx3的串口收发两根线和mcu相连。pc通过control out把内容发送到fx3,fx3从 control endpoint 0 取出后通过串口发送给mcu。mcu在接收到串口数据后,等待1ms左右,开始通过串口回发数据给fx3。pc通过control in 把fx3的串口接受到的数据获取上来。
PC端control out和control in 请求是连续执行,中间未有延时等
Status = USBControlOUT(&(((SA_Typedef*)(*pSA))->USBDev), TGT_DEVICE, REQ_CLASS, 0x01, 0, 0, (unsigned char*)QuerO_Buf, &Datalen);
Status = USBControlIN(&(((SA_Typedef*)(*pSA))->USBDev), TGT_DEVICE, REQ_CLASS, 0x81, Datalen, 0, QuerI_Buf, &Datalen);
fx3对应代码
/* UART Specific Requests */
if (bRequest == CY_FX_USB_UART_GET_CUR_REQ)
{
/* Host requests data. Send it over EP0. */
/*clear rx buffer*/
CyU3PUartSetTimeout (1, 10000);
CyU3PUartReceiveBytes(glUartRxBuffer, 4, &status); //clear buffer of uart receiver
CyU3PUartSetTimeout (0xfffff, 0xfffff);
actualCount = CyU3PUartReceiveBytes(glUartRxBuffer, wValue, &status);
if(status== CY_U3P_SUCCESS) //if all wanted data received sucessfully,sent it over endpiont 0
status = CyU3PUsbSendEP0Data(wValue, glUartRxBuffer);
else //if an error happened during receive process,send fx3 uart receive error code
status = CyU3PUsbSendEP0Data(1, gluartRXerr);
isHandled = CyTrue;
}
else if (bRequest == CY_FX_USB_UART_SET_CUR_REQ)
{
/* Get the UART data from EP0 */
status = CyU3PUsbGetEP0Data (wLength, glUartTxBuffer, &readCount);
if (status == CY_U3P_SUCCESS)
{
/* Send the byte */
CyU3PUartTransmitBytes (glUartTxBuffer, readCount, &status);
/* Check status and actual count transferred */
if (status != CY_U3P_SUCCESS)
{
/* Error handling */
//CyFxAppErrorHandler(apiRetStatus);
}
}
isHandled = CyTrue;
}
使用sdk1.3.3时,功能完全正常,pc可以通过 control in 获取到mcu发过来的数据。但使用sdk1.3.4时,fx3串口无法接收数据。
actualCount = CyU3PUartReceiveBytes(glUartRxBuffer, wValue, &status); 代码status提示超时,actualCount 为4,即只获取上来4个字节。通过示波器查看fx3串口接收引脚,不管是1.3.3编译出的代码,还是1.3.4编译出的代码,均能看到正常的数据波形,也即mcu都成功接受到了fx3发过来的数据,并且启动了回发到fx3的过程。
产生上述现象的原因是什么?希望有使用经验的各位能提供解答。谢谢!
已解决! 转到解答。
我理解的你现在的问题就是CyU3PUartReceiveBytes接收不到完整的数据,那你可以把你的这段清空数据的代码去掉尝试一下。
CyU3PUartReceiveBytes这个函数的使用方法可以参考SDK中的UartLpRegMode.
Hi,
您可以查看release note, 其中提到了SDK1.3.4 Updated CyU3PUartTransmitBytes() and CyU3PUartReceiveBytes() implementation to speed up data transfers.
您这里为什么要用CyU3PUartReceiveBytes() 作为一个clear buffer of uart receiver的功能?
加清空操作,是因为在实际使用中,发现通过CyU3PUartReceiveBytes()获取到的数据,有时前四个字节是额外多出来的不明来源数据,从第五个字节开始才是mcu传过来的数据。这个长度(4个字节)正好和ingress fifo长度一致。由于fx3的api并没有提供方便地清空的函数,故采用了这种读取四个字节来清空接收缓存的办法。
我也注意到了release note里关于CyU3PUartReceiveBytes()的描述,并且实际查看了1.3.3 和1.3.4源代码(EZ-usb SDK软件安装好后可以在安装目录里找到)。但其实CyU3PUartReceiveBytes()函数在1.3.3和1.3.4里的实现没有本质区别。(左侧为1.3.4版本,右侧为1.3.3版本)