|
本帖最后由 mii 于 2018-1-6 10:48 编辑
近期项目有用STM32F042F6P6做了简单的控制,后来需要扩展功能。随带做了USB的功能测试。
晶振使用外置的8M,当然可以使用内部48MRC。其他就是直接USB连接,做回环测试。
好了,废话不说,先上程序
下面开始修改
1.首先使用STM32CubeMX生成可用的HID程序。看图配置
2.生成的程序打开后,打开usbd_conf.h,新增端点定义
- /*
- * endpoint 0x80 and 0x00 are used for enumerating device.
- * endpoint 0x81 and 0x80 are used for control xfer.
- */
- #define HID_IN_EP 0x81
- #define HID_OUT_EP 0x01 //使用端点1
- #define HID_IN_BULK_PACKET 64 //按照协议,FS模式下最大就64个字节
- #define HID_OUT_BULK_PACKET 64
复制代码
3.打开usbd_desc.h修改VIP,PID。这个最好修改了不然又是识别为HID设备。
对应的驱动生成,可以使用libusb提供的工具。使用方法自行查找。
- /** @defgroup USBD_DESC_Private_Defines
- * @{
- */
- #define USBD_VID 0x0483//1155
- #define USBD_LANGID_STRING 1033
- #define USBD_MANUFACTURER_STRING "STMicroelectronics"
- #define USBD_PID_FS 0x5710//22315
- #define USBD_PRODUCT_STRING_FS "STM32 Human interface"
- #define USBD_SERIALNUMBER_STRING_FS "00000000001A"
- #define USBD_CONFIGURATION_STRING_FS "HID Config"
- #define USBD_INTERFACE_STRING_FS "HID Interface"
复制代码
4..打开usbd_desc.c开始修改描述符,首先修改设备描述符USBD_FS_DeviceDesc
bDeviceClass 修改为0xff,为自定义设备
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
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|