api_host.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 主机协议栈
  2. =========================
  3. 关于主机协议栈中结构体的命名、分类、成员组成,参考下面这两张图:
  4. .. figure:: img/api_host1.png
  5. .. figure:: img/api_host2.png
  6. CORE
  7. -----------------
  8. CLASS 驱动信息结构体
  9. """"""""""""""""""""""""""""""""""""
  10. .. code-block:: C
  11. struct usbh_class_info {
  12. uint8_t class; /* Base device class code */
  13. uint8_t subclass; /* Sub-class, depends on base class. Eg. */
  14. uint8_t protocol; /* Protocol, depends on base class. Eg. */
  15. uint16_t vid; /* Vendor ID (for vendor/product specific devices) */
  16. uint16_t pid; /* Product ID (for vendor/product specific devices) */
  17. const struct usbh_class_driver *class_driver;
  18. };
  19. 端点结构体
  20. """"""""""""""""""""""""""""""""""""
  21. .. code-block:: C
  22. typedef struct usbh_endpoint {
  23. struct usb_endpoint_descriptor ep_desc;
  24. } usbh_endpoint_t;
  25. 接口结构体
  26. """"""""""""""""""""""""""""""""""""
  27. .. code-block:: C
  28. typedef struct usbh_interface {
  29. struct usb_interface_descriptor intf_desc;
  30. struct usbh_endpoint ep[CONFIG_USBHOST_EP_NUM];
  31. char devname[CONFIG_USBHOST_DEV_NAMELEN];
  32. struct usbh_class_driver *class_driver;
  33. void *priv;
  34. } usbh_interface_t;
  35. 配置结构体
  36. """"""""""""""""""""""""""""""""""""
  37. .. code-block:: C
  38. typedef struct usbh_configuration {
  39. struct usb_configuration_descriptor config_desc;
  40. struct usbh_interface intf[CONFIG_USBHOST_INTF_NUM];
  41. } usbh_configuration_t;
  42. hubport 结构体
  43. """"""""""""""""""""""""""""""""""""
  44. .. code-block:: C
  45. typedef struct usbh_hubport {
  46. bool connected; /* True: device connected; false: disconnected */
  47. bool port_change; /* True: port changed; false: port do not change */
  48. uint8_t port; /* Hub port index */
  49. uint8_t dev_addr; /* device address */
  50. uint8_t speed; /* device speed */
  51. usbh_epinfo_t ep0; /* control ep info */
  52. struct usb_device_descriptor device_desc;
  53. struct usbh_configuration config;
  54. #if 0
  55. uint8_t* config_desc;
  56. #endif
  57. struct usb_setup_packet *setup;
  58. struct usbh_hub *parent; /*if NULL, is roothub*/
  59. } usbh_hubport_t;
  60. hub 结构体
  61. """"""""""""""""""""""""""""""""""""
  62. .. code-block:: C
  63. typedef struct usbh_hub {
  64. usb_slist_t list;
  65. uint8_t index; /* Hub index */
  66. uint8_t nports; /* Hub port number */
  67. uint8_t dev_addr; /* Hub device address */
  68. usbh_epinfo_t intin;
  69. uint8_t *int_buffer;
  70. struct hub_port_status *port_status;
  71. struct usb_hub_descriptor hub_desc;
  72. struct usbh_hubport child[CONFIG_USBHOST_EHPORTS];
  73. struct usbh_hubport *parent; /* Parent hub port */
  74. struct usb_work work;
  75. } usbh_hub_t;
  76. usbh_event_notify_handler
  77. """"""""""""""""""""""""""""""""""""
  78. ``usbh_event_notify_handler`` 是 USB 中断中的核心,主要用于处理 **设备连接** 和 **设备断开** 中断,从而唤醒线程去执行枚举。
  79. .. code-block:: C
  80. void usbh_event_notify_handler(uint8_t event, uint8_t rhport);
  81. - **event** 中断事件
  82. - **rhport** roothub 端口号
  83. 其中 ``event`` 有如下类型:
  84. .. code-block:: C
  85. enum usbh_event_type {
  86. USBH_EVENT_ATTACHED,
  87. USBH_EVENT_REMOVED,
  88. };
  89. usbh_initialize
  90. """"""""""""""""""""""""""""""""""""
  91. ``usbh_initialize`` 用来初始化 usb 主机协议栈,包括:创建插拔检测用的信号量和枚举线程、高低工作队列、初始化 roothub 端点0 配置,初始化 usb 主机控制器。
  92. .. code-block:: C
  93. int usbh_initialize(void);
  94. usbh_find_class_instance
  95. """"""""""""""""""""""""""""""""""""
  96. ``usbh_find_class_instance`` 根据注册的 class 名称查找对应的 class 结构体句柄。
  97. .. code-block:: C
  98. void *usbh_find_class_instance(const char *devname);
  99. - **devname** class 名称
  100. - **return** class 结构体句柄
  101. lsusb
  102. """"""""""""""""""""""""""""""""""""
  103. ``lsusb`` 用来查看和操作 hub 上的设备信息。需要借助 shell 插件使用。
  104. .. code-block:: C
  105. int lsusb(int argc, char **argv);
  106. CDC ACM
  107. -----------------
  108. HID
  109. -----------------
  110. MSC
  111. -----------------
  112. RNDIS
  113. -----------------
  114. PRINTER
  115. -----------------