api_device.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. Device Protocol Stack
  2. =======================================
  3. The device protocol stack is mainly responsible for enumeration and driver loading. We won't discuss enumeration here, but for driver loading (i.e., interface driver loading), it mainly relies on the `usbd_add_interface` function to record the passed-in interface driver and save it to the interface array table. When the host makes class requests, it can search the interface table for access.
  4. After calling `usbd_desc_register`, interface registration and endpoint registration need to be performed according to the following rules:
  5. - Call `usbd_add_interface` as many times as there are interfaces, with parameters filling in the relevant `xxx_init_intf`. If not supported, manually create an intf and fill it in
  6. - Call `usbd_add_endpoint` as many times as there are endpoints. When interrupts complete, the registered endpoint callback will be called.
  7. Refer to the diagram below:
  8. .. figure:: img/api_device1.png
  9. CORE
  10. -----------------
  11. Endpoint Structure
  12. """"""""""""""""""""""""""""""""""""
  13. The endpoint structure is mainly used to register interrupt completion callback functions for different endpoint addresses.
  14. .. code-block:: C
  15. struct usbd_endpoint {
  16. uint8_t ep_addr;
  17. usbd_endpoint_callback ep_cb;
  18. };
  19. - **ep_addr** Endpoint address (with direction)
  20. - **ep_cb** Endpoint completion interrupt callback function.
  21. .. note:: To summarize in one sentence: in callback function is equivalent to DMA transmission completion interrupt callback function; out callback function is equivalent to DMA reception completion interrupt callback function
  22. Interface Structure
  23. """"""""""""""""""""""""""""""""""""
  24. The interface structure is mainly used to register requests other than standard device requests for different class devices, including class device requests, vendor device requests, and custom device requests, as well as related notification callback functions in the protocol stack.
  25. .. code-block:: C
  26. struct usbd_interface {
  27. usbd_request_handler class_interface_handler;
  28. usbd_request_handler class_endpoint_handler;
  29. usbd_request_handler vendor_handler;
  30. usbd_notify_handler notify_handler;
  31. const uint8_t *hid_report_descriptor;
  32. uint32_t hid_report_descriptor_len;
  33. uint8_t intf_num;
  34. };
  35. - **class_interface_handler** class setup request callback function, recipient is interface
  36. - **class_endpoint_handler** class setup request callback function, recipient is endpoint
  37. - **vendor_handler** vendor setup request callback function
  38. - **notify_handler** interrupt flag, protocol stack related status callback function
  39. - **hid_report_descriptor** hid report descriptor
  40. - **hid_report_descriptor_len** hid report descriptor length
  41. - **intf_num** current interface offset
  42. usbd_desc_register
  43. """"""""""""""""""""""""""""""""""""
  44. ``usbd_desc_register`` is used to register USB descriptors. Descriptor types include: device descriptor, configuration descriptor (including configuration descriptor, interface descriptor, class descriptor, endpoint descriptor), string descriptor, device qualifier descriptor, other speed descriptor, BOS descriptor, WinUSB descriptor.
  45. .. code-block:: C
  46. // Enable CONFIG_USBDEV_ADVANCE_DESC
  47. void usbd_desc_register(uint8_t busid, const struct usb_descriptor *desc);
  48. // Disable CONFIG_USBDEV_ADVANCE_DESC
  49. void usbd_desc_register(uint8_t busid, const uint8_t *desc);
  50. void usbd_msosv1_desc_register(uint8_t busid, struct usb_msosv1_descriptor *desc);
  51. void usbd_msosv2_desc_register(uint8_t busid, struct usb_msosv2_descriptor *desc);
  52. void usbd_bos_desc_register(uint8_t busid, struct usb_bos_descriptor *desc);
  53. void usbd_webusb_desc_register(uint8_t busid, struct usb_webusb_descriptor *desc);
  54. - **desc** Descriptor handle
  55. .. note:: Currently CONFIG_USBDEV_ADVANCE_DESC is enabled by default. If you need to use the old version API, please disable this macro. Starting from v1.6.0, only APIs with CONFIG_USBDEV_ADVANCE_DESC enabled are available
  56. usbd_add_interface
  57. """"""""""""""""""""""""""""""""""""
  58. ``usbd_add_interface`` adds an interface driver. **The addition order must follow the interface order in the descriptor**.
  59. .. code-block:: C
  60. void usbd_add_interface(uint8_t busid, struct usbd_interface *intf);
  61. - **busid** USB bus ID
  62. - **intf** Interface driver handle, usually obtained from different class `xxx_init_intf` functions
  63. usbd_add_endpoint
  64. """"""""""""""""""""""""""""""""""""
  65. ``usbd_add_endpoint`` adds an endpoint interrupt completion callback function.
  66. .. code-block:: C
  67. void usbd_add_endpoint(uint8_t busid, struct usbd_endpoint *ep);
  68. - **busid** USB bus ID
  69. - **ep** Endpoint handle
  70. usbd_initialize
  71. """"""""""""""""""""""""""""""""""""
  72. ``usbd_initialize`` is used to initialize USB device register configuration, USB clock, interrupts, etc. Note that this function must be called last after registering descriptor APIs. **If using an OS, it must be executed within a thread**.
  73. .. code-block:: C
  74. int usbd_initialize(uint8_t busid, uintptr_t reg_base, usbd_event_handler_t event_handler);
  75. - **busid** USB bus ID
  76. - **reg_base** USB device register base address
  77. - **event_handler** Protocol stack interrupt or status callback function, event events
  78. - **return** Returns 0 for success, other values indicate failure
  79. Event events include:
  80. .. code-block:: C
  81. USBD_EVENT_ERROR, /** USB error reported by the controller */
  82. USBD_EVENT_RESET, /** USB reset */
  83. USBD_EVENT_SOF, /** Start of Frame received */
  84. USBD_EVENT_CONNECTED, /** USB connected*/
  85. USBD_EVENT_DISCONNECTED, /** USB disconnected */
  86. USBD_EVENT_SUSPEND, /** USB connection suspended by the HOST */
  87. USBD_EVENT_RESUME, /** USB connection resumed by the HOST */
  88. /* USB DEVICE STATUS */
  89. USBD_EVENT_CONFIGURED, /** USB configuration done */
  90. USBD_EVENT_SET_INTERFACE, /** USB interface selected */
  91. USBD_EVENT_SET_REMOTE_WAKEUP, /** USB set remote wakeup */
  92. USBD_EVENT_CLR_REMOTE_WAKEUP, /** USB clear remote wakeup */
  93. USBD_EVENT_INIT, /** USB init done when call usbd_initialize */
  94. USBD_EVENT_DEINIT, /** USB deinit done when call usbd_deinitialize */
  95. USBD_EVENT_UNKNOWN
  96. .. note:: Most IPs do not support USBD_EVENT_CONNECTED and USBD_EVENT_DISCONNECTED events. Currently only HPM chips support them. For other chips, design your own VBUS detection circuit as an alternative
  97. usbd_deinitialize
  98. """"""""""""""""""""""""""""""""""""
  99. ``usbd_deinitialize`` is used to deinitialize USB device, turn off USB device clock, interrupts, etc.
  100. .. code-block:: C
  101. int usbd_deinitialize(uint8_t busid);
  102. - **busid** USB bus ID
  103. - **return** Returns 0 for success, other values indicate failure
  104. CDC ACM
  105. -----------------
  106. usbd_cdc_acm_init_intf
  107. """"""""""""""""""""""""""""""""""""
  108. ``usbd_cdc_acm_init_intf`` is used to initialize USB CDC ACM class interface and implement related functions for this interface.
  109. - ``cdc_acm_class_interface_request_handler`` is used to handle USB CDC ACM class Setup requests.
  110. - ``cdc_notify_handler`` is used to handle other USB CDC interrupt callback functions.
  111. .. code-block:: C
  112. struct usbd_interface *usbd_cdc_acm_init_intf(uint8_t busid, struct usbd_interface *intf);
  113. - **busid** USB bus ID
  114. - **return** Interface handle
  115. usbd_cdc_acm_set_line_coding
  116. """"""""""""""""""""""""""""""""""""
  117. ``usbd_cdc_acm_set_line_coding`` is used to configure the serial port. If only using USB without serial port, this interface does not need to be implemented by the user and can use the default.
  118. .. code-block:: C
  119. void usbd_cdc_acm_set_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding);
  120. - **busid** USB bus ID
  121. - **intf** Control interface number
  122. - **line_coding** Serial port configuration
  123. usbd_cdc_acm_get_line_coding
  124. """"""""""""""""""""""""""""""""""""
  125. ``usbd_cdc_acm_get_line_coding`` is used to get serial port configuration. If only using USB without serial port, this interface does not need to be implemented by the user and can use the default.
  126. .. code-block:: C
  127. void usbd_cdc_acm_get_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding);
  128. - **busid** USB bus ID
  129. - **intf** Control interface number
  130. - **line_coding** Serial port configuration
  131. usbd_cdc_acm_set_dtr
  132. """"""""""""""""""""""""""""""""""""
  133. ``usbd_cdc_acm_set_dtr`` is used to control serial port DTR. If only using USB without serial port, this interface does not need to be implemented by the user and can use the default.
  134. .. code-block:: C
  135. void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr);
  136. - **busid** USB bus ID
  137. - **intf** Control interface number
  138. - **dtr** dtr = 1 means pull low level, 0 means pull high level
  139. usbd_cdc_acm_set_rts
  140. """"""""""""""""""""""""""""""""""""
  141. ``usbd_cdc_acm_set_rts`` is used to control serial port RTS. If only using USB without serial port, this interface does not need to be implemented by the user and can use the default.
  142. .. code-block:: C
  143. void usbd_cdc_acm_set_rts(uint8_t busid, uint8_t intf, bool rts);
  144. - **busid** USB bus ID
  145. - **intf** Control interface number
  146. - **rts** rts = 1 means pull low level, 0 means pull high level
  147. CDC_ACM_DESCRIPTOR_INIT
  148. """"""""""""""""""""""""""""""""""""
  149. ``CDC_ACM_DESCRIPTOR_INIT`` configures the default CDC ACM required descriptors and parameters for user convenience. Total length is `CDC_ACM_DESCRIPTOR_LEN`.
  150. .. code-block:: C
  151. CDC_ACM_DESCRIPTOR_INIT(bFirstInterface, int_ep, out_ep, in_ep, str_idx);
  152. - **bFirstInterface** Indicates the offset of the first interface of this CDC ACM in all interfaces
  153. - **int_ep** Indicates interrupt endpoint address (with direction)
  154. - **out_ep** Indicates bulk out endpoint address (with direction)
  155. - **in_ep** Indicates bulk in endpoint address (with direction)
  156. - **str_idx** String ID corresponding to control interface
  157. HID
  158. -----------------
  159. usbd_hid_init_intf
  160. """"""""""""""""""""""""""""""""""""
  161. ``usbd_hid_init_intf`` is used to initialize USB HID class interface and implement related functions for this interface:
  162. - ``hid_class_interface_request_handler`` is used to handle USB HID class Setup requests.
  163. - ``hid_notify_handler`` is used to handle other USB HID interrupt callback functions.
  164. .. code-block:: C
  165. struct usbd_interface *usbd_hid_init_intf(uint8_t busid, struct usbd_interface *intf, const uint8_t *desc, uint32_t desc_len);
  166. - **busid** USB bus ID
  167. - **desc** Report descriptor
  168. - **desc_len** Report descriptor length
  169. MSC
  170. -----------------
  171. usbd_msc_init_intf
  172. """"""""""""""""""""""""""""""""""""
  173. ``usbd_msc_init_intf`` is used to initialize MSC class interface, implement related functions for this interface, and register endpoint callback functions. (Since MSC BOT protocol is fixed, user implementation is not needed, so endpoint callback functions naturally don't need user implementation).
  174. - ``msc_storage_class_interface_request_handler`` is used to handle USB MSC Setup interrupt requests.
  175. - ``msc_storage_notify_handler`` is used to implement other USB MSC interrupt callback functions.
  176. - ``mass_storage_bulk_out`` is used to handle USB MSC endpoint out interrupts.
  177. - ``mass_storage_bulk_in`` is used to handle USB MSC endpoint in interrupts.
  178. .. code-block:: C
  179. struct usbd_interface *usbd_msc_init_intf(uint8_t busid, struct usbd_interface *intf, const uint8_t out_ep, const uint8_t in_ep);
  180. - **busid** USB bus ID
  181. - **out_ep** out endpoint address
  182. - **in_ep** in endpoint address
  183. usbd_msc_get_cap
  184. """"""""""""""""""""""""""""""""""""
  185. ``usbd_msc_get_cap`` is used to get the LUN, number of sectors, and sector size of the storage device. Users must implement this function.
  186. .. code-block:: C
  187. void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint16_t *block_size);
  188. - **busid** USB bus ID
  189. - **lun** Storage logical unit, currently unused, defaults to supporting one
  190. - **block_num** Number of storage sectors
  191. - **block_size** Storage sector size
  192. usbd_msc_sector_read
  193. """"""""""""""""""""""""""""""""""""
  194. ``usbd_msc_sector_read`` is used to read data from a storage device starting at a specific sector address. Users must implement this function.
  195. .. code-block:: C
  196. int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length);
  197. - **busid** USB bus ID
  198. - **lun** Storage logical unit, currently unused, defaults to supporting one
  199. - **sector** Sector offset
  200. - **buffer** Pointer to store read data
  201. - **length** Read length
  202. usbd_msc_sector_write
  203. """"""""""""""""""""""""""""""""""""
  204. ``usbd_msc_sector_write`` is used to write data to a storage device starting at a specific sector. Users must implement this function.
  205. .. code-block:: C
  206. int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length);
  207. - **busid** USB bus ID
  208. - **lun** Storage logical unit, currently unused, defaults to supporting one
  209. - **sector** Sector offset
  210. - **buffer** Write data pointer
  211. - **length** Write length
  212. UAC
  213. -----------------
  214. usbd_audio_init_intf
  215. """"""""""""""""""""""""""""""""""""
  216. ``usbd_audio_init_intf`` is used to initialize USB Audio class interface and implement related functions for this interface:
  217. - ``audio_class_interface_request_handler`` is used to handle USB Audio Setup interface recipient interrupt requests.
  218. - ``audio_class_endpoint_request_handler`` is used to handle USB Audio Setup endpoint recipient interrupt requests.
  219. - ``audio_notify_handler`` is used to implement other USB Audio interrupt callback functions.
  220. .. code-block:: C
  221. struct usbd_interface *usbd_audio_init_intf(uint8_t busid, struct usbd_interface *intf,
  222. uint16_t uac_version,
  223. struct audio_entity_info *table,
  224. uint8_t num);
  225. - **busid** USB bus ID
  226. - **intf** Interface handle
  227. - **uac_version** Audio class version, UAC1.0 or UAC2.0
  228. - **table** Audio entity information table
  229. - **num** Audio entity information table length
  230. usbd_audio_open
  231. """"""""""""""""""""""""""""""""""""
  232. ``usbd_audio_open`` is used to start audio data transmission. Host sends start command callback function.
  233. .. code-block:: C
  234. void usbd_audio_open(uint8_t intf);
  235. - **intf** Interface number to open
  236. usbd_audio_close
  237. """"""""""""""""""""""""""""""""""""
  238. ``usbd_audio_close`` is used to stop audio data transmission. Host sends stop command callback function.
  239. .. code-block:: C
  240. void usbd_audio_close(uint8_t intf);
  241. - **intf** Interface number to close
  242. usbd_audio_set_mute
  243. """"""""""""""""""""""""""""""""""""
  244. ``usbd_audio_set_mute`` is used to set mute.
  245. .. code-block:: C
  246. void usbd_audio_set_mute(uint8_t busid, uint8_t ep, uint8_t ch, bool mute);
  247. - **busid** USB bus ID
  248. - **ep** Endpoint to set mute
  249. - **ch** Channel to set mute
  250. - **mute** 1 means mute, 0 means opposite
  251. usbd_audio_set_volume
  252. """"""""""""""""""""""""""""""""""""
  253. ``usbd_audio_set_volume`` is used to set volume.
  254. .. code-block:: C
  255. void usbd_audio_set_volume(uint8_t busid, uint8_t ep, uint8_t ch, int volume_db);
  256. - **busid** USB bus ID
  257. - **ep** Endpoint to set volume
  258. - **ch** Channel to set volume
  259. - **volume_db** Volume to set in decibels, range -100dB ~ 0dB
  260. usbd_audio_set_sampling_freq
  261. """"""""""""""""""""""""""""""""""""
  262. ``usbd_audio_set_sampling_freq`` is used to set the sampling rate of the audio module on the device
  263. .. code-block:: C
  264. void usbd_audio_set_sampling_freq(uint8_t busid, uint8_t ep, uint32_t sampling_freq);
  265. - **ep** Endpoint to set sampling rate
  266. - **sampling_freq** Sampling rate to set
  267. usbd_audio_get_sampling_freq_table
  268. """"""""""""""""""""""""""""""""""""
  269. ``usbd_audio_get_sampling_freq_table`` is used to get the list of supported sampling rates. If the function is not implemented, the default sampling rate list is used. UAC2 only.
  270. .. code-block:: C
  271. void usbd_audio_get_sampling_freq_table(uint8_t busid, uint8_t ep, uint8_t **sampling_freq_table);
  272. - **ep** Endpoint to get sampling rate
  273. - **sampling_freq_table** Sampling rate list address, format refers to default sampling rate list
  274. UVC
  275. -----------------
  276. usbd_video_init_intf
  277. """"""""""""""""""""""""""""""""""""
  278. ``usbd_video_init_intf`` is used to initialize USB Video class interface and implement related functions for this interface:
  279. - ``video_class_interface_request_handler`` is used to handle USB Video Setup interrupt requests.
  280. - ``video_notify_handler`` is used to implement other USB Video interrupt callback functions.
  281. .. code-block:: C
  282. struct usbd_interface *usbd_video_init_intf(uint8_t busid, struct usbd_interface *intf,
  283. uint32_t dwFrameInterval,
  284. uint32_t dwMaxVideoFrameSize,
  285. uint32_t dwMaxPayloadTransferSize);
  286. - **busid** USB bus ID
  287. - **intf** Interface handle
  288. - **dwFrameInterval** Video frame interval, unit 100ns
  289. - **dwMaxVideoFrameSize** Maximum video frame size
  290. - **dwMaxPayloadTransferSize** Maximum payload transfer size
  291. usbd_video_open
  292. """"""""""""""""""""""""""""""""""""
  293. ``usbd_video_open`` is used to start video data transmission.
  294. .. code-block:: C
  295. void usbd_video_open(uint8_t intf);
  296. - **intf** Interface number to open
  297. usbd_video_close
  298. """"""""""""""""""""""""""""""""""""
  299. ``usbd_video_close`` is used to stop video data transmission.
  300. .. code-block:: C
  301. void usbd_video_open(uint8_t intf);
  302. - **intf** Interface number to close
  303. usbd_video_stream_start_write
  304. """"""""""""""""""""""""""""""""""""
  305. ``usbd_video_stream_start_write`` is used to start sending one frame of video data stream. Must be used together with `usbd_video_stream_split_transfer`.
  306. .. code-block:: C
  307. int usbd_video_stream_start_write(uint8_t busid, uint8_t ep, uint8_t *ep_buf, uint8_t *stream_buf, uint32_t stream_len, bool do_copy);
  308. - **busid** USB bus ID
  309. - **ep** Video data endpoint address
  310. - **ep_buf** Video data endpoint transfer buffer
  311. - **stream_buf** One frame video data source buffer
  312. - **stream_len** One frame video data source buffer size
  313. - **do_copy** Whether to copy stream_buf data to ep_buf. This parameter is false only when stream_buf is in nocache area and DCACHE_ENABLE is not enabled
  314. usbd_video_stream_split_transfer
  315. """"""""""""""""""""""""""""""""""""
  316. ``usbd_video_stream_split_transfer`` is used to split video data stream transmission. Must be used together with `usbd_video_stream_start_write`.
  317. .. code-block:: C
  318. int usbd_video_stream_split_transfer(uint8_t busid, uint8_t ep);
  319. - **busid** USB bus ID
  320. - **ep** Video data endpoint address
  321. - **return** Returns true when one frame data transmission is complete, false when data transmission is not complete
  322. RNDIS
  323. -----------------
  324. CDC ECM
  325. -----------------
  326. MTP
  327. -----------------