usb_dc.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * USB endpoint direction and number.
  31. */
  32. #define USB_EP_DIR_MASK 0x80U
  33. #define USB_EP_DIR_IN 0x80U
  34. #define USB_EP_DIR_OUT 0x00U
  35. /** Get endpoint index (number) from endpoint address */
  36. #define USB_EP_GET_IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
  37. /** Get direction from endpoint address */
  38. #define USB_EP_GET_DIR(ep) ((ep)&USB_EP_DIR_MASK)
  39. /** Get endpoint address from endpoint index and direction */
  40. #define USB_EP_GET_ADDR(idx, dir) ((idx) | ((dir)&USB_EP_DIR_MASK))
  41. /** True if the endpoint is an IN endpoint */
  42. #define USB_EP_DIR_IS_IN(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_IN)
  43. /** True if the endpoint is an OUT endpoint */
  44. #define USB_EP_DIR_IS_OUT(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_OUT)
  45. /* Default USB control EP, always 0 and 0x80 */
  46. #define USB_CONTROL_OUT_EP0 0
  47. #define USB_CONTROL_IN_EP0 0x80
  48. /**< maximum packet size (MPS) for EP 0 */
  49. #define USB_CTRL_EP_MPS 64
  50. /**
  51. * USB endpoint Transfer Type mask.
  52. */
  53. #define USBD_EP_TYPE_CTRL 0
  54. #define USBD_EP_TYPE_ISOC 1
  55. #define USBD_EP_TYPE_BULK 2
  56. #define USBD_EP_TYPE_INTR 3
  57. #define USBD_EP_TYPE_MASK 3
  58. /**
  59. * @brief USB Endpoint Configuration.
  60. *
  61. * Structure containing the USB endpoint configuration.
  62. */
  63. struct usbd_endpoint_cfg {
  64. /** The number associated with the EP in the device
  65. * configuration structure
  66. * IN EP = 0x80 | \<endpoint number\>
  67. * OUT EP = 0x00 | \<endpoint number\>
  68. */
  69. uint8_t ep_addr;
  70. /** Endpoint Transfer Type.
  71. * May be Bulk, Interrupt, Control or Isochronous
  72. */
  73. uint8_t ep_type;
  74. /** Endpoint max packet size */
  75. uint16_t ep_mps;
  76. };
  77. /**
  78. * @brief USB Device Core Layer API
  79. * @defgroup _usb_device_core_api USB Device Core API
  80. * @{
  81. */
  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
  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 is similar to usb_dc_ep_read, the difference being that, it doesn't
  165. * clear the endpoint NAKs so that the consumer is not bogged down by further
  166. * upcalls till he is done with the processing of the data. The caller should
  167. * reactivate ep by setting max_data_len 0 do so.
  168. *
  169. * @param[in] ep Endpoint address corresponding to the one
  170. * listed in the device configuration table
  171. * @param[in] data Pointer to data buffer to write to
  172. * @param[in] max_data_len Max length of data to read
  173. * @param[out] read_bytes Number of bytes read. If data is NULL and
  174. * max_data_len is 0 the number of bytes
  175. * available for read should be returned.
  176. *
  177. * @return 0 on success, negative errno code on fail.
  178. */
  179. int usbd_ep_read(const uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *read_bytes);
  180. /**
  181. * @}
  182. */
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif