[STM32H5] 【NUCLEO- H563ZI 测评】USBX 之 CDC+HID

[复制链接]
2380|7
 楼主| OldestTrick 发表于 2023-8-19 23:29 | 显示全部楼层 |阅读模式
USB, CD, dc, HID, NUC

1、STM32H5有一个全速度USB2.0和一个USB Type-C/USB power-delivery。基于Type-C支持正反插、速度更快、供电更强大等优点目前很多电子产品很多都是使用USB Type-C接口,STM32H5直接支持USB Type-C可以给我们在使用中带来更多的便利。
NUCLEO-H563ZI上USB口的硬件接口电路如下:D+1/D-1和D+2/D-2用于传输USB 2.0数据。
5120064e0de5a80a60.png

STM32Cube_FW_H5_V1.1.0开发包里面提供了使用Azure RTOS USBX 协议栈提供的USB demo,好久没有用ST的新产品了,都换成了USBX了吗。这和我之前熟悉的ST自己提供的USB device和host 协议栈不太一样,也是第一次接触USBX ,先了解学习下。

4304164e0de6f32422.png

下面测试的是HID和CDC的复合设备的例子。
程序中使用的RTOS 是ThreadX,总共创建了4个任务用于初始化USB驱动和启动USB、CDC接收数据、CDC发送数据、HID传输数据。

1)USB读取功能,用于从USB VCP接收数据,然后发送到STlink的虚拟串口上
  1. VOID usbx_cdc_acm_read_thread_entry(ULONG thread_input)
  2. {
  3.   ULONG actual_length;
  4.   ULONG senddataflag = 0;
  5.   UX_SLAVE_DEVICE *device;

  6.   UX_PARAMETER_NOT_USED(thread_input);

  7.   device = &_ux_system_slave->ux_system_slave_device;

  8.   while (1)
  9.   {
  10.     /* Check if device is configured */
  11.     if ((device->ux_slave_device_state == UX_DEVICE_CONFIGURED) && (cdc_acm != UX_NULL))
  12.     {

  13. #ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE

  14.       /* Set transmission_status to UX_FALSE for the first time */
  15.       cdc_acm -> ux_slave_class_cdc_acm_transmission_status = UX_FALSE;

  16. #endif /* UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE */

  17.       /* Read the received data in blocking mode */
  18.       ux_device_class_cdc_acm_read(cdc_acm, (UCHAR *)UserRxBufferFS, 64,
  19.                                    &actual_length);
  20.       if (actual_length != 0)
  21.       {
  22.         /* Send the data via UART */
  23.         if (HAL_UART_Transmit_DMA(uart_handler, (uint8_t *)UserRxBufferFS, actual_length) != HAL_OK)
  24.         {
  25.           Error_Handler();
  26.         }

  27.         /* Wait until the requested flag TX_NEW_TRANSMITTED_DATA is received */
  28.         if (tx_event_flags_get(&EventFlag, TX_NEW_TRANSMITTED_DATA, TX_OR_CLEAR,
  29.                                &senddataflag, TX_WAIT_FOREVER) != TX_SUCCESS)
  30.         {
  31.           Error_Handler();
  32.         }
  33.       }
  34.       else
  35.       {
  36.         /* Sleep thread for 10ms if no data received */
  37.         tx_thread_sleep(MS_TO_TICK(10));
  38.       }
  39.     }
  40.     else
  41.     {
  42.       /* Sleep thread for 10ms */
  43.       tx_thread_sleep(MS_TO_TICK(10));
  44.     }
  45.   }
  46. }
2)USB写数据功能。用于将从STlink 虚拟串口接收到的数据发送出去
  1. VOID usbx_cdc_acm_write_thread_entry(ULONG thread_input)
  2. {
  3.   ULONG receivedataflag = 0;
  4.   ULONG actual_length, buffptr, buffsize;

  5.   UX_PARAMETER_NOT_USED(thread_input);

  6.   while (1)
  7.   {
  8.     /* Wait until the requested flag RX_NEW_RECEIVED_DATA is received */
  9.     if (tx_event_flags_get(&EventFlag, RX_NEW_RECEIVED_DATA, TX_OR_CLEAR,
  10.                            &receivedataflag, TX_WAIT_FOREVER) != TX_SUCCESS)
  11.     {
  12.       Error_Handler();
  13.     }

  14. #ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE

  15.     /* Set transmission_status to UX_FALSE for the first time */
  16.     cdc_acm -> ux_slave_class_cdc_acm_transmission_status = UX_FALSE;

  17. #endif

  18.     /* Check if there is a new data to send */
  19.     if (UserTxBufPtrOut != UserTxBufPtrIn)
  20.     {
  21.       /* Check buffer overflow and Rollback */
  22.       if (UserTxBufPtrOut > UserTxBufPtrIn)
  23.       {
  24.         buffsize = APP_RX_DATA_SIZE - UserTxBufPtrOut;
  25.       }
  26.       else
  27.       {
  28.         /* Calculate data size */
  29.         buffsize = UserTxBufPtrIn - UserTxBufPtrOut;
  30.       }

  31.       /* Copy UserTxBufPtrOut in buffptr */
  32.       buffptr = UserTxBufPtrOut;

  33.       /* Send data over the class cdc_acm_write */
  34.       if (ux_device_class_cdc_acm_write(cdc_acm, (UCHAR *)(&UserTxBufferFS[buffptr]),
  35.                                         buffsize, &actual_length) == UX_SUCCESS)
  36.       {
  37.         /* Increment the UserTxBufPtrOut pointer */
  38.         UserTxBufPtrOut += buffsize;

  39.         /* Rollback UserTxBufPtrOut if it equal to APP_TX_DATA_SIZE */
  40.         if (UserTxBufPtrOut == APP_TX_DATA_SIZE)
  41.         {
  42.           UserTxBufPtrOut = 0;
  43.         }
  44.       }
  45.     }
  46.   }
  47. }
测试效果:打开2个串口助手,一个用于打开STlink的VCP 一个打开USB CDC 。这样2个串口就可以聊天了。

3271364e0defd37952.png

3)HID 功能用于将用户按下开发板上的按键之后发送到PC上,鼠标就会移动一下
  1. VOID usbx_hid_thread_entry(ULONG thread_input)
  2. {
  3.   UX_SLAVE_DEVICE *device;
  4.   UX_SLAVE_CLASS_HID_EVENT hid_event;

  5.   UX_PARAMETER_NOT_USED(thread_input);

  6.   device = &_ux_system_slave->ux_system_slave_device;

  7.   ux_utility_memory_set(&hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT));

  8.   while (1)
  9.   {
  10.     /* Check if the device state already configured */
  11.     if ((device->ux_slave_device_state == UX_DEVICE_CONFIGURED) && (hid_mouse != UX_NULL))
  12.     {
  13.       /* sleep for 10ms */
  14.       tx_thread_sleep(MS_TO_TICK(10));

  15.       /* Check if user button is pressed */
  16.       if (User_Button_State != 0U)
  17.       {
  18.         /* Get the new position */
  19.         GetPointerData(&hid_event);

  20.         /* Send an event to the hid */
  21.         ux_device_class_hid_event_set(hid_mouse, &hid_event);

  22.         /* Reset User Button state */
  23.         User_Button_State = 0U;
  24.       }
  25.     }
  26.     else
  27.     {
  28.       /* Sleep thread for 10ms */
  29.       tx_thread_sleep(MS_TO_TICK(10));
  30.     }
  31.   }
  32. }
本来想测试下STM32H5的CDC的速度的,但是发现USBX和之前熟悉的ST的USB协议栈有很多不一样的地方,在动手修改之前还要先熟悉USBX,所有此次的测试就是简单的了解下数据传输的各个任务。STM32H5没有F1/F4/F7那种ST USB协议栈了吗?






评论

很不错的资源,学习一下  发表于 2023-9-17 08:15
MessageRing 发表于 2023-8-23 21:33 | 显示全部楼层
ThreadX跟FreeRTOS哪个更好用啊
gumenggumeng 发表于 2023-9-1 15:49 | 显示全部楼层
是直接用的官方给的例程嘛?想实现usbx custom hid与PC端的通信,看了半天头大,可以交流下嘛
Stahan 发表于 2023-9-2 21:40 | 显示全部楼层
USBX是升级的usb协议吗?
香水城 发表于 2023-9-17 16:26 | 显示全部楼层
Bowclad 发表于 2023-9-17 21:26 | 显示全部楼层
Stahan 发表于 2023-9-2 21:40
USBX是升级的usb协议吗?

USBX 是一种高性能的 USB 主机、设备和移动 (OTG) 嵌入式堆栈
lulugl 发表于 2024-4-16 21:15 | 显示全部楼层
可以讲讲stm32cubeMAX如何配置的吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

23

主题

45

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部