本文章主要讲了使用STM32的USB Device,实现控制板和电脑通信功能。从而实现,上位机对控制板进行调试。
USB Device可以有多种类型,实现双向通信的话,推荐使用Custom HID类型。
首先使用STM32CubeMx实现功能引脚配置并且生成对应的工程文件。
Middleware设置USB_DEVICE
配置时钟,USB设置为48MHz。
生成驱动代码和工程:
生成的代码还有点问题,修改对应的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 */
0x06,0xFF,0x00, // USAGE_PAGE (Vendor Page: 0xFF00) ±íʾһ¸ö±¨ÎıêÇ©Ö®ÀàµÄÓÃ;ÀàÒ³
0x09,0x01, // USAGE (Vendor Usage 1) ±íʾһ¸ö±¨¸æID±êÖ¾
0xA1,0x01, // COLLECTION (Application) ±íʾӦÓü¯ºÏ£¬ÒªÒÔÏÂÃæ×îºóµÄ0xc0½áÊøËü
0x15,0x00, // LOGICAL_MINIMUM (0) ±íʾÿ¸ö´«ÊäÊý¾ÝÏÞ¶¨Îª0
0x26,0xFF,0x00, // LOGICAL_MAXIMUM (255) ±íʾÿ¸ö´«ÊäÊý¾ÝµÄ×î´óÖµÏÞ¶¨Îª255
0x75,0x08, // REPORT_SIZE (8) ´«Êä×ֶεĿí¶ÈΪ8bit£¬±íʾÿ¸ö´«ÊäµÄÊý¾Ý·¶Î§Îª0~ffff ffff
0x95,0x40, // REPORT_COUNT (64)ÿ´Î·¢Ë͵ÄÊý¾Ý³¤¶È£¬ÕâÀïÊÇ64λ
0x09,0x02, // USAGE (Vendor Usage 2) ±íʾһ¸ö±¨¸æID±êÖ¾
0x81,0x02, // INPUT (Data,Var,Abs) ±íʾUSBÒªÊäÈëÊý¾Ýµ½PCµÄ¹¦ÄÜ
0x09,0x03, // USAGE (Vendor Usage 3) ±íʾһ¸ö±¨¸æID±êÖ¾
0x91,0x02, // OUTPUT (Data,Var,Abs) ±íʾUSBÉ豸Ҫ½ÓÊÕPCµÄÊý¾ÝµÄ¹¦ÄÜ
0x0A,0x00,0xFF, // UsageS(0xFF00)
0x0B1,0x02, // feature (data, variable, absolute)
/* USER CODE END 0 */
0xC0 // END_COLLECTION ½áÊø±êÖ¾
};
配置数据长度:
/*---------- -----------*/
#define USBD_MAX_NUM_INTERFACES 1U
/*---------- -----------*/
#define USBD_MAX_NUM_CONFIGURATION 1U
/*---------- -----------*/
#define USBD_MAX_STR_DESC_SIZ 512U
/*---------- -----------*/
#define USBD_DEBUG_LEVEL 0U
/*---------- -----------*/
#define USBD_LPM_ENABLED 1U
/*---------- -----------*/
#define USBD_SELF_POWERED 1U
/*---------- -----------*/
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 64U
/*---------- -----------*/
#define USBD_CUSTOM_HID_REPORT_DESC_SIZE 30U
/*---------- -----------*/
#define CUSTOM_HID_FS_BINTERVAL 0x1U
usbd_customhid.c
#define CUSTOM_HID_EPIN_ADDR 0x81U
#define CUSTOM_HID_EPIN_SIZE 0x40U
#define CUSTOM_HID_EPOUT_ADDR 0x02U
#define CUSTOM_HID_EPOUT_SIZE 0x40U
#define USB_CUSTOM_HID_CONFIG_DESC_SIZ 41U
#define USB_CUSTOM_HID_DESC_SIZ 9U
#ifndef CUSTOM_HID_HS_BINTERVAL
#define CUSTOM_HID_HS_BINTERVAL 0x05U
#endif /* CUSTOM_HID_HS_BINTERVAL */
#ifndef CUSTOM_HID_FS_BINTERVAL
#define CUSTOM_HID_FS_BINTERVAL 0x05U
#endif /* CUSTOM_HID_FS_BINTERVAL */
#ifndef USBD_CUSTOMHID_OUTREPORT_BUF_SIZE
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 0x64U
#endif /* USBD_CUSTOMHID_OUTREPORT_BUF_SIZE */
#ifndef USBD_CUSTOM_HID_REPORT_DESC_SIZE
#define USBD_CUSTOM_HID_REPORT_DESC_SIZE 30U
#endif /* USBD_CUSTOM_HID_REPORT_DESC_SIZE */
#define CUSTOM_HID_DESCRIPTOR_TYPE 0x21U
#define CUSTOM_HID_REPORT_DESC 0x22U
#define CUSTOM_HID_REQ_SET_PROTOCOL 0x0BU
#define CUSTOM_HID_REQ_GET_PROTOCOL 0x03U
#define CUSTOM_HID_REQ_SET_IDLE 0x0AU
#define CUSTOM_HID_REQ_GET_IDLE 0x02U
#define CUSTOM_HID_REQ_SET_REPORT 0x09U
#define CUSTOM_HID_REQ_GET_REPORT 0x01U
#include "usb_device.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_customhid.h"
#include "usbd_custom_hid_if.h"
#include "usart.h"
/* USER CODE BEGIN Includes */
unsigned char USB_Send_Buffer[64] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
unsigned char USB_Recive_Buffer[64] = {1,2,3,4,5,6,7,8,9,}; //USB½ÓÊÕ»º´æ
unsigned char USB_Received_Count = 0;//USB½ÓÊÕÊý¾Ý¼ÆÊý
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USB Device Core handle declaration. */
USBD_HandleTypeDef hUsbDeviceFS;
/*
* -- Insert your variables declaration here --
*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*
* -- Insert your external function declaration here --
*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/**
* Init USB device Library, add supported class and start the library
* @retval None
*/
void MX_USB_DEVICE_Init(void)
{
/* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
/* USER CODE END USB_DEVICE_Init_PreTreatment */
/* Init Device Library, add supported class and start the library. */
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
{
Error_Handler();
}
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CUSTOM_HID) != USBD_OK)
{
Error_Handler();
}
if (USBD_CUSTOM_HID_RegisterInterface(&hUsbDeviceFS, &USBD_CustomHID_fops_FS) != USBD_OK)
{
Error_Handler();
}
if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
/* USER CODE END USB_DEVICE_Init_PostTreatment */
}
void Usbd_Send_Test(void)
{
Usart_Printf("USBD_CUSTOM_HID_SendReport!\r\n");
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, USB_Send_Buffer, 64);
}
void Usbd_Receive_Test(void)
{
Usart_Printf("\r\nUsbd_Receive_Test:\r\n");
for(int i = 0; i < 64; i++)
{
Usart_Printf("%02x", USB_Recive_Buffer[i]);
}
}
发送测试:
接收测试: