drv_usb_host.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*!
  2. \file drv_usb_host.h
  3. \brief USB host mode 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_HOST_H
  30. #define __DRV_USB_HOST_H
  31. #include "drv_usb_regs.h"
  32. #include "usb_ch9_std.h"
  33. #include "drv_usb_core.h"
  34. typedef enum _usb_pipe_status
  35. {
  36. PIPE_IDLE = 0U,
  37. PIPE_XF,
  38. PIPE_HALTED,
  39. PIPE_NAK,
  40. PIPE_NYET,
  41. PIPE_STALL,
  42. PIPE_TRACERR,
  43. PIPE_BBERR,
  44. PIPE_REQOVR,
  45. PIPE_DTGERR,
  46. } usb_pipe_staus;
  47. typedef enum _usb_pipe_mode
  48. {
  49. PIPE_PERIOD = 0U,
  50. PIPE_NON_PERIOD = 1U
  51. } usb_pipe_mode;
  52. typedef enum _usb_urb_state
  53. {
  54. URB_IDLE = 0U,
  55. URB_DONE,
  56. URB_NOTREADY,
  57. URB_ERROR,
  58. URB_STALL,
  59. URB_PING
  60. } usb_urb_state;
  61. typedef struct _usb_pipe
  62. {
  63. uint8_t in_used;
  64. uint8_t dev_addr;
  65. uint32_t dev_speed;
  66. struct {
  67. uint8_t num;
  68. uint8_t dir;
  69. uint8_t type;
  70. uint16_t mps;
  71. } ep;
  72. uint8_t ping;
  73. uint32_t DPID;
  74. uint8_t *xfer_buf;
  75. uint32_t xfer_len;
  76. uint32_t xfer_count;
  77. uint8_t data_toggle_in;
  78. uint8_t data_toggle_out;
  79. __IO uint32_t err_count;
  80. __IO usb_pipe_staus pp_status;
  81. __IO usb_urb_state urb_state;
  82. } usb_pipe;
  83. typedef struct _usb_host_drv
  84. {
  85. __IO uint32_t connect_status;
  86. __IO uint32_t port_enabled;
  87. __IO uint32_t backup_xfercount[USBFS_MAX_TX_FIFOS];
  88. usb_pipe pipe[USBFS_MAX_TX_FIFOS];
  89. void *data;
  90. } usb_host_drv;
  91. typedef struct _usb_core_driver
  92. {
  93. usb_core_basic bp;
  94. usb_core_regs regs;
  95. usb_host_drv host;
  96. } usb_core_driver;
  97. /*!
  98. \brief get USB even frame
  99. \param[in] pudev: pointer to USB device
  100. \param[out] none
  101. \retval none
  102. */
  103. __STATIC_INLINE uint8_t usb_frame_even (usb_core_driver *pudev)
  104. {
  105. return (uint8_t)!(pudev->regs.hr->HFINFR & 0x01U);
  106. }
  107. /*!
  108. \brief configure USB clock of PHY
  109. \param[in] pudev: pointer to USB device
  110. \param[in] clock: PHY clock
  111. \param[out] none
  112. \retval none
  113. */
  114. __STATIC_INLINE void usb_phyclock_config (usb_core_driver *pudev, uint8_t clock)
  115. {
  116. pudev->regs.hr->HCTL &= ~HCTL_CLKSEL;
  117. pudev->regs.hr->HCTL |= clock;
  118. }
  119. /*!
  120. \brief read USB port
  121. \param[in] pudev: pointer to USB device
  122. \param[out] none
  123. \retval port status
  124. */
  125. __STATIC_INLINE uint32_t usb_port_read (usb_core_driver *pudev)
  126. {
  127. return *pudev->regs.HPCS & ~(HPCS_PE | HPCS_PCD | HPCS_PEDC);
  128. }
  129. /*!
  130. \brief get USB current speed
  131. \param[in] pudev: pointer to USB device
  132. \param[out] none
  133. \retval USB current speed
  134. */
  135. __STATIC_INLINE uint32_t usb_curspeed_get (usb_core_driver *pudev)
  136. {
  137. return *pudev->regs.HPCS & HPCS_PS;
  138. }
  139. /*!
  140. \brief get USB current frame
  141. \param[in] pudev: pointer to USB device
  142. \param[out] none
  143. \retval USB current frame
  144. */
  145. __STATIC_INLINE uint32_t usb_curframe_get (usb_core_driver *pudev)
  146. {
  147. return (pudev->regs.hr->HFINFR & 0xFFFFU);
  148. }
  149. /* function declarations */
  150. /* initializes USB core for host mode */
  151. usb_status usb_host_init (usb_core_driver *pudev);
  152. /* control the VBUS to power */
  153. void usb_portvbus_switch (usb_core_driver *pudev, uint8_t state);
  154. /* reset host port */
  155. uint32_t usb_port_reset (usb_core_driver *pudev);
  156. /* initialize host pipe */
  157. usb_status usb_pipe_init (usb_core_driver *pudev, uint8_t pipe_num);
  158. /* prepare host pipe for transferring packets */
  159. usb_status usb_pipe_xfer (usb_core_driver *pudev, uint8_t pipe_num);
  160. /* halt host pipe */
  161. usb_status usb_pipe_halt (usb_core_driver *pudev, uint8_t pipe_num);
  162. /* configure host pipe to do ping operation */
  163. usb_status usb_pipe_ping (usb_core_driver *pudev, uint8_t pipe_num);
  164. /* stop the USB host and clean up FIFO */
  165. void usb_host_stop (usb_core_driver *pudev);
  166. #endif /* __DRV_USB_HOST_H */