api_host.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 match_flags; /* Used for product specific matches; range is inclusive */
  13. uint8_t class; /* Base device class code */
  14. uint8_t subclass; /* Sub-class, depends on base class. Eg. */
  15. uint8_t protocol; /* Protocol, depends on base class. Eg. */
  16. uint16_t vid; /* Vendor ID (for vendor/product specific devices) */
  17. uint16_t pid; /* Product ID (for vendor/product specific devices) */
  18. const struct usbh_class_driver *class_driver;
  19. };
  20. 端点结构体
  21. """"""""""""""""""""""""""""""""""""
  22. .. code-block:: C
  23. struct usbh_endpoint {
  24. struct usb_endpoint_descriptor ep_desc;
  25. };
  26. 接口备用结构体
  27. """"""""""""""""""""""""""""""""""""
  28. .. code-block:: C
  29. struct usbh_interface_altsetting {
  30. struct usb_interface_descriptor intf_desc;
  31. struct usbh_endpoint ep[CONFIG_USBHOST_MAX_ENDPOINTS];
  32. };
  33. 接口结构体
  34. """"""""""""""""""""""""""""""""""""
  35. .. code-block:: C
  36. struct usbh_interface {
  37. char devname[CONFIG_USBHOST_DEV_NAMELEN];
  38. struct usbh_class_driver *class_driver;
  39. void *priv;
  40. struct usbh_interface_altsetting altsetting[CONFIG_USBHOST_MAX_INTF_ALTSETTINGS];
  41. uint8_t altsetting_num;
  42. };
  43. 配置结构体
  44. """"""""""""""""""""""""""""""""""""
  45. .. code-block:: C
  46. struct usbh_configuration {
  47. struct usb_configuration_descriptor config_desc;
  48. struct usbh_interface intf[CONFIG_USBHOST_MAX_INTERFACES];
  49. };
  50. hubport 结构体
  51. """"""""""""""""""""""""""""""""""""
  52. .. code-block:: C
  53. struct usbh_hubport {
  54. bool connected; /* True: device connected; false: disconnected */
  55. uint8_t port; /* Hub port index */
  56. uint8_t dev_addr; /* device address */
  57. uint8_t speed; /* device speed */
  58. uint8_t depth; /* distance from root hub */
  59. uint8_t route; /* route string */
  60. uint8_t slot_id; /* slot id */
  61. struct usb_device_descriptor device_desc;
  62. struct usbh_configuration config;
  63. const char *iManufacturer;
  64. const char *iProduct;
  65. const char *iSerialNumber;
  66. uint8_t *raw_config_desc;
  67. struct usb_setup_packet *setup;
  68. struct usbh_hub *parent;
  69. struct usbh_hub *self; /* if this hubport is a hub */
  70. struct usbh_bus *bus;
  71. struct usb_endpoint_descriptor ep0;
  72. struct usbh_urb ep0_urb;
  73. usb_osal_mutex_t mutex;
  74. };
  75. hub 结构体
  76. """"""""""""""""""""""""""""""""""""
  77. .. code-block:: C
  78. struct usbh_hub {
  79. bool connected;
  80. bool is_roothub;
  81. uint8_t index;
  82. uint8_t hub_addr;
  83. uint8_t speed;
  84. uint8_t nports;
  85. uint8_t powerdelay;
  86. uint8_t tt_think;
  87. bool ismtt;
  88. struct usb_hub_descriptor hub_desc; /* USB 2.0 only */
  89. struct usb_hub_ss_descriptor hub_ss_desc; /* USB 3.0 only */
  90. struct usbh_hubport child[CONFIG_USBHOST_MAX_EHPORTS];
  91. struct usbh_hubport *parent;
  92. struct usbh_bus *bus;
  93. struct usb_endpoint_descriptor *intin;
  94. struct usbh_urb intin_urb;
  95. uint8_t *int_buffer;
  96. struct usb_osal_timer *int_timer;
  97. };
  98. usbh_initialize
  99. """"""""""""""""""""""""""""""""""""
  100. ``usbh_initialize`` 用来初始化 usb 主机协议栈,包括:初始化 usb 主机控制器,创建 roothub 设备,创建 hub 检测线程。
  101. .. code-block:: C
  102. int usbh_initialize(uint8_t busid, uint32_t reg_base);
  103. - **busid** bus id,从 0开始,不能超过 `CONFIG_USBHOST_MAX_BUS`
  104. - **reg_base** hcd 寄存器基地址
  105. - **return** 0 表示正常其他表示错误
  106. usbh_find_class_instance
  107. """"""""""""""""""""""""""""""""""""
  108. ``usbh_find_class_instance`` 根据注册的 class 名称查找对应的 class 结构体句柄。
  109. .. code-block:: C
  110. void *usbh_find_class_instance(const char *devname);
  111. - **devname** class 名称
  112. - **return** class 结构体句柄
  113. lsusb
  114. """"""""""""""""""""""""""""""""""""
  115. ``lsusb`` 用来查看和操作 hub 上的设备信息。需要借助 shell 插件使用。
  116. .. code-block:: C
  117. int lsusb(int argc, char **argv);
  118. CDC ACM
  119. -----------------
  120. HID
  121. -----------------
  122. MSC
  123. -----------------
  124. RNDIS
  125. -----------------