usart.c
中添加printf串口重定向代码usart.h
中引入头文件stdio.h
**#include "stdio.h" //引入此文件,为重定向printf
usart.c
中添加重定向代码// 重定向printf函数
int fputc(int ch,FILE *f)
{
uint8_t temp[1]={
ch};
HAL_UART_Transmit(&huart1,temp,1,2);
return 0;
}
64Byte
的报文描述符char ReportDescriptor[33] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x00, // USAGE (Undefined)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x00, // USAGE (Undefined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x95, 0x40, // REPORT_COUNT (64)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x09, 0x00, // USAGE (Undefined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x95, 0x40, // REPORT_COUNT (64)
0x75, 0x08, // REPORT_SIZE (8)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0xc0 // END_COLLECTION
};
Usb HID report descriptor.
数组**打开 usbd_custom_hid_if.c
文件,找到 CUSTOM_HID_ReportDesc_FS
并修改数组
/** Usb HID report descriptor. */
__ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
{
/* USER CODE BEGIN 0 */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x00, // USAGE (Undefined)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x00, // USAGE (Undefined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x95, 0x40, // REPORT_COUNT (64)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x09, 0x00, // USAGE (Undefined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x95, 0x40, // REPORT_COUNT (64)
0x75, 0x08, // REPORT_SIZE (8)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
/* USER CODE END 0 */
0xC0 /* END_COLLECTION */
};
#define USBD_VID 0x1995
#define USBD_LANGID_STRING 1033
#define USBD_MANUFACTURER_STRING "TANGXING"
#define USBD_PID_FS 0x0806
#define USBD_PRODUCT_STRING_FS "STM32_USBHID"
#define USBD_CONFIGURATION_STRING_FS "Custom HID Config"
#define USBD_INTERFACE_STRING_FS "Custom HID Interface"
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 64U //USBHID发送缓存 64Byte
#define USBD_CUSTOM_HID_REPORT_DESC_SIZE 33U //报文描述符长度
#define CUSTOM_HID_EPIN_SIZE 0x40U //USBHID接收长度 64Byte
#define CUSTOM_HID_EPOUT_SIZE 0x40U //USBHID发送长度 64Byte
#define CUSTOM_HID_HS_BINTERVAL 0x01U
#define CUSTOM_HID_FS_BINTERVAL 0x01U
usbd_custom_hid_if.c
文件打开 usbd_custom_hid_if.c
找到 USBD_CUSTOM_HID_SendReport_FS
函数,取消注释,删掉 static
属性,改成这样:
usbd_custom_hid_if.h
添加函数声明main.c
中添加测试代码在 usbd_custom_hid_if.c
中找到 CUSTOM_HID_OutEvent_FS
USBHID 接收处理函数
static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
{
/* USER CODE BEGIN 6 */
uint8_t i,USB_Recive_Buffer[64];
USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef *)(hUsbDeviceFS.pClassData);
for(i=0;i<64;i++)
{
USB_Recive_Buffer[i]=hhid->Report_buf[i]; //把接收到的数据送到自定义的缓存区保存(Report_buf[i]为USB的接收缓存区)
printf("USB_Recive_Buffer[%d] = 0x%02X \r\n",i,USB_Recive_Buffer[i]); //打印接收到的信息,确认是否正确,调试用
}
return (USBD_OK);
/* USER CODE END 6 */
}
PortHelper
测试效果源文件代码:https://download.csdn.net/download/u014779536/12193155