api_host.rst 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. Host Protocol Stack
  2. =======================================
  3. For the naming, classification, and member composition of structures in the host protocol stack, refer to the following two diagrams:
  4. .. figure:: img/api_host1.png
  5. .. figure:: img/api_host2.png
  6. CORE
  7. -----------------
  8. CLASS Driver Information Structure
  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 bInterfaceClass; /* Base device class code */
  14. uint8_t bInterfaceSubClass; /* Sub-class, depends on base class. Eg. */
  15. uint8_t bInterfaceProtocol; /* Protocol, depends on base class. Eg. */
  16. const uint16_t (*id_table)[2]; /* List of Vendor/Product ID pairs */
  17. const struct usbh_class_driver *class_driver;
  18. };
  19. Endpoint Structure
  20. """"""""""""""""""""""""""""""""""""
  21. .. code-block:: C
  22. struct usbh_endpoint {
  23. struct usb_endpoint_descriptor ep_desc;
  24. };
  25. Interface Altsetting Structure
  26. """"""""""""""""""""""""""""""""""""
  27. .. code-block:: C
  28. struct usbh_interface_altsetting {
  29. struct usb_interface_descriptor intf_desc;
  30. struct usbh_endpoint ep[CONFIG_USBHOST_MAX_ENDPOINTS];
  31. };
  32. Interface Structure
  33. """"""""""""""""""""""""""""""""""""
  34. .. code-block:: C
  35. struct usbh_interface {
  36. char devname[CONFIG_USBHOST_DEV_NAMELEN];
  37. struct usbh_class_driver *class_driver;
  38. void *priv;
  39. struct usbh_interface_altsetting altsetting[CONFIG_USBHOST_MAX_INTF_ALTSETTINGS];
  40. uint8_t altsetting_num;
  41. };
  42. Configuration Structure
  43. """"""""""""""""""""""""""""""""""""
  44. .. code-block:: C
  45. struct usbh_configuration {
  46. struct usb_configuration_descriptor config_desc;
  47. struct usbh_interface intf[CONFIG_USBHOST_MAX_INTERFACES];
  48. };
  49. hubport Structure
  50. """"""""""""""""""""""""""""""""""""
  51. .. code-block:: C
  52. struct usbh_hubport {
  53. bool connected; /* True: device connected; false: disconnected */
  54. uint8_t port; /* Hub port index */
  55. uint8_t dev_addr; /* device address */
  56. uint8_t speed; /* device speed */
  57. uint8_t depth; /* distance from root hub */
  58. uint8_t route; /* route string */
  59. uint8_t slot_id; /* slot id */
  60. struct usb_device_descriptor device_desc;
  61. struct usbh_configuration config;
  62. const char *iManufacturer;
  63. const char *iProduct;
  64. const char *iSerialNumber;
  65. uint8_t *raw_config_desc;
  66. struct usb_setup_packet *setup;
  67. struct usbh_hub *parent;
  68. struct usbh_hub *self; /* if this hubport is a hub */
  69. struct usbh_bus *bus;
  70. struct usb_endpoint_descriptor ep0;
  71. struct usbh_urb ep0_urb;
  72. usb_osal_mutex_t mutex;
  73. };
  74. hub Structure
  75. """"""""""""""""""""""""""""""""""""
  76. .. code-block:: C
  77. struct usbh_hub {
  78. bool connected;
  79. bool is_roothub;
  80. uint8_t index;
  81. uint8_t hub_addr;
  82. uint8_t speed;
  83. uint8_t nports;
  84. uint8_t powerdelay;
  85. uint8_t tt_think;
  86. bool ismtt;
  87. struct usb_hub_descriptor hub_desc; /* USB 2.0 only */
  88. struct usb_hub_ss_descriptor hub_ss_desc; /* USB 3.0 only */
  89. struct usbh_hubport child[CONFIG_USBHOST_MAX_EHPORTS];
  90. struct usbh_hubport *parent;
  91. struct usbh_bus *bus;
  92. struct usb_endpoint_descriptor *intin;
  93. struct usbh_urb intin_urb;
  94. uint8_t *int_buffer;
  95. struct usb_osal_timer *int_timer;
  96. };
  97. usbh_initialize
  98. """"""""""""""""""""""""""""""""""""
  99. ``usbh_initialize`` is used to initialize the USB host protocol stack, including: initializing the USB host controller, creating roothub device, creating hub detection thread.
  100. .. code-block:: C
  101. int usbh_initialize(uint8_t busid, uint32_t reg_base, usbh_event_handler_t event_handler);
  102. - **busid** bus id, starting from 0, cannot exceed `CONFIG_USBHOST_MAX_BUS`
  103. - **reg_base** hcd register base address
  104. - **event_handler** host event callback function, can be NULL
  105. - **return** 0 indicates normal, other values indicate error
  106. usbh_find_class_instance
  107. """"""""""""""""""""""""""""""""""""
  108. ``usbh_find_class_instance`` finds the corresponding class structure handle based on the registered class name.
  109. .. code-block:: C
  110. void *usbh_find_class_instance(const char *devname);
  111. - **devname** class name
  112. - **return** class structure handle
  113. lsusb
  114. """"""""""""""""""""""""""""""""""""
  115. ``lsusb`` is used to view and operate device information on the hub. Requires shell plugin to use.
  116. .. code-block:: C
  117. int lsusb(int argc, char **argv);
  118. SERIAL
  119. -----------------
  120. usbh_serial_open
  121. """"""""""""""""""""""""""""""""""""
  122. ``usbh_serial_open`` opens a serial device according to the path.
  123. .. code-block:: C
  124. struct usbh_serial *usbh_serial_open(const char *devname, uint32_t open_flags);
  125. - **devname** serial path
  126. - **open_flags** open flags, refer to `USBH_SERIAL_OFLAG_*` definitions
  127. - **return** serial structure handle
  128. usbh_serial_close
  129. """"""""""""""""""""""""""""""""""""
  130. ``usbh_serial_close`` closes the serial device.
  131. .. code-block:: C
  132. void usbh_serial_close(struct usbh_serial *serial);
  133. - **serial** serial structure handle
  134. usbh_serial_control
  135. """"""""""""""""""""""""""""""""""""
  136. ``usbh_serial_control`` configures the serial port.
  137. .. code-block:: C
  138. int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg);
  139. - **serial** serial structure handle
  140. - **cmd** control command, refer to `USBH_SERIAL_CMD_*` definitions
  141. - **arg** control parameter pointer
  142. - **return** 0 indicates normal, other values indicate error
  143. usbh_serial_write
  144. """"""""""""""""""""""""""""""""""""
  145. ``usbh_serial_write`` writes data to the serial port.
  146. .. code-block:: C
  147. int usbh_serial_write(struct usbh_serial *serial, const void *buffer, uint32_t buflen);
  148. - **serial** serial structure handle
  149. - **buffer** data buffer pointer
  150. - **buflen** length of data to write
  151. - **return** actual length of data written or error code
  152. .. note:: If CONFIG_USB_DCACHE_ENABLE is not enabled, buffer needs to be in nocache area, otherwise it needs to be aligned to CONFIG_USB_ALIGN_SIZE area.
  153. usbh_serial_read
  154. """"""""""""""""""""""""""""""""""""
  155. ``usbh_serial_read`` reads data from the serial port. **If baud rate is not set, this API is not allowed to be used. After setting baud rate, rx reception will be enabled internally and data will be written to ringbuf**.
  156. .. code-block:: C
  157. int usbh_serial_read(struct usbh_serial *serial, void *buffer, uint32_t buflen);
  158. - **serial** serial structure handle
  159. - **buffer** data buffer pointer
  160. - **buflen** maximum length of data to read
  161. - **return** actual length of data read or error code
  162. .. note:: Since ringbuffer is used internally, there are no restrictions on user buffer attributes.
  163. usbh_serial_cdc_write_async
  164. """"""""""""""""""""""""""""""""""""
  165. ``usbh_serial_cdc_write_async`` asynchronously writes data to the serial port. **If baud rate is set, this API is not allowed to be used**.
  166. .. code-block:: C
  167. int usbh_serial_cdc_write_async(struct usbh_serial *serial, uint8_t *buffer, uint32_t buflen, usbh_complete_callback_t complete, void *arg);
  168. - **serial** serial structure handle
  169. - **buffer** data buffer pointer
  170. - **buflen** length of data to send
  171. - **complete** data write completion callback function
  172. - **arg** callback function parameter
  173. - **return** 0 indicates normal, other values indicate error
  174. .. note:: If CONFIG_USB_DCACHE_ENABLE is not enabled, buffer needs to be in nocache area, otherwise it needs to be aligned to CONFIG_USB_ALIGN_SIZE area.
  175. usbh_serial_cdc_read_async
  176. """"""""""""""""""""""""""""""""""""
  177. ``usbh_serial_cdc_read_async`` asynchronously reads data from the serial port. **If baud rate is set, this API is not allowed to be used. After setting baud rate, rx reception will be enabled internally and data will be written to ringbuf**.
  178. .. code-block:: C
  179. int usbh_serial_cdc_read_async(struct usbh_serial *serial, uint8_t *buffer, uint32_t buflen, usbh_complete_callback_t complete, void *arg);
  180. - **serial** serial structure handle
  181. - **buffer** data buffer pointer
  182. - **buflen** maximum length of data to read, up to 16K at a time. Must be a multiple of wMaxPacketSize
  183. - **complete** data read completion callback function
  184. - **arg** callback function parameter
  185. - **return** 0 indicates normal, other values indicate error
  186. .. note:: If CONFIG_USB_DCACHE_ENABLE is not enabled, buffer needs to be in nocache area, otherwise it needs to be aligned to CONFIG_USB_ALIGN_SIZE area.
  187. HID
  188. -----------------
  189. MSC
  190. -----------------
  191. usbh_msc_scsi_init
  192. """"""""""""""""""""""""""""""""""""
  193. ``usbh_msc_scsi_init`` initializes msc scsi device. Gets MSC status and capacity information.
  194. .. code-block:: C
  195. int usbh_msc_scsi_init(struct usbh_msc *msc_class);
  196. - **msc_class** msc structure handle
  197. - **return** 0 indicates normal, other values indicate error
  198. usbh_msc_scsi_write10
  199. """"""""""""""""""""""""""""""""""""
  200. ``usbh_msc_scsi_write10`` writes data to msc device.
  201. .. code-block:: C
  202. int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors);
  203. - **msc_class** msc structure handle
  204. - **start_sector** starting sector
  205. - **buffer** data buffer pointer
  206. - **nsectors** number of sectors to write
  207. - **return** returns 0 for normal, other values indicate error
  208. usbh_msc_scsi_read10
  209. """"""""""""""""""""""""""""""""""""
  210. ``usbh_msc_scsi_read10`` reads data from msc device.
  211. .. code-block:: C
  212. int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, uint8_t *buffer, uint32_t nsectors);
  213. - **msc_class** msc structure handle
  214. - **start_sector** starting sector
  215. - **buffer** data buffer pointer
  216. - **nsectors** number of sectors to read
  217. - **return** returns 0 for normal, other values indicate error
  218. NETWORK
  219. -----------------
  220. Already integrated with lwIP protocol stack or other network protocol stacks, use socket API.