usb_dc.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USB_DC_H
  7. #define USB_DC_H
  8. #include <stdint.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /**
  13. * @brief init device controller registers.
  14. * @return On success will return 0, and others indicate fail.
  15. */
  16. int usb_dc_init(uint8_t busid);
  17. /**
  18. * @brief deinit device controller registers.
  19. * @return On success will return 0, and others indicate fail.
  20. */
  21. int usb_dc_deinit(uint8_t busid);
  22. /**
  23. * @brief Set USB device address
  24. *
  25. * @param[in] addr Device address
  26. *
  27. * @return On success will return 0, and others indicate fail.
  28. */
  29. int usbd_set_address(uint8_t busid, const uint8_t addr);
  30. /**
  31. * @brief Set remote wakeup feature
  32. *
  33. * @return On success will return 0, and others indicate fail.
  34. */
  35. int usbd_set_remote_wakeup(uint8_t busid);
  36. /**
  37. * @brief Get USB device speed
  38. *
  39. * @param[in] busid bus index
  40. *
  41. * @return port speed, USB_SPEED_LOW or USB_SPEED_FULL or USB_SPEED_HIGH
  42. */
  43. uint8_t usbd_get_port_speed(uint8_t busid);
  44. /**
  45. * @brief configure and enable endpoint.
  46. *
  47. * @param [in] ep_cfg Endpoint config.
  48. *
  49. * @return On success will return 0, and others indicate fail.
  50. */
  51. int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep);
  52. /**
  53. * @brief Disable the selected endpoint
  54. *
  55. * @param[in] ep Endpoint address
  56. *
  57. * @return On success will return 0, and others indicate fail.
  58. */
  59. int usbd_ep_close(uint8_t busid, const uint8_t ep);
  60. /**
  61. * @brief Set stall condition for the selected endpoint
  62. *
  63. * @param[in] ep Endpoint address
  64. *
  65. *
  66. * @return On success will return 0, and others indicate fail.
  67. */
  68. int usbd_ep_set_stall(uint8_t busid, const uint8_t ep);
  69. /**
  70. * @brief Clear stall condition for the selected endpoint
  71. *
  72. * @param[in] ep Endpoint address corresponding to the one
  73. * listed in the device configuration table
  74. *
  75. * @return On success will return 0, and others indicate fail.
  76. */
  77. int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep);
  78. /**
  79. * @brief Check if the selected endpoint is stalled
  80. *
  81. * @param[in] ep Endpoint address
  82. *
  83. * @param[out] stalled Endpoint stall status
  84. *
  85. * @return On success will return 0, and others indicate fail.
  86. */
  87. int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled);
  88. /**
  89. * @brief Setup in ep transfer setting and start transfer.
  90. *
  91. * This function is asynchronous.
  92. * This function is similar to uart with tx dma.
  93. *
  94. * This function is called to write data to the specified endpoint. The
  95. * supplied usbd_endpoint_callback function will be called when data is transmitted
  96. * out.
  97. *
  98. * @param[in] ep Endpoint address corresponding to the one
  99. * listed in the device configuration table
  100. * @param[in] data Pointer to data to write
  101. * @param[in] data_len Length of the data requested to write. This may
  102. * be zero for a zero length status packet.
  103. * @return 0 on success, negative errno code on fail.
  104. */
  105. int usbd_ep_start_write(uint8_t busid, const uint8_t ep, const uint8_t *data, uint32_t data_len);
  106. /**
  107. * @brief Setup out ep transfer setting and start transfer.
  108. *
  109. * This function is asynchronous.
  110. * This function is similar to uart with rx dma.
  111. *
  112. * This function is called to read data to the specified endpoint. The
  113. * supplied usbd_endpoint_callback function will be called when data is received
  114. * in.
  115. *
  116. * @param[in] ep Endpoint address corresponding to the one
  117. * listed in the device configuration table
  118. * @param[in] data Pointer to data to read
  119. * @param[in] data_len Max length of the data requested to read.
  120. *
  121. * @return 0 on success, negative errno code on fail.
  122. */
  123. int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t data_len);
  124. /* usb dcd irq callback, called by user */
  125. /**
  126. * @brief Usb connect irq callback.
  127. */
  128. void usbd_event_connect_handler(uint8_t busid);
  129. /**
  130. * @brief Usb disconnect irq callback.
  131. */
  132. void usbd_event_disconnect_handler(uint8_t busid);
  133. /**
  134. * @brief Usb resume irq callback.
  135. */
  136. void usbd_event_resume_handler(uint8_t busid);
  137. /**
  138. * @brief Usb suspend irq callback.
  139. */
  140. void usbd_event_suspend_handler(uint8_t busid);
  141. /**
  142. * @brief Usb reset irq callback.
  143. */
  144. void usbd_event_reset_handler(uint8_t busid);
  145. /**
  146. * @brief Usb setup packet recv irq callback.
  147. * @param[in] psetup setup packet.
  148. */
  149. void usbd_event_ep0_setup_complete_handler(uint8_t busid, uint8_t *psetup);
  150. /**
  151. * @brief In ep transfer complete irq callback.
  152. * @param[in] ep Endpoint address corresponding to the one
  153. * listed in the device configuration table
  154. * @param[in] nbytes How many nbytes have transferred.
  155. */
  156. void usbd_event_ep_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes);
  157. /**
  158. * @brief Out ep transfer complete irq callback.
  159. * @param[in] ep Endpoint address corresponding to the one
  160. * listed in the device configuration table
  161. * @param[in] nbytes How many nbytes have transferred.
  162. */
  163. void usbd_event_ep_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes);
  164. #ifdef CONFIG_USBDEV_TEST_MODE
  165. /**
  166. * @brief Usb execute test mode
  167. * @param[in] busid device busid
  168. * @param[in] test_mode usb test mode
  169. */
  170. void usbd_execute_test_mode(uint8_t busid, uint8_t test_mode);
  171. #endif
  172. /* called by user */
  173. void USBD_IRQHandler(uint8_t busid);
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif /* USB_DC_H */