api_port.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. Host and Device Drivers
  2. =======================================
  3. .. note:: Please note that starting from version v1.1, the busid parameter has been added, while everything else remains unchanged, so API documentation is not updated
  4. device controller(dcd)
  5. -------------------------
  6. usb_dc_init
  7. """"""""""""""""""""""""""""""""""""
  8. ``usb_dc_init`` is used to initialize USB device controller registers, set USB pins, clock, interrupts, etc. **This function is not open to users**.
  9. .. code-block:: C
  10. int usb_dc_init(void);
  11. - **return** Returns 0 for success, other values indicate error
  12. usb_dc_deinit
  13. """"""""""""""""""""""""""""""""""""
  14. ``usb_dc_deinit`` is used to de-initialize USB device controller registers. **This function is not open to users**.
  15. .. code-block:: C
  16. int usb_dc_deinit(void);
  17. - **return** Returns 0 for success, other values indicate error
  18. usbd_set_address
  19. """"""""""""""""""""""""""""""""""""
  20. ``usbd_set_address`` sets the device address. **This function is not open to users**.
  21. .. code-block:: C
  22. int usbd_set_address(const uint8_t addr);
  23. - **addr** Device address
  24. - **return** Returns 0 for success, other values indicate error
  25. usbd_ep_open
  26. """"""""""""""""""""""""""""""""""""
  27. ``usbd_ep_open`` sets endpoint properties and enables corresponding endpoint interrupts. **This function is not open to users**.
  28. .. code-block:: C
  29. int usbd_ep_open(const struct usb_endpoint_descriptor *ep);
  30. - **ep** Endpoint descriptor
  31. - **return** Returns 0 for success, other values indicate error
  32. usbd_ep_close
  33. """"""""""""""""""""""""""""""""""""
  34. ``usbd_ep_close`` closes an endpoint. **This function is not open to users**.
  35. .. code-block:: C
  36. int usbd_ep_close(const uint8_t ep);
  37. - **ep** Endpoint address
  38. - **return** Returns 0 for success, other values indicate error
  39. usbd_ep_set_stall
  40. """"""""""""""""""""""""""""""""""""
  41. ``usbd_ep_set_stall`` sets an endpoint to stall state and sends a stall handshake packet. **This function is open to users**.
  42. .. code-block:: C
  43. int usbd_ep_set_stall(const uint8_t ep);
  44. - **ep** Endpoint address
  45. - **return** Returns 0 for success, other values indicate error
  46. usbd_ep_clear_stall
  47. """"""""""""""""""""""""""""""""""""
  48. ``usbd_ep_clear_stall`` clears the stall state of an endpoint. **This function is not open to users**.
  49. .. code-block:: C
  50. int usbd_ep_clear_stall(const uint8_t ep);
  51. - **ep** Endpoint address
  52. - **return** Returns 0 for success, other values indicate error
  53. usbd_ep_is_stalled
  54. """"""""""""""""""""""""""""""""""""
  55. ``usbd_ep_is_stalled`` reads the current stall state of an endpoint. **This function is not open to users**.
  56. .. code-block:: C
  57. int usbd_ep_is_stalled(const uint8_t ep, uint8_t *stalled);
  58. - **ep** Endpoint address
  59. - **return** Returns 1 for stalled, 0 for not stalled
  60. usbd_ep_start_write
  61. """"""""""""""""""""""""""""""""""""
  62. ``usbd_ep_start_write`` starts endpoint transmission. After transmission completion, it will call the registered IN endpoint transfer completion interrupt callback function. This function performs asynchronous transmission. **This function is open to users**.
  63. .. code-block:: C
  64. int usbd_ep_start_write(const uint8_t ep, const uint8_t *data, uint32_t data_len);
  65. - **ep** IN endpoint address
  66. - **data** Transmission data buffer
  67. - **data_len** Transmission length, theoretically unlimited, recommended within 16K bytes
  68. - **return** Returns 0 for success, other values indicate error
  69. usbd_ep_start_read
  70. """"""""""""""""""""""""""""""""""""
  71. ``usbd_ep_start_read`` starts endpoint reception. After reception completion, it will call the registered OUT endpoint transfer completion interrupt callback function. This function performs asynchronous reception. **This function is open to users**.
  72. .. code-block:: C
  73. int usbd_ep_start_read(const uint8_t ep, uint8_t *data, uint32_t data_len);
  74. - **ep** OUT endpoint address
  75. - **data** Reception data buffer
  76. - **data_len** Reception length, theoretically unlimited, recommended within 16K bytes, and preferably a multiple of maximum packet size
  77. - **return** Returns 0 for success, other values indicate error
  78. .. note:: After starting reception, transfer completion interrupt will be triggered under two conditions: 1. Last packet is a short packet (less than EP MPS); 2. Total received length equals data_len
  79. .. note:: For bulk transfers, data_len is usually designed as EP MPS. The following three cases can be modified to multiple EP MPS: fixed length; custom protocol with length information (MSC); host manually sends ZLP or short packet (RNDIS)
  80. host controller(hcd)
  81. ------------------------
  82. usb_hc_init
  83. """"""""""""""""""""""""""""""""""""
  84. ``usb_hc_init`` is used to initialize USB host controller registers, set USB pins, clock, interrupts, etc. **This function is not open to users**.
  85. .. code-block:: C
  86. int usb_hc_init(void);
  87. - **return** Returns 0 for success, other values indicate error
  88. usb_hc_deinit
  89. """"""""""""""""""""""""""""""""""""
  90. ``usb_hc_deinit`` is used to de-initialize USB host controller registers. **This function is not open to users**.
  91. .. code-block:: C
  92. int usb_hc_deinit(void);
  93. - **return** Returns 0 for success, other values indicate error
  94. usbh_roothub_control
  95. """"""""""""""""""""""""""""""""""""
  96. ``usbh_roothub_control`` is used to send requests to the root hub. **This function is not open to users**.
  97. .. code-block:: C
  98. int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf);
  99. - **setup** Request
  100. - **buf** Reception buffer
  101. - **return** Returns 0 for success, other values indicate error
  102. usbh_submit_urb
  103. """"""""""""""""""""""""""""""""""""
  104. ``usbh_submit_urb`` performs data requests to endpoints at a specific address. **This function is open to users**.
  105. .. code-block:: C
  106. int usbh_submit_urb(struct usbh_urb *urb);
  107. - **urb** USB request block
  108. - **return** Returns 0 for success, other values indicate error
  109. Among them, the `urb` structure information is as follows:
  110. .. code-block:: C
  111. struct usbh_urb {
  112. usb_slist_t list;
  113. void *hcpriv;
  114. struct usbh_hubport *hport;
  115. struct usb_endpoint_descriptor *ep;
  116. uint8_t data_toggle;
  117. uint8_t interval;
  118. struct usb_setup_packet *setup;
  119. uint8_t *transfer_buffer;
  120. uint32_t transfer_buffer_length;
  121. int transfer_flags;
  122. uint32_t actual_length;
  123. uint32_t timeout;
  124. int errorcode;
  125. uint32_t num_of_iso_packets;
  126. uint32_t start_frame;
  127. usbh_complete_callback_t complete;
  128. void *arg;
  129. #if defined(__ICCARM__) || defined(__ICCRISCV__) || defined(__ICCRX__)
  130. struct usbh_iso_frame_packet *iso_packet;
  131. #else
  132. struct usbh_iso_frame_packet iso_packet[0];
  133. #endif
  134. };
  135. - **hcpriv** Host controller driver private member
  136. - **hport** The hport used by current URB
  137. - **ep** The endpoint used by current URB
  138. - **data_toggle** Current data toggle
  139. - **interval** URB transfer interval in microseconds. If interval is greater than 1000us, software timer needs to be used for maintenance
  140. - **setup** Setup request buffer, used by endpoint 0
  141. - **transfer_buffer** Transfer data buffer
  142. - **transfer_buffer_length** Transfer length
  143. - **transfer_flags** Flags carried during transfer
  144. - **actual_length** Actual transfer length
  145. - **timeout** Transfer timeout. If 0, the function is non-blocking and can be used in interrupts
  146. - **errorcode** Error code
  147. - **num_of_iso_packets** Number of ISO frames or microframes
  148. - **complete** Transfer completion callback function
  149. - **arg** Parameters carried when transfer completes
  150. - **iso_packet** ISO data packet
  151. .. note:: If there are no special time requirements for timeout, it must be set to 0xffffffff. In principle, timeout is not allowed. If timeout occurs, generally cannot continue working
  152. `errorcode` can return the following values:
  153. .. code-block:: C
  154. #define USB_ERR_NOMEM 1
  155. #define USB_ERR_INVAL 2
  156. #define USB_ERR_NODEV 3
  157. #define USB_ERR_NOTCONN 4
  158. #define USB_ERR_NOTSUPP 5
  159. #define USB_ERR_BUSY 6
  160. #define USB_ERR_RANGE 7
  161. #define USB_ERR_STALL 8
  162. #define USB_ERR_BABBLE 9
  163. #define USB_ERR_NAK 10
  164. #define USB_ERR_DT 11
  165. #define USB_ERR_IO 12
  166. #define USB_ERR_SHUTDOWN 13
  167. #define USB_ERR_TIMEOUT 14
  168. Among them, the `iso_packet` structure information is as follows:
  169. .. code-block:: C
  170. struct usbh_iso_frame_packet {
  171. uint8_t *transfer_buffer;
  172. uint32_t transfer_buffer_length;
  173. uint32_t actual_length;
  174. int errorcode;
  175. };
  176. - **transfer_buffer** Transfer data buffer
  177. - **transfer_buffer_length** Transfer length
  178. - **actual_length** Actual transfer length
  179. - **errorcode** Error code