您好!將CH559操作CH340代碼移植到CH32F103C8中,出現(xiàn)以下問題
1、CH32接收數(shù)據(jù)成功一次與失敗一次交替出現(xiàn)
2、CH32接收數(shù)據(jù)和發(fā)送數(shù)據(jù)不能并存,CH只能發(fā)送一次數(shù)據(jù),接收數(shù)據(jù)成功與失敗交替出現(xiàn)。如果加代碼InitRootDevice( Com_Buffer );在發(fā)送數(shù)據(jù)前面?每次循環(huán)能夠發(fā)出數(shù)據(jù) ,但是不能接收數(shù)據(jù)。
3、使用CH559移植的接收和發(fā)送函數(shù)?TXDUsbCH341BulkOut((PUINT8)tmpBuf, 6);和RXDUsbCH341BulkIn();
不想換芯片,希望能夠得到幫助!謝謝!
主循環(huán)代碼:
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 );//用于多次發(fā)送數(shù)據(jù)? 沒有這句只能發(fā)送一次
? ? ? ? TXDUsbCH341BulkOut((PUINT8)tmpBuf, 6);
? ?RXDUsbCH341BulkIn();? // 341批量接收
? ? }
函數(shù)TXDUsbCH341BulkOut((PUINT8)tmpBuf, 6)代碼:
UINT8TXDUsbCH341BulkOut( PUINT8 pBuf,UINT8 len )? // 341批量發(fā)送
{
? 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數(shù)據(jù)
if ( s == ERR_SUCCESS ){?
len -= R8_UH_TX_LEN;//R8_UEP2_T_LEN;
endp ^= 0x80;? // 同步標(biāo)志翻轉(zhuǎn)
ThisUsbDev.GpVar[2] = endp;? // 保存同步標(biāo)志位
//printf("ERR_SUCCESS\r\n");
}
? ? else return( s );
}
? return ERR_SUCCESS;
}
函數(shù)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;? // 同步標(biāo)志翻轉(zhuǎn)
ThisUsbDev.GpVar[1] = endp;? // 保存同步標(biāo)志位
len = R8_USB_RX_LEN;? // 接收到的數(shù)據(jù)長(zhǎng)度
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;
}