固件的修改: usbd_hid_core.c文件与cdc一致,定义了一个HID的callback结构体以及描述符的配置,但是官方HID中并未有DataOut部分,即没有接收数据的部分。并且描述符中只定义了一个IN的端点。 USBD_Class_cb_TypeDef USBD_HID_cb =
{
USBD_HID_Init,
USBD_HID_DeInit,
USBD_HID_Setup,
NULL, /*EP0_TxSent*/
NULL, /*EP0_RxReady*/
USBD_HID_DataIn, /*DataIn*/
USBD_HID_DataOut,//NULL, /*DataOut*/
NULL, /*SOF */
NULL,
NULL,
USBD_HID_GetCfgDesc,
#ifdef USB_OTG_HS_CORE
USBD_HID_GetCfgDesc, /* use same config as per FS */
#endif
};
因此,需要修改的为:1、增加一个OUT端点,修改相应的描述符。2、增加DataOut函数,处理接收来自PC的数据。
首先需要在__ALIGN_BEGIN static uint8_tUSBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ]最后增加OUT端点,与自带IN端点类似。 0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */ /* Endpoint descriptor type */
HID_OUT_EP, /* bEndpointAddress: */ /* Endpoint Address (OUT) */
0x03, /* bmAttributes: Interrupt endpoint */
HID_OUT_PACKET,//0x02, /* wMaxPacketSize: 20 Bytes max */
0x00,
0x0A, /* bInterval: Polling Interval (16 ms) */ 增加之后,需要相应修改数组的大小USB_HID_CONFIG_DESC_SIZ的值为41以及描述符内端点的个数为 0x02, /*bNumEndpoints*/。
_ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END =
{
0x05, 0x01, /* USAGE_PAGE (ST Page) */
0x09, 0x02, /* USAGE (Demo Kit) */
0xa1, 0x01, /* COLLECTION (Application) */
// The Input report
0x09,0x01, // USAGE ID - Vendor defined
0x15,0x00, // LOGICAL_MINIMUM (0)
0x26,0x00, 0xFF, // LOGICAL_MAXIMUM (255)
0x75,0x08, // REPORT_SIZE (8)
0x95,HID_IN_PACKET,
0x81,0x02, // INPUT (Data,Var,Abs)
// The Output report
0x09,0x04, // USAGE ID - Vendor defined
0x15,0x00, // LOGICAL_MINIMUM (0)
0x26,0x00,0xFF, // LOGICAL_MAXIMUM (255)
0x75,0x08, // REPORT_SIZE (8)
0x95,HID_OUT_PACKET, // REPORT_COUNT (64)
0x91,0x02, // OUTPUT (Data,Var,Abs)
0xc0 /* END_COLLECTION */
} 然后是对HID_Init函数增加对OUT端点的初始化 /* Open EP OUT */
DCD_EP_Open(pdev,
HID_OUT_EP,
HID_OUT_PACKET,
USB_OTG_EP_INT);
/* Prepare Out endpoint to receive next packet */
DCD_EP_PrepareRx(pdev,
HID_OUT_EP,
(uint8_t*)(USB_Rx_Buffer),
HID_OUT_PACKET); 最后是增加DataOUt函数 static uint8_t USBD_HID_DataOut (void *pdev, uint8_t epnum)
{
uint16_t USB_Rx_Cnt,i;
/* Get the received data buffer and update the counter */
USB_Rx_Cnt = ((USB_OTG_CORE_HANDLE*)pdev)->dev.out_ep[epnum].xfer_count;
/* USB data will be immediately processed, this allow next USB traffic being
NAKed till the end of the application Xfer */
/* Prepare Out endpoint to receive next packet */
DCD_EP_PrepareRx(pdev,
HID_OUT_EP,
(uint8_t*)(USB_Rx_Buffer),
HID_OUT_PACKET);
// DCD_EP_Tx ( pdev,HID_IN_EP, USB_Rx_Buffe ,USB_Rx_Cnt);//将数据返回
return USBD_OK;
}
然后可以根据需要修改包的大小,FS最大为64,上位机发送数据或接收数据数组定义大小需加1,如下位机为64,上位机为65,增加的1字节为报告ID) #define HID_IN_PACKET 64//4 #define HID_OUT_PACKET 64//4
|