drv_usb_host.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*!
  2. \file drv_usb_host.c
  3. \brief USB host mode low level driver
  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. #include "drv_usb_hw.h"
  30. #include "drv_usb_core.h"
  31. #include "drv_usb_host.h"
  32. const uint32_t PIPE_DPID[2] =
  33. {
  34. PIPE_DPID_DATA0,
  35. PIPE_DPID_DATA1
  36. };
  37. /*!
  38. \brief initializes USB core for host mode
  39. \param[in] pudev: pointer to selected usb host
  40. \param[out] none
  41. \retval operation status
  42. */
  43. usb_status usb_host_init (usb_core_driver *pudev)
  44. {
  45. uint32_t i = 0U, inten = 0U;
  46. uint32_t nptxfifolen = 0U;
  47. uint32_t ptxfifolen = 0U;
  48. /* restart the PHY Clock */
  49. *pudev->regs.PWRCLKCTL = 0U;
  50. /* support FS/LS only */
  51. pudev->regs.hr->HCTL &= ~HCTL_SPDFSLS;
  52. usb_phyclock_config (pudev, HCTL_48MHZ);
  53. /* configure data FIFOs size */
  54. #ifdef USB_FS_CORE
  55. if (USB_CORE_ENUM_FS == pudev->bp.core_enum) {
  56. /* set Rx FIFO size */
  57. pudev->regs.gr->GRFLEN = USB_RX_FIFO_FS_SIZE;
  58. /* set non-periodic Tx FIFO size and address */
  59. nptxfifolen |= USB_RX_FIFO_FS_SIZE;
  60. nptxfifolen |= USB_HTX_NPFIFO_FS_SIZE << 16U;
  61. pudev->regs.gr->DIEP0TFLEN_HNPTFLEN = nptxfifolen;
  62. /* set periodic Tx FIFO size and address */
  63. ptxfifolen |= USB_RX_FIFO_FS_SIZE + USB_HTX_NPFIFO_FS_SIZE;
  64. ptxfifolen |= USB_HTX_PFIFO_FS_SIZE << 16U;
  65. pudev->regs.gr->HPTFLEN = ptxfifolen;
  66. }
  67. #endif /* USB_FS_CORE */
  68. #ifdef USE_OTG_MODE
  69. /* clear host set hnp enable in the usb_otg control register */
  70. pudev->regs.gr->GOTGCS &= ~GOTGCS_HHNPEN;
  71. #endif /* USE_OTG_MODE */
  72. /* make sure the FIFOs are flushed */
  73. /* flush all Tx FIFOs in device or host mode */
  74. usb_txfifo_flush (&pudev->regs, 0x10U);
  75. /* flush the entire Rx FIFO */
  76. usb_rxfifo_flush (&pudev->regs);
  77. /* disable all interrupts */
  78. pudev->regs.gr->GINTEN = 0U;
  79. /* clear any pending USB OTG interrupts */
  80. pudev->regs.gr->GOTGINTF = 0xFFFFFFFFU;
  81. /* enable the USB wakeup and suspend interrupts */
  82. pudev->regs.gr->GINTF = 0xBFFFFFFFU;
  83. /* clear all pending host channel interrupts */
  84. for (i = 0U; i < pudev->bp.num_pipe; i++) {
  85. pudev->regs.pr[i]->HCHINTF = 0xFFFFFFFFU;
  86. pudev->regs.pr[i]->HCHINTEN = 0U;
  87. }
  88. #ifndef USE_OTG_MODE
  89. usb_portvbus_switch (pudev, 1U);
  90. #endif /* USE_OTG_MODE */
  91. pudev->regs.gr->GINTEN = GINTEN_WKUPIE | GINTEN_SPIE;
  92. /* enable host_mode-related interrupts */
  93. if (USB_USE_FIFO == pudev->bp.transfer_mode) {
  94. inten = GINTEN_RXFNEIE;
  95. }
  96. inten |= GINTEN_SESIE | GINTEN_HPIE | GINTEN_HCIE | GINTEN_ISOINCIE;
  97. pudev->regs.gr->GINTEN |= inten;
  98. inten = GINTEN_DISCIE | GINTEN_SOFIE;
  99. pudev->regs.gr->GINTEN &= ~inten;
  100. return USB_OK;
  101. }
  102. /*!
  103. \brief control the VBUS to power
  104. \param[in] pudev: pointer to selected usb host
  105. \param[in] state: VBUS state
  106. \param[out] none
  107. \retval none
  108. */
  109. void usb_portvbus_switch (usb_core_driver *pudev, uint8_t state)
  110. {
  111. uint32_t port = 0U;
  112. /* enable or disable the external charge pump */
  113. usb_vbus_drive (state);
  114. /* turn on the host port power. */
  115. port = usb_port_read (pudev);
  116. if (!(port & HPCS_PP) && (1U == state)) {
  117. port |= HPCS_PP;
  118. }
  119. if ((port & HPCS_PP) && (0U == state)) {
  120. port &= ~HPCS_PP;
  121. }
  122. *pudev->regs.HPCS = port;
  123. usb_mdelay (200U);
  124. }
  125. /*!
  126. \brief reset host port
  127. \param[in] pudev: pointer to usb device
  128. \param[out] none
  129. \retval operation status
  130. */
  131. uint32_t usb_port_reset (usb_core_driver *pudev)
  132. {
  133. __IO uint32_t port = usb_port_read (pudev);
  134. *pudev->regs.HPCS = port | HPCS_PRST;
  135. usb_mdelay(20U); /* see note */
  136. *pudev->regs.HPCS = port & ~HPCS_PRST;
  137. usb_mdelay(20U);
  138. return 1U;
  139. }
  140. /*!
  141. \brief initialize host pipe
  142. \param[in] pudev: pointer to usb device
  143. \param[in] pipe_num: host pipe number which is in (0..7)
  144. \param[out] none
  145. \retval operation status
  146. */
  147. usb_status usb_pipe_init (usb_core_driver *pudev, uint8_t pipe_num)
  148. {
  149. usb_status status = USB_OK;
  150. __IO uint32_t pp_ctl = 0U;
  151. __IO uint32_t pp_inten = HCHINTEN_TFIE;
  152. usb_pipe *pp = &pudev->host.pipe[pipe_num];
  153. /* clear old interrupt conditions for this host channel */
  154. pudev->regs.pr[pipe_num]->HCHINTF = 0xFFFFFFFFU;
  155. if (pp->ep.dir) {
  156. pp_inten |= HCHINTEN_BBERIE;
  157. }
  158. /* enable channel interrupts required for this transfer */
  159. switch (pp->ep.type) {
  160. case USB_EPTYPE_CTRL:
  161. case USB_EPTYPE_BULK:
  162. pp_inten |= HCHINTEN_STALLIE | HCHINTEN_USBERIE \
  163. | HCHINTEN_DTERIE | HCHINTEN_NAKIE;
  164. if (!pp->ep.dir) {
  165. pp_inten |= HCHINTEN_NYETIE;
  166. if (pp->ping) {
  167. pp_inten |= HCHINTEN_ACKIE;
  168. }
  169. }
  170. break;
  171. case USB_EPTYPE_INTR:
  172. pp_inten |= HCHINTEN_STALLIE | HCHINTEN_USBERIE | HCHINTEN_DTERIE \
  173. | HCHINTEN_NAKIE | HCHINTEN_REQOVRIE;
  174. break;
  175. case USB_EPTYPE_ISOC:
  176. pp_inten |= HCHINTEN_REQOVRIE | HCHINTEN_ACKIE;
  177. if (pp->ep.dir) {
  178. pp_inten |= HCHINTEN_USBERIE;
  179. }
  180. break;
  181. default:
  182. break;
  183. }
  184. pudev->regs.pr[pipe_num]->HCHINTEN = pp_inten;
  185. /* enable the top level host channel interrupt */
  186. pudev->regs.hr->HACHINTEN |= 1U << pipe_num;
  187. /* make sure host channel interrupts are enabled */
  188. pudev->regs.gr->GINTEN |= GINTEN_HCIE;
  189. /* program the host channel control register */
  190. pp_ctl |= PIPE_CTL_DAR(pp->dev_addr);
  191. pp_ctl |= PIPE_CTL_EPNUM(pp->ep.num);
  192. pp_ctl |= PIPE_CTL_EPDIR(pp->ep.dir);
  193. pp_ctl |= PIPE_CTL_EPTYPE(pp->ep.type);
  194. pp_ctl |= PIPE_CTL_LSD(pp->dev_speed == PORT_SPEED_LOW);
  195. pp_ctl |= pp->ep.mps;
  196. pp_ctl |= ((uint32_t)(pp->ep.type == USB_EPTYPE_INTR) << 29U) & HCHCTL_ODDFRM;
  197. pudev->regs.pr[pipe_num]->HCHCTL = pp_ctl;
  198. return status;
  199. }
  200. /*!
  201. \brief prepare host channel for transferring packets
  202. \param[in] pudev: pointer to usb device
  203. \param[in] pipe_num: host pipe number which is in (0..7)
  204. \param[out] none
  205. \retval operation status
  206. */
  207. usb_status usb_pipe_xfer (usb_core_driver *pudev, uint8_t pipe_num)
  208. {
  209. usb_status status = USB_OK;
  210. uint16_t dword_len = 0U;
  211. uint16_t packet_count = 0U;
  212. __IO uint32_t pp_ctl = 0U;
  213. usb_pipe *pp = &pudev->host.pipe[pipe_num];
  214. uint16_t max_packet_len = pp->ep.mps;
  215. /* compute the expected number of packets associated to the transfer */
  216. if (pp->xfer_len > 0U) {
  217. packet_count = (uint16_t)((pp->xfer_len + max_packet_len - 1U) / max_packet_len);
  218. if (packet_count > HC_MAX_PACKET_COUNT) {
  219. packet_count = HC_MAX_PACKET_COUNT;
  220. pp->xfer_len = (uint16_t)(packet_count * max_packet_len);
  221. }
  222. } else {
  223. packet_count = 1U;
  224. }
  225. if (pp->ep.dir) {
  226. pp->xfer_len = (uint16_t)(packet_count * max_packet_len);
  227. }
  228. /* initialize the host channel transfer information */
  229. pudev->regs.pr[pipe_num]->HCHLEN = pp->xfer_len | pp->DPID | PIPE_XFER_PCNT(packet_count);
  230. pp_ctl = pudev->regs.pr[pipe_num]->HCHCTL;
  231. if (usb_frame_even(pudev)) {
  232. pp_ctl |= HCHCTL_ODDFRM;
  233. } else {
  234. pp_ctl &= ~HCHCTL_ODDFRM;
  235. }
  236. /* set host channel enabled */
  237. pp_ctl |= HCHCTL_CEN;
  238. pp_ctl &= ~HCHCTL_CDIS;
  239. pudev->regs.pr[pipe_num]->HCHCTL = pp_ctl;
  240. if (USB_USE_FIFO == pudev->bp.transfer_mode) {
  241. if ((0U == pp->ep.dir) && (pp->xfer_len > 0U)) {
  242. switch (pp->ep.type) {
  243. /* non-periodic transfer */
  244. case USB_EPTYPE_CTRL:
  245. case USB_EPTYPE_BULK:
  246. dword_len = (uint16_t)((pp->xfer_len + 3U) / 4U);
  247. /* check if there is enough space in fifo space */
  248. if (dword_len > (pudev->regs.gr->HNPTFQSTAT & HNPTFQSTAT_NPTXFS)) {
  249. /* need to process data in nptxfempty interrupt */
  250. pudev->regs.gr->GINTEN |= GINTEN_NPTXFEIE;
  251. }
  252. break;
  253. /* periodic transfer */
  254. case USB_EPTYPE_INTR:
  255. case USB_EPTYPE_ISOC:
  256. dword_len = (uint16_t)((pp->xfer_len + 3U) / 4U);
  257. /* check if there is enough space in fifo space */
  258. if (dword_len > (pudev->regs.hr->HPTFQSTAT & HPTFQSTAT_PTXFS)) {
  259. /* need to process data in ptxfempty interrupt */
  260. pudev->regs.gr->GINTEN |= GINTEN_PTXFEIE;
  261. }
  262. break;
  263. default:
  264. break;
  265. }
  266. /* write packet into the tx fifo. */
  267. usb_txfifo_write (&pudev->regs, pp->xfer_buf, pipe_num, (uint16_t)pp->xfer_len);
  268. }
  269. }
  270. return status;
  271. }
  272. /*!
  273. \brief halt pipe
  274. \param[in] pudev: pointer to usb device
  275. \param[in] pipe_num: host pipe number which is in (0..7)
  276. \param[out] none
  277. \retval operation status
  278. */
  279. usb_status usb_pipe_halt (usb_core_driver *pudev, uint8_t pipe_num)
  280. {
  281. __IO uint32_t pp_ctl = pudev->regs.pr[pipe_num]->HCHCTL;
  282. uint8_t ep_type = (uint8_t)((pp_ctl & HCHCTL_EPTYPE) >> 18U);
  283. pp_ctl |= HCHCTL_CEN | HCHCTL_CDIS;
  284. switch (ep_type) {
  285. case USB_EPTYPE_CTRL:
  286. case USB_EPTYPE_BULK:
  287. if (0U == (pudev->regs.gr->HNPTFQSTAT & HNPTFQSTAT_NPTXFS)) {
  288. pp_ctl &= ~HCHCTL_CEN;
  289. }
  290. break;
  291. case USB_EPTYPE_INTR:
  292. case USB_EPTYPE_ISOC:
  293. if (0U == (pudev->regs.hr->HPTFQSTAT & HPTFQSTAT_PTXFS)) {
  294. pp_ctl &= ~HCHCTL_CEN;
  295. }
  296. break;
  297. default:
  298. break;
  299. }
  300. pudev->regs.pr[pipe_num]->HCHCTL = pp_ctl;
  301. return USB_OK;
  302. }
  303. /*!
  304. \brief configure host pipe to do ping operation
  305. \param[in] pudev: pointer to usb device
  306. \param[in] pipe_num: host pipe number which is in (0..7)
  307. \param[out] none
  308. \retval operation status
  309. */
  310. usb_status usb_pipe_ping (usb_core_driver *pudev, uint8_t pipe_num)
  311. {
  312. uint32_t pp_ctl = 0U;
  313. pudev->regs.pr[pipe_num]->HCHLEN = HCHLEN_PING | (HCHLEN_PCNT & (1U << 19U));
  314. pp_ctl = pudev->regs.pr[pipe_num]->HCHCTL;
  315. pp_ctl |= HCHCTL_CEN;
  316. pp_ctl &= ~HCHCTL_CDIS;
  317. pudev->regs.pr[pipe_num]->HCHCTL = pp_ctl;
  318. return USB_OK;
  319. }
  320. /*!
  321. \brief stop the USB host and clean up FIFO
  322. \param[in] pudev: pointer to usb device
  323. \param[out] none
  324. \retval none
  325. */
  326. void usb_host_stop (usb_core_driver *pudev)
  327. {
  328. uint32_t i;
  329. __IO uint32_t pp_ctl = 0U;
  330. pudev->regs.hr->HACHINTEN = 0x0U;
  331. pudev->regs.hr->HACHINT = 0xFFFFFFFFU;
  332. /* flush out any leftover queued requests. */
  333. for (i = 0U; i < pudev->bp.num_pipe; i++) {
  334. pp_ctl = pudev->regs.pr[i]->HCHCTL;
  335. pp_ctl &= ~(HCHCTL_CEN | HCHCTL_EPDIR);
  336. pp_ctl |= HCHCTL_CDIS;
  337. pudev->regs.pr[i]->HCHCTL = pp_ctl;
  338. }
  339. /* flush the FIFO */
  340. usb_rxfifo_flush (&pudev->regs);
  341. usb_txfifo_flush (&pudev->regs, 0x10U);
  342. }