usb_core.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*!
  2. \file usb_core.h
  3. \brief USB core driver header file
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #ifndef USB_CORE_H
  33. #define USB_CORE_H
  34. #include "usb_conf.h"
  35. #include "usb_regs.h"
  36. #include "usb_defines.h"
  37. /* constants definitions */
  38. #define USB_MAX_EP0_SIZE 64U /* endpoint 0 max packet size */
  39. #define RX_MAX_DATA_LENGTH 512U /* host rx buffer max data length */
  40. #define HC_MAX_PACKET_COUNT 140U /* host channel max packet count */
  41. #define USB_MAX_DEV_EPCOUNT USBFS_MAX_DEV_EPCOUNT
  42. #define USB_MAX_FIFOS (USBFS_MAX_HOST_CHANNELCOUNT * 2U - 1U)
  43. /* USB core status */
  44. typedef enum
  45. {
  46. USB_OK = 0, /* USB core OK status */
  47. USB_FAIL /* USB core fail status */
  48. }usb_status_enum;
  49. /* USB host channel status */
  50. typedef enum
  51. {
  52. HC_IDLE = 0, /* USB host channel idle status */
  53. HC_XF, /* USB host channel transfer status */
  54. HC_HALTED, /* USB host channel halted status */
  55. HC_NAK, /* USB host channel nak status */
  56. HC_NYET, /* USB host channel nyet status */
  57. HC_STALL, /* USB host channel stall status */
  58. HC_TRACERR, /* USB host channel tracerr status */
  59. HC_BBERR, /* USB host channel bberr status */
  60. HC_DTGERR, /* USB host channel dtgerr status */
  61. }hc_status_enum;
  62. /* USB URB(USB request block) state */
  63. typedef enum
  64. {
  65. URB_IDLE = 0, /* USB URB idle status */
  66. URB_DONE, /* USB URB done status */
  67. URB_NOTREADY, /* USB URB not ready status */
  68. URB_ERROR, /* USB URB error status */
  69. URB_STALL, /* USB URB stall status */
  70. URB_PING /* USB URB ping status */
  71. }urb_state_enum;
  72. /* USB core configuration */
  73. typedef struct
  74. {
  75. uint8_t core_id; /* USB core id */
  76. uint8_t core_speed; /* USB core speed */
  77. uint8_t phy_interface; /* USB PHY interface */
  78. uint8_t host_channel_num; /* USB host channel number */
  79. uint8_t dev_endp_num; /* USB device endpoint number */
  80. uint8_t sof_output; /* USB SOF output */
  81. uint8_t low_power; /* USB low power */
  82. uint16_t max_packet_size; /* USB max packet size */
  83. uint16_t max_fifo_size; /* USB fifo size */
  84. }usb_core_cfgs_struct;
  85. typedef enum
  86. {
  87. USBD_OK = 0, /* USB device ok status */
  88. USBD_BUSY, /* USB device busy status */
  89. USBD_FAIL, /* USB device fail stauts */
  90. }usbd_status_enum;
  91. /* USB control transfer state */
  92. typedef enum
  93. {
  94. USB_CTRL_IDLE = 0, /* USB control transfer idle state */
  95. USB_CTRL_SETUP, /* USB control transfer setup state */
  96. USB_CTRL_DATA_IN, /* USB control transfer data in state */
  97. USB_CTRL_DATA_OUT, /* USB control transfer data out state */
  98. USB_CTRL_STATUS_IN, /* USB control transfer status in state*/
  99. USB_CTRL_STATUS_OUT, /* USB control transfer status out state */
  100. USB_CTRL_STALL /* USB control transfer stall state */
  101. }usbd_control_state_enum;
  102. /* USB transfer direction */
  103. typedef enum
  104. {
  105. USB_RX = 0, /* receive direction type value */
  106. USB_TX /* transmit direction type value */
  107. }usb_dir_enum;
  108. /* USB endpoint in device mode */
  109. typedef struct
  110. {
  111. uint8_t endp_type; /* USB endpoint type */
  112. uint8_t endp_frame; /* USB endpoint frame */
  113. uint32_t endp_mps; /* USB endpoint max packet size */
  114. /* Transaction level variables */
  115. uint8_t *xfer_buff; /* USB transfer buffer */
  116. uint32_t xfer_len; /* USB transfer length */
  117. uint32_t xfer_count; /* USB transfer count */
  118. uint32_t dma_addr; /* USBHS can use DMA */
  119. }usb_ep_struct;
  120. /* USB device standard request */
  121. typedef struct
  122. {
  123. uint8_t bmRequestType; /* USB device request type */
  124. uint8_t bRequest; /* USB device request */
  125. uint16_t wValue; /* USB device request value */
  126. uint16_t wIndex; /* USB device request index */
  127. uint16_t wLength; /* USB device request length */
  128. }usb_device_req_struct;
  129. /* USB core device driver */
  130. typedef struct
  131. {
  132. uint8_t config_num; /* USB configuration number */
  133. __IO uint8_t status; /* USB status */
  134. uint8_t ctl_status; /* USB control status */
  135. uint8_t prev_status; /* USB previous status */
  136. uint8_t connection_status; /* USB connection status */
  137. uint32_t remote_wakeup; /* USB remote wakeup */
  138. /* transfer level variables */
  139. uint32_t remain_len; /* USB remain length */
  140. uint32_t sum_len; /* USB sum length */
  141. uint32_t ctl_len; /* USB control length */
  142. uint8_t setup_packet[8 * 3]; /* USB setup packet */
  143. usb_ep_struct in_ep[USB_MAX_DEV_EPCOUNT]; /* USB in endpoint */
  144. usb_ep_struct out_ep[USB_MAX_DEV_EPCOUNT]; /* USB out endpoint */
  145. uint8_t *dev_desc; /* device descriptor */
  146. uint8_t *config_desc; /* configuration descriptor */
  147. uint8_t* *strings; /* configuration strings */
  148. /* device class handler */
  149. uint8_t (*class_init) (void *pudev, uint8_t config_index); /* device class initialize */
  150. uint8_t (*class_deinit) (void *pudev, uint8_t config_index); /* device class deinitialize */
  151. uint8_t (*class_req_handler) (void *pudev, usb_device_req_struct *req); /* device request handler */
  152. uint8_t (*class_data_handler) (void *pudev, usb_dir_enum rx_tx, uint8_t ep_num); /* device data handler */
  153. }dcd_dev_struct;
  154. /* USB core host mode channel */
  155. typedef struct
  156. {
  157. uint8_t dev_addr; /* device address */
  158. uint8_t dev_speed; /* device speed */
  159. uint8_t DPID; /* endpoint transfer data pid */
  160. uint8_t endp_id; /* endpoint number */
  161. uint8_t endp_in; /* endpoint in */
  162. uint8_t endp_type; /* endpoint type */
  163. uint16_t endp_mps; /* endpoint max pactet size */
  164. uint16_t info; /* channel information */
  165. uint8_t *xfer_buff; /* transfer buffer */
  166. uint32_t xfer_len; /* transfer length */
  167. uint32_t xfer_count; /* trasnfer count */
  168. uint32_t err_count; /* USB transfer error count */
  169. hc_status_enum status; /* channel status */
  170. urb_state_enum urb_state; /* URB state */
  171. uint8_t data_tg_in; /* data in toggle */
  172. uint8_t data_tg_out; /* data out toggle */
  173. }usb_hostchannel_struct;
  174. /* USB core host driver */
  175. typedef struct
  176. {
  177. uint8_t rx_buffer[RX_MAX_DATA_LENGTH]; /* rx buffer */
  178. uint8_t connect_status; /* device connect status */
  179. usb_hostchannel_struct host_channel[USB_MAX_FIFOS]; /* host channel */
  180. void (*vbus_drive) (void *pudev, uint8_t state); /* the vbus driver function */
  181. }hcd_dev_struct;
  182. #ifdef USE_OTG_MODE
  183. /* USB core OTG-mode driver */
  184. typedef struct
  185. {
  186. uint8_t OTG_State; /* OTG state */
  187. uint8_t OTG_PrevState; /* OTG previous state */
  188. uint8_t OTG_Mode; /* OTG mode */
  189. }otg_dev_struct;
  190. #endif /* USE_OTG_MODE */
  191. /* USB core driver */
  192. typedef struct
  193. {
  194. usb_core_cfgs_struct cfg;
  195. #ifdef USE_DEVICE_MODE
  196. dcd_dev_struct dev;
  197. #endif /* USE_DEVICE_MODE */
  198. #ifdef USE_HOST_MODE
  199. hcd_dev_struct host;
  200. #endif /* USE_HOST_MODE */
  201. #ifdef USE_OTG_MODE
  202. otg_dev_struct otg;
  203. #endif /* USE_OTG_MODE */
  204. void (*udelay) (const uint32_t usec);
  205. void (*mdelay) (const uint32_t msec);
  206. }usb_core_handle_struct;
  207. /* function declarations */
  208. /* global APIs */
  209. /* initializes the USB controller registers and prepares the core device mode or host mode operation */
  210. usb_status_enum usb_core_init (usb_core_handle_struct *pudev);
  211. /* initialize core parameters */
  212. usb_status_enum usb_core_select (usb_core_handle_struct *pudev, usb_core_id_enum core_id);
  213. /* read a packet from the rx fifo associated with the endpoint */
  214. void* usb_fifo_read (uint8_t *dest, uint16_t len);
  215. /* write a packet into the tx fifo associated with the endpoint */
  216. usb_status_enum usb_fifo_write (uint8_t *src, uint8_t chep_num, uint16_t len);
  217. /* flush a tx fifo or all tx fifos */
  218. usb_status_enum usb_txfifo_flush (usb_core_handle_struct *pudev, uint8_t fifo_num);
  219. /* flush the entire rx fifo */
  220. usb_status_enum usb_rxfifo_flush (usb_core_handle_struct *pudev);
  221. /* set operation mode (host or device) */
  222. usb_status_enum usb_mode_set (usb_core_handle_struct *pudev, uint8_t mode);
  223. /* host APIs */
  224. #ifdef USE_HOST_MODE
  225. /* initializes USB core for host mode */
  226. usb_status_enum usb_hostcore_init (usb_core_handle_struct *pudev);
  227. /* enables the host mode interrupts */
  228. usb_status_enum usb_hostint_enable (usb_core_handle_struct *pudev);
  229. /* initialize host channel */
  230. usb_status_enum usb_hostchannel_init (usb_core_handle_struct *pudev, uint8_t hc_num);
  231. /* halt channel */
  232. usb_status_enum usb_hostchannel_halt (usb_core_handle_struct *pudev, uint8_t hc_num);
  233. /* prepare host channel for transferring packets */
  234. usb_status_enum usb_hostchannel_startxfer (usb_core_handle_struct *pudev, uint8_t hc_num);
  235. /* reset host port */
  236. uint32_t usb_port_reset (usb_core_handle_struct *pudev);
  237. /* control the VBUS to power */
  238. void usb_vbus_drive (usb_core_handle_struct *pudev, uint8_t state);
  239. /* stop the USB host and clean up fifos */
  240. void usb_host_stop (usb_core_handle_struct *pudev);
  241. #endif /* USE_HOST_MODE */
  242. /* device APIs */
  243. #ifdef USE_DEVICE_MODE
  244. /* initialize USB core registers for device mode */
  245. usb_status_enum usb_devcore_init (usb_core_handle_struct *pudev);
  246. /* configures endpoint 0 to receive setup packets */
  247. void usb_ep0_startout (usb_core_handle_struct *pudev);
  248. /* active remote wakeup signalling */
  249. void usb_remotewakeup_active (usb_core_handle_struct *pudev);
  250. /* active USB core clock */
  251. void usb_clock_ungate (usb_core_handle_struct *pudev);
  252. /* stop the device and clean up fifos */
  253. void usb_device_stop (usb_core_handle_struct *pudev);
  254. #endif /* USE_DEVICE_MODE */
  255. #endif /* USB_CORE_H */