本帖最后由 mii 于 2018-1-6 10:48 编辑
近期项目有用STM32F042F6P6做了简单的控制,后来需要扩展功能。随带做了USB的功能测试。
晶振使用外置的8M,当然可以使用内部48MRC。其他就是直接USB连接,做回环测试。
好了,废话不说,先上程序
HIDTOBULK.rar
(5.1 MB, 下载次数: 59)
2018-1-5 14:32 上传
点击文件名下载附件
下面开始修改
1.首先使用STM32CubeMX生成可用的HID程序。看图配置
2.png (20.88 KB, 下载次数: 0)
下载附件
2018-1-5 14:49 上传
5.打开usbd_hid.c,修改配置,接口,端口等描述符,直接上代码
其中描述符的长度要修改一下,就是其中的 USB_HID_CONFIG_DESC_SIZ = 36;
- /* USB HID device Configuration Descriptor */
- __ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END =
- {
- 0x09, /* bLength: Configuration Descriptor size */
- USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
- USB_HID_CONFIG_DESC_SIZ,
- /* wTotalLength: Bytes returned */
- 0x00,
- 0x01, /*bNumInterfaces: 1 interface*/
- 0x01, /*bConfigurationValue: Configuration value*/
- 0x00, /*iConfiguration: Index of string descriptor describing
- the configuration*/
- 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */
- 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
-
- /************** Descriptor of Joystick Mouse interface ****************/
- /* 09 */
- 0x09, /*bLength: Interface Descriptor size*/
- USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
- 0x00, /*bInterfaceNumber: Number of Interface*/
- 0x00, /*bAlternateSetting: Alternate setting*/
- 0x02, /*bNumEndpoints*/
- 0x00, /*bInterfaceClass: HID*/
- 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
- 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
- 0, /*iInterface: Index of string descriptor*/
- // /******************** Descriptor of Joystick Mouse HID ********************/
- // /* 18 */
- // 0x09, /*bLength: HID Descriptor size*/
- // HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
- // 0x11, /*bcdHID: HID Class Spec release number*/
- // 0x01,
- // 0x00, /*bCountryCode: Hardware target country*/
- // 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
- // 0x22, /*bDescriptorType*/
- // HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
- // 0x00,
- /******************** Descriptor of Mouse endpoint ********************/
- /* 18 */
- 0x07, /*bLength: Endpoint Descriptor size*/
- USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
-
- HID_IN_EP, /*bEndpointAddress: Endpoint Address (IN)*/
- 0x02, /*bmAttributes: bulk endpoint*/
- HID_IN_BULK_PACKET, /*wMaxPacketSize: 64 Byte max */
- 0x00,
- HID_FS_BINTERVAL, /*bInterval: Polling Interval (10 ms)*/
- /* 25 */
- 0x07, /*bLength: Endpoint Descriptor size*/
- USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
-
- HID_OUT_EP, /*bEndpointAddress: Endpoint Address (OUT)*/
- 0x02, /*bmAttributes: bulk endpoint*/
- HID_OUT_BULK_PACKET, /*wMaxPacketSize: 64 Byte max */
- 0x00,
- HID_FS_BINTERVAL, /*bInterval: Polling Interval (10 ms)*/
- /* 32 */
- } ;
复制代码
6.初始化端点与接收缓冲,USB_Rx_Buffer为自己定义接收缓冲区大小 为64个字节
- /**
- * @brief USBD_HID_Init
- * Initialize the HID interface
- * @param pdev: device instance
- * @param cfgidx: Configuration index
- * @retval status
- */
- static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev,
- uint8_t cfgidx)
- {
- uint8_t ret = 0;
-
- /* Open EP IN */
- USBD_LL_OpenEP(pdev,
- HID_IN_EP,
- USBD_EP_TYPE_BULK,
- HID_IN_BULK_PACKET);
-
- /* Open EP OUT */
- USBD_LL_OpenEP(pdev,
- HID_OUT_EP,
- USBD_EP_TYPE_BULK,
- HID_OUT_BULK_PACKET);
-
- /* Prepare Out endpoint to receive next packet */
- USBD_LL_PrepareReceive(pdev,
- HID_OUT_EP,
- (uint8_t*)(USB_Rx_Buffer),
- HID_OUT_BULK_PACKET);
-
-
- pdev->pClassData = USBD_malloc(sizeof (USBD_HID_HandleTypeDef));
-
- if(pdev->pClassData == NULL)
- {
- ret = 1;
- }
- else
- {
- ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE;
- }
- return ret;
- }
复制代码
7.同时也设置一下重置函数
- /**
- * @brief USBD_HID_Init
- * DeInitialize the HID layer
- * @param pdev: device instance
- * @param cfgidx: Configuration index
- * @retval status
- */
- static uint8_t USBD_HID_DeInit (USBD_HandleTypeDef *pdev,
- uint8_t cfgidx)
- {
- /* Close HID EPs */
- USBD_LL_CloseEP(pdev,
- HID_IN_EP);
- /* Close HID EPs */
- USBD_LL_CloseEP(pdev,
- HID_OUT_EP);
-
- /* FRee allocated memory */
- if(pdev->pClassData != NULL)
- {
- USBD_free(pdev->pClassData);
- pdev->pClassData = NULL;
- }
-
- return USBD_OK;
- }
复制代码
8.新增out函数,先声明
static uint8_t USBD_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum);
然后注册一下
- /** @defgroup USBD_HID_Private_Variables
- * @{
- */
- USBD_ClassTypeDef USBD_HID =
- {
- USBD_HID_Init,
- USBD_HID_DeInit,
- USBD_HID_Setup,
- NULL, /*EP0_TxSent*/
- NULL, /*EP0_RxReady*/
- USBD_HID_DataIn, /*DataIn*/
- USBD_HID_DataOut, /*DataOut*/
- NULL, /*SOF */
- NULL,
- NULL,
- USBD_HID_GetCfgDesc,
- USBD_HID_GetCfgDesc,
- USBD_HID_GetCfgDesc,
- USBD_HID_GetDeviceQualifierDesc,
- };
复制代码
再然后就是在里面实现回环
- /**
- * @brief USBD_HID_DataIn
- * handle data IN Stage
- * @param pdev: device instance
- * @param epnum: endpoint index
- * @retval status
- */
- static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev,
- uint8_t epnum)
- {
-
- uCountRXbuff = USBD_GetRxCount(pdev,epnum);
- // add your own data to process the received data
- /*
- * printf_the_received_data((char *)USB_Rx_Buffer, USB_Rx_Cnt);
- */
- /* Prepare Out endpoint to receive next packet */
- USBD_LL_PrepareReceive(pdev,
- HID_OUT_EP,
- (uint8_t*)(USB_Rx_Buffer),
- HID_OUT_BULK_PACKET);
-
- USBD_HID_SendReport (pdev,
- (uint8_t*)&USB_Rx_Buffer,
- uCountRXbuff);
-
- return USBD_OK;
- }
复制代码
9.基本完成了,然后打开usbd_conf.c,新增OUT缓冲地址,分配 64个字节
- /**
- * @brief Initializes the Low Level portion of the Device driver.
- * @param pdev: Device handle
- * @retval USBD Status
- */
- USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
- {
- /* Init USB_IP */
- /* Link The driver to the stack */
- hpcd_USB_FS.pData = pdev;
- pdev->pData = &hpcd_USB_FS;
- hpcd_USB_FS.Instance = USB;
- hpcd_USB_FS.Init.dev_endpoints = 8;
- hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
- hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
- hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
- hpcd_USB_FS.Init.low_power_enable = DISABLE;
- hpcd_USB_FS.Init.lpm_enable = DISABLE;
- hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
- if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18);
- HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58);
- HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0x100);
- HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x140);
- return USBD_OK;
- }
复制代码
10.编译下载,你就可以测试了。测试使用了bus hound
BusHound64_jb51.rar
(1.32 MB, 下载次数: 54)
2018-1-5 15:22 上传
点击文件名下载附件
生成就可以识别为HID设备,网上有例程,修改一下就可以变成鼠标了,传送门http://blog.csdn.net/xqhrs232/article/details/77740508
一周热门 更多>