您好!将CH559操作CH340代码移植到CH32F103C8中,出现以下问题
1、CH32接收数据成功一次与失败一次交替出现
2、CH32接收数据和发送数据不能并存,CH只能发送一次数据,接收数据成功与失败交替出现。如果加代码InitRootDevice( Com_Buffer );在发送数据前面 每次循环能够发出数据 ,但是不能接收数据。
3、使用CH559移植的接收和发送函数 TXDUsbCH341BulkOut((PUINT8)tmpBuf, 6);和RXDUsbCH341BulkIn();
不想换芯片,希望能够得到帮助!谢谢!
主循环代码:
s = ERR_SUCCESS;
if ( R8_USB_INT_FG & RB_UIF_DETECT )
{
R8_USB_INT_FG = RB_UIF_DETECT ;
s = AnalyzeRootHub( );
if ( s == ERR_USB_CONNECT )
{
printf( "New Device In\r\n" );
FoundNewDev = 1;
}
if( s == ERR_USB_DISCON )
{
printf( "Device Out\r\n" );
}
}
if ( FoundNewDev || s == ERR_USB_CONNECT )
{
FoundNewDev = 0;
Delay_Ms( 200 );
s = InitRootDevice( Com_Buffer );
if ( s == ERR_SUCCESS ) printf( "Device Enum Succeed\r\n" );
else printf( "Device Enum Failed\r\n" );
}
if(ThisUsbDev.DeviceStatus == ROOT_DEV_SUCCESS)
{
InitRootDevice( Com_Buffer );//用于多次发送数据 没有这句只能发送一次
TXDUsbCH341BulkOut((PUINT8)tmpBuf, 6);
RXDUsbCH341BulkIn(); // 341批量接收
}
函数TXDUsbCH341BulkOut((PUINT8)tmpBuf, 6)代码:
UINT8TXDUsbCH341BulkOut( PUINT8 pBuf,UINT8 len ) // 341批量发送
{
UINT8 TxCnt,s,endp;
endp = ThisUsbDev.GpVar[2];
while ( len ) {
//printf("len:%d\r\n",len);
Delay_Us( 200 );
R8_UH_TX_LEN = len >= 32 ? 32 : len;
//printf("TX Len:%d\r\n",R8_UH_TX_LEN);
for ( TxCnt = 0; TxCnt != R8_UH_TX_LEN; TxCnt ++ ) {
pHOST_TX_RAM_Addr[ TxCnt ] = *pBuf;
pBuf ++;
}
//printf("USB_PID_OUT:%x\r\n",USB_PID_OUT);
s = USBHostTransact( USB_PID_OUT << 4| (endp & 0x7F) , endp & 0x80 ? R8_UH_RX_CTRL | R8_UH_TX_CTRL : 0, 10 ); // OUT数据
if ( s == ERR_SUCCESS ){
len -= R8_UH_TX_LEN;//R8_UEP2_T_LEN;
endp ^= 0x80; // 同步标志翻转
ThisUsbDev.GpVar[2] = endp; // 保存同步标志位
//printf("ERR_SUCCESS\r\n");
}
else return( s );
}
return ERR_SUCCESS;
}
函数RXDUsbCH341BulkIn()代码:
UINT8RXDUsbCH341BulkIn( void ) // 341批量接收
{
UINT8 i,s,len,endp = ThisUsbDev.GpVar[1];
s = USBHostTransact( USB_PID_IN << 4 | (endp & 0x7F), endp & 0x80 ? R8_UH_RX_CTRL | R8_UH_TX_CTRL : 0, 10 );
if ( s == ERR_SUCCESS ) {
endp ^= 0x80; // 同步标志翻转
ThisUsbDev.GpVar[1] = endp; // 保存同步标志位
len = R8_USB_RX_LEN; // 接收到的数据长度
if ( len ) {
printf("CH341 In data: \n");
for ( i = 0; i < 32; i ++ ) printf("x%02X ",(UINT16)(pHOST_RX_RAM_Addr[i]) );
printf("\n");
}
return len;
}
else if ( s != ( USB_PID_NAK | ERR_USB_TRANSFER ) ) printf("CH341 error %02x\n",(UINT16)s); // 可能是断开了
return 0;
}