usb_dc.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @file usb_dc.h
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #ifndef _USB_DC_H
  24. #define _USB_DC_H
  25. #include <stdint.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. * @brief USB Endpoint Configuration.
  31. *
  32. * Structure containing the USB endpoint configuration.
  33. */
  34. struct usbd_endpoint_cfg {
  35. /** The number associated with the EP in the device
  36. * configuration structure
  37. * IN EP = 0x80 | \<endpoint number\>
  38. * OUT EP = 0x00 | \<endpoint number\>
  39. */
  40. uint8_t ep_addr;
  41. /** Endpoint Transfer Type.
  42. * May be Bulk, Interrupt, Control or Isochronous
  43. */
  44. uint8_t ep_type;
  45. /** Endpoint max packet size */
  46. uint16_t ep_mps;
  47. };
  48. /**
  49. * @brief USB Device Core Layer API
  50. * @defgroup _usb_device_core_api USB Device Core API
  51. * @{
  52. */
  53. /**
  54. * @brief init device controller registers.
  55. * @return 0 on success, negative errno code on fail.
  56. */
  57. int usb_dc_init(void);
  58. /**
  59. * @brief deinit device controller registers.
  60. * @return 0 on success, negative errno code on fail.
  61. */
  62. int usb_dc_deinit(void);
  63. /**
  64. * @brief Attach USB for device connection
  65. *
  66. * Function to attach USB for device connection. Upon success, the USB PLL
  67. * is enabled, and the USB device is now capable of transmitting and receiving
  68. * on the USB bus and of generating interrupts.
  69. *
  70. * @return 0 on success, negative errno code on fail.
  71. */
  72. int usb_dc_attach(void);
  73. /**
  74. * @brief Detach the USB device
  75. *
  76. * Function to detach the USB device. Upon success, the USB hardware PLL
  77. * is powered down and USB communication is disabled.
  78. *
  79. * @return 0 on success, negative errno code on fail.
  80. */
  81. int usb_dc_detach(void);
  82. /**
  83. * @brief Set USB device address
  84. *
  85. * @param[in] addr Device address
  86. *
  87. * @return 0 on success, negative errno code on fail.
  88. */
  89. int usbd_set_address(const uint8_t addr);
  90. /**
  91. * @brief configure and enable endpoint.
  92. *
  93. * This function sets endpoint configuration according to one specified in USB.
  94. * endpoint descriptor and then enables it for data transfers.
  95. *
  96. * @param [in] ep_desc Endpoint descriptor byte array.
  97. *
  98. * @return true if successfully configured and enabled.
  99. */
  100. int usbd_ep_open(const struct usbd_endpoint_cfg *ep_cfg);
  101. /**
  102. * @brief Disable the selected endpoint
  103. *
  104. * Function to disable the selected endpoint. Upon success interrupts are
  105. * disabled for the corresponding endpoint and the endpoint is no longer able
  106. * for transmitting/receiving data.
  107. *
  108. * @param[in] ep Endpoint address corresponding to the one
  109. * listed in the device configuration table
  110. *
  111. * @return 0 on success, negative errno code on fail.
  112. */
  113. int usbd_ep_close(const uint8_t ep);
  114. /**
  115. * @brief Set stall condition for the selected endpoint
  116. *
  117. * @param[in] ep Endpoint address corresponding to the one
  118. * listed in the device configuration table
  119. *
  120. * @return 0 on success, negative errno code on fail.
  121. */
  122. int usbd_ep_set_stall(const uint8_t ep);
  123. /**
  124. * @brief Clear stall condition for the selected endpoint
  125. *
  126. * @param[in] ep Endpoint address corresponding to the one
  127. * listed in the device configuration table
  128. *
  129. * @return 0 on success, negative errno code on fail.
  130. */
  131. int usbd_ep_clear_stall(const uint8_t ep);
  132. /**
  133. * @brief Check if the selected endpoint is stalled
  134. *
  135. * @param[in] ep Endpoint address corresponding to the one
  136. * listed in the device configuration table
  137. * @param[out] stalled Endpoint stall status
  138. *
  139. * @return 0 on success, negative errno code on fail.
  140. */
  141. int usbd_ep_is_stalled(const uint8_t ep, uint8_t *stalled);
  142. /**
  143. * @brief Write data to the specified endpoint with poll mode.
  144. *
  145. * This function is called to write data to the specified endpoint. The
  146. * supplied usbd_endpoint_callback function will be called when data is transmitted
  147. * out.
  148. *
  149. * @param[in] ep Endpoint address corresponding to the one
  150. * listed in the device configuration table
  151. * @param[in] data Pointer to data to write
  152. * @param[in] data_len Length of the data requested to write. This may
  153. * be zero for a zero length status packet.
  154. * @param[out] ret_bytes Bytes scheduled for transmission. This value
  155. * may be NULL if the application expects all
  156. * bytes to be written
  157. *
  158. * @return 0 on success, negative errno code on fail.
  159. */
  160. int usbd_ep_write(const uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t *ret_bytes);
  161. /**
  162. * @brief Read data from the specified endpoint
  163. *
  164. * This function is called by the endpoint handler function, after an OUT
  165. * interrupt has been received for that EP. The application must only call this
  166. * function through the supplied usbd_ep_callback function. This function clears
  167. * the ENDPOINT NAK when max_data_len is 0, if all data in the endpoint FIFO has been read,
  168. * so as to accept more data from host.
  169. *
  170. * @param[in] ep Endpoint address corresponding to the one
  171. * listed in the device configuration table
  172. * @param[in] data Pointer to data buffer to write to
  173. * @param[in] max_data_len Max length of data to read
  174. * @param[out] read_bytes Number of bytes read. If data is NULL and
  175. * max_data_len is 0 the number of bytes
  176. * available for read should be returned.
  177. *
  178. * @return 0 on success, negative errno code on fail.
  179. */
  180. int usbd_ep_read(const uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *read_bytes);
  181. /**
  182. * @brief Write data to the specified endpoint with async mode.
  183. *
  184. * @param[in] ep Endpoint address corresponding to the one
  185. * listed in the device configuration table
  186. * @param[in] data Pointer to data to write
  187. * @param[in] data_len Length of the data requested to write. This may
  188. * be zero for a zero length status packet.
  189. *
  190. * @return 0 on success, negative errno code on fail.
  191. */
  192. int usbd_ep_write_async(const uint8_t ep, const uint8_t *data, uint32_t data_len);
  193. /**
  194. * @brief Read data from the specified endpoint with async mode.Actually,this function is used for these endpoint transferring with dma mode.
  195. *
  196. * @param[in] ep Endpoint address corresponding to the one
  197. * listed in the device configuration table
  198. * @param[in] data Pointer to data buffer to write to
  199. * @param[in] data_len Max length of data to read
  200. *
  201. * @return 0 on success, negative errno code on fail.
  202. */
  203. int usbd_ep_read_async(const uint8_t ep, uint8_t *data, uint32_t data_len);
  204. /**
  205. * @brief Get actual read len when ep transfers completely by usbd_ep_read_async.
  206. *
  207. * @param[in] ep Endpoint address corresponding to the one
  208. * listed in the device configuration table
  209. * @return Actual read len.
  210. */
  211. uint32_t usbd_ep_get_read_len(const uint8_t ep);
  212. /**
  213. * @brief Get endpoint max packet size.
  214. *
  215. * @param[in] ep Endpoint address corresponding to the one
  216. * listed in the device configuration table
  217. * @return endpoint max packet size.
  218. */
  219. uint16_t usbd_ep_get_mps(const uint8_t ep);
  220. /**
  221. * @}
  222. */
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif