使用USBHID模式與電腦、手機終端通訊,速率和準確率比虛擬串口和CH340轉(zhuǎn)接速度要快。
在USB_OTG_FS中,選擇Device_Only,其它默認。
在Middleware中的Class For FS IP中,如下選擇
在Device Descriptor中可以按需要填寫
設置USB頻率為48Mhz
在Project Manager里,修改Heap和Stack的大小
在程序里main.c中,加入頭文件和引用
#include "usbd_custom_hid_if.h"
extern USBD_HandleTypeDef hUsbDeviceFS;
在usbd_custom_hid_if.c中修改
/** 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,0x8c, /* USAGE_PAGE (ST Page) */
0x09,0x01, /* USAGE (Demo Kit) */
0xa1,0x01, /* COLLECTION (Application) */
// The Input report
0x09,0x03, // USAGE ID - Vendor defined
0x15,0x00, // LOGICAL_MINIMUM (0)
0x26,0x00, 0xFF, // LOGICAL_MAXIMUM (255)
0x75,0x08, // REPORT_SIZE (8bit)
0x95,0x40, // REPORT_COUNT (64Byte)
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 (8bit)
0x95,0x40, // REPORT_COUNT (64Byte)
0x91,0x02, // OUTPUT (Data,Var,Abs)
/* USER CODE END 0 */
0xC0 /* END_COLLECTION */
};
?USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, SendBuff, 64);? ? //發(fā)送函數(shù),必須64字節(jié)
static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)? //在usbd_custom_hid_if.c中的接收函數(shù)
static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
{
/* USER CODE BEGIN 6 */
UNUSED(event_idx);
UNUSED(state);
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]; //把接收到的數(shù)據(jù)送到自定義的緩存區(qū)保存(Report_buf[i]為USB的接收緩存區(qū))
}
/* Start next USB packet transfer once data processing is completed */
USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceFS);
return (USBD_OK);
/* USER CODE END 6 */
}
使用USB調(diào)試助手
?先查找USB,在下拉框中找到STM設備,便可進行通訊了。
以下是示例代碼文章來源:http://www.zghlxwxcb.cn/news/detail-503427.html
STM32F407代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-503427.html
到了這里,關于STM32設置USB HID模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!