CH32F205 USBFS配置为Device 但是不管如何转发描述符到USB端点 都是让鼠标往右移动

CH32F205 两个usb 一个配置为主机 一个配置为HID设备 主机接收HID设备数据 然后通过从机USB端口 转发给PC 

这是主函数设置

int main( void )

{

    /* Initialize system configuration */

    Delay_Init( );

    USART_Printf_Init( 115200 );

    DUG_PRINTF( "SystemClk:%d\r\n", SystemCoreClock );

    DUG_PRINTF( "USB HOST KM Test\r\n" );


    /* Initialize TIM3 */

    TIM3_Init( 9, SystemCoreClock / 10000 - 1 );

    DUG_PRINTF( "TIM3 Init OK!\r\n" );


    /* Initialize USBHS host */

    /* Note: Only CH32F205/CH32F207 support USB high-speed port. */

#if DEF_USBHS_PORT_EN

    DUG_PRINTF( "USBHS Host Init\r\n" );

    USBHS_RCC_Init( );

    USBHS_Host_Init( ENABLE );

    memset( &RootHubDev[ DEF_USBHS_PORT_INDEX ].bStatus, 0, sizeof( ROOT_HUB_DEVICE ) );

    memset( &HostCtl[ DEF_USBHS_PORT_INDEX * DEF_ONE_USB_SUP_DEV_TOTAL ].InterfaceNum, 0, DEF_ONE_USB_SUP_DEV_TOTAL * sizeof( HOST_CTL ) );

#endif


    /* Initialize USBFS host */

#if DEF_USBFS_PORT_EN

    DUG_PRINTF( "USBFS Host Init\r\n" );

//    USBFS_RCC_Init( );

//    USBFS_Host_Init( ENABLE );


    USBFS_RCC_Init( );

     USBFS_Device_Init( ENABLE );

     DUG_PRINTF(" < - USB DEVCIE INIT OK - > \r\n");

    memset( &RootHubDev[ DEF_USBFS_PORT_INDEX ].bStatus, 0, sizeof( ROOT_HUB_DEVICE ) );

    memset( &HostCtl[ DEF_USBFS_PORT_INDEX * DEF_ONE_USB_SUP_DEV_TOTAL ].InterfaceNum, 0, DEF_ONE_USB_SUP_DEV_TOTAL * sizeof( HOST_CTL ) );

#endif


    

    while( 1 )

    {

        USBH_MainDeal( );


    }

}



这是USBH_MainDeal 函数里获取到描述符的部分 不管我接入键盘还是鼠标 都是鼠标一直往右移动

/* Get the data of the HID device connected to the USB host port */

    for( usb_port = 0; usb_port < DEF_TOTAL_ROOT_HUB; usb_port++ )

    {

        if( RootHubDev[ usb_port ].bStatus >= ROOT_DEV_SUCCESS )

        {

            index = RootHubDev[ usb_port ].DeviceIndex; 

            if( RootHubDev[ usb_port ].bType == USB_DEV_CLASS_HID )

            {

                for( intf_num = 0; intf_num < HostCtl[ index ].InterfaceNum; intf_num++ )

                {

                    for( in_num = 0; in_num < HostCtl[ index ].Interface[ intf_num ].InEndpNum; in_num++ )

                    {                   

                        /* Get endpoint data based on the interval time of the device */

                        if( HostCtl[ index ].Interface[ intf_num ].InEndpTimeCount[ in_num ] >= HostCtl[ index ].Interface[ intf_num ].InEndpInterval[ in_num ] )

                        {

                            HostCtl[ index ].Interface[ intf_num ].InEndpTimeCount[ in_num ] %= HostCtl[ index ].Interface[ intf_num ].InEndpInterval[ in_num ];

               

                            /* Get endpoint data */

                            s = USBH_GetHidData( usb_port, index, intf_num, in_num, Com_Buf, &len );

                            if( s == ERR_SUCCESS )

                            {

#if DEF_DEBUG_PRINTF

                                for( i = 0; i < len; i++ )

                                {

                                    DUG_PRINTF( "%02x ", Com_Buf[ i ] );

                                }

                                DUG_PRINTF( "\r\n" );

#endif

                                

int8_t x_move = (int8_t)Com_Buf[1]; // X 位移

int8_t y_move = (int8_t)Com_Buf[2]; // Y 位移

int8_t wheel_move = (int8_t)Com_Buf[3]; // 滚轮位移


DUG_PRINTF("Mouse Move: X=%d, Y=%d, Wheel=%d\r\n", x_move, y_move, wheel_move);


// 上传数据到端点

DUG_PRINTF("Uploading Data: %02x %02x %02x %02x\r\n", Com_Buf[0], Com_Buf[1], Com_Buf[2], Com_Buf[3]);

uint8_t report_len = 4; // HID 描述符定义了 4 字节

if (USBFS_Endp_DataUp(endp, Com_Buf, report_len, DEF_UEP_DMA_LOAD) == READY) {

DUG_PRINTF("Mouse Data Uploaded to Endpoint %d\r\n", endp);

} else {

DUG_PRINTF("Mouse Data Upload Failed!\r\n");

然后串口的打印数据一直是往一个方向累加 



SystemClk:96000000

USB HOST KM Test

TIM3 Init OK!

USBHS Host Init

USBFS Host Init

 < - USB DEVCIE INIT OK - > 

USB Port1 Dev In.

Enum:

Get DevDesc: 12 01 10 01 00 00 00 08 4f 1c 34 00 10 01 01 02 00 01 

Set DevAddr: OK

Get CfgDesc: 09 02 22 00 01 01 00 a0 32 09 04 00 00 01 03 01 02 00 09 21 10 01 00 01 22 34 00 07 05 81 03 04 00 0a 

DevType: 03

Set Cfg: OK

Root Device Is HID. Enum Hid:

Analyze CfgDesc: OK

Get Interface0 RepDesc: HID Descriptor:

05 01 09 02 a1 01 09 01 a1 00 05 09 19 01 29 03 15 00 25 01 95 03 75 01 81 02 95 01 75 05 81 01 05 01 09 30 09 31 09 38 15 81 25 7f 75 08 95 03 81 06 c0 c0 

05 01 09 02 a1 01 09 01 a1 00 05 09 19 01 29 03 15 00 25 01 95 03 75 01 81 02 95 01 75 05 81 01 05 01 09 30 09 31 09 38 15 81 25 7f 75 08 95 03 81 06 c0 c0 

Get StringDesc1: 14 03 53 00 49 00 47 00 4d 00 41 00 43 00 48 00 49 00 50 00 

Get StringDesc2: 14 03 55 00 73 00 62 00 20 00 4d 00 6f 00 75 00 73 00 65 00 

Further Enum Result: OK

00 00 00 00 

Mouse Move: X=0, Y=0, Wheel=0

Uploading Data: 00 00 00 00

Mouse Data Uploaded to Endpoint 2

00 01 00 00 


Mouse Data Uploaded to Endpoint 2

00 01 ff 00 

Mouse Move: X=1, Y=-1, Wheel=0

Uploading Data: 00 01 ff 00

Mouse Data Uploaded to Endpoint 2

00 01 ff 00 

Mouse Move: X=1, Y=-1, Wheel=0

Uploading Data: 00 01 ff 00

Mouse Data Uploaded to Endpoint 2

00 02 00 00 

Mouse Move: X=2, Y=0, Wheel=0

Uploading Data: 00 02 00 00

Mouse Data Uploaded to Endpoint 2

00 03 ff 00 

Mouse Move: X=3, Y=-1, Wheel=0

Uploading Data: 00 03 ff 00

Mouse Data Uploaded to Endpoint 2

00 05 00 00 

Mouse Move: X=5, Y=0, Wheel=0

Uploading Data: 00 05 00 00

Mouse Data Uploaded to Endpoint 2

00 06 00 00 

Mouse Move: X=6, Y=0, Wheel=0

Uploading Data: 00 06 00 00

Mouse Data Uploaded to Endpoint 2

00 04 00 00 

Mouse Move: X=4, Y=0, Wheel=0

Uploading Data: 00 04 00 00

Mouse Data Uploaded to Endpoint 2

00 02 00 00 

Mouse Move: X=2, Y=0, Wheel=0

Uploading Data: 00 02 00 00

Mouse Data Uploaded to Endpoint 2

00 05 fe 00 

Mouse Move: X=5, Y=-2, Wheel=0

Uploading Data: 00 05 fe 00

Mouse Data Uploaded to Endpoint 2

00 09 fc 00 

Mouse Move: X=9, Y=-4, Wheel=0

Uploading Data: 00 09 fc 00

Mouse Data Uploaded to Endpoint 2

00 06 fe 00 

Mouse Move: X=6, Y=-2, Wheel=0

Uploading Data: 00 06 fe 00

Mouse Data Uploaded to Endpoint 2

00 06 ff 00 

Mouse Move: X=6, Y=-1, Wheel=0

Uploading Data: 00 06 ff 00

Mouse Data Uploaded to Endpoint 2

00 05 ff 00 

Mouse Move: X=5, Y=-1, Wheel=0

Uploading Data: 00 05 ff 00

Mouse Data Uploaded to Endpoint 2

00 06 fe 00 

Mouse Move: X=6, Y=-2, Wheel=0

Uploading Data: 00 06 fe 00

Mouse Data Uploaded to Endpoint 2

00 09 ff 00 

Mouse Move: X=9, Y=-1, Wheel=0

Uploading Data: 00 09 ff 00

Mouse Data Uploaded to Endpoint 2

00 09 fe 00 

Mouse Move: X=9, Y=-2, Wheel=0

Uploading Data: 00 09 fe 00

Mouse Data Uploaded to Endpoint 2

00 0a 00 00 

Mouse Move: X=10, Y=0, Wheel=0

Uploading Data: 00 0a 00 00

Mouse Data Uploaded to Endpoint 2

00 09 fe 00 

Mouse Move: X=9, Y=-2, Wheel=0

Uploading Data: 00 09 fe 00

Mouse Data Uploaded to Endpoint 2

00 09 fd 00 

Mouse Move: X=9, Y=-3, Wheel=0

Uploading Data: 00 09 fd 00

Mouse Data Uploaded to Endpoint 2

00 09 fe 00 

Mouse Move: X=9, Y=-2, Wheel=0

Uploading Data: 00 09 fe 00

Mouse Data Uploaded to Endpoint 2

00 0a fd 00 

Mouse Move: X=10, Y=-3, Wheel=0

Uploading Data: 00 0a fd 00

Mouse Data Uploaded to Endpoint 2

00 0a fd 00 

Mouse Move: X=10, Y=-3, Wheel=0

Uploading Data: 00 0a fd 00

Mouse Data Uploaded to Endpoint 2

00 0a fd 00 

Mouse Move: X=10, Y=-3, Wheel=0

Uploading Data: 00 0a fd 00

Mouse Data Uploaded to Endpoint 2

00 0a ff 00 

Mouse Move: X=10, Y=-1, Wheel=0

Uploading Data: 00 0a ff 00

Mouse Data Uploaded to Endpoint 2

00 0a 00 00 

Mouse Move: X=10, Y=0, Wheel=0

Uploading Data: 00 0a 00 00

Mouse Data Uploaded to Endpoint 2

00 08 ff 00 

Mouse Move: X=8, Y=-1, Wheel=0

Uploading Data: 00 08 ff 00

Mouse Data Uploaded to Endpoint 2

00 06 00 00 

Mouse Move: X=6, Y=0, Wheel=0

Uploading Data: 00 06 00 00

Mouse Data Uploaded to Endpoint 2

00 05 ff 00 

Mouse Move: X=5, Y=-1, Wheel=0

Uploading Data: 00 05 ff 00

Mouse Data Uploaded to Endpoint 2

00 03 ff 00 

Mouse Move: X=3, Y=-1, Wheel=0

Uploading Data: 00 03 ff 00

Mouse Data Uploaded to Endpoint 2

00 ff 00 00 

Mouse Move: X=-1, Y=0, Wheel=0

Uploading Data: 00 ff 00 00

Mouse Data Uploaded to Endpoint 2

00 fd 01 00 

Mouse Move: X=-3, Y=1, Wheel=0

Uploading Data: 00 fd 01 00

Mouse Data Uploaded to Endpoint 2

00 f6 01 00 

Mouse Move: X=-10, Y=1, Wheel=0

Uploading Data: 00 f6 01 00

Mouse Data Uploaded to Endpoint 2

00 ee 01 00 

Mouse Move: X=-18, Y=1, Wheel=0

Uploading Data: 00 ee 01 00

Mouse Data Uploaded to Endpoint 2

00 e9 00 00 

Mouse Move: X=-23, Y=0, Wheel=0

Uploading Data: 00 e9 00 00

Mouse Data Uploaded to Endpoint 2

00 e7 00 00 

Mouse Move: X=-25, Y=0, Wheel=0

Uploading Data: 00 e7 00 00

Mouse Data Uploaded to Endpoint 2

00 e9 ff 00 

Mouse Move: X=-23, Y=-1, Wheel=0

Uploading Data: 00 e9 ff 00

Mouse Data Uploaded to Endpoint 2

00 eb fd 00 

Mouse Move: X=-21, Y=-3, Wheel=0

Uploading Data: 00 eb fd 00

Mouse Data Uploaded to Endpoint 2

00 ed fc 00 

Mouse Move: X=-19, Y=-4, Wheel=0

Uploading Data: 00 ed fc 00

Mouse Data Uploaded to Endpoint 2

00 ef fc 00 

Mouse Move: X=-17, Y=-4, Wheel=0

Uploading Data: 00 ef fc 00

Mouse Data Uploaded to Endpoint 2

00 ee fb 00 

Mouse Move: X=-18, Y=-5, Wheel=0

Uploading Data: 00 ee fb 00

Mouse Data Uploaded to Endpoint 2

00 ed fb 00 

Mouse Move: X=-19, Y=-5, Wheel=0

Uploading Data: 00 ed fb 00

Mouse Data Uploaded to Endpoint 2

00 eb fb 00 

Mouse Move: X=-21, Y=-5, Wheel=0

Uploading Data: 00 eb fb 00

Mouse Data Uploaded to Endpoint 2

00 e9 fc 00 

Mouse Move: X=-23, Y=-4, Wheel=0

Uploading Data: 00 e9 fc 00

Mouse Data Uploaded to Endpoint 2

00 eb fb 00 

Mouse Move: X=-21, Y=-5, Wheel=0

Uploading Data: 00 eb fb 00

Mouse Data Uploaded to Endpoint 2

00 eb fd 00 

Mouse Move: X=-21, Y=-3, Wheel=0

Uploading Data: 00 eb fd 00

Mouse Data Uploaded to Endpoint 2

00 eb fe 00 

Mouse Move: X=-21, Y=-2, Wheel=0

Uploading Data: 00 eb fe 00

Mouse Data Uploaded to Endpoint 2

00 ef fd 00 

Mouse Move: X=-17, Y=-3, Wheel=0

Uploading Data: 00 ef fd 00

Mouse Data Uploaded to Endpoint 2

00 f2 fe 00 

Mouse Move: X=-14, Y=-2, Wheel=0

Uploading Data: 00 f2 fe 00

Mouse Data Uploaded to Endpoint 2

00 f4 ff 00 



}


您好,按照你的描述,你是不是想实现一个类似HUB的功能,或者说仅是想通过主机口获取连接鼠标的数据,然后通过设备口连接电脑实现鼠标的功能。若仅是想要实现鼠标的功能,建议设备端改成模拟鼠标设备,可以使用bushound抓一下你鼠标的枚举过程,然后根据其修改一下HID设备的描述符,模拟成具体的鼠标设备,然后再将主机抓取的设备通过设备端口上传电脑实现鼠标功能。后续若有问题,可以邮箱(lzs@wch.cn)和我沟通。


您好 (通过主机口获取连接键鼠的数据,然后通过设备口连接电脑实现键鼠的功能)这个问题 我已经解决了
只连接一个鼠标 或者 换成 一个键盘 可以被主机识别并且转发数据到PC 
但是我用一个HUB连接主机 只能识别到HUB设备 无法正确枚举连接在HUB上的鼠标或者键盘

/* Get HUB port HID device data */

                for( hub_port = 0; hub_port < RootHubDev[ usb_port ].bPortNum; hub_port++ )

                {

                    if( RootHubDev[ usb_port ].Device[ hub_port ].bStatus == ROOT_DEV_SUCCESS )

                    {

                        index = RootHubDev[ usb_port ].Device[ hub_port ].DeviceIndex;


                        if( RootHubDev[ usb_port ].Device[ hub_port ].bType == USB_DEV_CLASS_HID )

                        {                  

                            for( intf_num = 0; intf_num < HostCtl[ index ].InterfaceNum; intf_num++ )

                            {

                                for( in_num = 0; in_num < HostCtl[ index ].Interface[ intf_num ].InEndpNum; in_num++ )

                                {         

                                    /* Get endpoint data based on the interval time of the device */

                                    if( HostCtl[ index ].Interface[ intf_num ].InEndpTimeCount[ in_num ] >= HostCtl[ index ].Interface[ intf_num ].InEndpInterval[ in_num ] )

                                    {

                                        HostCtl[ index ].Interface[ intf_num ].InEndpTimeCount[ in_num ] %= HostCtl[ index ].Interface[ intf_num ].InEndpInterval[ in_num ];


                                        /* Select HUB device port */

                                        USBFSH_SetSelfAddr( RootHubDev[ usb_port ].Device[ hub_port ].bAddress );

                                        USBFSH_SetSelfSpeed( RootHubDev[ usb_port ].Device[ hub_port ].bSpeed );

                                        if( RootHubDev[ usb_port ].bSpeed != USB_LOW_SPEED )

                                        {

                                            USBFSH->HOST_CTRL &= ~USBFS_UH_LOW_SPEED;

                                        }

                                        

                                        /* Get endpoint data */

                                        s = USBH_GetHidData( usb_port, index, intf_num, in_num, Com_Buf, &len );                 

                                        if( s == ERR_SUCCESS )

                                        {     

#if DEF_DEBUG_PRINTF

                                            for( i = 0; i < len; i++ )

                                            {

                                                DUG_PRINTF( "%02x ", Com_Buf[ i ] );

                                            }

                                            DUG_PRINTF( "\r\n" );

#endif




if (HostCtl[index].Interface[intf_num].Type == DEC_MOUSE)

{

/* Forward mouse data */

int8_t btn = (int8_t)Com_Buf[0];    // Buttons

int8_t x_move = (int8_t)Com_Buf[1]; // X movement

int8_t y_move = (int8_t)Com_Buf[2]; // Y movement

int8_t wheel_move = (int8_t)Com_Buf[3]; // Wheel movement


DUG_PRINTF("Hub Mouse Data: btn=%d, X=%d, Y=%d, Wheel=%d\r\n", btn, x_move, y_move, wheel_move);


if (USBFS_Endp_DataUp(endp_mouse, Com_Buf, len, DEF_UEP_CPY_LOAD) == READY)

{

DUG_PRINTF("Hub Mouse Data Uploaded to Endpoint %d\r\n", endp_mouse);

}

else

{

DUG_PRINTF("Hub Mouse Data Upload Failed!\r\n");

}

}

else if (HostCtl[index].Interface[intf_num].Type == DEC_KEY)

{

/* Forward keyboard data */

DUG_PRINTF("Hub Keyboard Data: ");

for (i = 0; i < len; i++)

{

DUG_PRINTF("%02x ", Com_Buf[i]);

}

DUG_PRINTF("\r\n");


if (USBFS_Endp_DataUp(endp_keyboard, Com_Buf, len, DEF_UEP_CPY_LOAD) == READY)

{

DUG_PRINTF("Hub Keyboard Data Uploaded to Endpoint %d\r\n", endp_keyboard);

}

else

{

DUG_PRINTF("Hub Keyboard Data Upload Failed!\r\n");

}

}

                                            

                                            if( HostCtl[ index ].Interface[ intf_num ].Type == DEC_KEY )

                                            {                                    

                                                KB_AnalyzeKeyValue( index, intf_num, Com_Buf, len );


                                                if( HostCtl[ index ].Interface[ intf_num ].SetReport_Flag )

                                                {

                                                    KB_SetReport( usb_port, index, RootHubDev[ usb_port ].Device[ hub_port ].bEp0MaxPks, intf_num );

                                                }

                                            }

                                        }

                                        else if( s == ERR_USB_DISCON )

                                        {

                                            break;

                                        }

                                    }

                                }


                                if( s == ERR_USB_DISCON )

                                {

                                    break;

                                }

                            }

                        }

                    }

                }

            }   

#endif   

        }

    }

}


然后串口的打印也只显示 HUB-Device 

SystemClk:96000000

USB HOST KM Test

TIM3 Init OK!

USBHS Host Init

 < - USB DEVCIE INIT OK - > 

USB Port1 Dev Out.

USB Port1 Dev In.

Enum:

Get DevDesc: 12 01 10 02 09 00 01 40 e3 05 10 06 56 06 01 02 00 01 

Set DevAddr: OK

Get CfgDesc: 09 02 19 00 01 01 00 e0 32 09 04 00 00 01 09 00 00 00 07 05 81 03 01 00 0c 

DevType: 09

Set Cfg: OK

Root Device Is HUB. Enum Hub:

Analyze CfgDesc: OK

Get StringDesc1: 1a 03 47 00 65 00 6e 00 65 00 73 00 79 00 73 00 4c 00 6f 00 67 00 69 00 63 00 

Get StringDesc2: 16 03 55 00 53 00 42 00 32 00 2e 00 31 00 20 00 48 00 75 00 62 00 

Get Hub Desc: 09 29 04 e0 00 32 64 00 ff 

RootHubDev[ 01 ].bPortNum: 04

Port Power Status: 00

Port Power Status: 00

Port Power Status: 00

Port Power Status: 00

Further Enum Result: OK



只有登录才能回复,可以选择微信账号登录