usbh_hid.rst 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. usbh_hid
  2. ===============
  3. 本节主要介绍 HID 类的使用。
  4. - HID 枚举完成回调中创建一次性线程
  5. .. code-block:: C
  6. void usbh_hid_run(struct usbh_hid *hid_class)
  7. {
  8. usb_osal_thread_create("usbh_hid", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_hid_thread, hid_class);
  9. }
  10. void usbh_hid_stop(struct usbh_hid *hid_class)
  11. {
  12. }
  13. - 这里我们使用 usbh_submit_urb 的异步操作,在中断中处理数据并继续接收下一次数据。
  14. .. code-block:: C
  15. static void usbh_hid_thread(void *argument)
  16. {
  17. int ret;
  18. struct usbh_hid *hid_class = (struct usbh_hid *)argument;
  19. ;
  20. /* test with only one buffer, if you have more hid class, modify by yourself */
  21. /* Suggest you to use timer for int transfer and use ep interval */
  22. usbh_int_urb_fill(&hid_class->intin_urb, hid_class->hport, hid_class->intin, hid_buffer, hid_class->intin->wMaxPacketSize, 0, usbh_hid_callback, hid_class);
  23. ret = usbh_submit_urb(&hid_class->intin_urb);
  24. if (ret < 0) {
  25. goto delete;
  26. }
  27. // clang-format off
  28. delete:
  29. usb_osal_thread_delete(NULL);
  30. // clang-format on
  31. }
  32. - 当然,也可以不使用异步操作,而是使用 timeout 的同步操作。
  33. - HID 使用的是中断传输,因此正常来说,我们需要根据 **bInterval** 来设置定时器,定时触发中断传输,demo 这里没有使用,如果对时间有精确要求,可以选择使用定时器来触发异步发送。
  34. - 以 hub 通信为例,采用的是一次性定时器,也可以使用周期性定时器。
  35. .. code-block:: C
  36. hub->int_timer = usb_osal_timer_create("hubint_tim", USBH_GET_URB_INTERVAL(hub->intin->bInterval, hport->speed) / 1000, hub_int_timeout, hub, 0);
  37. .. note::
  38. 这里的 `USBH_GET_URB_INTERVAL` 是一个宏定义,用于根据 binterval 计算 URB 的传输间隔时间, 单位是 us,而定时器最低是 ms ,因此需要除以 1000。对于小于等于 1ms 的不需要使用定时器。