drv_usb_dev.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*!
  2. \file drv_usb_dev.h
  3. \brief USB device low level driver header file
  4. \version 2023-06-30, V2.1.6, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2023, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #ifndef __DRV_USB_DEV_H
  30. #define __DRV_USB_DEV_H
  31. #include "usbd_conf.h"
  32. #include "drv_usb_core.h"
  33. enum usb_ctl_status
  34. {
  35. USB_CTL_IDLE = 0U, /*!< USB control transfer idle state */
  36. USB_CTL_DATA_IN, /*!< USB control transfer data in state */
  37. USB_CTL_LAST_DATA_IN, /*!< USB control transfer last data in state */
  38. USB_CTL_DATA_OUT, /*!< USB control transfer data out state */
  39. USB_CTL_LAST_DATA_OUT, /*!< USB control transfer last data out state */
  40. USB_CTL_STATUS_IN, /*!< USB control transfer status in state*/
  41. USB_CTL_STATUS_OUT /*!< USB control transfer status out state */
  42. };
  43. #define EP_IN(x) ((uint8_t)(0x80U | (x))) /*!< device IN endpoint */
  44. #define EP_OUT(x) ((uint8_t)(x)) /*!< device OUT endpoint */
  45. /* USB descriptor */
  46. typedef struct _usb_desc
  47. {
  48. uint8_t *dev_desc; /*!< device descriptor */
  49. uint8_t *config_desc; /*!< configure descriptor */
  50. uint8_t *bos_desc; /*!< BOS descriptor */
  51. void* const *strings; /*!< string descriptor */
  52. } usb_desc;
  53. /* USB power management */
  54. typedef struct _usb_pm
  55. {
  56. uint8_t power_mode; /*!< power mode */
  57. uint8_t power_low; /*!< power low */
  58. uint8_t dev_remote_wakeup; /*!< remote wakeup */
  59. uint8_t remote_wakeup_on; /*!< remote wakeup on */
  60. } usb_pm;
  61. /* USB control information */
  62. typedef struct _usb_control
  63. {
  64. usb_req req; /*!< USB standard device request */
  65. uint8_t ctl_state; /*!< USB control transfer state */
  66. uint8_t ctl_zlp; /*!< zero length package */
  67. } usb_control;
  68. typedef struct
  69. {
  70. struct {
  71. uint8_t num: 4; /*!< the endpoint number.it can be from 0 to 6 */
  72. uint8_t pad: 3; /*!< padding between number and direction */
  73. uint8_t dir: 1; /*!< the endpoint direction */
  74. } ep_addr;
  75. uint8_t ep_type; /*!< USB endpoint type */
  76. uint8_t ep_stall; /*!< USB endpoint stall status */
  77. uint8_t frame_num; /*!< number of frame */
  78. uint16_t max_len; /*!< Maximum packet length */
  79. /* transaction level variables */
  80. uint8_t *xfer_buf; /*!< transmit buffer */
  81. uint32_t xfer_len; /*!< transmit buffer length */
  82. uint32_t xfer_count; /*!< transmit buffer count */
  83. uint32_t remain_len; /*!< remain packet length */
  84. } usb_transc;
  85. typedef struct _usb_core_driver usb_dev;
  86. typedef struct _usb_class_core
  87. {
  88. uint8_t command; /*!< device class request command */
  89. uint8_t alter_set; /*!< alternative set */
  90. uint8_t (*init) (usb_dev *udev, uint8_t config_index); /*!< initialize handler */
  91. uint8_t (*deinit) (usb_dev *udev, uint8_t config_index); /*!< de-initialize handler */
  92. uint8_t (*req_proc) (usb_dev *udev, usb_req *req); /*!< device request handler */
  93. uint8_t (*set_intf) (usb_dev *udev, usb_req *req); /*!< device set interface callback */
  94. uint8_t (*ctlx_in) (usb_dev *udev); /*!< device contrl in callback */
  95. uint8_t (*ctlx_out) (usb_dev *udev); /*!< device contrl out callback */
  96. uint8_t (*data_in) (usb_dev *udev, uint8_t ep_num); /*!< device data in handler */
  97. uint8_t (*data_out) (usb_dev *udev, uint8_t ep_num); /*!< device data out handler */
  98. uint8_t (*SOF) (usb_dev *udev); /*!< Start of frame handler */
  99. uint8_t (*incomplete_isoc_in) (usb_dev *udev); /*!< Incomplete synchronization IN transfer handler */
  100. uint8_t (*incomplete_isoc_out) (usb_dev *udev); /*!< Incomplete synchronization OUT transfer handler */
  101. } usb_class_core;
  102. typedef struct _usb_perp_dev
  103. {
  104. uint8_t config; /*!< configuration */
  105. uint8_t dev_addr; /*!< device address */
  106. __IO uint8_t cur_status; /*!< current status */
  107. __IO uint8_t backup_status; /*!< backup status */
  108. usb_transc transc_in[USBFS_MAX_TX_FIFOS]; /*!< endpoint IN transaction */
  109. usb_transc transc_out[USBFS_MAX_TX_FIFOS]; /*!< endpoint OUT transaction */
  110. usb_pm pm; /*!< power management */
  111. usb_control control; /*!< USB control information */
  112. usb_desc *desc; /*!< USB descriptors pointer */
  113. usb_class_core *class_core; /*!< class driver */
  114. void *class_data[USBD_ITF_MAX_NUM]; /*!< class data pointer */
  115. void *user_data; /*!< user data pointer */
  116. void *pdata; /*!< reserved data pointer */
  117. } usb_perp_dev;
  118. typedef struct _usb_core_driver
  119. {
  120. usb_core_basic bp; /*!< USB basic parameters */
  121. usb_core_regs regs; /*!< USB registers */
  122. usb_perp_dev dev; /*!< USB peripheral device */
  123. } usb_core_driver;
  124. /* static inline function definitions */
  125. /*!
  126. \brief configure the USB device to be disconnected
  127. \param[in] udev: pointer to USB device
  128. \param[out] none
  129. \retval operation status
  130. */
  131. __STATIC_INLINE void usb_dev_disconnect (usb_core_driver *udev)
  132. {
  133. udev->regs.dr->DCTL |= DCTL_SD;
  134. }
  135. /*!
  136. \brief configure the USB device to be connected
  137. \param[in] udev: pointer to USB device
  138. \param[out] none
  139. \retval operation status
  140. */
  141. __STATIC_INLINE void usb_dev_connect (usb_core_driver *udev)
  142. {
  143. udev->regs.dr->DCTL &= ~DCTL_SD;
  144. }
  145. /*!
  146. \brief set the USB device address
  147. \param[in] udev: pointer to USB device
  148. \param[in] dev_addr: device address for setting
  149. \param[out] none
  150. \retval operation status
  151. */
  152. __STATIC_INLINE void usb_devaddr_set (usb_core_driver *udev, uint8_t dev_addr)
  153. {
  154. udev->regs.dr->DCFG &= ~DCFG_DAR;
  155. udev->regs.dr->DCFG |= (uint32_t)dev_addr << 4U;
  156. }
  157. /*!
  158. \brief read device all OUT endpoint interrupt register
  159. \param[in] udev: pointer to USB device
  160. \param[out] none
  161. \retval interrupt status
  162. */
  163. __STATIC_INLINE uint32_t usb_oepintnum_read (usb_core_driver *udev)
  164. {
  165. uint32_t value = udev->regs.dr->DAEPINT;
  166. value &= udev->regs.dr->DAEPINTEN;
  167. return (value & DAEPINT_OEPITB) >> 16U;
  168. }
  169. /*!
  170. \brief read device OUT endpoint interrupt flag register
  171. \param[in] udev: pointer to USB device
  172. \param[in] ep_num: endpoint number
  173. \param[out] none
  174. \retval interrupt status
  175. */
  176. __STATIC_INLINE uint32_t usb_oepintr_read (usb_core_driver *udev, uint8_t ep_num)
  177. {
  178. uint32_t value = udev->regs.er_out[ep_num]->DOEPINTF;
  179. value &= udev->regs.dr->DOEPINTEN;
  180. return value;
  181. }
  182. /*!
  183. \brief read device all IN endpoint interrupt register
  184. \param[in] udev: pointer to USB device
  185. \param[out] none
  186. \retval interrupt status
  187. */
  188. __STATIC_INLINE uint32_t usb_iepintnum_read (usb_core_driver *udev)
  189. {
  190. uint32_t value = udev->regs.dr->DAEPINT;
  191. value &= udev->regs.dr->DAEPINTEN;
  192. return value & DAEPINT_IEPITB;
  193. }
  194. /*!
  195. \brief set remote wakeup signaling
  196. \param[in] udev: pointer to USB device
  197. \param[out] none
  198. \retval none
  199. */
  200. __STATIC_INLINE void usb_rwkup_set (usb_core_driver *udev)
  201. {
  202. if (udev->dev.pm.dev_remote_wakeup) {
  203. /* enable remote wakeup signaling */
  204. udev->regs.dr->DCTL |= DCTL_RWKUP;
  205. }
  206. }
  207. /*!
  208. \brief reset remote wakeup signaling
  209. \param[in] udev: pointer to USB device
  210. \param[out] none
  211. \retval none
  212. */
  213. __STATIC_INLINE void usb_rwkup_reset (usb_core_driver *udev)
  214. {
  215. if (udev->dev.pm.dev_remote_wakeup) {
  216. /* disable remote wakeup signaling */
  217. udev->regs.dr->DCTL &= ~DCTL_RWKUP;
  218. }
  219. }
  220. /* function declarations */
  221. /* initialize USB core registers for device mode */
  222. usb_status usb_devcore_init (usb_core_driver *udev);
  223. /* enable the USB device mode interrupts */
  224. usb_status usb_devint_enable (usb_core_driver *udev);
  225. /* active the USB endpoint 0 transaction */
  226. usb_status usb_transc0_active (usb_core_driver *udev, usb_transc *transc);
  227. /* active the USB transaction */
  228. usb_status usb_transc_active (usb_core_driver *udev, usb_transc *transc);
  229. /* deactivate the USB transaction */
  230. usb_status usb_transc_deactivate (usb_core_driver *udev, usb_transc *transc);
  231. /* configure USB transaction to start IN transfer */
  232. usb_status usb_transc_inxfer (usb_core_driver *udev, usb_transc *transc);
  233. /* configure USB transaction to start OUT transfer */
  234. usb_status usb_transc_outxfer (usb_core_driver *udev, usb_transc *transc);
  235. /* set the USB transaction STALL status */
  236. usb_status usb_transc_stall (usb_core_driver *udev, usb_transc *transc);
  237. /* clear the USB transaction STALL status */
  238. usb_status usb_transc_clrstall (usb_core_driver *udev, usb_transc *transc);
  239. /* read device IN endpoint interrupt flag register */
  240. uint32_t usb_iepintr_read (usb_core_driver *udev, uint8_t ep_num);
  241. /* configures OUT endpoint 0 to receive SETUP packets */
  242. void usb_ctlep_startout (usb_core_driver *udev);
  243. /* active remote wakeup signaling */
  244. void usb_rwkup_active (usb_core_driver *udev);
  245. /* active USB core clock */
  246. void usb_clock_active (usb_core_driver *udev);
  247. /* USB device suspend */
  248. void usb_dev_suspend (usb_core_driver *udev);
  249. /* stop the device and clean up FIFOs */
  250. void usb_dev_stop (usb_core_driver *udev);
  251. #endif /* __DRV_USB_DEV_H */