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
}