分享stm32f042f6p6 USB HID改BULK传输程序

2019-12-10 18:31发布

本帖最后由 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;

  1. /* USB HID device Configuration Descriptor */
  2. __ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ]  __ALIGN_END =
  3. {
  4.   0x09, /* bLength: Configuration Descriptor size */
  5.   USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
  6.   USB_HID_CONFIG_DESC_SIZ,
  7.   /* wTotalLength: Bytes returned */
  8.   0x00,
  9.   0x01,         /*bNumInterfaces: 1 interface*/
  10.   0x01,         /*bConfigurationValue: Configuration value*/
  11.   0x00,         /*iConfiguration: Index of string descriptor describing
  12.   the configuration*/
  13.   0xE0,         /*bmAttributes: bus powered and Support Remote Wake-up */
  14.   0x32,         /*MaxPower 100 mA: this current is used for detecting Vbus*/
  15.   
  16.   /************** Descriptor of Joystick Mouse interface ****************/
  17.   /* 09 */
  18.   0x09,         /*bLength: Interface Descriptor size*/
  19.   USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
  20.   0x00,         /*bInterfaceNumber: Number of Interface*/
  21.   0x00,         /*bAlternateSetting: Alternate setting*/
  22.   0x02,         /*bNumEndpoints*/
  23.   0x00,         /*bInterfaceClass: HID*/
  24.   0x00,         /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
  25.   0x00,         /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
  26.   0,            /*iInterface: Index of string descriptor*/
  27. //  /******************** Descriptor of Joystick Mouse HID ********************/
  28. //  /* 18 */
  29. //  0x09,         /*bLength: HID Descriptor size*/
  30. //  HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
  31. //  0x11,         /*bcdHID: HID Class Spec release number*/
  32. //  0x01,
  33. //  0x00,         /*bCountryCode: Hardware target country*/
  34. //  0x01,         /*bNumDescriptors: Number of HID class descriptors to follow*/
  35. //  0x22,         /*bDescriptorType*/
  36. //  HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
  37. //  0x00,
  38.   /******************** Descriptor of Mouse endpoint ********************/
  39.   /* 18 */
  40.   0x07,          /*bLength: Endpoint Descriptor size*/
  41.   USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
  42.   
  43.   HID_IN_EP,     /*bEndpointAddress: Endpoint Address (IN)*/
  44.   0x02,          /*bmAttributes: bulk endpoint*/
  45.   HID_IN_BULK_PACKET, /*wMaxPacketSize: 64 Byte max */
  46.   0x00,
  47.   HID_FS_BINTERVAL,          /*bInterval: Polling Interval (10 ms)*/
  48.   /* 25 */
  49.         0x07,          /*bLength: Endpoint Descriptor size*/
  50.   USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
  51.   
  52.   HID_OUT_EP,     /*bEndpointAddress: Endpoint Address (OUT)*/
  53.   0x02,          /*bmAttributes: bulk endpoint*/
  54.   HID_OUT_BULK_PACKET, /*wMaxPacketSize: 64 Byte max */
  55.   0x00,
  56.   HID_FS_BINTERVAL,          /*bInterval: Polling Interval (10 ms)*/
  57.         /* 32 */
  58. } ;
复制代码

6.初始化端点与接收缓冲,USB_Rx_Buffer为自己定义接收缓冲区大小 为64个字节
  1. /**
  2.   * @brief  USBD_HID_Init
  3.   *         Initialize the HID interface
  4.   * @param  pdev: device instance
  5.   * @param  cfgidx: Configuration index
  6.   * @retval status
  7.   */
  8. static uint8_t  USBD_HID_Init (USBD_HandleTypeDef *pdev,
  9.                                uint8_t cfgidx)
  10. {
  11.   uint8_t ret = 0;
  12.   
  13.   /* Open EP IN */
  14.   USBD_LL_OpenEP(pdev,
  15.                  HID_IN_EP,
  16.                  USBD_EP_TYPE_BULK,
  17.                  HID_IN_BULK_PACKET);  
  18.        
  19.         /* Open EP OUT */
  20.   USBD_LL_OpenEP(pdev,
  21.                  HID_OUT_EP,
  22.                  USBD_EP_TYPE_BULK,
  23.                  HID_OUT_BULK_PACKET);  
  24.        
  25.                 /* Prepare Out endpoint to receive next packet */                       
  26.         USBD_LL_PrepareReceive(pdev,
  27.                  HID_OUT_EP,
  28.                  (uint8_t*)(USB_Rx_Buffer),
  29.                  HID_OUT_BULK_PACKET);        
  30.        
  31.   
  32.   pdev->pClassData = USBD_malloc(sizeof (USBD_HID_HandleTypeDef));
  33.   
  34.   if(pdev->pClassData == NULL)
  35.   {
  36.     ret = 1;
  37.   }
  38.   else
  39.   {
  40.     ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE;
  41.   }
  42.   return ret;
  43. }
复制代码

7.同时也设置一下重置函数
  1. /**
  2.   * @brief  USBD_HID_Init
  3.   *         DeInitialize the HID layer
  4.   * @param  pdev: device instance
  5.   * @param  cfgidx: Configuration index
  6.   * @retval status
  7.   */
  8. static uint8_t  USBD_HID_DeInit (USBD_HandleTypeDef *pdev,
  9.                                  uint8_t cfgidx)
  10. {
  11.   /* Close HID EPs */
  12.   USBD_LL_CloseEP(pdev,
  13.                   HID_IN_EP);
  14.         /* Close HID EPs */
  15.   USBD_LL_CloseEP(pdev,
  16.                   HID_OUT_EP);

  17.   
  18.   /* FRee allocated memory */
  19.   if(pdev->pClassData != NULL)
  20.   {
  21.     USBD_free(pdev->pClassData);
  22.     pdev->pClassData = NULL;
  23.   }
  24.   
  25.   return USBD_OK;
  26. }
复制代码
8.新增out函数,先声明
static uint8_t  USBD_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum);
然后注册一下
  1. /** @defgroup USBD_HID_Private_Variables
  2.   * @{
  3.   */

  4. USBD_ClassTypeDef  USBD_HID =
  5. {
  6.   USBD_HID_Init,
  7.   USBD_HID_DeInit,
  8.   USBD_HID_Setup,
  9.   NULL, /*EP0_TxSent*/  
  10.   NULL, /*EP0_RxReady*/
  11.   USBD_HID_DataIn, /*DataIn*/
  12.   USBD_HID_DataOut, /*DataOut*/
  13.   NULL, /*SOF */
  14.   NULL,
  15.   NULL,      
  16.   USBD_HID_GetCfgDesc,
  17.   USBD_HID_GetCfgDesc,
  18.   USBD_HID_GetCfgDesc,
  19.   USBD_HID_GetDeviceQualifierDesc,
  20. };
复制代码
再然后就是在里面实现回环
  1. /**
  2.   * @brief  USBD_HID_DataIn
  3.   *         handle data IN Stage
  4.   * @param  pdev: device instance
  5.   * @param  epnum: endpoint index
  6.   * @retval status
  7.   */
  8. static uint8_t  USBD_HID_DataOut(USBD_HandleTypeDef *pdev,
  9.                               uint8_t epnum)
  10. {
  11.   
  12.   uCountRXbuff = USBD_GetRxCount(pdev,epnum);

  13.     // add your own data to process the received data
  14.     /*   
  15.     *    printf_the_received_data((char *)USB_Rx_Buffer, USB_Rx_Cnt);
  16.     */

  17.   /* Prepare Out endpoint to receive next packet */
  18.     USBD_LL_PrepareReceive(pdev,
  19.                        HID_OUT_EP,
  20.                        (uint8_t*)(USB_Rx_Buffer),
  21.                        HID_OUT_BULK_PACKET);
  22.        
  23.         USBD_HID_SendReport (pdev,
  24.                  (uint8_t*)&USB_Rx_Buffer,
  25.                  uCountRXbuff);
  26.        
  27.         return USBD_OK;
  28. }
复制代码

9.基本完成了,然后打开usbd_conf.c,新增OUT缓冲地址,分配 64个字节
  1. /**
  2.   * @brief  Initializes the Low Level portion of the Device driver.
  3.   * @param  pdev: Device handle
  4.   * @retval USBD Status
  5.   */
  6. USBD_StatusTypeDef  USBD_LL_Init (USBD_HandleTypeDef *pdev)
  7. {
  8.   /* Init USB_IP */
  9.   /* Link The driver to the stack */
  10.   hpcd_USB_FS.pData = pdev;
  11.   pdev->pData = &hpcd_USB_FS;

  12.   hpcd_USB_FS.Instance = USB;
  13.   hpcd_USB_FS.Init.dev_endpoints = 8;
  14.   hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
  15.   hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
  16.   hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  17.   hpcd_USB_FS.Init.low_power_enable = DISABLE;
  18.   hpcd_USB_FS.Init.lpm_enable = DISABLE;
  19.   hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
  20.   if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
  21.   {
  22.     _Error_Handler(__FILE__, __LINE__);
  23.   }

  24.   HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18);
  25.   HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58);
  26.   HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0x100);
  27.         HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x140);   
  28.   return USBD_OK;
  29. }
复制代码

10.编译下载,你就可以测试了。测试使用了bus hound BusHound64_jb51.rar (1.32 MB, 下载次数: 54) 2018-1-5 15:22 上传 点击文件名下载附件


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。