usb_dc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 Get USB device speed
  32. *
  33. * @param[in] busid bus index
  34. *
  35. * @return port speed, USB_SPEED_LOW or USB_SPEED_FULL or USB_SPEED_HIGH
  36. */
  37. uint8_t usbd_get_port_speed(uint8_t busid);
  38. /**
  39. * @brief configure and enable endpoint.
  40. *
  41. * @param [in] ep_cfg Endpoint config.
  42. *
  43. * @return On success will return 0, and others indicate fail.
  44. */
  45. int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep);
  46. /**
  47. * @brief Disable the selected endpoint
  48. *
  49. * @param[in] ep Endpoint address
  50. *
  51. * @return On success will return 0, and others indicate fail.
  52. */
  53. int usbd_ep_close(uint8_t busid, const uint8_t ep);
  54. /**
  55. * @brief Set stall condition for the selected endpoint
  56. *
  57. * @param[in] ep Endpoint address
  58. *
  59. *
  60. * @return On success will return 0, and others indicate fail.
  61. */
  62. int usbd_ep_set_stall(uint8_t busid, const uint8_t ep);
  63. /**
  64. * @brief Clear stall condition for the selected endpoint
  65. *
  66. * @param[in] ep Endpoint address corresponding to the one
  67. * listed in the device configuration table
  68. *
  69. * @return On success will return 0, and others indicate fail.
  70. */
  71. int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep);
  72. /**
  73. * @brief Check if the selected endpoint is stalled
  74. *
  75. * @param[in] ep Endpoint address
  76. *
  77. * @param[out] stalled Endpoint stall status
  78. *
  79. * @return On success will return 0, and others indicate fail.
  80. */
  81. int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled);
  82. /**
  83. * @brief Setup in ep transfer setting and start transfer.
  84. *
  85. * This function is asynchronous.
  86. * This function is similar to uart with tx dma.
  87. *
  88. * This function is called to write data to the specified endpoint. The
  89. * supplied usbd_endpoint_callback function will be called when data is transmitted
  90. * out.
  91. *
  92. * @param[in] ep Endpoint address corresponding to the one
  93. * listed in the device configuration table
  94. * @param[in] data Pointer to data to write
  95. * @param[in] data_len Length of the data requested to write. This may
  96. * be zero for a zero length status packet.
  97. * @return 0 on success, negative errno code on fail.
  98. */
  99. int usbd_ep_start_write(uint8_t busid, const uint8_t ep, const uint8_t *data, uint32_t data_len);
  100. /**
  101. * @brief Setup out ep transfer setting and start transfer.
  102. *
  103. * This function is asynchronous.
  104. * This function is similar to uart with rx dma.
  105. *
  106. * This function is called to read data to the specified endpoint. The
  107. * supplied usbd_endpoint_callback function will be called when data is received
  108. * in.
  109. *
  110. * @param[in] ep Endpoint address corresponding to the one
  111. * listed in the device configuration table
  112. * @param[in] data Pointer to data to read
  113. * @param[in] data_len Max length of the data requested to read.
  114. *
  115. * @return 0 on success, negative errno code on fail.
  116. */
  117. int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t data_len);
  118. /* usb dcd irq callback */
  119. /**
  120. * @brief Usb connect irq callback.
  121. */
  122. void usbd_event_connect_handler(uint8_t busid);
  123. /**
  124. * @brief Usb disconnect irq callback.
  125. */
  126. void usbd_event_disconnect_handler(uint8_t busid);
  127. /**
  128. * @brief Usb resume irq callback.
  129. */
  130. void usbd_event_resume_handler(uint8_t busid);
  131. /**
  132. * @brief Usb suspend irq callback.
  133. */
  134. void usbd_event_suspend_handler(uint8_t busid);
  135. /**
  136. * @brief Usb reset irq callback.
  137. */
  138. void usbd_event_reset_handler(uint8_t busid);
  139. /**
  140. * @brief Usb setup packet recv irq callback.
  141. * @param[in] psetup setup packet.
  142. */
  143. void usbd_event_ep0_setup_complete_handler(uint8_t busid, uint8_t *psetup);
  144. /**
  145. * @brief In ep transfer complete irq callback.
  146. * @param[in] ep Endpoint address corresponding to the one
  147. * listed in the device configuration table
  148. * @param[in] nbytes How many nbytes have transferred.
  149. */
  150. void usbd_event_ep_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes);
  151. /**
  152. * @brief Out ep transfer complete irq callback.
  153. * @param[in] ep Endpoint address corresponding to the one
  154. * listed in the device configuration table
  155. * @param[in] nbytes How many nbytes have transferred.
  156. */
  157. void usbd_event_ep_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes);
  158. #ifdef CONFIG_USBDEV_TEST_MODE
  159. /**
  160. * @brief Usb execute test mode
  161. * @param[in] busid device busid
  162. * @param[in] test_mode usb test mode
  163. */
  164. void usbd_execute_test_mode(uint8_t busid, uint8_t test_mode);
  165. #endif
  166. #ifdef __cplusplus
  167. }
  168. #endif
  169. #endif /* USB_DC_H */